Cross-Platform C++

ot
class RefPtrMember< T >

#include "ot/base/RefPtrMember.h"

Template class designed to be used as a member variable within a class that needs to store object references, possibly even a reference to itself.

Overview

The RefPtrMember class is very similar in design and operation to RefPtr<T>, with the exception that it will not increment or decrement the reference count of an object that is referring to itself.

Use RefPtrMember in preference to RefPtr when a class contains an object reference as a member variable, and the possibility exists for that member to be set equal to the containing object.

In the following example, the CoffeeFilter class uses a FilterEventHandler class, presumably to report interesting events that happen to the coffee filter. The derived class MyFilter wishes to implement a customized filter and deal with its events. To achieve this, it needs to call setEventHandler(this), which would create a cyclic reference if the reference count of the FilterEventHandler was incremented naively.

    class FilterEventHandler;
    class CoffeeFilter : public ManagedObject
    {
        public:
            void setEventHandler(FilterEventHandler* pHandler);

        private:
            RefPtrMember<FilterEventHandler> m_rpEventHandler;
    };

    class MyFilter : public CoffeeFilter, public FilterEventHandler
    {
        public:
            MyFilter()  {
                setEventHandler(this);
            }
    };




Constructor/Destructor Summary
RefPtrMember(ManagedObject* pParent)
         Standard constructor, requires a pointer to a ManagedObject which is the parent object containing this RefPtrMember as a member.
RefPtrMember(ManagedObject* pParent, T* ptr)
         Constructs a RefPtrMember with a pointer to T.
RefPtrMember(ManagedObject* pParent, const RefPtrMember< T >& rhs)
         Constructs a RefPtrMember with another RefPtrMember.
RefPtrMember(ManagedObject* pParent, const RefPtr< T >& rhs)
         Constructs a RefPtrMember from a RefPtr for the same type.
~RefPtrMember()
         Destructor.

Method Summary
 T* get() const
         Returns the contained object pointer.
 bool isNull() const
         Tests if the contained object pointer is null.
 T& operator *() const
         A dereferencing operator.
  operator T *() const
         Conversion operator.
 bool operator!=(const RefPtrMember< T >& rhs) const
         Inequality operator.
 bool operator!=(const T* rhs) const
         Inequality operator.
 bool operator<(const RefPtrMember< T >& rhs) const
         Less than operator.
 RefPtrMember< T >& operator=(const RefPtrMember< T >& rhs)
         Assigns one RefPtrMember to another.
 RefPtrMember< T >& operator=(const RefPtr< T >& rhs)
         Assigns a RefPtr to a RefPtrMember.
 RefPtrMember< T >& operator=(T* ptr)
         Assigns a pointer.
 bool operator==(const RefPtrMember< T >& rhs) const
         Equality operator.
 bool operator==(const T* rhs) const
         Equality operator.
 T* operator->() const
         A dereferencing operator.
 void release()
         Decrements the reference count of the contained object pointer if it is not null and is not the same as the parent object.

Constructor/Destructor Detail

RefPtrMember

 RefPtrMember(ManagedObject* pParent)
Standard constructor, requires a pointer to a ManagedObject which is the parent object containing this RefPtrMember as a member. The internal object pointer is initialized to null.

Parameters:
pParent - the parent ManagedObject containing this RefPtrMember as a member

RefPtrMember

 RefPtrMember(ManagedObject* pParent,
              T* ptr)
Constructs a RefPtrMember with a pointer to T. If ptr is not null and is not a reference to the parent object, addRef() is called to increment the reference count.

Parameters:
pParent - the parent ManagedObject containing this RefPtrMember as a member
ptr - pointer to an object of type T, which must be a class derived from ManagedObject.

RefPtrMember

 RefPtrMember(ManagedObject* pParent,
              const RefPtrMember< T >& rhs)
Constructs a RefPtrMember with another RefPtrMember. If rhs is not null and is not a reference to the parent object, addRef() is called to increment the reference count.

Parameters:
pParent - the parent ManagedObject containing this RefPtrMember as a member
rhs - a RefPtrMember pointing to an object of type T, which must be a class derived from ManagedObject.

RefPtrMember

 RefPtrMember(ManagedObject* pParent,
              const RefPtr< T >& rhs)
Constructs a RefPtrMember from a RefPtr for the same type. If rhs is not null and is not a reference to the parent object, addRef() is called to increment the reference count.

Parameters:
pParent - the parent ManagedObject containing this RefPtrMember as a member
rhs - a RefPtr pointing to an object of type T, which must be a class derived from ManagedObject.

~RefPtrMember

 ~RefPtrMember()
Destructor. Decrements the reference count of the contained object pointer if it is not null and is not the same as the parent object.


Method Detail

get

T* get() const
Returns the contained object pointer. Neither the RefPtrMember nor the reference count of its object are changed.


isNull

bool isNull() const
Tests if the contained object pointer is null.

Returns:
true if the object pointer is null; false otherwise

operator *

T& operator *() const
A dereferencing operator. Returns the contained object. This operator allows RefPtrMembers to be used as if they were raw C++ pointers.


operator T *

 operator T *() const
Conversion operator. Silently converts this RefPtrMember<T> into a T*.


operator!=

bool operator!=(const RefPtrMember< T >& rhs) const
Inequality operator. Returns true if this RefPtrMember refers to a different object than rhs. If this and rhs both refer to null, false is returned.

Parameters:
rhs - the RefPtrMember to compare.

operator!=

bool operator!=(const T* rhs) const
Inequality operator. Returns true if this RefPtrMember refers to a different object than rhs. If this and rhs both refer to null, false is returned.

Parameters:
rhs - the pointer to compare.

operator<

bool operator<(const RefPtrMember< T >& rhs) const
Less than operator. Returns true if this RefPtrMember should be ordered before rhs. If this and rhs both refer to null, false is returned.

Parameters:
rhs - the RefPtr to compare.
Since:
1.3

operator=

RefPtrMember< T >& operator=(const RefPtrMember< T >& rhs)
Assigns one RefPtrMember to another. If rhs contains a non-null object pointer, and is not a reference to the parent, addRef() is called to increment the reference count.

Parameters:
rhs - the RefPtrMember<T> to copy.

operator=

RefPtrMember< T >& operator=(const RefPtr< T >& rhs)
Assigns a RefPtr to a RefPtrMember. If rhs contains a non-null object pointer which is not a reference to the parent, addRef() is called to increment the reference count.

Parameters:
rhs - the RefPtr<T> to copy.

operator=

RefPtrMember< T >& operator=(T* ptr)
Assigns a pointer. If ptr is not null, and does not refer to the parent object, addRef() is called to increment the reference count. If this RefPtrMember previously referenced an object, release() is called on that object.

Parameters:
ptr - pointer to an object of type T, which must be a class derived from ManagedObject.

operator==

bool operator==(const RefPtrMember< T >& rhs) const
Equality operator. Returns true if this RefPtrMember refers to the same object as rhs. If this and rhs both refer to null, true is returned.

Parameters:
rhs - the RefPtrMember to compare.

operator==

bool operator==(const T* rhs) const
Equality operator. Returns true if this RefPtrMember refers to the same object as rhs. If this and rhs both refer to null, true is returned.

Parameters:
rhs - the pointer to compare.

operator->

T* operator->() const
A dereferencing operator. Returns the contained object pointer. This operator allows RefPtrMembers to be used as if they were raw C++ pointers.


release

void release()
Decrements the reference count of the contained object pointer if it is not null and is not the same as the parent object. The contained object pointer is then set equal to null.



Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements