Back to index

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

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

#include <ObjectPtr.h>

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

List of all members.

Public Member Functions

 ObjectWeakPtr ()
 Constructor.
 ObjectWeakPtr (T *ptr)
 Construction with a base pointer of type T.
template<typename O >
 ObjectWeakPtr (O *ptr, bool WarnMissuse=false)
 Construction with a base pointer of type O that inherits from type T.
 ObjectWeakPtr (ObjectWeakPtr< T > const &other)
 Copy constructor.
template<typename O >
 ObjectWeakPtr (const ObjectWeakPtr< O > &other)
 Copy constructor.
template<typename O >
 ObjectWeakPtr (const ObjectPtr< O > &other)
 Construction from a smart pointer of type O that inherits from type T.
ObjectWeakPtroperator= (ObjectWeakPtr< T > const &other)
 Assignment of a weak smart pointer of type T.
template<typename O >
ObjectWeakPtroperator= (const ObjectWeakPtr< O > &other)
 Assignment of a weak smart pointer of Type O that inherits from type T.
template<typename O >
ObjectWeakPtroperator= (const ObjectPtr< O > &other)
 Assignment of a smart pointer of Type O that inherits from type T.
ObjectWeakPtroperator= (T *ptr)
 Construction with a base pointer of type T.
template<typename O >
ObjectWeakPtroperator= (O *ptr)
 ~ObjectWeakPtr ()
T & operator* () const
T * operator-> () const
bool operator< (T *ptr) const
bool operator> (T *ptr) const
bool operator< (ObjectWeakPtr< T > other) const
bool operator> (ObjectWeakPtr< T > other) const
template<typename U >
bool operator!= (U other) const
bool operator== (T *ptr) const
template<typename U >
bool operator== (U *ptr) const
template<typename U >
bool operator== (const ObjectWeakPtr< U > &other) const
 A weak pointer
template<typename U >
bool operator== (const ObjectPtr< U > &other) const
 An object pointer
bool operator() () const
 Return true is the hosted pointer is not null or has not been destroyed.
bool IsValid () const
 Return true is the hosted pointer is not null or has not been destroyed.
bool IsNull () const
 Return true is the hosted pointer is null or has been destroyed.
bool Release ()
 Release the hosted pointer from this object.
T * GetPointer () const
 Return the stored pointer.

Private Member Functions

void Disconnect ()
void ConnectListener ()
void TargetDestroyed (Object *ptr)

Private Attributes

T * ptr_
sigc::connection destroy_listener_

Friends

class ObjectWeakPtr
template<typename U >
bool operator== (U *, const ObjectWeakPtr< U > &a)
template<typename U >
bool operator!= (U *, const ObjectWeakPtr< U > &a)
template<typename U , typename F >
ObjectWeakPtr< U > staticCast (const ObjectWeakPtr< F > &from)
template<typename U , typename F >
ObjectWeakPtr< U > constCast (const ObjectWeakPtr< F > &from)
template<typename U , typename F >
ObjectWeakPtr< U > dynamicCast (const ObjectWeakPtr< F > &from)
template<typename U , typename F >
ObjectWeakPtr< U > checkedCast (const ObjectWeakPtr< F > &from)
template<typename U , typename F >
ObjectWeakPtr< U > queryCast (const ObjectWeakPtr< F > &from)

Detailed Description

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

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

A weak smart pointer is built from a smart pointer or another weak smart pointer. It increments and decrements the total reference count of an pointer. Even is the original pointer is destroyed, weak smart pointers still point to the RefCounts pointers of the original pointer and can use it to check if the pointer is still valid or not.

Definition at line 354 of file ObjectPtr.h.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 358 of file ObjectPtr.h.

      : ptr_(NULL)
    {
    }
