Back to index

nux  3.0.0
Public Member Functions | Private Member Functions | Private Attributes | Friends
nux::ObjectPtr< T > Class Template Reference

A smart pointer class. Implemented as an intrusive smart pointer. More...

#include <ObjectPtr.h>

Inheritance diagram for nux::ObjectPtr< T >:
Inheritance graph
[legend]
Collaboration diagram for nux::ObjectPtr< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ObjectPtr ()
 Constructor.
 ObjectPtr (ObjectPtr< T > const &other)
 Copy constructor.
template<typename O >
 ObjectPtr (ObjectPtr< O > const &other)
 Copy constructor.
 ObjectPtr (T *ptr, bool WarnMissuse=false)
 Construction with a base pointer of type T.
template<typename O >
 ObjectPtr (O *ptr, bool WarnMissuse=false)
 Construction with a base pointer of type O that inherits from type T.
void Adopt (T *ptr)
 Take ownership of ptr.
ObjectPtroperator= (T *ptr)
 Assignment of a smart pointer of type T.
ObjectPtroperator= (ObjectPtr< T > const &other)
 Assignment of a smart pointer of type T.
template<typename O >
ObjectPtroperator= (ObjectPtr< O > const &other)
 Assignment of a smart pointer of type O that inherits from type T.
 ~ObjectPtr ()
T & operator* () const
T * operator-> () const
T * GetPointer () const
 Return the stored pointer.
void Swap (ObjectPtr< T > &other)
 Swap the content of 2 smart pointers.
 operator bool () const
bool operator() () const
 Test validity of the smart pointer.
bool IsNull () const
 Test validity of the smart pointer.
bool IsValid () const
 Test validity of the smart pointer.
bool operator< (T *ptr) const
bool operator> (T *ptr) const
bool operator< (ObjectPtr< T > other) const
bool operator> (ObjectPtr< T > other) const
bool operator== (T *ptr) const
template<typename U >
bool operator!= (U other) const
template<typename U >
bool operator== (U *ptr) const
template<typename U >
bool operator== (ObjectPtr< U > const &other) const
template<typename U >
bool operator== (ObjectWeakPtr< U > const &other) const
bool Release ()
 Release the hosted pointer from this object.

Private Member Functions

bool ReleaseReference ()

Private Attributes

T * ptr_

Friends

class ObjectPtr
class ObjectWeakPtr

Detailed Description

template<typename T>
class nux::ObjectPtr< T >

A smart pointer class. Implemented as an intrusive smart pointer.

Definition at line 40 of file ObjectPtr.h.


Constructor & Destructor Documentation

template<typename T>
nux::ObjectPtr< T >::ObjectPtr ( ) [inline]

Constructor.

Definition at line 44 of file ObjectPtr.h.

      : ptr_(NULL)
    {
    }
template<typename T>
nux::ObjectPtr< T >::ObjectPtr ( ObjectPtr< T > const &  other) [inline]

Copy constructor.

Definition at line 50 of file ObjectPtr.h.

      : ptr_(other.ptr_)
    {
      if (ptr_)
      {
        ptr_->objectptr_count_->Increment();
        ptr_->Reference();
      }
    }
template<typename T>
template<typename O >
nux::ObjectPtr< T >::ObjectPtr ( ObjectPtr< O > const &  other) [inline]

Copy constructor.

This method takes advantage of the nux type information using the virtual Type function.

Parameters:
otherParameter with a type derived from T.

Definition at line 68 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (other.ptr_ &&
          other.ptr_->Type().IsDerivedFromType(T::StaticObjectType))
      {
        ptr_ = static_cast<T*>(other.ptr_);
        ptr_->objectptr_count_->Increment();
        ptr_->Reference();
      }
    }
template<typename T>
nux::ObjectPtr< T >::ObjectPtr ( T *  ptr,
bool  WarnMissuse = false 
) [inline, explicit]

Construction with a base pointer of type T.

Parameters:
ptrStart maintaining a reference count of the passed pointer.
WarnMissuseIf True, then ObjectPtr test is ptr is owned or not. If ptr is not owned and WarnMissuse is True, then Print a warning message. This is a debug feature to detect cases such as "ObjectPtr(ObjectA) myobject(ptr);", because the calling code will no longer have a reference on ptr.

