00001
00020 #ifndef MANGO_OBJECT_SHARING_H
00021 #define MANGO_OBJECT_SHARING_H
00022
00023 #include "PtrUsageSharing.hh"
00024 #include "OwnershipTypes.hh"
00025
00026 namespace MangoPtr
00027 {
00035 template <OwnershipType oshiptype>
00036 class ObjSharing: public MangoPtrPrivateNamespace::PtrUsageSharing
00037 {
00038 public:
00040 ObjSharing(): MangoPtrPrivateNamespace::PtrUsageSharing() {}
00042 void swap(ObjSharing& rhs) { usageVal.swap(rhs.usageVal); }
00045 ObjSharing& operator()(void* optr) {
00046 MangoPtrPrivateNamespace::PtrUsageSharing::setFrom(optr);
00047 return *this;}
00048
00049 bool isValid(void* optr) const { return isNotNull(optr); }
00053 bool destructNeedsDelete(void* optr) const {return isValid(optr);}
00055 size_t getCountOwned() const {return 1;}
00057 size_t getCountUser() const {
00058 return usageVal.getRefCount() - getCountOwned();}
00059
00060 private:
00062 ObjSharing(const ObjSharing&);
00064 void operator=(const ObjSharing&);
00065 };
00066
00096 template <>
00097 class ObjSharing<OShared>
00098 : public MangoPtrPrivateNamespace::PtrUsageSharing
00099 {
00100 private:
00102 typedef MangoPtrPrivateNamespace::PtrUsageSharing US;
00103
00104 public:
00106 ObjSharing(): US() {}
00108 ObjSharing(const ObjSharing& rhs)
00109 : US(rhs), ownership(rhs.ownership)
00110 {
00111 if (! ownershipHasBeenShared())
00112 ownership.setValid(true);
00113 }
00115 void operator=(const ObjSharing& rhs)
00116 {
00117 usageVal = rhs.usageVal;
00118 ownership = rhs.ownership;
00119 if (!ownershipHasBeenShared()) ownership.setValid(true);
00120 }
00121
00123 void swap(ObjSharing& rhs)
00124 {
00125 US::swap(rhs);
00126 ownership.swap(rhs.ownership);
00127 }
00128
00129 public:
00134 ObjSharing& operator()(void* optr)
00135 {
00136 MangoPtrPrivateNamespace::PtrUsageSharing::setFrom(optr);
00137 return *this;
00138 }
00139
00141 void unlinkNew()
00142 {
00143 US::unlinkNew();
00144 ownership.reset(false);
00145 }
00146
00147 public:
00151 bool isValid(void* optr) const
00152 {
00153
00154
00155 assert((! ownershipHasBeenShared()) || wasUsageInitialized());
00156
00157 return ownershipHasBeenShared() ?
00158 usageVal.isValid() : isNotNull(optr);
00159 }
00160
00162 bool destructNeedsDelete(void* optr) const
00163 {
00164 return isValid(optr) && !ownership.isShared();
00165 }
00166
00168 size_t getCountOwned() const {return ownership.getRefCount();}
00170 size_t getCountUser() const
00171 {
00172
00173
00174 assert(usageVal.getRefCount() >= getCountOwned());
00175
00176 return usageVal.getRefCount() - getCountOwned();
00177 }
00178
00179 private:
00181 bool ownershipHasBeenShared() const {return ownership.isValid();}
00182
00183 private:
00185 ValidityOwner ownership;
00186 };
00187
00188 }
00189
00190 #endif // MANGO_OBJECT_SHARING_H
00191