#include <BLink.hh>
Collaboration diagram for MangoPtr::BLink< OType >:
A BLink can be linked to owned containers, owned pointers, and objects that inherit from BLinkable<ForOwnedBy::This>. When the linkee (what is being linked to) is destroyed, it sets a flag that the BLink can check, that tells it that the linkee no longer exists. You can therefore always test whether resources linked to still exist or not.
Using a BLink<T> is simple: tie it to an Owned<DT>, Owned<DT*>, BLinkable<> or another BLink<DT> (DT is T or a class derived from T) when you construct the BLink or via a call to tieTo() after construction. Then do usual pointer-like operations (dereference etc), and call isNull() or isNotNull() to test whether or not the pointee still exists. Tying a BLink to another BLink is like tying it to the object that the other BLink is tied to. All BLinks tied to the same object can tell if the resource still exists or not.
The op= operator only accepts another BLink since this is the only use that makes sense. It is equivalent to tying a BLink to the same Owned or BLinkable<> as the BLink on the RHS.
There is nothing to prevent you from tying a BLink to an Owned that doesn't own anything (because Owned was not initialized, or what it owned was discarded or released()).
Here is an example of usage. You can find another in the MangoPtr::Owned<T> docs:
class Window; // some sort of window class class Foo {...}; void Foo::setWindow(const BLink<Window>& win_arg) { win = win_arg; // later, foo can check win.isNotNull() to see if // the win window still exists } int main() { Window mainApp; mainApp.createWindows(); Foo foo; // assume mainApp.getRootWindow() returns a BLink foo.setWindow( mainApp.getRootWindow() ); }
The different transfer of ownership for owned containers vs owned pointers has an impact on BLinks.
void func(const BLink<const T>&); // will only use pointer void gunc(BLink<const T>&); // will likely change sptrship //... BLink<T> sptr; BLink<const T> sptrConst; func(sptrConst); // ok gunc(sptrConst); // ok func(sptr); // ok gunc(sptr); // compile error since can't convert
See the note in the MangoPtr::Owned<T*> docs regarding the relationship between BLink<T> and BLink<const T> in terms of implementation.
Definition at line 45 of file BLink.hh.
Public Types | |
typedef OType | ObjectType |
Class of object held, a la auto_ptr. | |
Public Methods | |
Constructors | |
BLink () throw () | |
Empty BLink. Can't be used until tied to an owner. | |
BLink (const BLink &) | |
Empty BLink. Can't be used until tied to an owner. | |
template<typename OType2> | BLink (const BLink< OType2 > &) |
Tie a user to same owner as rhs is tied to. | |
BLink (OType &) | |
Create from an object that derives publically from BLinkable<>. | |
template<typename OType2, OwnershipType oshiptype> | BLink (const Owned< OType2, oshiptype > &) |
Create from an Owned. | |
Mutators | |
template<typename OType2, OwnershipType oshiptype> void | tieTo (const Owned< OType2, oshiptype > &) |
Tie this BLink to an Owned. | |
void | tieTo (OType &) |
Tie to an object. | |
BLink & | operator= (const BLink &) |
Make this BLink use the same pointee as rhs. | |
template<typename OType2> BLink & | operator= (const BLink< OType2 > &) |
Same as operator=(const BLink &) but for BLink of different type. | |
void | swap (BLink &) throw () |
Swap two BLink's. | |
void | reset () |
reset to default-constructed state | |
Query/Utility | |
bool | isNotNull () const throw () |
True only if pointee still alive. | |
bool | isNull () const throw () |
Return true if resource linked to no longer exists. | |
PType | cloneObjNew () const |
Clone the pointee using new. | |
Operators | |
operator const BLink () const | |
There is no difference in the layout of a BLink<T> vs that of a BLink<const T>. | |
template<typename OType2> bool | operator< (const BLink< OType2 > &) const throw () |
For sorted STL containers (amongst other things). | |
template<typename OType2> bool | operator== (const BLink< OType2 > &) const throw () |
Compare to another BLink. | |
template<typename OType2> bool | operator!= (const BLink< OType2 > &) const throw () |
Compare to another BLink. | |
CheckedPtr< OType > | operator() () const |
Use to dereference the BLink. | |
OType * | ptr () const |
Use this to get pointer to object blinked-to. | |
Friends | |
class | BLink |
make conversion of OType possible |
|
Create from an object that derives publically from BLinkable<>. The validity of the BLink will be tied to the lifetime of oobj. |
|
This allows the caller to use the pointee managed by the owner. BLink will be invalid if owner is invalid. Definition at line 238 of file BLink.hh. References MangoPtr::SharedValidity::isShared(), and MangoPtr::SharedValidity::isValid(). |
|
Tie to an object. Class OType must inherit from BLinkable<ForOwnedBy::This>. |
|
Make this BLink use the same pointee as rhs. This is equivalent to calling tieTo() on the owner of rhs (which is not always available at the point of call). Definition at line 203 of file BLink.hh. References MangoPtr::SharedValidity::isShared(), MangoPtr::SharedValidity::isValid(), MangoPtr::BLink< OType >::optr, and MangoPtr::BLink< OType >::validator. |
|
Clone the pointee using new. This is just a convenience method that does the call to new for you, but is otherwise pure gravy. If you object requires a special call, do instead something like: // assume an owner exists somewhere BLink<T> blink( owner ); Owned<T*> newOwner( blink()->clone() ); // newOwner now has a clone of the original pointee |
|
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). |
|
Compare to another BLink. Same iff the pointers being stored are same. |
|
Compare to another BLink. Different iff the pointers being stored are different. |
|
Use to dereference the BLink. I.e., for BLink<Foo> use blink()->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. |