Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 = (char*)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
00083 DataBuffer (char *iData, size_t iSize, bool should_delete,
00084 const Allocator& alloc)
00085 : scfImplementation1<DataBuffer, iDataBuffer> (this), Data (alloc),
00086 Size (iSize), do_delete (should_delete)
00087 {
00088 Data.p = iData;
00089 }
00090
00092 DataBuffer (iDataBuffer *source, bool appendNull = true)
00093 : scfImplementation1<DataBuffer, iDataBuffer> (this), Size (source->GetSize()),
00094 do_delete (true)
00095 {
00096 if (appendNull)
00097 {
00098 Data.p = (char*)Data.Alloc (Size);
00099 memcpy (Data.p, source->GetData(), Size);
00100 }
00101 else
00102 {
00103 Data.p = (char*)Data.Alloc (Size+1);
00104 memcpy (Data.p, source->GetData(), Size);
00105 Data.p[Size] = 0;
00106 }
00107 }
00108
00110 virtual ~DataBuffer ()
00111 {
00112 if (do_delete)
00113 Data.Free (Data.p);
00114 }
00115
00117 virtual size_t GetSize () const
00118 { return Size; }
00119
00121 virtual char* GetData () const
00122 { return Data.p; }
00123
00128 bool GetDeleteOnDestruct () const { return do_delete; }
00129 };
00130 }
00131
00135 typedef CS::DataBuffer<CS::Memory::AllocatorNewChar<false > > csDataBuffer;
00136
00137 #endif // __CS_DATABUF_H__