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

BufferChunk.h

00001 #ifndef _STR_BufferChunk_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/BufferChunk.h,v 1.9 2002/08/29 00:58:04 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #include <StrMod/ChunkVisitor.h>
00030 #include <StrMod/StrChunk.h>
00031 #include <StrMod/STR_ClassIdent.h>
00032 #include <StrMod/LinearExtent.h>
00033 #include <LCore/GenTypes.h>
00034 #include <LCore/Debugable.h>
00035 #include <new>
00036 #include <iosfwd>
00037 
00038 #define _STR_BufferChunk_H_
00039 
00040 namespace strmod {
00041 namespace strmod {
00042 
00043 /** \class BufferChunk BufferChunk.h StrMod/BufferChunk.h
00044  * This is an abstract base class for StrChunks that are really just bags of
00045  * bytes.
00046  */
00047 class BufferChunk : public StrChunk, virtual public lcore::Debugable
00048 {
00049    typedef lcore::U1Byte U1Byte;
00050  public:
00051    class Factory;
00052    static const STR_ClassIdent identifier;
00053 
00054    /** As a convenience, initialize the protected variables maintained by the
00055     * derived classes.
00056     */
00057    BufferChunk() : buf_(0), buflen_(0)                  { }
00058    /** Doesn't delete any storage whatsoever.
00059     *
00060     * Since this is an abstract class that doesn't ever actually allocate
00061     * any storage, it's left to the derived classes to do the actual
00062     * deletion.
00063     */
00064    virtual ~BufferChunk()                               { }
00065 
00066    inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00067 
00068    inline virtual bool invariant() const;
00069 
00070    virtual void printState(std::ostream &os) const;
00071 
00072    virtual unsigned int Length() const                  { return(buflen_); }
00073 
00074    /** Returns a reference to the byte at index bnum.
00075     *
00076     * If bnum is out of range, the behavior is undefined.
00077     */
00078    inline U1Byte &operator [](unsigned int bnum);
00079    /** Gets a void pointer to at least Length() bytes of data.
00080     *
00081     * This guarantees that you can read or write to any byte in the valid
00082     * range using this pointer as long as you make no calls to resize() in
00083     * between accesses.
00084     */
00085    inline void *getVoidP();
00086    /** Gets a <code>U1Byte</code> pointer to at least <code>Length()</code>
00087     * bytes of data.
00088     *
00089     * This guarantees that you can read or write to any byte in the valid
00090     * range using this pointer as long as you make no calls to resize() in
00091     * between accesses.
00092     */
00093    inline U1Byte *getCharP();
00094 
00095    /** Change the size of the chunk to newsize.
00096     *
00097     * Throws bad_alloc if the allocation fails, just like operator new.
00098     */
00099    virtual void resize(unsigned int newsize) throw(std::bad_alloc) = 0;
00100 
00101  protected:
00102    virtual const lcore::ClassIdent *i_GetIdent() const  { return &identifier; }
00103 
00104    virtual void acceptVisitor(ChunkVisitor &visitor)
00105       throw(ChunkVisitor::halt_visitation);
00106 
00107  protected:
00108    /** <code>buf_</code> is expected to be maintained by the derived class.
00109     *
00110     * This exists so that the above inline functions are truly inline.  It's
00111     * only allowed to change in the constructor, or in respone to the resize()
00112     * method being called.
00113     *
00114     * <code>buf_</code> is required to be non-null when <code>buflen_</code>
00115     * is greater than 0.  */
00116    void *buf_;
00117    /** <code>buflen_</code> is expected to be maintained by the derived class.
00118     *
00119     * This exist so that the above inline functions are truly inline.  It's
00120     * only allowed to change in the constructor, or in respone to the resize()
00121     * method being called.
00122     */
00123    unsigned int buflen_;
00124 
00125  private:
00126    static U1Byte junk_;
00127 
00128    // Left undefined on purpose.
00129    void operator =(const BufferChunk &b);
00130    BufferChunk(const BufferChunk &b);
00131 };
00132 
00133 //-----------------------------inline functions--------------------------------
00134 
00135 inline int BufferChunk::AreYouA(const lcore::ClassIdent &cid) const
00136 {
00137    return((identifier == cid) || StrChunk::AreYouA(cid));
00138 }
00139 
00140 inline bool BufferChunk::invariant() const
00141 {
00142    return((buflen_ == 0) || (buf_ != 0));
00143 }
00144 
00145 inline lcore::U1Byte &BufferChunk::operator [](unsigned int bnum)
00146 {
00147    return((bnum < buflen_) ? static_cast<U1Byte *>(buf_)[bnum] : junk_);
00148 }
00149 
00150 inline void *BufferChunk::getVoidP()
00151 {
00152    return((buflen_ > 0) ? buf_ : &junk_);
00153 }
00154 
00155 inline lcore::U1Byte *BufferChunk::getCharP()
00156 {
00157    return((buflen_ > 0) ? static_cast<U1Byte *>(buf_) : &junk_);
00158 }
00159 
00160 }  // namespace strmod
00161 }  // namespace strmod
00162 
00163 #endif

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