CrystalSpace

Public API Reference

csutil/databuf.h

Go to the documentation of this file.
00001 /*
00002     Crystal Space Data Buffer class
00003     Copyright (C) 2000 by Andrew Zabolotny
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_DATABUF_H__
00021 #define __CS_DATABUF_H__
00022 
00027 #include "csextern.h"
00028 #include "csutil/allocator.h"
00029 #include "csutil/scf_implementation.h"
00030 #include "iutil/databuff.h"
00031 
00032 namespace CS
00033 {
00040   template<class Allocator = Memory::AllocatorMalloc>
00041   class DataBuffer : public scfImplementation1<DataBuffer<Allocator>, 
00042                                                iDataBuffer>
00043   {
00045     Memory::AllocatorPointerWrapper<char, Allocator> Data;
00047     size_t Size;
00049     bool do_delete;
00050 
00051   public:
00053     DataBuffer (size_t iSize)
00054       : scfImplementation1<DataBuffer, iDataBuffer> (this), Size (iSize), 
00055       do_delete (true)
00056     {
00057       Data.p = (char*)Data.Alloc (Size);
00058     }
00060     DataBuffer (size_t iSize, const Allocator& alloc)
00061       : scfImplementation1<DataBuffer, iDataBuffer> (this), Data (alloc), 
00062       Size (iSize), do_delete (true)
00063     {
00064       Data.p = Data.Alloc (Size);
00065     }
00066 
00067 
00072     DataBuffer (char *iData, size_t iSize, bool should_delete = true)
00073       : scfImplementation1<DataBuffer, iDataBuffer> (this), Size (iSize), 
00074       do_delete (should_delete)
00075     {
00076       Data.p = iData; 
00077     }
00078 
00080     DataBuffer (iDataBuffer *source)
00081       : scfImplementation1<DataBuffer, iDataBuffer> (this), Size (source->GetSize()),
00082         do_delete (true)
00083     {
00084       Data.p = (char*)Data.Alloc (Size+1);
00085       memcpy (Data.p, source->GetData(), Size);
00086       Data.p[Size] = 0;
00087     }
00088 
00090     virtual ~DataBuffer ()
00091     {
00092       if (do_delete)
00093         Data.Free (Data.p);
00094     }
00095 
00097     virtual size_t GetSize () const
00098     { return Size; }
00099 
00101     virtual char* GetData () const
00102     { return Data.p; }
00103 
00108     bool GetDeleteOnDestruct () const { return do_delete; }
00109   };
00110 } // namespace CS
00111 
00115 typedef CS::DataBuffer<CS::Memory::AllocatorNewChar<false > > csDataBuffer;
00116 
00117 #endif // __CS_DATABUF_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3