#include <OwnedPtr.hh>
Inheritance diagram for MangoPtr::Owned< OType *, oshiptype >:


An owned pointer differs from a normal pointer in that it owns the resource associated with the pointer. It is therefore responsible for destroying the resource, for enforcing clear transfer of ownership, and for allowing users of the resource to test whether the resource has been destroyed before using it.
The ownership is either strict (MangoPtr::OStrict) or shared (MangoPtr::OShared). If strict, only one owner can have the resource, and it is destroyed at the same time as that owner. If shared, then several owners can own the same pointer, and the resource they own will be destroyed when the last of the owners is destroyed.
The most important methods of Owned<T*> are:
Owned<Foo*> owner;
owner()->callFooMethod();
The resource usage can be shared with
struct Foo { // ... void doSomething() {} }; int main() { BLink<Foo> p1; { // first clone owner1 using a call to new(): Owned<Foo*> owner( new Foo ); owner()->doSomething(); // now tie p1 to owner p1.tieTo( owner ); assert( p1.isNotNull() ); p1()->doSomething(); // work with owned Foo } // owner dies, its Foo is destroyed // so its resource is no longer valid: assert( p1.isNull() ); }
Note that a function that expects an Owned<const T*> can be given an Owned<T*>, as would be legal with raw pointers:
void func(const Owned<const T*>&); // will only use pointer void gunc(Owned<const T*>&); // will likely change ownership //... Owned<T*> owner; Owned<const T*> ownerConst; func(ownerConst); // ok gunc(ownerConst); // ok func(owner); // ok, can convert gunc(owner); // compile error since can't convert
Definition at line 49 of file OwnedPtr.hh.
Public Types | |
| typedef OType | ObjectType |
| Class of object held, a la auto_ptr. | |
Public Methods | |
| ~Owned () | |
| Free memory of pointee and invalidate. | |
Constructors | |
| Owned () | |
| Empty owner, doens't own anything. | |
| Owned (const Owned &) | |
| Construct copy from an Owned of same type. | |
| template<typename O2Type> | Owned (const Owned< O2Type, oshiptype > &) |
| Construct copy from an Owned of derived type. | |
| Owned (OType *) | |
| Create owner of pointer p. | |
| template<typename O2Type> | Owned (std::auto_ptr< O2Type > &) |
| Take ownership of the pointee owned by a std::auto_ptr. | |
| template<typename O2Type> | Owned (Ownership< O2Type, oshiptype >) |
| Create an owner from an ownership Original owner looses ownership. | |
Mutators | |
| void | swap (Owned &rhs) |
| Swap with another owner rhs. | |
| PType | release () |
| Release pointee from ownership by this Owned. | |
| void | reset () |
| reset to default-construted state | |
| Ownership< PType, oshiptype > | giveOwnership () |
| Give ownership to another object. | |
| Owned & | operator= (const Owned &rhs) |
| Assign from an owned pointer of different type. Only allowed for shared ownership. | |
| template<typename O2Type> Owned & | operator= (const Owned< O2Type, oshiptype > &rhs) |
| Assign from an owned pointer of different type Only allowed for shared ownership. | |
| void | takeOwnership (OType *p) |
| Assume ownership of a pointee. | |
| template<typename O2Type> void | takeOwnership (std::auto_ptr< O2Type > &) |
| Transfer ownership from a std::auto_ptr. | |
| template<typename O2Type> void | takeOwnership (Ownership< O2Type, oshiptype >) |
| Accept ownership from another Owned. | |
Utility methods | |
| PType | cloneObjNew () const |
| Get a dumb pointer clone of the pointee. | |
| PType | shareUsage (ValidityChecker &) const |
| Other classes that may be able to share the usage of the pointee can do so by calling this method with a ValidityChecker. | |
| operator const Owned () const | |
| There is no difference in the layout of a BLink<T> vs that of a BLink<const T>. | |
| bool | isNotNull () const |
| Returns true if ptr owned is not null. | |
| bool | isNull () const |
| Returns true if ptr owned is null. | |
| size_t | getCountUser () const |
| How many owner are sharing ownership of pointer. | |
| size_t | getCountOwned () const |
| How many owner are sharing ownership of pointer. | |
| bool | owns (PType rhs) const |
| Is rhs same as pointer owned? Returns false if rhs=null. | |
| template<typename O2Type> bool | owns (const BLink< O2Type > &rhs) const |
| Is rhs BLink the pointee owned by us? Also returns false if rhs.isNull(). | |
| CheckedPtr< OType > | operator() () const |
| Use to dereference the Owned pointer. | |
| OType * | ptr () const |
| Get the stored pointer directly; get Null if pointer isNull(). | |
| bool | operator== (const Owned &rhs) const |
| Compare two owners. Equal if both hold same pointer, OR if both hold null pointer. | |
| bool | operator!= (const Owned &rhs) const |
| Compare two owners. Two owners are always unequal. | |
| bool | operator< (const Owned &rhs) const |
| Compare two owners for sorting. | |
Protected Types | |
| typedef OType * | PType |
| For clarity, pointer type. | |
Friends | |
| class | Owned |
| make inter-type operations possible | |
|
||||||||||||||
|
Take ownership of the pointee owned by a std::auto_ptr. After construction of the Owned, the auto_ptr is invalid. Definition at line 230 of file OwnedPtr.hh. References MangoPtr::isNull(), and MangoPtr::NullPtr. |
|
||||||||||||||
|
Create an owner from an ownership Original owner looses ownership.
Definition at line 243 of file OwnedPtr.hh. |
|
|||||||||
|
Release pointee from ownership by this Owned. All BLink's, and other Owneds if shared ownership, tied to *this will think pointee is no longer valid. This is necessary because the release means the validity can no longer be managed.
Definition at line 406 of file OwnedPtr.hh. |
|
|||||||||
|
Give ownership to another object. If the ownership is accepted by that object, the current owner will show isNull() as true. All BLink<T>'s that were tied to this owner will be tied to the new owner. Definition at line 357 of file OwnedPtr.hh. |
|
||||||||||
|
Assume ownership of a pointee.
Definition at line 294 of file OwnedPtr.hh. |
|
||||||||||||||
|
Transfer ownership from a std::auto_ptr.
Definition at line 307 of file OwnedPtr.hh. References MangoPtr::isNull(), MangoPtr::nullify(), and MangoPtr::Ownership< OType, oshiptype >::owner. |
|
||||||||||||||
|
Accept ownership from another Owned. If we already own an object, the object is first destroyed, and any users that were sharing the destroyed pointee know that the pointee is no longer valid.
Definition at line 323 of file OwnedPtr.hh. |
|
|||||||||
|
Get a dumb pointer clone of the pointee. This is used when copying owners. It can also be used just to get a dumb pointer clone. Since this method can end up being called automatically by compiler when copying Owned, an exception is used instead of assertion since user can often not avoid cloneObj(). Definition at line 381 of file OwnedPtr.hh. |
|
||||||||||
|
Other classes that may be able to share the usage of the pointee can do so by calling this method with a ValidityChecker. The pointer returned is a pointer to the object owned. Note that this method is only to share usage, not ownership. There is no shared ownership. Definition at line 448 of file OwnedPtr.hh. |
|
|||||||||
|
There is no difference in the layout of a BLink<T> vs that of a BLink<const T>. Since converting from a BLink<T> to a const BLink<const T> is often necessary, to mimic the behavior of raw pointers, we have to provide some way of creating a const BLink<const T> from a BLink<T>. This conversion operator is efficient since it just returns a reference to *this, but properly (and portably, ie standardly) cast. Note that converting to a non-const BLink<const T> is not allowed since this would allow (inadvertant) violation of const correctness. This technique is better than the inheritance scheme previously used only because it is more maintainable (and possibly less code is generated). Definition at line 431 of file OwnedPtr.hh. References MangoPtr::isNotNull(). |
|
|||||||||
|
Returns true if ptr owned is null. Note: use isNotNull() instead of isNotNull() for better performance. Definition at line 141 of file OwnedPtr.hh. |
|
|||||||||
|
Use to dereference the Owned pointer. I.e., for Owned<Foo*> use owned()->method() where method() is a method of Foo. The distinguishes pure use of pointer, vs dereferencing of pointer, which requires a check that pointer is not null. Definition at line 157 of file OwnedPtr.hh. |
1.2.18