src/foundation/ZHandle.h

00001 /*  @(#) $Id: ZHandle.h,v 1.12 2006/04/10 20:44:20 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2000 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZHandle__
00026 #define __ZHandle__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZCompat_NonCopyable.h"
00030 #include "ZRefCount.h"
00031 #include "ZStreamer.h"
00032 
00033 #include <cstddef> // For size_t
00034 
00035 #if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
00036 #       include <MacMemory.h>
00037 #endif
00038 
00039 // =================================================================================================
00040 #pragma mark -
00041 #pragma mark * ZHandle
00042 
00043 class ZHandle
00044         {
00045 public:
00046         class Rep;
00047 
00048         class Ref;
00049         friend class Ref;
00050 
00051         class ConstRef;
00052         friend class ConstRef;
00053 
00054         ZHandle();
00055         explicit ZHandle(size_t iInitialSize);
00056         ZHandle(const void* iSourceData, size_t iSize);
00057         ZHandle(const ZHandle& iOther);
00058         ~ZHandle();
00059 
00060         ZHandle& operator=(const ZHandle& iOther);
00061 
00062         size_t GetSize() const;
00063         void SetSize(size_t iSize);
00064 
00065         void CopyFrom(const void* sSource, size_t sSize, size_t iOffset);
00066         void CopyFrom(const void* sSource, size_t iSize)
00067                 { this->CopyFrom(sSource, iSize, 0); }
00068 
00069         void CopyTo(void* iDest, size_t iSize, size_t iOffset) const;
00070         void CopyTo(void* iDest, size_t iSize) const
00071                 { this->CopyTo(iDest, iSize, 0); }
00072 
00073         void Touch();
00074 
00075         #if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
00076                 Handle GetMacHandle();
00077         #endif
00078 
00079 private:
00080         ZRef<Rep> fRep;
00081         };
00082 
00083 // =================================================================================================
00084 #pragma mark -
00085 #pragma mark * ZHandle::Rep
00086 
00087 class ZHandle::Rep : public ZRefCounted
00088         {
00089 public:
00090         Rep();
00091         Rep(size_t iSize);
00092         ~Rep();
00093 
00094 private:
00095         ZThreadSafe_t fLockCount;
00096 
00097         #if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon)
00098                 Handle fMacHandle;
00099         #else
00100                 char* fData;
00101                 size_t fSize;
00102         #endif
00103 
00104         friend class ZHandle;
00105         friend class ZHandle::Ref;
00106         friend class ZHandle::ConstRef;
00107         };
00108 
00109 // =================================================================================================
00110 #pragma mark -
00111 #pragma mark * ZHandle::Ref
00112 
00113 class ZHandle::Ref : ZooLib::NonCopyable
00114         {
00115 public:
00116         explicit Ref(ZHandle& iHandle);
00117         ~Ref();
00118         void* GetData();
00119         size_t GetSize();
00120 
00121 private:
00122         ZHandle& fHandle;
00123         };
00124 
00125 // =================================================================================================
00126 #pragma mark -
00127 #pragma mark * ZHandle::ConstRef
00128 
00129 class ZHandle::ConstRef : ZooLib::NonCopyable
00130         {
00131 public:
00132         explicit ConstRef(const ZHandle& iHandle);
00133         ~ConstRef();
00134         const void* GetData();
00135         size_t GetSize();
00136 
00137 private:
00138         const ZHandle& fHandle;
00139         };
00140 
00141 // =================================================================================================
00142 #pragma mark -
00143 #pragma mark * ZStreamRWPos_Handle
00144 
00145 class ZStreamRWPos_Handle : public ZStreamRWPos
00146         {
00147 public:
00148         ZStreamRWPos_Handle(ZHandle& iHandle, size_t iGrowIncrement);
00149         ZStreamRWPos_Handle(ZHandle& iHandle);
00150         ~ZStreamRWPos_Handle();
00151 
00152 // From ZStreamR via ZStreamRWPos
00153         virtual void Imp_Read(void* iDest, size_t iCount, size_t* oCountRead);
00154         virtual void Imp_Skip(uint64 iCount, uint64* oCountSkipped);
00155 
00156 // From ZStreamW via ZStreamRWPos
00157         virtual void Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten);
00158         virtual void Imp_Flush();
00159 
00160 // From ZStreamRPos/ZStreamWPos via ZStreamRWPos
00161         virtual uint64 Imp_GetPosition();
00162         virtual void Imp_SetPosition(uint64 iPosition);
00163 
00164         virtual uint64 Imp_GetSize();
00165 
00166 // From ZStreamWPos via ZStreamRWPos
00167         virtual void Imp_SetSize(uint64 iSize);
00168 
00169 private:
00170         ZHandle& fHandle;
00171         size_t fGrowIncrement;
00172         uint64 fPosition;
00173         size_t fSizeLogical;
00174         };
00175 
00176 #endif // __ZHandle__

Generated on Thu Jul 26 11:21:51 2007 for ZooLib by  doxygen 1.4.7