Mbed OS Reference
Loading...
Searching...
No Matches
SharedPtr< T > Class Template Reference

Shared pointer class. More...

#include <SharedPtr.h>

Public Member Functions

constexpr SharedPtr ()
 Create empty SharedPtr not pointing to anything. More...
 
constexpr SharedPtr (std::nullptr_t)
 Create empty SharedPtr not pointing to anything. More...
 
 SharedPtr (T *ptr)
 Create new SharedPtr. More...
 
 ~SharedPtr ()
 Destructor. More...
 
 SharedPtr (const SharedPtr &source)
 Copy constructor. More...
 
 SharedPtr (SharedPtr &&source)
 Move constructor. More...
 
SharedPtr operator= (const SharedPtr &source)
 Copy assignment operator. More...
 
SharedPtr operator= (SharedPtr &&source)
 Move assignment operator. More...
 
void reset (T *ptr)
 Replaces the managed pointer with a new unmanaged pointer. More...
 
void reset ()
 Replace the managed pointer with a null pointer. More...
 
T * get () const
 Raw pointer accessor. More...
 
uint32_t use_count () const
 Reference count accessor. More...
 
T & operator* () const
 Dereference object operator. More...
 
T * operator-> () const
 Dereference object member operator. More...
 
 operator bool () const
 Boolean conversion operator. More...
 

Detailed Description

template<class T>
class mbed::SharedPtr< T >

Shared pointer class.

A shared pointer is a "smart" pointer that retains ownership of an object using reference counting across all smart pointers referencing that object.

#include "platform/SharedPtr.h"
void test() {
struct MyStruct { int a; };
// Create shared pointer
SharedPtr<MyStruct> ptr( new MyStruct );
// Increase reference count
SharedPtr<MyStruct> ptr2( ptr );
ptr = nullptr; // Reference to the struct instance is still held by ptr2
ptr2 = nullptr; // The raw pointer is freed
}
Shared pointer class.
Definition: SharedPtr.h:68

It is similar to the std::shared_ptr class introduced in C++11; however, this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters and so on.)

Usage: SharedPtr<Class> ptr(new Class())

When ptr is passed around by value, the copy constructor and destructor manages the reference count of the raw pointer. If the counter reaches zero, delete is called on the raw pointer.

To avoid loops, use "weak" references by calling the original pointer directly through ptr.get().

Definition at line 68 of file SharedPtr.h.

Constructor & Destructor Documentation

◆ SharedPtr() [1/5]

constexpr SharedPtr ( )
constexpr

Create empty SharedPtr not pointing to anything.

Used for variable declaration.

Definition at line 74 of file SharedPtr.h.

◆ SharedPtr() [2/5]

constexpr SharedPtr ( std::nullptr_t  )
constexpr

Create empty SharedPtr not pointing to anything.

Definition at line 81 of file SharedPtr.h.

◆ SharedPtr() [3/5]

SharedPtr ( T *  ptr)

Create new SharedPtr.

Parameters
ptrPointer to take control over

Definition at line 89 of file SharedPtr.h.

◆ ~SharedPtr()

~SharedPtr ( )

Destructor.

Decrement reference counter, and delete object if no longer pointed to.

Definition at line 101 of file SharedPtr.h.

◆ SharedPtr() [4/5]

SharedPtr ( const SharedPtr< T > &  source)

Copy constructor.

Create new SharedPtr from other SharedPtr by copying pointer to original object and pointer to counter.

Parameters
sourceObject being copied from.

Definition at line 112 of file SharedPtr.h.

◆ SharedPtr() [5/5]

SharedPtr ( SharedPtr< T > &&  source)

Move constructor.

Create new SharedPtr from other SharedPtr by moving pointer to original object and pointer to counter.

Parameters
sourceObject being copied from.

Definition at line 126 of file SharedPtr.h.

Member Function Documentation

◆ operator=() [1/2]

SharedPtr operator= ( const SharedPtr< T > &  source)

Copy assignment operator.

Cleanup previous reference and assign new pointer and counter.

Parameters
sourceObject being assigned from.
Returns
Object being assigned.

Definition at line 138 of file SharedPtr.h.

◆ operator=() [2/2]

SharedPtr operator= ( SharedPtr< T > &&  source)

Move assignment operator.

Cleanup previous reference and assign new pointer and counter.

Parameters
sourceObject being assigned from.
Returns
Object being assigned.

Definition at line 163 of file SharedPtr.h.

◆ reset() [1/2]

void reset ( T *  ptr)

Replaces the managed pointer with a new unmanaged pointer.

Parameters
[in]ptrthe new raw pointer to manage.

Definition at line 184 of file SharedPtr.h.

◆ reset() [2/2]

void reset ( )

Replace the managed pointer with a null pointer.

Definition at line 201 of file SharedPtr.h.

◆ get()

T * get ( ) const

Raw pointer accessor.

Get raw pointer to object pointed to.

Returns
Pointer.

Definition at line 215 of file SharedPtr.h.

◆ use_count()

uint32_t use_count ( ) const

Reference count accessor.

Returns
Reference count.

Definition at line 224 of file SharedPtr.h.

◆ operator*()

T & operator* ( ) const

Dereference object operator.

Override to return the object pointed to.

Definition at line 237 of file SharedPtr.h.

◆ operator->()

T * operator-> ( ) const

Dereference object member operator.

Override to return return member in object pointed to.

Definition at line 246 of file SharedPtr.h.

◆ operator bool()

operator bool ( ) const

Boolean conversion operator.

Returns
Whether or not the pointer is null.

Definition at line 255 of file SharedPtr.h.