Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

PreAllocBuffer.h

00001 #ifndef _STR_PreAllocBuffer_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/PreAllocBuffer.h,v 1.7 2002/08/29 00:58:04 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
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 /** \class PreAllocBufferBase PreAllocBuffer.h StrMod/PreAllocBuffer.h
00039  * \brief Just a base class that defines the functions for the template class
00040  * that don't depend on the template argument.
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    //: See class Debugable
00056    virtual bool invariant() const = 0;
00057 
00058    //: See class Debugable
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 /** \class PreAllocBuffer PreAllocBuffer.h StrMod/PreAllocBuffer.h
00080  * \brief A template class for buffers that contain a certain fixed amount of
00081  * storage that's not dynamically allocated.
00082  */
00083 template <unsigned int TInitialAlloc>
00084 class PreAllocBuffer : public PreAllocBufferBase {
00085  private:
00086    typedef lcore::U1Byte U1Byte;
00087  public:
00088    // There isn't any identifier here because there's no good way (in the
00089    // identifier system) to generate a unique identifier for every template
00090    // instantiation.
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 //-----------------------------inline functions--------------------------------
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 };  // namespace strmod
00144 };  // namespace strmod
00145 
00146 #endif

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1