00001 00020 #ifndef MANGO_USAGE_SHARING_H 00021 #define MANGO_USAGE_SHARING_H 00022 00023 #include "ValidityOwner.hh" // contain 00024 00030 namespace MangoPtrPrivateNamespace 00031 { 00046 class PtrUsageSharing 00047 { 00048 protected: 00050 PtrUsageSharing(): initialized(false) {} 00052 PtrUsageSharing(const PtrUsageSharing& rhs) 00053 : usageVal(rhs.usageVal) 00054 { 00055 assert(usageVal.isShared()); 00056 assert(rhs.initialized); 00057 initialized = true; 00058 } 00060 void operator=(const PtrUsageSharing& rhs) 00061 { 00062 assert(rhs.initialized); 00063 initialized = true; 00064 00065 usageVal = rhs.usageVal; 00066 assert(usageVal.isShared()); 00067 } 00069 void swap(PtrUsageSharing& rhs) 00070 { 00071 usageVal.swap(rhs.usageVal); 00072 std::swap(initialized, rhs.initialized); 00073 } 00074 00075 public: 00077 void invalidateUsage() { usageVal.setValid(false); } 00079 void unlinkNew() 00080 { 00081 if (initialized) // optimized 00082 { 00083 // don't bother resetting if not shared 00084 usageVal.reset(false); 00085 // else don't care 00086 initialized = false; 00087 } 00088 else 00089 { 00090 // uninitialized ==> invalid (default state) 00091 assert(! usageVal.isValid()); 00092 // also implies has never been shared since sharing 00093 // requires initialization 00094 assert(! usageVal.isShared()); 00095 } 00096 } 00102 const MangoPtr::ValidityOwner& usage() const 00103 { 00104 assert(initialized); 00105 return usageVal; 00106 } 00107 00108 protected: 00116 void setFrom(void* optr) 00117 { 00118 if (!initialized) 00119 { 00120 usageVal.setValid( MangoPtr::isNotNull(optr) ); 00121 initialized = true; 00122 } 00123 } 00125 bool wasUsageInitialized() const {return initialized;} 00126 00127 protected: 00128 MangoPtr::ValidityOwner usageVal; 00129 00130 private: 00131 bool initialized; 00132 }; 00133 00134 } // end namespace MangoPtrPrivateNamespace 00135 00136 #endif // MANGO_USAGE_SHARING_H 00137