Into

Modules

Documentation

classPiiSharedPtr

#include <PiiSharedPtr.h>

An implicitly shared pointer.

Description

The PiiSharedPtr class holds a reference-counted pointer (PiiSharedObject) and masquerades as one itself. The class automatically increases and decreases the reference count when copies of it are requested. Thus, you never need to care about deleting the memory. Typical usage (compare this to the explicit sharing example in PiiSharedObject):

 class MyObject : public PiiSharedObject;
 typedef PiiSharedPtr<MyObject> MyPtr;

 // Sender creates an explicitly shared pointer and passes it.
 MyPtr ptr(new MyObject);
 receiver.takeThis(ptr);
 otherReceiver.takeThis(ptr);

 //in the receiver, we just store the pointer. It is automatically
 //released when the Receiver class is deleted.
 void Receiver::takeThis(MyPtr ptr)
 {
   //the MyPtr object works just as a pointer
   ptr->callMethod();
   internalVariable = ptr;
   doWhatEverNeeded();
 }

The class supports all types types, including arrays. If the reference-counted type is not directly derived from PiiSharedObject, a reference-counted wrapper will be automatically created.

 PiiSharedPtr<string> ptr(new string("this is"));
 *ptr += " a test";
 cout << ptr->c_str() << endl; //outputs "this is a test"
 PiiSharedPtr<int> ptr(new int);
 ptr = new int; //releases the old one
 *ptr = 3; //use the class just like an int pointer
 PiiSharedPtr<int[]> ptr(new int[5]);
 ptr = new int[6]; //releases the old array
 ptr[0] = 3; //use the class just like an int array

The computational overhead of using a shared pointer instead of a direct one is minimal. Copying a pointer costs one (non-virtual and inlineable) function call, and accessing it is as fast as direct access, provided that the pointer type is derived from PiiSharedObject. If it is not, there will be one more memory indirection.

To ensure no memory leaks, one should never store the internal pointer as an ordinary pointer only, although it is possible. Once you initialize a PiiSharedPtr with a newly allocated pointer, just forget about the pointer and make sure you only make assignments between PiiSharedPtrs. You may, however, use the internal pointer directly if you ensure that the PiiSharedPtr instance stays in memory.

 MyPtr ptr(new MyObject);
 MyObject* ptr2 = ptr;
 // You may now safely use ptr2 as long as ptr is in memory

One can assign PiiSharedPtrs just like ordinary pointers. That is, a PiiSharedPtr<Base> can hold a pointer to a class derived from Base.

If the types are not derived from PiiSharedObject, multiple inheritance is not always handled correctly. You can assign derived pointers to base class pointers only if the base class is the first one in iheritance order.

See also

Constructors and destructor

(
  • T * obj
)

Creates an implicitly shared pointer that holds the given pointer.

template<class U>
(
  • const PiiSharedPtr< U > & other
  • typename OnlyDerived< U >::Type = 0
)

Create a copy of a pointer.

( )

Create a copy of a pointer.

Public member functions

Casts the shared pointer to the internal pointer type.

bool
( )

Compare two pointers.

bool
(
  • const void * ptr
)

Compare the internal pointer to the given ptr.

T &
( )

Converts the shared pointer to a reference.

T *

Returns the internal pointer.

(
  • T * ptr
)

Assigns a new value to this pointer.

template<class U>
( )

Assigns a new value to this pointer and release the old pointer.

( )

Assigns a new value to this pointer and releases the old pointer.

bool
( )

Compare two pointers.

bool
(
  • const void * ptr
)

Compare the internal pointer to the given ptr.

T &
(
  • int index
)

Returns a reference to the element at index, if the wrapped pointer is an array.

T
(
  • int index
)

Returns the element at index, if the wrapped pointer is an array.

Friends

friend struct

Function details

  • PiiSharedPtr

    (
    • T * obj
    )
    [inline, explicit]

    Creates an implicitly shared pointer that holds the given pointer.

    Parameters
    obj

    a pointer to the object to be shared. PiiSharedPtr takes the ownership of the pointer.

  • PiiSharedPtr

    ()
    [inline]
  • template<class U>

    PiiSharedPtr

    (
    • const PiiSharedPtr< U > & other
    • typename OnlyDerived< U >::Type = 0
    )
    [inline]

    Create a copy of a pointer.

    This constructor only accepts pointers to objects that are derived from the template type T of this class.

  • PiiSharedPtr

    ( )
    [inline]

    Create a copy of a pointer.

  • ~PiiSharedPtr

    ()
    [inline]
  • template<class Archive>

    void serialize

    (
    • Archive & archive
    • const unsigned int
    )
    [inline]
  • operator T *

    ()
    [inline]

    Casts the shared pointer to the internal pointer type.

  • bool operator!=

    ( )
    [inline]

    Compare two pointers.

    Returns

    true iff the internal pointers are different

  • bool operator!=

    (
    • const void * ptr
    )
    [inline]

    Compare the internal pointer to the given ptr.

  • T & operator*

    ()
    [inline]

    Converts the shared pointer to a reference.

  • T * operator->

    ()
    [inline]

    Returns the internal pointer.

    This operator makes it possible to use this class directly as a pointer.

  • PiiSharedPtr & operator=

    (
    • T * ptr
    )
    [inline]

    Assigns a new value to this pointer.

    The old pointer will be released. PiiSharedPtr takes the ownership of ptr.

     PiiSharedPtr<MyObject> ptr(new MyObject);
     ptr = new MyObject; //releases the old pointer in ptr
    
  • template<class U>

    PiiSharedPtr & operator=

    ( )
    [inline]

    Assigns a new value to this pointer and release the old pointer.

    This operator only accepts pointers to objects that are derived from the template type T of this class.

  • PiiSharedPtr & operator=

    ( )
    [inline]

    Assigns a new value to this pointer and releases the old pointer.

     PiiSharedPtr<MyObject> ptr1(new MyObject), ptr2(new MyObject);
     ptr1 = ptr2; //releases the old pointer in ptr1, increases refcount in ptr2
    
  • bool operator==

    ( )
    [inline]

    Compare two pointers.

    Returns

    true iff the internal pointers are equal

  • bool operator==

    (
    • const void * ptr
    )
    [inline]

    Compare the internal pointer to the given ptr.

  • T & operator[]

    (
    • int index
    )
    [inline]

    Returns a reference to the element at index, if the wrapped pointer is an array.

  • T operator[]

    (
    • int index
    )
    [inline]

    Returns the element at index, if the wrapped pointer is an array.

Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.