mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

SharedPtr< T > Class Template Reference

SharedPtr< T > Class Template Reference

Shared pointer class. More...

#include <SharedPtr.h>

Public Member Functions

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

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 = NULL; // Reference to the struct instance is still held by ptr2

     ptr2 = NULL; // The raw pointer is freed
 }

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 cmsis/BUILD/mbed/platform/SharedPtr.h.


Constructor & Destructor Documentation

SharedPtr (  )

Create empty SharedPtr not pointing to anything.

Used for variable declaration.

Definition at line 74 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

SharedPtr ( T *  ptr )

Create new SharedPtr.

Parameters:
ptrPointer to take control over

Definition at line 82 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

~SharedPtr (  )

Destructor.

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

Definition at line 95 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

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 106 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

SharedPtr (  )

Create empty SharedPtr not pointing to anything.

Used for variable declaration.

Definition at line 74 of file platform/SharedPtr.h.

SharedPtr ( T *  ptr )

Create new SharedPtr.

Parameters:
ptrPointer to take control over

Definition at line 82 of file platform/SharedPtr.h.

~SharedPtr (  )

Destructor.

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

Definition at line 95 of file platform/SharedPtr.h.

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 106 of file platform/SharedPtr.h.


Member Function Documentation

T* get (  ) const

Raw pointer accessor.

Get raw pointer to object pointed to.

Returns:
Pointer.

Definition at line 171 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

T* get (  ) const

Raw pointer accessor.

Get raw pointer to object pointed to.

Returns:
Pointer.

Definition at line 171 of file platform/SharedPtr.h.

operator bool (  ) const

Boolean conversion operator.

Returns:
Whether or not the pointer is NULL.

Definition at line 214 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

operator bool (  ) const

Boolean conversion operator.

Returns:
Whether or not the pointer is NULL.

Definition at line 214 of file platform/SharedPtr.h.

T& operator* (  ) const

Dereference object operator.

Override to return the object pointed to.

Definition at line 196 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

T& operator* (  ) const

Dereference object operator.

Override to return the object pointed to.

Definition at line 196 of file platform/SharedPtr.h.

T* operator-> (  ) const

Dereference object member operator.

Override to return return member in object pointed to.

Definition at line 205 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

T* operator-> (  ) const

Dereference object member operator.

Override to return return member in object pointed to.

Definition at line 205 of file platform/SharedPtr.h.

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

Assignment operator.

Cleanup previous reference and assign new pointer and counter.

Parameters:
sourceObject being assigned from.
Returns:
Object being assigned.

Definition at line 120 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

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

Assignment operator.

Cleanup previous reference and assign new pointer and counter.

Parameters:
sourceObject being assigned from.
Returns:
Object being assigned.

Definition at line 120 of file platform/SharedPtr.h.

void reset (  )

Replace the managed pointer with a NULL pointer.

Definition at line 161 of file platform/SharedPtr.h.

void reset ( T *  ptr )

Replaces the managed pointer with a new unmanaged pointer.

Parameters:
[in]ptrthe new raw pointer to manage.

Definition at line 143 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

void reset ( T *  ptr )

Replaces the managed pointer with a new unmanaged pointer.

Parameters:
[in]ptrthe new raw pointer to manage.

Definition at line 143 of file platform/SharedPtr.h.

void reset (  )

Replace the managed pointer with a NULL pointer.

Definition at line 161 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

uint32_t use_count (  ) const

Reference count accessor.

Returns:
Reference count.

Definition at line 180 of file cmsis/BUILD/mbed/platform/SharedPtr.h.

uint32_t use_count (  ) const

Reference count accessor.

Returns:
Reference count.

Definition at line 180 of file platform/SharedPtr.h.