Evo C++ Library v0.5.1
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
SharedPtr< T, TSize > Class Template Reference

#include <evo/ptr.h>

Inheritance diagram for SharedPtr< T, TSize >:
Inheritance graph
[legend]

Detailed Description

template<class T, class TSize = SizeT>
class evo::SharedPtr< T, TSize >

Shared smart pointer to single object.

SharedPtr<T[]> is specialized for arrays so the pointer is freed with delete[]. However an array SharedPtr<T[]> doesn't support unshare() (array size isn't known).

See Managed Pointers

C++11:

Template Parameters
TType to use pointer to (not the raw pointer type)
Example
#include <evo/ptr.h>
#include <evo/string.h>
#include <evo/io.h>
using namespace evo;
static Console& c = con();
int main() {
// Create shared pointer, set a value
*ptr = "testing"; // use dereference operator* like normal pointer
// Copying shared pointer makes a shared copy
{
*ptr2 = "foo"; // dereference ptr2 and change value (affects ptr as well)
}
// Print ptr value as terminated string (just for example) -- use operator-> like normal pointer
c.out << ptr->cstr() << NL;
// May use Ptr to reference a shared pointer without sharing it (does not increment reference count)
{
Ptr<String> ptr2(ptr); // this can be dangerous if Ptr outlives SharedPtr
*ptr2 = "bar"; // dereference ptr2 and change value again (affects ptr as well)
}
// Print ptr value as terminated string (just for example) -- use operator-> like normal pointer
c.out << ptr->cstr() << NL;
return 0;
} // ptr automatically freed

Output:

foo
bar

Public Types

typedef PtrBase< T, T * > Base
 This pointer base type. More...
 
typedef void EvoNullableType
 Identify as nullable type. More...
 
typedef T Item
 Item type dereferenced to. More...
 
typedef SharedPtr< T, TSize > This
 This pointer type. More...
 

Public Member Functions

 SharedPtr ()
 Constructor to start with null pointer. More...
 
 SharedPtr (T *ptr)
 Constructor. More...
 
 SharedPtr (const This &src)
 Copy constructor. More...
 
 SharedPtr (This &&src)
 Move constructor (C++11). More...
 
 ~SharedPtr ()
 Destructor. More...
 
Thisclear ()
 Release pointer and set as null. More...
 
bool null () const
 Get whether pointer is null. More...
 
 operator SafeBoolType () const
 Safe (explicit) evaluation as bool type. More...
 
bool operator! () const
 Negation operator checks if NULL. More...
 
bool operator!= (const Base &ptr) const
 Inequality operator. More...
 
bool operator!= (void *ptr) const
 Inequality operator. More...
 
const T & operator* () const
 Dereference operator (const). More...
 
T & operator* ()
 Dereference operator (mutable). More...
 
const T * operator-> () const
 Member access operator (const). More...
 
T * operator-> ()
 Member access operator (mutable). More...
 
bool operator< (const Base &ptr) const
 Less-than operator. More...
 
bool operator< (void *ptr) const
 Less-than operator. More...
 
bool operator<= (const Base &ptr) const
 Less-than-or-equals operator. More...
 
bool operator<= (void *ptr) const
 Less-than-or-equals operator. More...
 
Thisoperator= (const This &src)
 Copy/Assignment operator. More...
 
Thisoperator= (T *ptr)
 Assignment operator for new pointer. More...
 
Thisoperator= (This &&src)
 Move assignment operator (C++11). More...
 
bool operator== (const Base &ptr) const
 Equality operator. More...
 
bool operator== (void *ptr) const
 Equality operator. More...
 
bool operator> (const Base &ptr) const
 Greater-than operator. More...
 
bool operator> (void *ptr) const
 Greater-than operator. More...
 
bool operator>= (const Base &ptr) const
 Greater-than-or-equals operator. More...
 
bool operator>= (void *ptr) const
 Greater-than-or-equals operator. More...
 
const T & operator[] (ulong index) const
 Array access operator (const). More...
 
T & operator[] (ulong index)
 Array access operator (mutable). More...
 
const T * ptr () const
 Get current pointer (const). More...
 
T * ptr ()
 Get current pointer (mutable). More...
 
Thisset ()
 Set as null – same as clear(). More...
 
bool shared () const
 Get whether pointer is shared (reference count > 1). More...
 
Thisunshare ()
 Unshare pointer by setting as a new copy, if shared. More...
 
bool valid () const
 Get whether pointer is valid (not null). More...
 

Public Attributes

T * ptr_
 Pointer. More...
 

Protected Member Functions

void free ()
 

Protected Attributes

TSize * refs_
 

Member Typedef Documentation

◆ Base

typedef PtrBase<T,T* > Base
inherited

This pointer base type.

◆ EvoNullableType

typedef void EvoNullableType
inherited

Identify as nullable type.

◆ Item

typedef T Item

Item type dereferenced to.

◆ This

typedef SharedPtr<T,TSize> This

This pointer type.

Constructor & Destructor Documentation

◆ SharedPtr() [1/4]

SharedPtr ( )
inline

Constructor to start with null pointer.

◆ SharedPtr() [2/4]

SharedPtr ( T *  ptr)
inline

Constructor.

  • This takes ownership of the pointer and will free it later – starts reference count at 1
Parameters
ptrPointer to set, NULL for none

◆ SharedPtr() [3/4]

SharedPtr ( const This src)
inline

Copy constructor.

  • This makes a shared copy of source object, incrementing reference count
Parameters
srcSource pointer

◆ ~SharedPtr()

~SharedPtr ( )
inline

Destructor.

◆ SharedPtr() [4/4]

SharedPtr ( This &&  src)
inline

Move constructor (C++11).

Parameters
srcSource to move

Member Function Documentation

