CsPointer  1.0.1
CsPointer::CsSharedPointer< T > Class Template Reference

Stores a pointer to a potentially shared object. More...

Public Typedefs

using element_type = typename std::shared_ptr< T >::element_type
 
using weak_type = CsWeakPointer< T >
 
using pointer = element_type *
 
using ElementType = element_type
 
using WeakType = weak_type
 
using Pointer = pointer
 

Public Member Functions

constexpr CsSharedPointer () noexcept
 
constexpr CsSharedPointer (std::nullptr_t) noexcept
 
template<typename U >
 CsSharedPointer (U *p)
 
template<typename U , typename Deleter >
 CsSharedPointer (U *p, Deleter d)
 
template<typename Deleter >
 CsSharedPointer (std::nullptr_t, Deleter d)
 
template<typename U , typename Deleter , typename Alloc >
 CsSharedPointer (U *p, Deleter d, Alloc a)
 
template<typename Deleter , typename Alloc >
 CsSharedPointer (std::nullptr_t, Deleter d, Alloc a)
 
template<typename U , typename Deleter >
 CsSharedPointer (CsUniquePointer< U, Deleter > &&p)
 
template<typename U >
 CsSharedPointer (const std::shared_ptr< U > &p) noexcept
 
template<typename U >
 CsSharedPointer (std::shared_ptr< U > &&p) noexcept
 
template<typename U >
 CsSharedPointer (const CsSharedPointer< U > &p1, element_type *p2) noexcept
 
template<typename U >
 CsSharedPointer (CsSharedPointer< U > &&p1, element_type *p2) noexcept
 
 ~CsSharedPointer () = default
 
 CsSharedPointer (const CsSharedPointer &other) = default
 
CsSharedPointer & operator= (const CsSharedPointer &other) = default
 
 CsSharedPointer (CsSharedPointer &&other) = default
 
CsSharedPointer & operator= (CsSharedPointer &&other) = default
 
template<typename U >
 CsSharedPointer (const CsSharedPointer< U > &p) noexcept
 
template<typename U >
CsSharedPointer & operator= (const CsSharedPointer< U > &p) noexcept
 
template<typename U >
 CsSharedPointer (CsSharedPointer< U > &&p) noexcept
 
template<typename U >
CsSharedPointer & operator= (CsSharedPointer< U > &&p) noexcept
 
template<typename U >
 CsSharedPointer (const CsWeakPointer< U > &p)
 
template<typename U >
CsSharedPointer & operator= (const CsWeakPointer< U > &p)
 
element_typeoperator* () const noexcept
 
pointer operator-> () const noexcept
 
bool operator! () const noexcept
 
 operator bool () const noexcept
 
template<typename U >
 operator std::shared_ptr< U > () const &noexcept
 
template<typename U >
 operator std::shared_ptr< U > () &&noexcept
 
void clear () noexcept
 
pointer data () const noexcept
 
pointer get () const noexcept
 
bool is_null () const noexcept
 
template<typename U >
bool owner_before (const CsSharedPointer< U > &p) const noexcept
 
template<typename U >
bool owner_before (const CsWeakPointer< U > &p) const noexcept
 
void reset () noexcept
 
template<typename U >
void reset (U *p)
 
template<typename U , typename Deleter >
void reset (U *p, Deleter d)
 
template<typename U , typename Deleter , typename Alloc >
void reset (U *p, Deleter d, Alloc a)
 
void swap (CsSharedPointer &other) noexcept
 
CsWeakPointer< T > toWeakRef () const
 
bool unique () const noexcept
 
long use_count () const noexcept
 

Static Public Member Functions

template<typename... Args>
static CsSharedPointer< T > create (Args &&...args)
 

Related Functions

These are not member functions

CsSharedPointer< T > make_shared (Args &&...args)
 
CsSharedPointer< T > const_pointer_cast (const CsSharedPointer< U > &ptr)
 
