CsPointer  1.0.1
CsPointer::CsUniquePointer< T, Deleter > Class Template Reference

Contains a pointer to an object and takes exclusive ownership. More...

Public Typedefs

using pointer = typename std::unique_ptr< T, Deleter >::pointer
 
using element_type = typename std::unique_ptr< T, Deleter >::element_type
 
using deleter_type = typename std::unique_ptr< T, Deleter >::deleter_type
 
using Pointer = pointer
 
using ElementType = element_type
 
using DeleterType = deleter_type
 

Public Member Functions

 CsUniquePointer (Pointer p=nullptr) noexcept
 
 CsUniquePointer (Pointer p, Deleter d) noexcept
 
 CsUniquePointer (std::unique_ptr< T, Deleter > &&p) noexcept
 
 ~CsUniquePointer () = default
 
 CsUniquePointer (CsUniquePointer &&other) = default
 
CsUniquePointer & operator= (CsUniquePointer &&other) = default
 
ElementTypeoperator* () const noexcept (noexcept (*std::declval< Pointer >()))
 
Pointer operator-> () const noexcept
 
bool operator! () const noexcept
 
 operator bool () const noexcept
 
 operator std::unique_ptr< T, Deleter > () &&noexcept
 
Pointer data () const noexcept
 
Pointer get () const noexcept
 
Deleter & get_deleter () noexcept
 
const Deleter & get_deleter () const noexcept
 
bool is_null () const noexcept
 
Pointer release () noexcept
 
void reset (Pointer other=nullptr) noexcept
 
void swap (CsUniquePointer &other) noexcept
 
Pointer take () noexcept
 

Related Functions

These are not member functions

CsUniquePointer< T > make_unique (Args &&...args)
 
bool operator== (const CsUniquePointer< T1, Deleter1 > &ptr1, const CsUniquePointer< T2, Deleter2 > &ptr2)
 
bool operator== (const CsUniquePointer< T1, Deleter1 > &ptr1, const T2 *ptr2)
 
bool operator== (const T1 *ptr1, const CsUniquePointer< T2, Deleter2 > &ptr2)
 
bool operator== (const CsUniquePointer< T, Deleter > &ptr1, std::nullptr_t)
 
bool operator== (std::nullptr_t, const CsUniquePointer< T, Deleter > &ptr2)
 
bool operator!= (const CsUniquePointer< T1, Deleter1 > &ptr1, const CsUniquePointer< T2, Deleter2 > &ptr2)
 
bool operator!= (const CsUniquePointer< T1, Deleter1 > &ptr1, const T2 *ptr2)
 
bool operator!= (const T1 *ptr1, const CsUniquePointer< T2, Deleter2 > &ptr2)
 
bool operator!= (const CsUniquePointer< T, Deleter > &ptr1, std::nullptr_t)
 
bool operator!= (std::nullptr_t, const CsUniquePointer< T, Deleter > &ptr2)
 
bool operator< (const CsUniquePointer< T1 > &ptr1, const CsUniquePointer< T2 > ptr2)
 
bool operator<= (const CsUniquePointer< T1 > &ptr1, const CsUniquePointer< T2 > ptr2)
 
bool operator> (const CsUniquePointer< T1 > &ptr1, const CsUniquePointer< T2 > ptr2)
 
bool operator>= (const CsUniquePointer< T1 > &ptr1, const CsUniquePointer< T2 > ptr2)
 

Detailed Description

template<typename T, typename Deleter = std::default_delete<T>>
class CsPointer::CsUniquePointer< T, Deleter >

The CsUniquePointer class contains a pointer to an object and takes exclusive ownership of the lifetime of the object. There is no reference count for a CsUniquePointer since no other smart pointers can point to the given object. Any pointer class which takes responsibility for the lifetime of the object it points to is considered a smart pointer.

When this smart pointer class is destroyed the object it points to will be deleted. This ensures the object is always destroyed when the pointer goes out of scope.

Destroying Objects

