Poco

template < class C, class RC = ReferenceCounter, class RP = ReleasePolicy < C >>

class SharedPtr

Library: Foundation
Package: Core
Header: Poco/SharedPtr.h

Description

SharedPtr is a "smart" pointer for classes implementing reference counting based garbage collection. SharedPtr is thus similar to AutoPtr. Unlike the AutoPtr template, which can only be used with classes that support reference counting, SharedPtr can be used with any class. For this to work, a SharedPtr manages a reference count for the object it manages.

SharedPtr works in the following way: If an SharedPtr is assigned an ordinary pointer to an object (via the constructor or the assignment operator), it takes ownership of the object and the object's reference count is initialized to one. If the SharedPtr is assigned another SharedPtr, the object's reference count is incremented by one. The destructor of SharedPtr decrements the object's reference count by one and deletes the object if the reference count reaches zero. SharedPtr supports dereferencing with both the -> and the * operator. An attempt to dereference a null SharedPtr results in a NullPointerException being thrown. SharedPtr also implements all relational operators and a cast operator in case dynamic casting of the encapsulated data types is required.

Member Summary

Member Functions: assign, cast, get, isNull, operator !, operator !=, operator *, operator <, operator <=, operator =, operator ==, operator >, operator >=, operator C *, operator const C *, operator->, referenceCount, reset, swap, unsafeCast

Types

Type

typedef C Type;

Constructors

SharedPtr inline

SharedPtr();

SharedPtr inline

SharedPtr(
    C * ptr
);

SharedPtr inline

template < class Other, class OtherRP > SharedPtr(
    const SharedPtr < Other, RC, OtherRP > & ptr
);

SharedPtr inline

SharedPtr(
    const SharedPtr & ptr
);

SharedPtr inline

SharedPtr(
    SharedPtr && ptr
) noexcept;

Destructor

~SharedPtr inline

~SharedPtr();

Member Functions

assign inline

SharedPtr & assign(
    C * ptr
);

assign inline

SharedPtr & assign(
    const SharedPtr & ptr
);

assign inline

template < class Other, class OtherRP > SharedPtr & assign(
    const SharedPtr < Other, RC, OtherRP > & ptr
);

cast inline

template < class Other > SharedPtr < Other, RC, RP > cast() const;

Casts the SharedPtr via a dynamic cast to the given type. Returns an SharedPtr containing NULL if the cast fails. Example: (assume class Sub: public Super)

SharedPtr<Super> super(new Sub());
SharedPtr<Sub> sub = super.cast<Sub>();
poco_assert (sub.get());

get inline

C * get();

get inline

const C * get() const;

isNull inline

bool isNull() const;

operator ! inline

bool operator ! () const;

operator != inline

bool operator != (
    const SharedPtr & ptr
) const;

operator != inline

bool operator != (
    const C * ptr
) const;

operator != inline

bool operator != (
    C * ptr
) const;

operator != inline

bool operator != (
    std::nullptr_t ptr
) const;

operator * inline

C & operator * ();

operator * inline

const C & operator * () const;

operator < inline

bool operator < (
    const SharedPtr & ptr
) const;

operator < inline

bool operator < (
    const C * ptr
) const;

operator < inline

bool operator < (
    C * ptr
) const;

operator <= inline

bool operator <= (
    const SharedPtr & ptr
) const;

operator <= inline

bool operator <= (
    const C * ptr
) const;

operator <= inline

bool operator <= (
    C * ptr
) const;

operator = inline

SharedPtr & operator = (
    C * ptr
);

operator = inline

SharedPtr & operator = (
    const SharedPtr & ptr
);

operator = inline

SharedPtr & operator = (
    SharedPtr && ptr
) noexcept;

operator = inline

template < class Other, class OtherRP > SharedPtr & operator = (
    const SharedPtr < Other, RC, OtherRP > & ptr
);

operator == inline

bool operator == (
    const SharedPtr & ptr
) const;

operator == inline

bool operator == (
    const C * ptr
) const;

operator == inline

bool operator == (
    C * ptr
) const;

operator == inline

bool operator == (
    std::nullptr_t ptr
) const;

operator > inline

bool operator > (
    const SharedPtr & ptr
) const;

operator > inline

bool operator > (
    const C * ptr
) const;

operator > inline

bool operator > (
    C * ptr
) const;

operator >= inline

bool operator >= (
    const SharedPtr & ptr
) const;

operator >= inline

bool operator >= (
    const C * ptr
) const;

operator >= inline

bool operator >= (
    C * ptr
) const;

operator C * inline

operator C * ();

operator const C * inline

operator const C * () const;

operator-> inline

C * operator-> ();

operator-> inline

const C * operator-> () const;

referenceCount inline

int referenceCount() const;

reset inline

void reset();

reset inline

void reset(
    C * ptr
);

reset inline

void reset(
    const SharedPtr & ptr
);

reset inline

template < class Other, class OtherRP > void reset(
    const SharedPtr < Other, RC, OtherRP > & ptr
);

swap inline

void swap(
    SharedPtr & ptr
);

unsafeCast inline

template < class Other > SharedPtr < Other, RC, RP > unsafeCast() const;

Casts the SharedPtr via a static cast to the given type. Example: (assume class Sub: public Super)

SharedPtr<Super> super(new Sub());
SharedPtr<Sub> sub = super.unsafeCast<Sub>();
poco_assert (sub.get());