◆ clear()

This& clear ( )
inline

Release pointer and set as null.

  • This will decrement the reference count, and free the pointer if not shared
Returns
This

◆ free()

void free ( )
inlineprotected

◆ null()

bool null ( ) const
inlineinherited

Get whether pointer is null.

Returns
Whether null

◆ operator SafeBoolType()

operator SafeBoolType ( ) const
inlineinherited

Safe (explicit) evaluation as bool type.

  • This is called when object is directly evaluated as a bool, and is equivalent to: !operator!()
  • See SafeBool

◆ operator!()

bool operator! ( ) const
inlineinherited

Negation operator checks if NULL.

Returns
Whether NULL

◆ operator!=() [1/2]

bool operator!= ( const Base ptr) const
inlineinherited

Inequality operator.

Parameters
ptrPointer to compare to
Returns
Whether not equal

◆ operator!=() [2/2]

bool operator!= ( void *  ptr) const
inlineinherited

Inequality operator.

Parameters
ptrPointer to compare to
Returns
Whether not equal

◆ operator*() [1/2]

const T& operator* ( ) const
inlineinherited

Dereference operator (const).

  • Results are undefined if pointer is NULL
Returns
Dereferenced pointer

◆ operator*() [2/2]

T& operator* ( )
inlineinherited

Dereference operator (mutable).

  • Results are undefined if pointer is NULL
Returns
Dereferenced pointer

◆ operator->() [1/2]

const T* operator-> ( ) const
inlineinherited

Member access operator (const).

  • Results are undefined if pointer is NULL
Returns
Pointer

◆ operator->() [2/2]

T* operator-> ( )
inlineinherited

Member access operator (mutable).

  • Results are undefined if pointer is NULL
Returns
Pointer

◆ operator<() [1/2]

bool operator< ( const Base ptr) const
inlineinherited

Less-than operator.

Parameters
ptrPointer to compare to
Returns
Whether less than ptr

◆ operator<() [2/2]

bool operator< ( void *  ptr) const
inlineinherited

Less-than operator.

Parameters
ptrPointer to compare to
Returns
Whether less than ptr

◆ operator<=() [1/2]

bool operator<= ( const Base ptr) const
inlineinherited

Less-than-or-equals operator.

Parameters
ptrPointer to compare to
Returns
Whether less than or equal to ptr

◆ operator<=() [2/2]

bool operator<= ( void *  ptr) const
inlineinherited

Less-than-or-equals operator.

Parameters
ptrPointer to compare to
Returns
Whether less than or equal to ptr

◆ operator=() [1/3]

This& operator= ( const This src)
inline

Copy/Assignment operator.

  • This makes a copy of source object using T copy constructor
  • The previous pointer is released (freed, if applicable)
Parameters
srcSource pointer
Returns
This

◆ operator=() [2/3]

This& operator= ( T *  ptr)
inline

Assignment operator for new pointer.

  • This takes ownership of the pointer and will free it later – starts reference count at 1
  • The previous pointer is released (freed, if applicable)
Parameters
ptrPointer to set
Returns
This

◆ operator=() [3/3]

This& operator= ( This &&  src)
inline

Move assignment operator (C++11).

Parameters
srcSource to move
Returns
This

◆ operator==() [1/2]

bool operator== ( const Base ptr) const
inlineinherited

Equality operator.

Parameters
ptrPointer to compare to
Returns
Whether equal

◆ operator==() [2/2]

bool operator== ( void *  ptr) const
inlineinherited

Equality operator.

Parameters
ptrPointer to compare to
Returns
Whether equal

◆ operator>() [1/2]

bool operator> ( const Base ptr) const
inlineinherited

Greater-than operator.

Parameters
ptrPointer to compare to
Returns
Whether greater than ptr

◆ operator>() [2/2]

bool operator> ( void *  ptr) const
inlineinherited

Greater-than operator.

Parameters
ptrPointer to compare to
Returns
Whether greater than ptr

◆ operator>=() [1/2]

bool operator>= ( const Base ptr) const
inlineinherited

Greater-than-or-equals operator.

Parameters
ptrPointer to compare to
Returns
Whether greater than or equal to ptr

◆ operator>=() [2/2]

bool operator>= ( void *  ptr) const
inlineinherited

Greater-than-or-equals operator.

Parameters
ptrPointer to compare to
Returns
Whether greater than or equal to ptr

◆ operator[]() [1/2]

const T& operator[] ( ulong  index) const
inlineinherited

Array access operator (const).

  • Results are undefined if pointer is NULL or index is out of bounds
Returns
Item reference

◆ operator[]() [2/2]

T& operator[] ( ulong  index)
inlineinherited

Array access operator (mutable).

  • Results are undefined if pointer is NULL or index is out of bounds
Returns
Item reference

◆ ptr() [1/2]

const T* ptr ( ) const
inlineinherited

Get current pointer (const).

  • Caution: This does not release ownership of the pointer
Returns
Current pointer, or NULL if none

◆ ptr() [2/2]

T* ptr ( )
inlineinherited

Get current pointer (mutable).

  • Caution: This does not release ownership of the pointer
Returns
Current pointer, or NULL if none

◆ set()

This& set ( )
inline

Set as null – same as clear().

Returns
This

◆ shared()

bool shared ( ) const
inline

Get whether pointer is shared (reference count > 1).

Returns
Whether shared

◆ unshare()

This& unshare ( )
inline

Unshare pointer by setting as a new copy, if shared.

  • Disabled if array T[]
Returns
This

◆ valid()

bool valid ( ) const
inlineinherited

Get whether pointer is valid (not null).

Returns
Whether valid

Member Data Documentation

◆ ptr_

T* ptr_
inherited

Pointer.

◆ refs_

TSize* refs_
protected

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