nux-1.14.0
nux::ObjectPtr< T > Class Template Reference

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

#include <NuxCore/ObjectPtr.h>

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.
T & operator* () const
T * operator-> () const
const T * GetPointer () const
 Return the stored pointer.
T * GetPointer ()
 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.

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>
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_(nullptr)
    {
      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_(nullptr)
    {
      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_(nullptr)
    {
      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();
      }
    }

Member Function Documentation

template<typename T>
T* nux::ObjectPtr< T >::GetPointer ( ) [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 212 of file ObjectPtr.h.

    {
      return ptr_;
    }
template<typename T>
const 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 244 of file ObjectPtr.h.

Referenced by nux::GraphicsEngine::GraphicsEngine().

    {
        return !IsValid();
    }
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 235 of file ObjectPtr.h.

    {
        return bool(ptr_);
    }
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>
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>
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 325 of file ObjectPtr.h.

Referenced by nux::CachedIndexBuffer::UpdateResource(), and nux::CachedVertexBuffer::UpdateResource().

    {
      return ReleaseReference();
    }
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 221 of file ObjectPtr.h.

Referenced by nux::ObjectPtr< IOpenGLAsmShaderProgram >::Adopt(), and nux::ObjectPtr< IOpenGLAsmShaderProgram >::operator=().

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

The documentation for this class was generated from the following file:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends