#include <RRef.hh>
It is the NoPtr equivalent of a normal reference, but more versatile in that the reference can be reseated. It is also more versatile in that, in debug mode (i.e. the -DNDEBUG compiler flag is NOT given), it will assert that the referenced object actually exists before any access to it is attempted. In a release build, the check is not done, so the cost of this feature is 0 when it matters.
The main methods:
Example 1:
class Foo: public RRefable { void doSomething() const; }; void getFooRef(RRef<const Foo>& fooRef) { Foo realFoo; realFoo.doSomething(); foo = realFoo; // foo is now a reference to realFoo assert(foo.isNotNull()); foo().doSomething(); // same as last call // oops, forgot that realFoo gets destroyed here, // means fooRef is dangling! } int main() { RRef<const Foo> foo( getFooRef() ); assert(foo.isNull()); foo().doSomething(); // causes an assertion to fail if !NDEBUG }
Example 2:
class HoldsADynObj { DynObj<Foo> foo; public: HoldsADynObj(): foo(new Foo(8,3)) {} RRef<const Foo> getFoo() const {return foo;} }; int main() { HoldsADynObj* holder = new HoldsADynObj; RRef<const Foo> foo( holder->getFoo() ); assert(foo.isNotNull()); foo().doSomething(); delete HoldsADynObj; assert(foo.isNull()); foo().doSomething(); // causes assertion of existence to fail }
Definition at line 40 of file RRef.hh.
Public Types | |
typedef ObjType | ObjectType |
Class of object held, a la auto_ptr. | |
Public Member Functions | |
Constructors | |
RRef () throw () | |
Empty reference. Can't be used until tied to a DynObj or RRefable. | |
RRef (const RRef &rhs) | |
Create from another reference. Refers to what rhs refers to. | |
template<typename ObjType2> | RRef (const RRef< ObjType2 > &rhs) |
Create from another reference of different type. | |
template<typename ObjType2, class Context> | RRef (const DynObj< ObjType2, Context > &rhs) |
Create a reference to a DynObj. | |
RRef (ObjType &rrefable) | |
Create a reference to a RRefable object. | |
Resets | |
void | reset () |
Reset to null reference. | |
template<typename ObjType2> void | reset (const RRef< ObjType2 > &rhs) |
Reset reference to another reference. | |
template<typename ObjType2, class Context> void | reset (const DynObj< ObjType2, Context > &rhs) |
Reset reference to a DynObj. | |
void | reset (ObjType &rrefable) |
Reset reference to a RRefable. | |
Other methods | |
void | swap (RRef &rhs) throw () |
Swap two references. | |
bool | isNotNull () const throw () |
Test that referant exists. Return true if not. | |
bool | isNull () const throw () |
Test that referant exists. Return false if not. | |
ObjType * | getPtr () const throw () |
Get a raw pointer to the referant. | |
Operators | |
ObjType & | operator() () const |
The operator that gets you a reference to the referant. | |
ObjType & | operator * () const |
This operator is more convenient than operator() if you want to use ObjType from an iterator. | |
RRef & | operator= (const RRef &rhs) |
Same as this->reset(rhs). | |
template<typename ObjType2> RRef & | operator= (const RRef< ObjType2 > &rhs) |
Same as this->reset(rhs). | |
template<typename ObjType2, class Context> RRef & | operator= (const DynObj< ObjType2, Context > &rhs) |
Same as this->reset(rhs). | |
RRef & | operator= (ObjType &rrefable) |
Same as this->reset(rhs). | |
operator const RRef () const throw () | |
This is the conversion operator that allows you to pass a RRef<TT> to a function expecting a RRef<const TT>&. | |
template<typename ObjType2> bool | operator< (const RRef< ObjType2 > &rhs) const throw () |
Compare for ordering. | |
template<typename ObjType2> bool | operator== (const RRef< ObjType2 > &rhs) const throw () |
Compare for equality. | |
template<typename ObjType2> bool | operator!= (const RRef< ObjType2 > &rhs) const throw () |
Compare for inequality. Just uses !operator==. |
|
Create from another reference of different type. Will compile only if the conversion from ObjType to ObjType2 exists. Refers to what rhs refers to. Definition at line 64 of file RRef.hh. References NoPtr::RRef< ObjType >::getPtr(). |
|
Create a reference to a DynObj. Will compile only if ObjType = ObjType2, or if the conversion from ObjType to ObjType2 exists. In debug mode, when the DynObj is destroyed, the RRef will know and cause an assert to fail if you attempt to access the referant via operator(). Definition at line 77 of file RRef.hh. References NoPtr::RRef< ObjType >::getPtr(). |
|
Create a reference to a RRefable object. In debug mode, when the RRefable is destroyed, the RRef will know and cause an assert to fail if you attempt to access the RRefable via operator(). |
|
Reset to null reference. Calling operator() will cause an assertion to fail (if -DNDEBUG was not given to compiler). Definition at line 109 of file RRef.hh. References NoPtr::NullPtr. Referenced by NoPtr::RRef< ObjType >::operator=(). |
|
Reset reference to another reference. If rhs is null or its referant has been destroyed, *this is reset to a null reference. As with construction, a valid rhs means that *this refers to what rhs is referring to. Does nothing if rhs already same as this->getPtr(). Definition at line 122 of file RRef.hh. References NoPtr::RRef< ObjType >::_rrefUsage, and NoPtr::RRef< ObjType >::getPtr(). |
|
Reset reference to a DynObj. If rhs is null this is reset to a null reference. Does nothing if rhs already same as this->getPtr(). Definition at line 137 of file RRef.hh. References NoPtr::DynObj< ObjType, Context >::getPtr(), and NoPtr::DynObj< ObjType, Context >::getRRefUsage(). |
|
Reset reference to a RRefable. This always resets to a valid reference, unless of course rrefable is itself a (raw) reference to an object that has already died (duh). After rrefable is destroyed, calling operator() on *this will cause an assertion to fail (if -DNDEBUG was not given to compiler). Note: Does nothing if *this already refers to rrefable. |
|
Get a raw pointer to the referant. For a RRef<Foo> foo, this is the same as &(foo()). Definition at line 186 of file RRef.hh. References NoPtr::NullPtr. Referenced by NoPtr::RRef< ObjType >::isNotNull(), NoPtr::RRef< ObjType >::isNull(), NoPtr::RRef< ObjType >::operator const RRef(), NoPtr::RRef< ObjType >::reset(), and NoPtr::RRef< ObjType >::RRef(). |
|
The operator that gets you a reference to the referant. This is the closest that C++ gets us to true proxy syntax. I.e., given an RRef<Foo> refobj, you do refobj().method() to access Foo::method(). True proxy syntax would require us to overload operator. instead of operator(), this is not yet allowed in C++. |
|
This operator is more convenient than operator() if you want to use ObjType from an iterator. See DynObj::operator*(). |
|
This is the conversion operator that allows you to pass a RRef<TT> to a function expecting a RRef<const TT>&. Naturally, the reverse operation is not allowed. Definition at line 262 of file RRef.hh. References NoPtr::RRef< ObjType >::getPtr(), and NoPtr::NullPtr. |
|
Compare for ordering. See the discussion for DynObj's operator< for effects of null referants.
|
|
Compare for equality. Equal if both are null, if both refer to same object, or if not, if the two objects compare equal. Therefore, operator==(const ObjType&,const ObjType2&) must be defined. |