00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __ZMemoryBlock__
00026 #define __ZMemoryBlock__ 1
00027 #include "zconfig.h"
00028
00029 #include "ZCompare.h"
00030 #include "ZRefCount.h"
00031 #include "ZStream.h"
00032
00033
00034 #pragma mark -
00035 #pragma mark * ZMemoryBlock
00036
00037 class ZMemoryBlock
00038 {
00039 ZOOLIB_DEFINE_OPERATOR_BOOL_TYPES(ZMemoryBlock,
00040 operator_bool_generator_type, operator_bool_type);
00041 public:
00042 class Rep;
00043
00044 ZMemoryBlock();
00045 ZMemoryBlock(size_t iSize);
00046 ZMemoryBlock(const void* iSourceData, size_t iSize);
00047 ZMemoryBlock(const ZMemoryBlock& iOther);
00048 ~ZMemoryBlock();
00049
00050 ZMemoryBlock& operator=(const ZMemoryBlock& iOther);
00051
00052 operator operator_bool_type() const;
00053
00054 int Compare(const ZMemoryBlock& iOther) const;
00055 bool operator<(const ZMemoryBlock& iOther) const;
00056 bool operator==(const ZMemoryBlock& iOther) const;
00057
00058 void Touch();
00059
00060 size_t GetSize() const;
00061 void SetSize(size_t iSize);
00062
00063 const void* GetData() const;
00064 void* GetData();
00065
00066 void CopyFrom(size_t iOffset, const void* iSource, size_t iCount);
00067 void CopyFrom(const void* iSource, size_t iCount)
00068 { this->CopyFrom(0, iSource, iCount); }
00069 void CopyTo(size_t iOffset, void* iDest, size_t iCount) const;
00070 void CopyTo(void* iDest, size_t iCount) const
00071 { this->CopyTo(0, iDest, iCount); }
00072
00073 private:
00074 ZRef<Rep> fRep;
00075 };
00076
00077 namespace ZooLib {
00078 template <> inline int sCompare_T(const ZMemoryBlock& iL, const ZMemoryBlock& iR)
00079 { return iL.Compare(iR); }
00080 }
00081
00082
00083 #pragma mark -
00084 #pragma mark * ZMemoryBlock::Rep
00085
00086 class ZMemoryBlock::Rep : public ZRefCounted
00087 {
00088 public:
00089 static bool sCheckAccessEnabled() { return false; }
00090
00091 void* operator new(size_t iObjectSize, size_t iDataSize);
00092 void operator delete(void* iPtr, size_t iObjectSize);
00093
00094 Rep();
00095 Rep(size_t inSize);
00096
00097 private:
00098 size_t fSize;
00099 char fData[0];
00100
00101 friend class ZMemoryBlock;
00102 };
00103
00104
00105 #pragma mark -
00106 #pragma mark * ZMemoryBlock::Rep inlines
00107
00108 inline void* ZMemoryBlock::Rep::operator new(size_t iObjectSize, size_t iDataSize)
00109 { return new char[iObjectSize + iDataSize]; }
00110
00111 inline void ZMemoryBlock::Rep::operator delete(void* iPtr, size_t iDataSize)
00112 { delete[] static_cast<char*>(iPtr); }
00113
00114 inline ZMemoryBlock::Rep::Rep()
00115 : fSize(0)
00116 {}
00117
00118 inline ZMemoryBlock::Rep::Rep(size_t iSize)
00119 : fSize(iSize)
00120 {}
00121
00122
00123 #pragma mark -
00124 #pragma mark * ZMemoryBlock inlines
00125
00126 inline ZMemoryBlock::ZMemoryBlock()
00127 : fRep(new (0) ZMemoryBlock::Rep)
00128 {}
00129
00130 inline ZMemoryBlock::ZMemoryBlock(size_t iSize)
00131 : fRep(new (iSize) ZMemoryBlock::Rep(iSize))
00132 {}
00133
00134 inline ZMemoryBlock::ZMemoryBlock(const ZMemoryBlock& iOther)
00135 : fRep(iOther.fRep)
00136 {}
00137
00138 inline ZMemoryBlock::~ZMemoryBlock()
00139 {}
00140
00141 inline ZMemoryBlock& ZMemoryBlock::operator=(const ZMemoryBlock& iOther)
00142 {
00143 fRep = iOther.fRep;
00144 return *this;
00145 }
00146
00147 inline ZMemoryBlock::operator operator_bool_type() const
00148 { return operator_bool_generator_type::translate(fRep->fSize); }
00149
00150 inline int ZMemoryBlock::Compare(const ZMemoryBlock& iOther) const
00151 {
00152 Rep* myRep = fRep.GetObject();
00153 Rep* otherRep = iOther.fRep.GetObject();
00154
00155 if (myRep == otherRep)
00156 return 0;
00157
00158 if (int result = memcmp(myRep->fData, otherRep->fData, min(myRep->fSize, otherRep->fSize)))
00159 return result;
00160
00161 return int(myRep->fSize) - int(otherRep->fSize);
00162 }
00163
00164 inline bool ZMemoryBlock::operator<(const ZMemoryBlock& iOther) const
00165 {
00166 Rep* myRep = fRep.GetObject();
00167 Rep* otherRep = iOther.fRep.GetObject();
00168
00169 if (myRep == otherRep)
00170 return false;
00171
00172 if (int result = memcmp(myRep->fData, otherRep->fData, min(myRep->fSize, otherRep->fSize)))
00173 return result < 0;
00174
00175 return fRep->fSize < otherRep->fSize;
00176 }
00177
00178 inline bool ZMemoryBlock::operator==(const ZMemoryBlock& iOther) const
00179 {
00180 Rep* myRep = fRep.GetObject();
00181 Rep* otherRep = iOther.fRep.GetObject();
00182
00183 if (myRep == otherRep)
00184 return true;
00185
00186 if (myRep->fSize != otherRep->fSize)
00187 return false;
00188
00189 return 0 == memcmp(myRep->fData, otherRep->fData, myRep->fSize);
00190 }
00191
00192 inline size_t ZMemoryBlock::GetSize() const
00193 { return fRep->fSize; }
00194
00195 inline const void* ZMemoryBlock::GetData() const
00196 { return fRep->fData; }
00197
00198 inline void* ZMemoryBlock::GetData()
00199 { return fRep->fData; }
00200
00201
00202 #pragma mark -
00203 #pragma mark * ZStreamRPos_MemoryBlock
00204
00205 class ZStreamRPos_MemoryBlock : public ZStreamRPos
00206 {
00207 public:
00208 ZStreamRPos_MemoryBlock(const ZMemoryBlock& iMemoryBlock);
00209 ~ZStreamRPos_MemoryBlock();
00210
00211
00212 virtual void Imp_Read(void* iDest, size_t iCount, size_t* oCountRead);
00213 virtual void Imp_Skip(uint64 iCount, uint64* oCountSkipped);
00214
00215
00216 virtual uint64 Imp_GetPosition();
00217 virtual void Imp_SetPosition(uint64 iPosition);
00218
00219 virtual uint64 Imp_GetSize();
00220
00221 private:
00222 ZMemoryBlock fMemoryBlock;
00223 uint64 fPosition;
00224 };
00225
00226
00227 #pragma mark -
00228 #pragma mark * ZStreamRWPos_MemoryBlock
00229
00230 class ZStreamRWPos_MemoryBlock : public ZStreamRWPos
00231 {
00232 public:
00233 ZStreamRWPos_MemoryBlock(ZMemoryBlock& iMemoryBlock, size_t iGrowIncrement);
00234 ZStreamRWPos_MemoryBlock(ZMemoryBlock& iMemoryBlock);
00235 ~ZStreamRWPos_MemoryBlock();
00236
00237
00238 virtual void Imp_Read(void* iDest, size_t iCount, size_t* oCountRead);
00239 virtual void Imp_Skip(uint64 iCount, uint64* oCountSkipped);
00240
00241
00242 virtual void Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten);
00243 virtual void Imp_Flush();
00244
00245
00246 virtual uint64 Imp_GetPosition();
00247 virtual void Imp_SetPosition(uint64 iPosition);
00248
00249 virtual uint64 Imp_GetSize();
00250
00251
00252 virtual void Imp_SetSize(uint64 iSize);
00253
00254 private:
00255 ZMemoryBlock& fMemoryBlock;
00256 size_t fGrowIncrement;
00257 uint64 fPosition;
00258 size_t fSizeLogical;
00259 };
00260
00261
00262
00263 #endif // __ZMemoryBlock__