template<typename T>
nux::ObjectWeakPtr< T >::ObjectWeakPtr ( T *  ptr) [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 372 of file ObjectPtr.h.

      : ptr_(ptr)
    {
      ConnectListener();
    }
template<typename T>
template<typename O >
nux::ObjectWeakPtr< T >::ObjectWeakPtr ( 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 388 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (ptr &&
          (ptr->Type().IsDerivedFromType(T::StaticObjectType)))
      {
        ptr_ = static_cast<T*>(ptr);
        ConnectListener();
      }
    }
template<typename T>
nux::ObjectWeakPtr< T >::ObjectWeakPtr ( ObjectWeakPtr< T > const &  other) [inline]

Copy constructor.

Parameters:
otherParameter with type T.

Definition at line 403 of file ObjectPtr.h.

      : ptr_(other.ptr_)
    {
      ConnectListener();
    }
template<typename T>
template<typename O >
nux::ObjectWeakPtr< T >::ObjectWeakPtr ( const ObjectWeakPtr< O > &  other) [inline]

Copy constructor.

Parameters:
otherParameter with a type derived from T.

Definition at line 414 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (other.ptr_ &&
          (other.ptr_->Type().IsDerivedFromType(T::StaticObjectType)))
      {
        ptr_ = static_cast<T*>(other.ptr_);
        ConnectListener();
      }
    }
template<typename T>
template<typename O >
nux::ObjectWeakPtr< T >::ObjectWeakPtr ( const ObjectPtr< O > &  other) [inline]

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

Parameters:
otherMaintains a weak smart pointer reference to this parameter.

Definition at line 430 of file ObjectPtr.h.

      : ptr_(NULL)
    {
      if (other.ptr_ &&
          (other.ptr_->Type().IsDerivedFromType(T::StaticObjectType)))
      {
        ptr_ = static_cast<T*>(other.ptr_);
        ConnectListener();
      }
    }
template<typename T>
nux::ObjectWeakPtr< T >::~ObjectWeakPtr ( ) [inline]

Definition at line 517 of file ObjectPtr.h.

    {
      Disconnect();
    }

Member Function Documentation

template<typename T>
void nux::ObjectWeakPtr< T >::ConnectListener ( ) [inline, private]

Definition at line 660 of file ObjectPtr.h.

    {
      if (ptr_)
      {
        sigc::slot<void, Object*> slot = sigc::mem_fun(this, &ObjectWeakPtr<T>::TargetDestroyed);
        destroy_listener_ = ptr_->object_destroyed.connect(slot);
      }
    }

Here is the caller graph for this function:

template<typename T>
void nux::ObjectWeakPtr< T >::Disconnect ( ) [inline, private]

Definition at line 654 of file ObjectPtr.h.

    {
      if (destroy_listener_.connected())
        destroy_listener_.disconnect();
    }

Here is the caller graph for this function:

template<typename T>
T* nux::ObjectWeakPtr< 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 648 of file ObjectPtr.h.

    {
      return ptr_;
    }

Here is the caller graph for this function:

template<typename T>
bool nux::ObjectWeakPtr< T >::IsNull ( ) const [inline]

Return true is the hosted pointer is null or has been destroyed.

Returns:
True if the internal pointer is null or has been destroyed.

Definition at line 621 of file ObjectPtr.h.

    {
      return !IsValid();
    }

Here is the caller graph for this function:

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

Return true is the hosted pointer is not null or has not been destroyed.

Returns:
True if the internal pointer is not null or has not been destroyed.

Definition at line 612 of file ObjectPtr.h.

    {
      return (ptr_!= NULL) ? true : false;
    }

Here is the caller graph for this function:

template<typename T>
template<typename U >
bool nux::ObjectWeakPtr< T >::operator!= ( other) const [inline]

Definition at line 555 of file ObjectPtr.h.

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

Return true is the hosted pointer is not null or has not been destroyed.

Returns:
True if the internal pointer is not null.

Definition at line 603 of file ObjectPtr.h.

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

Definition at line 522 of file ObjectPtr.h.

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

Definition at line 528 of file ObjectPtr.h.

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

Definition at line 534 of file ObjectPtr.h.

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

Definition at line 544 of file ObjectPtr.h.

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

Assignment of a weak smart pointer of type T.

Parameters:
otherWeak smart pointer of type T.

Definition at line 445 of file ObjectPtr.h.

    {
      Disconnect();
      ptr_ = other.ptr_;
      ConnectListener();
      return *this;
    }
template<typename T>
template<typename O >
ObjectWeakPtr& nux::ObjectWeakPtr< T >::operator= ( const ObjectWeakPtr< O > &  other) [inline]

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

Parameters:
otherWeak smart pointer of type O.

Definition at line 460 of file ObjectPtr.h.

    {
      Disconnect();
      if (other.ptr_ &&
          other.ptr_->Type().IsDerivedFromType(T::StaticObjectType))
      {
        ptr_ = static_cast<T*>(other.ptr_);
        ConnectListener();
      }
      return *this;
    }