CsSharedPointer< T > dynamic_pointer_cast (const CsSharedPointer< U > &ptr)
 
CsSharedPointer< T > static_pointer_cast (const CsSharedPointer< U > &ptr)
 
CsSharedPointer< T > const_pointer_cast (CsSharedPointer< U > &&ptr)
 
CsSharedPointer< T > dynamic_pointer_cast (CsSharedPointer< U > &&ptr)
 
CsSharedPointer< T > static_pointer_cast (CsSharedPointer< U > &&ptr)
 
bool operator== (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator== (const CsSharedPointer< T1 > &ptr1, const T2 *ptr2)
 
bool operator== (const T1 *ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator== (const CsSharedPointer< T > &ptr1, std::nullptr_t) noexcept
 
bool operator== (std::nullptr_t, const CsSharedPointer< T > &ptr2) noexcept
 
bool operator!= (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator!= (const CsSharedPointer< T1 > &ptr1, const T2 *ptr2)
 
bool operator!= (const T1 *ptr1, const CsSharedPointer< T2 > &ptr2)
 
bool operator!= (const CsSharedPointer< T > &ptr1, std::nullptr_t) noexcept
 
bool operator!= (std::nullptr_t, const CsSharedPointer< T > &ptr2) noexcept
 
bool operator< (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > ptr2)
 
bool operator<= (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > ptr2)
 
bool operator> (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > ptr2)
 
bool operator>= (const CsSharedPointer< T1 > &ptr1, const CsSharedPointer< T2 > ptr2)
 
void swap (CsSharedPointer< T > &ptr1, CsSharedPointer< T > &ptr2) noexcept
 

Detailed Description

template<typename T>
class CsPointer::CsSharedPointer< T >

The CsSharedPointer class stores a pointer to a potentially shared object. This class maintains a shared reference count which indicates how many shared pointers are pointing to the current object. Any pointer class which takes responsibility for the lifetime of the object it points to is considered a smart pointer.

A CsSharedPointer object can be constructed from a raw pointer, another CsSharedPointer object, or by a CsWeakPointer object. When this class is destroyed the object it points to will be deleted if no other shared pointers are pointing to the given object.

See also
CsSharedArrayPointer, CsWeakPointer

Member Typedef Documentation

template<typename T >
CsSharedPointer::element_type

Typedef for std::shared_ptr<T>::element_type.

template<typename T >
CsSharedPointer::ElementType

Typedef for element_type.

template<typename T >
CsSharedPointer::pointer

Typedef for element_type *.

template<typename T >
CsSharedPointer::Pointer

Typedef for pointer.

template<typename T >
CsSharedPointer::weak_type

Typedef for CsWeakPointer<T>.

template<typename T >
CsSharedPointer::WeakType

Typedef for weak_type.

Constructor & Destructor Documentation

template<typename T >
constexpr CsSharedPointer::CsSharedPointer ( )
inlineconstexprnoexcept

Creates a new CsSharedPointer which contains a nullptr.

template<typename T >
constexpr CsSharedPointer::CsSharedPointer ( std::nullptr_t  )
inlineconstexprnoexcept

Creates a new CsSharedPointer which contains a nullptr.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( U *  p)
inlineexplicit

Creates a new CsSharedPointer which points to p. Ownership of the object is transferred to the new shared pointer.

template<typename T >
template<typename U , typename Deleter >
CsSharedPointer::CsSharedPointer ( U *  p,
Deleter  d 
)
inline

Creates a new CsSharedPointer which points to the given raw pointer p. Ownership of the object is transferred to the new shared pointer. The value for d is a deleter object and will be used when the shared pointer is being destroyed.

template<typename T >
template<typename Deleter >
CsSharedPointer::CsSharedPointer ( std::nullptr_t  ,
Deleter  d 
)
inline

Creates a new CsSharedPointer which contains a nullptr. The value for d is a deleter object and will be used when the shared pointer is being destroyed.

template<typename T >
template<typename U , typename Deleter , typename Alloc >
CsSharedPointer::CsSharedPointer ( U *  p,
Deleter  d,
Alloc  a 
)
inline

Creates a new CsSharedPointer which points to p. Ownership of the object is transferred to the new shared pointer. The value for d is a deleter object and will be used when the shared pointer is being destroyed. The value for a is an allocator object and will be used when the shared pointer needs to allocate memory.

template<typename T >
template<typename Deleter , typename Alloc >
CsSharedPointer::CsSharedPointer ( std::nullptr_t  ,
Deleter  d,
Alloc  a 
)
inline

Creates a new CsSharedPointer which contains a nullptr. The value for d is a deleter object and will be used when the shared pointer is being destroyed. The value for a is an allocator object and will be used when the shared pointer needs to allocate memory.

template<typename T >
template<typename U , typename Deleter >
CsSharedPointer::CsSharedPointer ( CsUniquePointer< U, Deleter > &&  p)
inline

Creates a new CsSharedPointer using the given unique pointer p. The unique pointer will be set to a nullptr.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( const std::shared_ptr< U > &  p)
inlinenoexcept

Creates a new CsSharedPointer using the given C++ Standard Library shared pointer p.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( std::shared_ptr< U > &&  p)
inlinenoexcept

Creates a new CsSharedPointer by moving from the given C++ Standard Library shared pointer p.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( const CsSharedPointer< U > &  p1,
element_type p2 
)
inlinenoexcept

Creates a new CsSharedPointer which points to the object referenced by p2. The reference count for p1 is incremented by one. The reference count for the new shared pointer uses the reference count in p1. You must ensure p2 is not destroyed before p1.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( CsSharedPointer< U > &&  p1,
element_type p2 
)
inlinenoexcept

Creates a new CsSharedPointer which points to the object referenced by p2. The reference count for p1 is incremented by one. The reference count for the new shared pointer uses the reference count in p1. You must ensure p2 is not destroyed before p1.

template<typename T >
CsSharedPointer::~CsSharedPointer ( )
default

Destroys this CsSharedPointer. If the current shared pointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
CsSharedPointer::CsSharedPointer ( const CsSharedPointer< T > &  other)
default

Copy constructs a new CsSharedPointer from other. The current shared pointer shares ownership with other.

template<typename T >
CsSharedPointer::CsSharedPointer ( CsSharedPointer< T > &&  other)
default

Move constructs a new CsSharedPointer from other.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( const CsSharedPointer< U > &  p)
inlinenoexcept

Constructs a new CsSharedPointer from p. This shared pointer shares ownership with other. The template parameter U must inherit from T.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( CsSharedPointer< U > &&  p)
inlinenoexcept

Constructs a CsSharedPointer by moving the ownership from p. The template parameter U must inherit from T.

template<typename T >
template<typename U >
CsSharedPointer::CsSharedPointer ( const CsWeakPointer< U > &  p)
inlineexplicit

Creates a CsSharedPointer by converting the weak pointer p to a shared pointer. The template parameter U must inherit from T.

See also
CsWeakPointer::toStrongRef()

Member Function Documentation

template<typename T >
void CsSharedPointer::clear ( )
inlinenoexcept

Sets the current CsSharedPointer to a nullptr. If the current shared pointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename... Args>
CsSharedPointer< T > CsSharedPointer::create ( Args &&...  args)
inlinestatic

Creates a new CsSharedPointer pointing to a new object of type T. This method can be more efficient than calling new to create the object and using the constructor which accepts a raw pointer.

int arg1 = 5;
bool arg2 = true;
// preferred
// equivalent, usually slower
T *rawPtr = new T(arg1, arg2);
CsSharedPointer<T> ptrB(rawPtr);
template<typename T >
T * CsSharedPointer::data ( ) const
inlinenoexcept

Returns the value of the pointer contained in this object.

Note
Do not delete the object pointed to by the raw pointer returned by this method. This will cause undefined behavior.
template<typename T >
void CsSharedPointer::get ( ) const
inlinenoexcept

Equivalent to calling data().

template<typename T >
bool CsSharedPointer::is_null ( ) const
inlinenoexcept

Returns true if the current CsSharedPointer contains a nullptr.

template<typename T >
CsSharedPointer::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the current CsSharedPointer is not a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected. Since this method is explicit it is not possible to assign a shared pointer to a boolean.

// example 1
if (mySharedPtr) {
...
}
// example 2
bool isTest_A = mySharedPtr; // will *not* compile
bool isTest_B = (mySharedPtr != nullptr); // compiles, valid code
bool isTest_C = bool(mySharedPtr); // compiles, valid code
template<typename T >
template<typename U >
CsSharedPointer::operator std::shared_ptr< U > ( ) &&
inlinenoexcept

Converts the current CsSharedPointer to an std::shared_ptr.

template<typename T >
template<typename U >
CsSharedPointer::operator std::shared_ptr< U > ( ) const &
inlinenoexcept

Converts the current CsSharedPointer to an std::shared_ptr.

template<typename T >
bool CsSharedPointer::operator! ( ) const
inlinenoexcept

Returns true if this object is a nullptr. This method is called when a shared pointer is used in an "if statement" or another context where a boolean value is expected.

if (! mySharedPtr) {
...
}
template<typename T >
T & CsSharedPointer::operator* ( ) const
inlinenoexcept

Returns a reference to the object the current CsSharedPointer is pointing to.

template<typename T >
T * CsSharedPointer::operator-> ( ) const
inlinenoexcept

Returns a pointer to the object the current CsSharedPointer is pointing to.

template<typename T >
CsSharedPointer & CsSharedPointer::operator= ( const CsSharedPointer< T > &  other)
default

Copy assigns from other and returns a reference to this object. If the current shared pointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
CsSharedPointer & CsSharedPointer::operator= ( const CsSharedPointer< U > &  p)
inlinenoexcept

Assigns from p and returns a reference to this object. The template parameter U must inherit from T.

If the current shared pointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
CsSharedPointer & CsSharedPointer::operator= ( const CsWeakPointer< U > &  p)
inline

Converts the weak pointer p to a shared pointer and then assigns it to the current CsSharedPointer. The template parameter U must inherit from T.

If the current CsSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
CsSharedPointer & CsSharedPointer::operator= ( CsSharedPointer< T > &&  other)
default

Move assigns from other and returns a reference to this object. If the current CsSharedPointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
CsSharedPointer & CsSharedPointer::operator= ( CsSharedPointer< U > &&  p)
inlinenoexcept

Assigns by moving the ownership from p and returns a reference to this object. The template parameter U must inherit from T.

If the current shared pointer is not a nullptr, then the internal reference count is decremented. If the reference count is zero then the object it is pointing to will be destroyed.

template<typename T >
template<typename U >
bool CsSharedPointer::owner_before ( const CsSharedPointer< U > &  p) const
inlinenoexcept

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
template<typename U >
bool CsSharedPointer::owner_before ( const CsWeakPointer< U > &  p) const
inlinenoexcept

Returns true if the current pointer is less than p, otherwise returns false.

template<typename T >
void CsSharedPointer::reset ( )
inlinenoexcept

Equivalent to calling clear().

template<typename T >
template<typename U >
void CsSharedPointer::reset ( U *  p)
inline

Resets the current CsSharedPointer object to point to p. The template parameter U must inherit from T.

template<typename T >
template<typename U , typename Deleter >
void CsSharedPointer::reset ( U *  p,
Deleter  d 
)
inline

Resets the current CsSharedPointer object to point to p and updates the Deleter to d. The template parameter U must inherit from T.

template<typename T >
template<typename U , typename Deleter , typename Alloc >
void CsSharedPointer::reset ( U *  p,
Deleter  d,
Alloc  a 
)
inline

Resets the current CsSharedPointer object to point to p and updates the Deleter to d. The value for a is an allocator object and will be used when CsSharedPointer needs to allocate memory. The template parameter U must inherit from T.

template<typename T >
void CsSharedPointer::swap ( CsSharedPointer< T > &  other)
inlinenoexcept

Swaps the current CsSharedPointer with other. This method is very fast and never fails.

template<typename T >
CsWeakPointer< T > CsSharedPointer::toWeakRef ( ) const
inline

Returns a CsWeakPointer which shares the internal reference count with the current shared pointer.

template<typename T >
bool CsSharedPointer::unique ( ) const
inlinenoexcept

Returns true if the use_count() is equal to one.

template<typename T >
long CsSharedPointer::use_count ( ) const
inlinenoexcept

Returns the reference count for the current CsSharedPointer.

Friends And Related Function Documentation

CsSharedPointer< T > const_pointer_cast ( const CsSharedPointer< U > &  ptr)
related

Returns a CsSharedPointer which points to the same object as ptr. The shared pointer is cast from type T to type U using a const_cast.

See also
dynamic_pointer_cast(), static_pointer_cast()
CsSharedPointer< T > const_pointer_cast ( CsSharedPointer< U > &&  ptr)
related

Returns a CsSharedPointer by moving from ptr. The shared pointer is cast from type T to type U using a const_cast.

See also
dynamic_pointer_cast(), static_pointer_cast()
CsSharedPointer< T > dynamic_pointer_cast ( const CsSharedPointer< U > &  ptr)
related

Returns a CsSharedPointer which points to the same object as ptr. The shared pointer is cast from type T to type U using a dynamic_cast.

The U must have the same cv-qualifiers (const and volatile) that T has. If you need to cast away either of these qualifiers then use const_pointer_cast().

See also
const_pointer_cast(), static_pointer_cast()
CsSharedPointer< T > dynamic_pointer_cast ( CsSharedPointer< U > &&  ptr)
related

Returns a CsSharedPointer by moving from ptr. The shared pointer is cast from type T to type U using a dynamic_cast.

The U must have the same cv-qualifiers (const and volatile) that T has. If you need to cast away either of these qualifiers then use const_pointer_cast().

See also
const_pointer_cast(), static_pointer_cast()
CsSharedPointer< T > make_shared ( Args &&...  args)
related

This function constructs a new object of type T and returns a shared pointer to the newly created object in a single operation.

CsSharedPointer<MyFileClass> filePtr = make_shared<MyFileClass>( myFileName.path() );
bool operator!= ( const CsSharedPointer< T > &  ptr1,
std::nullptr_t   
)
related

Returns true if ptr1 is not a nullptr.

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

Returns true if ptr1 and ptr2 do not point to the same object.

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

Returns true if ptr1 and ptr2 do not point to the same object.

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

Returns true if ptr1 and ptr2 do not point to the same object.

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

Returns true if ptr2 is not a nullptr.

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

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

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

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

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

Returns true if ptr1 is a nullptr.

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

Returns true if ptr1 and ptr2 point to the same object.

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

Returns true if ptr1 and ptr2 point to the same object.

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

Returns true if ptr1 and ptr2 point to the same object.

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

Returns true if ptr2 is a nullptr.

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

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

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

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

CsSharedPointer< T > static_pointer_cast ( const CsSharedPointer< U > &  ptr)
related

Returns a CsSharedPointer which points to the same object as ptr. The shared pointer is cast from type T to type U using a static_cast.

See also
const_pointer_cast(), dynamic_pointer_cast()
CsSharedPointer< T > static_pointer_cast ( CsSharedPointer< U > &&  ptr)
related

Returns a CsSharedPointer by moving from ptr. The shared pointer is cast from type T to type U using a static_cast.

See also
const_pointer_cast(), dynamic_pointer_cast()
void swap ( CsSharedPointer< T > &  ptr1,
CsSharedPointer< T > &  ptr2 
)
related

Swaps ptr1 with ptr2. This method is very fast and never fails.