datachunk.h

Go to the documentation of this file.
00001 
00007 /*
00008  *  Copyright (c) 2003, Matt Beard
00009  *
00010  *  This software is provided 'as-is', without any express or implied warranty.
00011  *  In no event will the authors be held liable for any damages arising from
00012  *  the use of this software.
00013  *
00014  *  Permission is granted to anyone to use this software for any purpose,
00015  *  including commercial applications, and to alter it and redistribute it
00016  *  freely, subject to the following restrictions:
00017  *
00018  *    1. The origin of this software must not be misrepresented; you must
00019  *       not claim that you wrote the original software. If you use this
00020  *       software in a product, an acknowledgment in the product
00021  *       documentation would be appreciated but is not required.
00022  *  
00023  *    2. Altered source versions must be plainly marked as such, and must
00024  *       not be misrepresented as being the original software.
00025  *  
00026  *    3. This notice may not be removed or altered from any source
00027  *       distribution.
00028  */
00029 
00030 #ifndef MXFLIB__DATACHUNK_H
00031 #define MXFLIB__DATACHUNK_H
00032 
00033 namespace mxflib
00034 {
00035     // Forward declare so the class can be used
00036     class DataChunk;
00037 
00039     typedef SmartPtr<DataChunk> DataChunkPtr;
00040 
00042     typedef std::list<DataChunkPtr> DataChunkList;
00043 }
00044 
00045 
00046 namespace mxflib
00047 {
00048     class DataChunk : public RefCount<DataChunk>
00049     {
00050     private:
00051         size_t DataSize;                        
00052         size_t AllocationGranularity;           
00053         bool ExternalBuffer;                    
00054 
00055     public:
00056         size_t Size;                            
00057         UInt8 *Data;                            
00058 
00060         DataChunk() : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) {};
00061 
00063         DataChunk(size_t BufferSize) : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) { Resize(BufferSize); };
00064 
00066         DataChunk(size_t MemSize, const UInt8 *Buffer) : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) { Set(MemSize, Buffer); };
00067 
00069         template<int SIZE> DataChunk(const Identifier<SIZE> *ID)  : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) { Set(ID->Size(), ID->GetValue() ); }
00070 
00072         DataChunk(const DataChunk &Chunk) : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) { Set(Chunk.Size, Chunk.Data); };
00073 
00075         DataChunk(const DataChunkPtr &Chunk) : DataSize(0), AllocationGranularity(0), ExternalBuffer(false), Size(0), Data(NULL) { Set(Chunk->Size, Chunk->Data); };
00076 
00077         ~DataChunk() 
00078         { 
00079             if((!ExternalBuffer) && (Data)) delete[] Data; 
00080         };
00081 
00083         void Resize(size_t NewSize, bool PreserveContents = true);
00084 
00086 
00087         void ResizeBuffer(size_t NewSize, bool PreserveContents = true);
00088 
00090 
00096         UInt8 *StealBuffer(bool MakeEmpty = false);
00097 
00099         void Set(const DataChunk &Buffer, size_t Start = 0)
00100         {
00101             Set(Buffer.Size, Buffer.Data, Start);
00102         }
00103 
00105         void Set(const DataChunkPtr &Buffer, size_t Start = 0)
00106         {
00107             Set(Buffer->Size, Buffer->Data, Start);
00108         }
00109 
00111         void Set(size_t MemSize, const UInt8 *Buffer, size_t Start = 0)
00112         {
00113             if(Size < (MemSize + Start)) Resize(MemSize + Start);
00114 
00115             memcpy(&Data[Start], Buffer, MemSize);
00116         }
00117 
00119         void Append(const DataChunk &Buffer)
00120         {
00121             Set(Buffer.Size, Buffer.Data, Size);
00122         }
00123 
00125         void Append(const DataChunkPtr &Buffer)
00126         {
00127             Set(Buffer->Size, Buffer->Data, Size);
00128         }
00129 
00131         void Append(size_t MemSize, const UInt8 *Buffer)
00132         {
00133             Set(MemSize, Buffer, Size);
00134         }
00135 
00136         DataChunk& operator=(const DataChunk &Right)
00137         {
00138             Set(Right.Size, Right.Data);
00139 
00140             return *this;
00141         }
00142 
00143         bool operator==(const DataChunk &Right) const
00144         {
00145             if(Size != Right.Size) return false;
00146             
00147             if(memcmp(Data, Right.Data, Size) == 0) return true;
00148 
00149             return false;
00150         }
00151 
00152         bool operator!=(const DataChunk &Right) const { return !operator==(Right); };
00153 
00155         std::string GetString(void);
00156 
00158         void SetGranularity(size_t Gran) { AllocationGranularity = Gran; };
00159         size_t GetGranularity(void) { return AllocationGranularity; };
00160 
00162 
00168         void SetBuffer(UInt8 *Buffer, size_t BuffSize, size_t AllocatedSize = 0);
00169 
00171 
00175         bool TakeBuffer(DataChunk &OldOwner, bool MakeEmpty = false);
00176 
00178 
00182         bool TakeBuffer(DataChunkPtr &OldOwner, bool MakeEmpty = false);
00183     };
00184 }
00185 
00186 #endif // MXFLIB__DATACHUNK_H

Generated on Mon Apr 2 15:20:51 2007 for MXFLib by  doxygen 1.5.1-p1