Definition at line 89 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (ptr)
      {
        if (WarnMissuse && (!ptr->OwnsTheReference()))
        {
          nuxDebugMsg (TEXT ("[ObjectPtr::ObjectPtr] Warning: Constructing a smart pointer from an object with a floating reference.") );
        }

        ptr_ = ptr;
        ptr_->objectptr_count_->Increment();
        ptr_->Reference();
      }
    }
template<typename T>
template<typename O >
nux::ObjectPtr< T >::ObjectPtr ( O *  ptr,
bool  WarnMissuse = false 
) [inline, explicit]

Construction with a base pointer of type O that inherits from type T.

Parameters:
ptrStart maintaining a reference count of the passed pointer.
WarnMissuseIf True, then ObjectPtr test is ptr is owned or not. If ptr is not owned and WarnMissuse is True, then Print a warning message. This is a debug feature to detect cases such as "ObjectPtr(ObjectA) myobject(ptr);", because the calling code will no longer have a reference on ptr.

Definition at line 115 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (ptr &&
          ptr->Type().IsDerivedFromType(T::StaticObjectType))
      {
        if (WarnMissuse && (!ptr->OwnsTheReference()))
        {
          nuxDebugMsg (TEXT ("[ObjectPtr::ObjectPtr] Warning: Constructing a smart pointer from an object with a floating reference.") );
        }

        ptr_ = static_cast<T*>(ptr);
        ptr_->objectptr_count_->Increment();
        ptr_->Reference();
      }
    }
template<typename T>
nux::ObjectPtr< T >::~ObjectPtr ( ) [inline]

Definition at line 178 of file ObjectPtr.h.


Member Function Documentation

template<typename T>
void nux::ObjectPtr< T >::Adopt ( T *  ptr) [inline]

Take ownership of ptr.

Definition at line 133 of file ObjectPtr.h.

    {
        ObjectPtr<T> temp(ptr);
        Swap(temp);
        // Now we want to release the reference that was added on construction,
        // but keep the smart pointer count.
        if (ptr_)
          ptr_->UnReference();
    }

Here is the caller graph for this function:

template<typename T>
T* nux::ObjectPtr< T >::GetPointer ( ) const [inline]

Return the stored pointer.

Caller of this function should Reference the pointer if they intend to keep it.

Parameters:
Returnthe stored pointer.

Definition at line 201 of file ObjectPtr.h.

    {
      return ptr_;
    }
template<typename T>
bool nux::ObjectPtr< T >::IsNull ( ) const [inline]

Test validity of the smart pointer.

Return True if the internal pointer is null.

Definition at line 233 of file ObjectPtr.h.

    {
        return !IsValid();
    }

Here is the caller graph for this function:

template<typename T>
bool nux::ObjectPtr< T >::IsValid ( ) const [inline]

Test validity of the smart pointer.

Return True if the internal pointer is not null.

Definition at line 242 of file ObjectPtr.h.

    {
      return (ptr_ != NULL) ? true : false;
    }
template<typename T>
nux::ObjectPtr< T >::operator bool ( ) const [inline]

Definition at line 215 of file ObjectPtr.h.

    {
        return bool(ptr_);
    }
template<typename T>
template<typename U >
bool nux::ObjectPtr< T >::operator!= ( other) const [inline]

Definition at line 273 of file ObjectPtr.h.

    {
      return !(*this == other);
    }
template<typename T>
bool nux::ObjectPtr< T >::operator() ( ) const [inline]

Test validity of the smart pointer.

Return True if the internal pointer is not null.

Definition at line 224 of file ObjectPtr.h.

    {
        return bool(ptr_);
    }
template<typename T>
T& nux::ObjectPtr< T >::operator* ( ) const [inline]

Definition at line 183 of file ObjectPtr.h.

    {
      nuxAssert(ptr_ != 0);
      return *ptr_;
    }
template<typename T>
T* nux::ObjectPtr< T >::operator-> ( ) const [inline]

Definition at line 189 of file ObjectPtr.h.

    {
      nuxAssert(ptr_ != 0);
      return ptr_;
    }
template<typename T>
bool nux::ObjectPtr< T >::operator< ( T *  ptr) const [inline]

Definition at line 247 of file ObjectPtr.h.

    {
      return (ptr_ < ptr);
    }
template<typename T>
bool nux::ObjectPtr< T >::operator< ( ObjectPtr< T >  other) const [inline]

Definition at line 257 of file ObjectPtr.h.

    {
      return (ptr_ < other.ptr_);
    }
