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 = (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 } // namespace CS 00131 00135 typedef CS::DataBuffer<CS::Memory::AllocatorNewChar<false > > csDataBuffer; 00136 00137 #endif // __CS_DATABUF_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1