template<typename T>
template<typename O >
ObjectWeakPtr& nux::ObjectWeakPtr< T >::operator= ( const ObjectPtr< O > &  other) [inline]

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

Parameters:
otherMaintains a weak smart pointer reference to this parameter.

Definition at line 477 of file ObjectPtr.h.

    {
      Disconnect();
      if (other.ptr_ &&
          other.ptr_->Type().IsDerivedFromType(T::StaticObjectType))
      {
        ptr_ = static_cast<T*>(other.ptr_);
        ConnectListener();
      }
      return *this;
    }
template<typename T>
ObjectWeakPtr& nux::ObjectWeakPtr< T >::operator= ( T *  ptr) [inline]

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 496 of file ObjectPtr.h.

    {
      Disconnect();
      ptr_ = ptr;
      ConnectListener();
      return *this;
    }
template<typename T>
template<typename O >
ObjectWeakPtr& nux::ObjectWeakPtr< T >::operator= ( O *  ptr) [inline]

Definition at line 505 of file ObjectPtr.h.

    {
      Disconnect();
      if (ptr &&
          ptr->Type().IsDerivedFromType(T::StaticObjectType))
      {
        ptr_ = static_cast<T*>(ptr);
        ConnectListener();
      }
      return *this;
    }
template<typename T>
bool nux::ObjectWeakPtr< T >::operator== ( T *  ptr) const [inline]

Definition at line 559 of file ObjectPtr.h.

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

Definition at line 565 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::ObjectWeakPtr< T >::operator== ( const ObjectWeakPtr< U > &  other) const [inline]

A weak pointer

Returns:
True is this weak pointer host the same pointer as the weak pointer passed as parameter.

Definition at line 578 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::ObjectWeakPtr< T >::operator== ( const ObjectPtr< U > &  other) const [inline]

An object pointer

Returns:
True is this weak pointer host the same pointer as the object pointer passed as parameter.

Definition at line 591 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::ObjectWeakPtr< T >::operator> ( T *  ptr) const [inline]

Definition at line 539 of file ObjectPtr.h.

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

Definition at line 549 of file ObjectPtr.h.

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

Release the hosted pointer from this object.

Release the hosted pointer from this object. After this call, the following members are null: reference_count _weak_reference_count ptr This call decreases the count of weak reference before setting _weak_reference_count to null.

Returns:
True this was the last weak reference on the hosted object.

Definition at line 636 of file ObjectPtr.h.

    {
        Disconnect();
        ptr_ = NULL;
        return false;
    }

Here is the caller graph for this function:

template<typename T>
void nux::ObjectWeakPtr< T >::TargetDestroyed ( Object ptr) [inline, private]

Definition at line 669 of file ObjectPtr.h.

    {
        ptr_ = NULL;
        // rese the connetion too
        destroy_listener_ = sigc::connection();
    }

Friends And Related Function Documentation

template<typename T>
template<typename U , typename F >
ObjectWeakPtr<U> checkedCast ( const ObjectWeakPtr< F > &  from) [friend]
template<typename T>
template<typename U , typename F >
ObjectWeakPtr<U> constCast ( const ObjectWeakPtr< F > &  from) [friend]
template<typename T>
template<typename U , typename F >
ObjectWeakPtr<U> dynamicCast ( const ObjectWeakPtr< F > &  from) [friend]
template<typename T>
friend class ObjectWeakPtr [friend]

Definition at line 680 of file ObjectPtr.h.

template<typename T>
template<typename U >
bool operator!= ( U *  ,
const ObjectWeakPtr< U > &  a 
) [friend]
template<typename T>
template<typename U >
bool operator== ( U *  ,
const ObjectWeakPtr< U > &  a 
) [friend]
template<typename T>
template<typename U , typename F >
ObjectWeakPtr<U> queryCast ( const ObjectWeakPtr< F > &  from) [friend]
template<typename T>
template<typename U , typename F >
ObjectWeakPtr<U> staticCast ( const ObjectWeakPtr< F > &  from) [friend]

Member Data Documentation

template<typename T>
sigc::connection nux::ObjectWeakPtr< T >::destroy_listener_ [private]

Definition at line 677 of file ObjectPtr.h.

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

Definition at line 676 of file ObjectPtr.h.


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