template<typename T>
ObjectPtr& nux::ObjectPtr< T >::operator= ( T *  ptr) [inline]

Assignment of a smart pointer of type T.

Parameters:
otherSmart pointer of type T.

Definition at line 148 of file ObjectPtr.h.

    {
        ObjectPtr<T> temp(ptr);
        Swap(temp);
        return *this;
    }
template<typename T>
ObjectPtr& nux::ObjectPtr< T >::operator= ( ObjectPtr< T > const &  other) [inline]

Assignment of a smart pointer of type T.

Parameters:
otherSmart pointer of type T.

Definition at line 159 of file ObjectPtr.h.

    {
        ObjectPtr<T> temp(other);
        Swap(temp);
        return *this;
    }
template<typename T>
template<typename O >
ObjectPtr& nux::ObjectPtr< T >::operator= ( ObjectPtr< O > const &  other) [inline]

Assignment of a smart pointer of type O that inherits from type T.

Parameters:
otherSmart pointer of type O.

Definition at line 171 of file ObjectPtr.h.

    {
        ObjectPtr<T> temp(other);
        Swap(temp);
        return *this;
    }
template<typename T>
bool nux::ObjectPtr< T >::operator== ( T *  ptr) const [inline]

Definition at line 267 of file ObjectPtr.h.

    {
      return ptr_ == ptr;
    }
template<typename T>
template<typename U >
bool nux::ObjectPtr< T >::operator== ( U *  ptr) const [inline]

Definition at line 279 of file ObjectPtr.h.

    {
      if (ptr && (!ptr->Type().IsDerivedFromType(T::StaticObjectType)))
        return false;

      return ptr_ == static_cast<T*>(ptr);
    }
template<typename T>
template<typename U >
bool nux::ObjectPtr< T >::operator== ( ObjectPtr< U > const &  other) const [inline]

Definition at line 288 of file ObjectPtr.h.

    {
      if (other.ptr_ &&
          (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
        return false;

      return ptr_ == static_cast<T*>(other.ptr_);
    }
template<typename T>
template<typename U >
bool nux::ObjectPtr< T >::operator== ( ObjectWeakPtr< U > const &  other) const [inline]

Definition at line 298 of file ObjectPtr.h.

    {
      if (other.ptr_ &&
          (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
        return false;

      return ptr_ == static_cast<T*>(other.ptr_);
    }
template<typename T>
bool nux::ObjectPtr< T >::operator> ( T *  ptr) const [inline]

Definition at line 252 of file ObjectPtr.h.

    {
      return (ptr_ > ptr);
    }
template<typename T>
bool nux::ObjectPtr< T >::operator> ( ObjectPtr< T >  other) const [inline]

Definition at line 262 of file ObjectPtr.h.

    {
      return (ptr_ > other.ptr_);
    }
template<typename T>
bool nux::ObjectPtr< T >::Release ( ) [inline]

Release the hosted pointer from this object.

Release the hosted pointer from this object. After this call, all the members are null.

Returns:
True if the hosted object was destroyed.

Definition at line 314 of file ObjectPtr.h.

    {
      return ReleaseReference();
    }

Here is the caller graph for this function:

template<typename T>
bool nux::ObjectPtr< T >::ReleaseReference ( ) [inline, private]

Definition at line 321 of file ObjectPtr.h.

    {
      if (!ptr_)
      {
        return false;
      }

      // Decrease the number of strong reference on the hosted pointer.
      ptr_->objectptr_count_->Decrement();
      bool destroyed = ptr_->UnReference();
      ptr_ = NULL;
      return destroyed;
    }

Here is the caller graph for this function:

template<typename T>
void nux::ObjectPtr< T >::Swap ( ObjectPtr< T > &  other) [inline]

Swap the content of 2 smart pointers.

Parameters:
otherSmart pointer to swap with.

Definition at line 210 of file ObjectPtr.h.

    {
        std::swap(ptr_, other.ptr_);
    }

Here is the caller graph for this function:


Friends And Related Function Documentation

template<typename T>
friend class ObjectPtr [friend]

Definition at line 338 of file ObjectPtr.h.

template<typename T>
friend class ObjectWeakPtr [friend]

Definition at line 341 of file ObjectPtr.h.


Member Data Documentation

template<typename T>
T* nux::ObjectPtr< T >::ptr_ [private]

Definition at line 335 of file ObjectPtr.h.


The documentation for this class was generated from the following file: