MPMA Framework 0.4
|
00001 00002 //Luke Lenhart (2008) 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 #ifndef REFERENCECOUNT_H_INCLUDED 00006 #define REFERENCECOUNT_H_INCLUDED 00007 00008 #include "Types.h" 00009 00010 namespace MPMA 00011 { 00013 template <typename DataType> 00014 class ReferenceCountedData 00015 { 00016 public: 00018 ReferenceCountedData(); 00019 00021 virtual ~ReferenceCountedData(); 00022 00024 ReferenceCountedData(const ReferenceCountedData &other); 00026 ReferenceCountedData& operator=(const ReferenceCountedData &other); 00027 00029 inline bool SharesDataWith(const ReferenceCountedData<DataType> &o) 00030 { return referencedData==o.referencedData; } 00031 00032 protected: 00034 inline DataType& Data() 00035 { return referencedData->data; } 00037 inline const DataType& Data() const 00038 { return referencedData->data; } 00039 00041 inline nuint ReferenceCount() const 00042 { return referencedData->count; } 00043 00045 inline bool IsOnlyReference() const 00046 { return ReferenceCount()==1; } 00047 00049 void CreateNewData(); 00050 00051 private: 00052 struct ReferencedData 00053 { 00054 volatile nuint count; 00055 DataType data; 00056 }; 00057 00058 ReferencedData *referencedData; 00059 00060 //releases a reference and frees the data if needed 00061 void ReleaseReference(); 00062 }; 00063 } 00064 00065 //include template and inline implementations 00066 #ifndef REFERENCECOUNT_INCLUDE_INLINE 00067 #define REFERENCECOUNT_INCLUDE_INLINE 00068 #include "ReferenceCount.cpp" 00069 #endif 00070 00071 #endif //REFERENCECOUNT_H_INCLUDED