Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Namespace Members Compound Members File Members Related Pages
I created Mango-ptr because:
Mango-ptr addresses all those issues. It makes explicit the role of ownership as opposed to simply usage:
- Using raw pointers is error-prone:
- The preferable use of a pointer in C++ is by the one-owner-many-users idiom, yet C++ does not enforce this or provide support to enforce it;
- It is easy to mistakenly have many owners (ie multiple delete, dangling pointer), or no owner (memory leak)
- Users have no way of knowing when pointees have been destroyed
- Pointers are copied by value, which leads to implicit shallow copies; a mechanism to point out that a copy is attempted is needed;
- Transfer of ownership is implicit (ie not obvious from reading code);
- Std::auto_ptr is a strict owner (one owner, many users), but is "incomplete":
- it allows copy but instead does a move, so transfer of ownership is hidden or implicit,
- users are limited to being raw pointers, i.e. you can't test that a pointer held in a std::auto_ptr has been destroyed;
- Boost::shared_ptr, a superior smart-pointer for its simplicity, performance and thread-safety, implements only shared ownership. This implies 'non-deterministic' destruction of the resource held (because only gets destroyed when the last shared_ptr is destroyed, and you could easily forget that you've copied the shared_ptr somewhere else in your program), and suffers from cyclical references;
- I often need something that can automatically destroy elements of a container of pointers, so the same dumb loop doesn't have to be repeated throughout my code, yet I don't want to wrap every element in a shared pointer; also to avoid unexpected shallow copies of containers of pointers;
These classes were designed to provide various safety features useful for pointers:
- MangoPtr::Owned<T> is an owned container of pointers;
- MangoPtr::Owned<T*> is a pointer-to-T that is owned;
- MangoPtr::BLink<T> is a pointer-to-T that is simply for usage (it has no concept of ownership);
- MangoPtr::Ownership<T> forces the transfer of ownership from one Owned<T*> to another to be explicit and easy to discern in your code;
- MangoPtr::BLinkable<> is the base class of any object that can be used by a BLink.
In addition, Mango-ptr has the following capabilities:
- Use assertions in debug mode to automatically test for existence of resource when used (pointee dereferenced), in case you choose to assume validity instead of checking via method call.
- Support safe passing of Type<T> when a Type<const T> is expected, where Type is Owned or BLink.
- For an owner of strict ownership, copy/assignment are disallowed, since they don't make sense; this prevents shallow copies from going unnoticed and still gives you the freedom of deciding how the copy should take place (via new(), via a clone method, etc).
- template instantiation means that only the functions you use are instantiated in your code;
- inlines are used to eliminate function calls unless really necessary
- Pointee lifetime is deterministic for strict ownership: pointee destroyed at same time as the Owned that owns it. This is the same as std::auto_ptr.
- The explicit strict ownership makes it clear who owns what and how long the resource will be used.
- Clear transfer of ownership: via the takeOwnership/giveOwnership, the ownership of a pointee can be followed through an algorithm easily.
- Owned containers allow STL containers of pointers to be used without using shared owned pointers (strict owned pointers can't be used because of the value semantics of STL container elements).
- Ownership is represented as a transaction: there must be a giver and a receiver for the transaction to be completed, ie for the ownership to be successfully transfered.
- Pointee can be tested for validity to avoid use of dangling pointer; all BLink's will show isNull()=true when Owned has freed resource
- Efficient implementation and strong exception safe
- Constness and polymorphism supported
- Supports "usage validation without owner", for cases where e.g. self-registration is desired via a BLink
Generated on Tue Nov 12 20:44:01 2002 for Mango-ptr Library by