The purpose of a destructor is to clean up the resources owned by the object. Destroying a smart pointer object needs to do a bit more work. It is also responsible for releaseing the dynamic memory allocated to the object the smart pointer was pointing to. Most dynamically allocated objects are destroyed by simply calling the delete operator as shown below.

Widget *ptr = new Widget();
// do something
delete ptr;

When using a smart pointer which owns the object it is pointing to, users do not call delete directly. The destructor method is responsible for calling a method in a Deleter class which will release the memory allocated to the object. The exact call in the Deleter class must correspond to the way the object was created.

Allocation Deleter Class Destruction
new T() std::default_delete<T> Default Deleter class, calls operator delete()
new T[ ] std::default_delete<T[ ]> Default Deleter class, calls operator delete[ ]

Custom Deleter Classes

CsUniquePointer has a default Deleter class which is sufficient for most objects. If your application requires a custom Deleter class the name of this struct or class must be passed as the second template parameter to CsUniquePointer.

// uses default object deleter
CsUniquePointer<Widget> ptr_A(new Widget);
// uses default array deleter
CsUniquePointer<int[]> ptr_B(new int[42]);
// uses a custom deleter class supplied in your application
struct MyCustomDeleter
{
void operator()(MyClass *ptr) const {
myDeleteFunction(ptr);
}
};
CsUniquePointer<MyClass, MyCustomDeleter> ptr_D(new MyClass);

Member Typedef Documentation

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::deleter_type

Typedef for std::unique_ptr<T, Deleter>::deleter_type.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::DeleterType

Typedef for deleter_type;

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::element_type

Typedef for std::unique_ptr<T, Deleter>::element_type.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::ElementType

Typedef for element_type.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::pointer

Typedef for std::unique_ptr<T, Deleter>::pointer.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::Pointer

Typedef for pointer.

Constructor & Destructor Documentation

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::CsUniquePointer ( Pointer  p = nullptr)
inlineexplicitnoexcept

Constructs a new CsUniquePointer object and sets the internal pointer to p.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::CsUniquePointer ( Pointer  p,
Deleter  d 
)
inlineexplicitnoexcept

Constructs a new CsUniquePointer object and sets the internal pointer to p and the deleter to d.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::CsUniquePointer ( std::unique_ptr< T, Deleter > &&  p)
inlinenoexcept

Constructs a new CsUniquePointer by moving ownership from p.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::~CsUniquePointer ( )
default

Destroys this CsUniquePointer object.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::CsUniquePointer ( CsUniquePointer< T, Deleter > &&  other)
default

Move constructs a new CsUniquePointer from other.

Member Function Documentation

template<typename T , typename Deleter = std::default_delete<T>>
Pointer CsUniquePointer::data ( ) const
inlinenoexcept

Returns the value of the pointer in the current CsUniquePointer. This smart pointer class still owns the object it is pointing to. No other code should attempt to delete the object.

See also
get()
template<typename T , typename Deleter = std::default_delete<T>>
Pointer CsUniquePointer::get ( ) const
inlinenoexcept

Equivalent to calling CsUniquePointer::data().

template<typename T , typename Deleter = std::default_delete<T>>
const Deleter & CsUniquePointer::get_deleter ( ) const
inlinenoexcept

Returns the deleter object which will be used for destruction of this smart pointer.

See also
Destroying Objects
template<typename T , typename Deleter = std::default_delete<T>>
Deleter & CsUniquePointer::get_deleter ( )
inlinenoexcept

Returns the deleter object which will be used for destruction of this smart pointer.

See also
Destroying Objects
template<typename T , typename Deleter = std::default_delete<T>>
bool CsUniquePointer::is_null ( ) const
inlinenoexcept

Returns true if this CsUniquePointer contains a nullptr, otherwise returns false.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::operator bool ( ) const
inlineexplicitnoexcept

Returns true if this CsUniquePointer does not contain a nullptr. This method is invoked automatically when a unique pointer is used as a boolean condition.

CsUniquePointer<MyClass> scopedPointer = someFunction();
if (scopedPointer) {
...
}
template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer::operator std::unique_ptr< T, Deleter > ( ) &&
inlinenoexcept

Converts the current CsUniquePointer to an std::unique_ptr.

template<typename T , typename Deleter = std::default_delete<T>>
bool CsUniquePointer::operator! ( ) const
inlinenoexcept

Returns true if this CsUniquePointer contains a nullptr, otherwise returns false.

See also
is_null()
template<typename T , typename Deleter = std::default_delete<T>>
ElementType & CsUniquePointer::operator* ( ) const
inlinenoexcept

Dereferences this CsUniquePointer and returns the object it points to. If the unique pointer is a nullptr the behavior is undefined.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer CsUniquePointer::operator-> ( ) const
inlinenoexcept

Dereferences this CsUniquePointer and returns a raw pointer to the object it points to. If the unique pointer is a nullptr the behavior is undefined.

template<typename T , typename Deleter = std::default_delete<T>>
CsUniquePointer & CsUniquePointer::operator= ( CsUniquePointer< T, Deleter > &&  other)
default

Move assigns from other and returns a reference to this object.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer CsUniquePointer::release ( )
inlinenoexcept

Equivalent to calling CsUniquePointer::take().

template<typename T , typename Deleter = std::default_delete<T>>
void CsUniquePointer::reset ( Pointer  other = nullptr)
inlinenoexcept

Assigns other to this CsUniquePointer. If the current unique pointer is not a nullptr then the object it is pointing to will be destroyed.

template<typename T , typename Deleter = std::default_delete<T>>
void CsUniquePointer::swap ( CsUniquePointer< T, Deleter > &  other)
inlinenoexcept

Swap the current pointer with other.

template<typename T , typename Deleter = std::default_delete<T>>
Pointer CsUniquePointer::take ( )
inlinenoexcept

Releases the ownership of the managed object, if any. The caller is responsible for deleting the object. Sets this object to a nullptr and returns the old pointer value.

See also
release()

Friends And Related Function Documentation

CsUniquePointer< T > make_unique ( Args &&...  args)
related

Constructs a new object of type T by using the given args as the parameter list for the constructor of T.

bool operator!= ( const CsUniquePointer< T, Deleter > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is not a nullptr, otherwise returns false.

bool operator!= ( const CsUniquePointer< T1, Deleter1 > &  ptr1,
const CsUniquePointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( const CsUniquePointer< T1, Deleter1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( const T1 *  ptr1,
const CsUniquePointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 do not point to the same object, otherwise returns false.

bool operator!= ( std::nullptr_t  ,
const CsUniquePointer< T, Deleter > &  ptr2 
)
related

Returns true if ptr2 is not a nullptr, otherwise returns false.

bool operator< ( const CsUniquePointer< T1 > &  ptr1,
const CsUniquePointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than ptr2.

bool operator<= ( const CsUniquePointer< T1 > &  ptr1,
const CsUniquePointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is less than or equal to ptr2.

bool operator== ( const CsUniquePointer< T, Deleter > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is a nullptr, otherwise returns false.

bool operator== ( const CsUniquePointer< T1, Deleter1 > &  ptr1,
const CsUniquePointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( const CsUniquePointer< T1, Deleter1 > &  ptr1,
const T2 *  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( const T1 *  ptr1,
const CsUniquePointer< T2, Deleter2 > &  ptr2 
)
related

Returns true if ptr1 and ptr2 point to the same object, otherwise returns false.

bool operator== ( std::nullptr_t  ,
const CsUniquePointer< T, Deleter > &  ptr2 
)
related

Returns true if ptr2 is a nullptr, otherwise returns false.

bool operator> ( const CsUniquePointer< T1 > &  ptr1,
const CsUniquePointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than ptr2.

bool operator>= ( const CsUniquePointer< T1 > &  ptr1,
const CsUniquePointer< T2 >  ptr2 
)
related

Returns true if the value of ptr1 is greater than or equal to ptr2.