Poco

template < class C >

class AutoPtr

File Information

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

Description

AutoPtr is a "smart" pointer for classes implementing reference counting based garbage collection. To be usable with the AutoPtr template, a class must implement the following behaviour: A class must maintain a reference count. The constructors of the object initialize the reference count to one. The class must implement a public duplicate() method:

void duplicate();

that increments the reference count by one. The class must implement a public release() method:

void release()

that decrements the reference count by one, and, if the reference count reaches zero, deletes the object.

AutoPtr works in the following way: If an AutoPtr 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 remains unchanged. If the AutoPtr is assigned another AutoPtr, the object's reference count is incremented by one by calling duplicate() on its object. The destructor of AutoPtr calls release() on its object. AutoPtr supports dereferencing with both the -> and the * operator. An attempt to dereference a null AutoPtr results in a NullPointerException being thrown. AutoPtr also implements all relational operators. Note that AutoPtr allows casting of its encapsulated data types.

Member Summary

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

Constructors

AutoPtr inline

AutoPtr();

AutoPtr inline

AutoPtr(
    C * ptr
);

AutoPtr inline

AutoPtr(
    const AutoPtr & ptr
);

AutoPtr inline

AutoPtr(
    AutoPtr && ptr
) noexcept;

AutoPtr inline

template < class Other > AutoPtr(
    const AutoPtr < Other > & ptr
);

AutoPtr inline

AutoPtr(
    C * ptr,
    bool shared
);

Destructor

~AutoPtr inline

~AutoPtr();

Member Functions

assign inline

AutoPtr & assign(
    C * ptr
);

assign inline

AutoPtr & assign(
    C * ptr,
    bool shared
);

assign inline

AutoPtr & assign(
    const AutoPtr & ptr
);

assign inline

template < class Other > AutoPtr & assign(
    const AutoPtr < Other > & ptr
);

cast inline

template < class Other > AutoPtr < Other > cast() const;

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

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

duplicate inline

C * duplicate();

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 AutoPtr & 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 AutoPtr & ptr
) const;

operator < inline

bool operator < (
    const C * ptr
) const;

operator < inline

bool operator < (
    C * ptr
) const;

operator <= inline

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

operator <= inline

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

operator <= inline

bool operator <= (
    C * ptr
) const;

operator = inline

AutoPtr & operator = (
    C * ptr
);

operator = inline

AutoPtr & operator = (
    const AutoPtr & ptr
);

operator = inline

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

operator = inline

template < class Other > AutoPtr & operator = (
    const AutoPtr < Other > & ptr
);

operator == inline

bool operator == (
    const AutoPtr & 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 AutoPtr & ptr
) const;

operator > inline

bool operator > (
    const C * ptr
) const;

operator > inline

bool operator > (
    C * ptr
) const;

operator >= inline

bool operator >= (
    const AutoPtr & 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;

reset inline

void reset();

reset inline

void reset(
    C * ptr
);

reset inline

void reset(
    C * ptr,
    bool shared
);

reset inline

void reset(
    const AutoPtr & ptr
);

reset inline

template < class Other > void reset(
    const AutoPtr < Other > & ptr
);

swap inline

void swap(
    AutoPtr & ptr
) noexcept;

unsafeCast inline

template < class Other > AutoPtr < Other > unsafeCast() const;

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

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