00001 /* 00002 * Copyright (C) Sergey P. Derevyago, 2003-2004. 00003 * 00004 * Permission to copy, use, modify, sell and distribute this software is granted 00005 * provided this copyright notice appears in all copies. 00006 * This software is provided "as is" without express or implied warranty, and 00007 * with no claim as to its suitability for any purpose. 00008 * 00009 */ 00010 00015 #ifndef __STRINGBUF_HPP__ 00016 #define __STRINGBUF_HPP__ 00017 00018 #include <string> 00019 #include "fix_alloc.hpp" 00020 00024 namespace StringBuf_private { 00025 00029 template <class T> 00030 struct Node { 00032 T val; 00034 Node* next; 00035 00037 Node(const T& v) : val(v), next(0) {} 00038 00040 void* operator new(size_t) { return fixed_alloc<Node>::alloc(); } 00041 00043 void operator delete(void* ptr, size_t) 00044 { 00045 fixed_alloc<Node>::free(ptr); 00046 } 00047 }; 00048 00054 template <class T> 00055 struct List { 00057 Node<T>* head; 00059 Node<T>* tail; 00060 00062 List() { head=tail=0; } 00063 00065 ~List(); 00066 00068 void add(const T& v); 00069 }; 00070 00071 template<class T> 00072 List<T>::~List() 00073 { 00074 for (Node<T> *ptr=head, *next; ptr; ptr=next) { 00075 next=ptr->next; 00076 delete ptr; 00077 } 00078 } 00079 00080 template <class T> 00081 inline void List<T>::add(const T& v) 00082 { 00083 Node<T>* n=new Node<T>(v); 00084 00085 if (!head) head=tail=n; 00086 else tail=tail->next=n; 00087 } 00088 00089 } 00090 00098 class StringBuf { 00100 StringBuf_private::List<const std::string*> ptrs; 00102 StringBuf_private::List<std::string> tmps; 00103 00105 StringBuf(const StringBuf&); 00107 StringBuf& operator=(const StringBuf&); 00108 00109 public: 00113 StringBuf() {} 00114 00118 StringBuf(const std::string& str) { *this+str; } 00119 00126 StringBuf& operator+(const std::string& str); 00127 00131 StringBuf& operator+(int val); 00132 00136 StringBuf& operator+(unsigned int val); 00137 00141 StringBuf& operator+(long val); 00142 00146 StringBuf& operator+(unsigned long val); 00147 00155 operator std::string() const; 00156 }; 00157 00158 inline StringBuf& StringBuf::operator+(const std::string& str) 00159 { 00160 ptrs.add(&str); 00161 return *this; 00162 } 00163 00164 #endif