Inheritance diagram for MangoPtr::ObjSharing< OShared >:
Shared ownership is more complicated than strict ownership. Strict ownership means there is only one owner. Regardless of how many BLink objects are sharing usage with the owner, the owner controls the life of the pointee. With shared ownership, the life of the pointee is governed by the owners sharing it: the pointee will be destroyed if the program does a release() or reset() on one of the owners, or when the last of the owners gets destroyed. Such operations will invalidate the usage of the pointee in all owners, but sets the pointer to null only in the owner who executes the release() or reset().
This requires that we track whether the ownership has been shared at least once: if it has never been shared, then the validity of the pointee is simply whether or not it is null; if it has, the validity is tracked via the usage validator. ObjSharing<OShared> takes care of this. The other thing is to track whether the ownership is currently shared, which determines whether, upon destruction of the owner containing ObjSharing<OShared>, the pointee should be deleted.
The only tricky aspect of using ObjSharing is to make sure that operator() is called before the first copy is done. It can safely be called everytime a copy is done. In debug mode, if this call hasn't been done, an assertion will fail because the validators must have been initialized before the first copy/assign, and operator() is the method that takes care of initialization.
Definition at line 98 of file ObjSharing.hh.
|ObjSharing (const ObjSharing &rhs)|
|void||operator= (const ObjSharing &rhs)|
|assignment to rhs|
|void||swap (ObjSharing &rhs)|
|swap with another ObjSharing<OShared>|
|ObjSharing &||operator() (void *optr)|
|Set the validity from the value of optr, if appropriate. |
|Reset sharing to default construction state.|
|bool||isValid (void *optr) const|
|Testing for validity depends on whether ownership has yet been shared or not. |
|bool||destructNeedsDelete (void *optr) const|
|Pointer needs delete only if valid and not shared.|
|size_t||getCountOwned () const|
|get how many owners are sharing ownership|
|size_t||getCountUser () const|
|get how many users are sharing usage|
Set the validity from the value of optr, if appropriate.
This must be called at least once before the first copy/assign of this ObjSharing instance is done.
Testing for validity depends on whether ownership has yet been shared or not.
See class docs for details.