00001 #ifndef _STR_PreAllocBuffer_H_ // -*-c++-*-
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef __GNUG__
00022 # pragma interface
00023 #endif
00024
00025
00026
00027
00028
00029 #include <StrMod/BufferChunk.h>
00030 #include <iosfwd>
00031 #include <new>
00032
00033 #define _STR_PreAllocBuffer_H_
00034
00035 namespace strmod {
00036 namespace strmod {
00037
00038
00039
00040
00041
00042 class PreAllocBufferBase : public BufferChunk
00043 {
00044 private:
00045 void a_silly_member_function_to_make_sure_a_vtable_is_generated();
00046 typedef lcore::U1Byte U1Byte;
00047 public:
00048 static const STR_ClassIdent identifier;
00049
00050 PreAllocBufferBase() { }
00051 virtual ~PreAllocBufferBase() = 0;
00052
00053 inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00054
00055
00056 virtual bool invariant() const = 0;
00057
00058
00059 virtual void printState(std::ostream &os) const = 0;
00060
00061 virtual void resize(unsigned int newsize) throw(std::bad_alloc) = 0;
00062
00063 protected:
00064 virtual const lcore::ClassIdent *i_GetIdent() const { return &identifier; }
00065
00066 void i_destruct(const U1Byte * const preallocbuf);
00067 void i_resize(const unsigned int newsize,
00068 const unsigned int prebufsize,
00069 U1Byte * const preallocbuf) throw(std::bad_alloc);
00070 bool i_invariant(const unsigned int prebufsize,
00071 const void * const prebuf) const;
00072 void i_printState(std::ostream &os,
00073 const unsigned int prebufsize,
00074 const void * const prebuf) const;
00075 };
00076
00077
00078
00079
00080
00081
00082
00083 template <unsigned int TInitialAlloc>
00084 class PreAllocBuffer : public PreAllocBufferBase {
00085 private:
00086 typedef lcore::U1Byte U1Byte;
00087 public:
00088
00089
00090
00091 inline PreAllocBuffer();
00092 inline virtual ~PreAllocBuffer();
00093
00094 inline virtual bool invariant() const;
00095
00096 inline virtual void printState(std::ostream &os) const;
00097
00098 inline virtual void resize(unsigned int newsize) throw(std::bad_alloc);
00099
00100 private:
00101 U1Byte preallocbuf_[TInitialAlloc];
00102 };
00103
00104
00105
00106 inline int PreAllocBufferBase::AreYouA(const lcore::ClassIdent &cid) const
00107 {
00108 return((identifier == cid) || BufferChunk::AreYouA(cid));
00109 }
00110
00111
00112
00113 template <unsigned int TInitialAlloc>
00114 inline PreAllocBuffer<TInitialAlloc>::PreAllocBuffer()
00115 {
00116 }
00117
00118 template <unsigned int TInitialAlloc>
00119 inline PreAllocBuffer<TInitialAlloc>::~PreAllocBuffer()
00120 {
00121 i_destruct(preallocbuf_);
00122 }
00123
00124 template <unsigned int TInitialAlloc>
00125 inline bool PreAllocBuffer<TInitialAlloc>::invariant() const
00126 {
00127 return(i_invariant(TInitialAlloc, preallocbuf_));
00128 }
00129
00130 template <unsigned int TInitialAlloc>
00131 inline void PreAllocBuffer<TInitialAlloc>::printState(std::ostream &os) const
00132 {
00133 return(i_printState(os, TInitialAlloc, preallocbuf_));
00134 }
00135
00136 template <unsigned int TInitialAlloc>
00137 inline void
00138 PreAllocBuffer<TInitialAlloc>::resize(unsigned int newsize) throw(std::bad_alloc)
00139 {
00140 i_resize(newsize, TInitialAlloc, preallocbuf_);
00141 }
00142
00143 };
00144 };
00145
00146 #endif