Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages

NoPtr::RRef< ObjType > Class Template Reference

#include <RRef.hh>

List of all members.


Detailed Description

template<typename ObjType>
class NoPtr::RRef< ObjType >

This class represents a reference to another object, of type DynObj or subclass of RRefable.

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
    }

Todo:
Add tests for reset-to-same-as-self to make sure works
Author:
Oliver Schoenborn
Since:
Apr 2003

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.

RRefoperator= (const RRef &rhs)
 Same as this->reset(rhs).

template<typename ObjType2> RRefoperator= (const RRef< ObjType2 > &rhs)
 Same as this->reset(rhs).

template<typename ObjType2, class Context> RRefoperator= (const DynObj< ObjType2, Context > &rhs)
 Same as this->reset(rhs).

RRefoperator= (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==.


Constructor & Destructor Documentation

template<typename ObjType>
template<typename ObjType2>
NoPtr::RRef< ObjType >::RRef const RRef< ObjType2 > &  rhs  )  [inline]
 

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().

template<typename ObjType>
template<typename ObjType2, class Context>
NoPtr::RRef< ObjType >::RRef const DynObj< ObjType2, Context > &  rhs  )  [inline]
 

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().

template<typename ObjType>
NoPtr::RRef< ObjType >::RRef ObjType &  rrefable  )  [inline]
 

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().

Definition at line 92 of file RRef.hh.


Member Function Documentation

template<typename ObjType>
void NoPtr::RRef< ObjType >::reset  )  [inline]
 

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=().

template<typename ObjType>
template<typename ObjType2>
void NoPtr::RRef< ObjType >::reset const RRef< ObjType2 > &  rhs  )  [inline]
 

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().

template<typename ObjType>
template<typename ObjType2, class Context>
void NoPtr::RRef< ObjType >::reset const DynObj< ObjType2, Context > &  rhs  )  [inline]
 

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().

template<typename ObjType>
void NoPtr::RRef< ObjType >::reset ObjType &  rrefable  )  [inline]
 

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.

Definition at line 158 of file RRef.hh.

template<typename ObjType>
ObjType* NoPtr::RRef< ObjType >::getPtr  )  const throw () [inline]
 

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().

template<typename ObjType>
ObjType& NoPtr::RRef< ObjType >::operator()  )  const [inline]
 

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++.

Definition at line 203 of file RRef.hh.

template<typename ObjType>
ObjType& NoPtr::RRef< ObjType >::operator *  )  const [inline]
 

This operator is more convenient than operator() if you want to use ObjType from an iterator.

See DynObj::operator*().

Definition at line 212 of file RRef.hh.

template<typename ObjType>
NoPtr::RRef< ObjType >::operator const RRef  )  const throw () [inline]
 

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.

template<typename ObjType>
template<typename ObjType2>
bool NoPtr::RRef< ObjType >::operator< const RRef< ObjType2 > &  rhs  )  const throw () [inline]
 

Compare for ordering.

See the discussion for DynObj's operator< for effects of null referants.

Todo:
Test this rigorously esp. the null ptrs in sets

Definition at line 285 of file RRef.hh.

template<typename ObjType>
template<typename ObjType2>
bool NoPtr::RRef< ObjType >::operator== const RRef< ObjType2 > &  rhs  )  const throw () [inline]
 

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.

Definition at line 304 of file RRef.hh.


The documentation for this class was generated from the following file:
Generated on Mon Aug 4 18:51:33 2003 for NoPtr C++ Library by doxygen 1.3.2