glibmm 2.66.10

Public Member Functions | Static Public Member Functions | Related Symbols | List of all members
Glib::RefPtr< T_CppObject > Class Template Reference

RefPtr<> is a reference-counting shared smartpointer. More...

`#include <glibmm/refptr.h>`

## Public Member Functions
| RefPtr () noexcept
| Default constructor.
| ~RefPtr () noexcept
| Destructor - decrements reference count.
| RefPtr (T_CppObject * pCppObject) noexcept
| For use only by the ::create() methods.
| RefPtr (const RefPtr & src) noexcept
| Copy constructor.
| RefPtr (RefPtr && src) noexcept
| Move constructor.
template<class T_CastFrom>
| RefPtr (RefPtr< T_CastFrom > && src) noexcept
| Move constructor (from different, but castable type).
template<class T_CastFrom>
| RefPtr (const RefPtr< T_CastFrom > & src) noexcept
| Copy constructor (from different, but castable type).
void | swap (RefPtr & other) noexcept
| Swap the contents of two RefPtr<>.
RefPtr & | operator= (const RefPtr & src) noexcept
| Copy from another RefPtr:
RefPtr & | operator= (RefPtr && src) noexcept
| Move assignment operator:
template<class T_CastFrom>
RefPtr & | operator= (RefPtr< T_CastFrom > && src) noexcept
| Move assignment operator (from different, but castable type):
template<class T_CastFrom>
RefPtr & | operator= (const RefPtr< T_CastFrom > & src) noexcept
| Copy from different, but castable type.
bool | operator== (const RefPtr & src) const noexcept
| Tests whether the RefPtr<> point to the same underlying instance.
bool | operator!= (const RefPtr & src) const noexcept
| See operator==().
T_CppObject * | operator-> () const noexcept
| Dereferencing.
T_CppObject * | get () const noexcept
| Returns the stored pointer.
| operator bool () const noexcept
| Test whether the RefPtr<> points to any underlying instance.
void | clear () noexcept
void | reset () noexcept
| Set underlying instance to nullptr, decrementing reference count of existing instance appropriately.
T_CppObject * | release () noexcept G_GNUC_WARN_UNUSED_RESULT
| Release the ownership of underlying instance.
bool | operator< (const RefPtr & src) const noexcept
| Compare based on the underlying instance address.
bool | operator<= (const RefPtr & src) const noexcept
| See operator<().
bool | operator> (const RefPtr & src) const noexcept
| See operator<().
bool | operator>= (const RefPtr & src) const noexcept
| See operator<().

## Static Public Member Functions
template<class T_CastFrom>
static RefPtr | cast_dynamic (const RefPtr< T_CastFrom > & src) noexcept
| Dynamic cast to derived class.
template<class T_CastFrom>
static RefPtr | cast_static (const RefPtr< T_CastFrom > & src) noexcept
| Static cast to derived class.
template<class T_CastFrom>
static RefPtr | cast_const (const RefPtr< T_CastFrom > & src) noexcept
| Cast to non-const.

## Related Symbols
(Note that these are not member symbols.)
template<class T_CppObject>
void | swap (RefPtr< T_CppObject > & lhs, RefPtr< T_CppObject > & rhs) noexcept

## Detailed Description

template<class T_CppObject>
class Glib::RefPtr< T_CppObject >

RefPtr<> is a reference-counting shared smartpointer.

Some objects in gtkmm are obtained from a shared store. Consequently you cannot instantiate them yourself. Instead they return a RefPtr which behaves much like an ordinary pointer in that members can be reached with the usual `object_ptr->member` notation. Unlike most other smart pointers, RefPtr doesn't support dereferencing through `* object_ptr`.

Reference counting means that a shared reference count is incremented each time a RefPtr is copied, and decremented each time a RefPtr is destroyed, for instance when it leaves its scope. When the reference count reaches zero, the contained object is deleted, meaning you don't need to remember to delete the object.

RefPtr<> can store any class that has reference() and unreference() methods, and whose destructor is noexcept (the default for destructors). In gtkmm, that is anything derived from Glib::ObjectBase, such as Gdk::Pixbuf.

See the "Memory Management" section in the "Programming with gtkmm" book for further information.

Examples
  thread/dispatcher.cc.

## Constructor & Destructor Documentation

## ◆ RefPtr() [1/6]

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::RefPtr | ( |  | ) |
| inline noexcept

Default constructor.

Afterwards it will be null and use of -> will invoke undefined behaviour.

## ◆ ~RefPtr()

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::~RefPtr | ( |  | ) |
| inline noexcept

Destructor - decrements reference count.

## ◆ RefPtr() [2/6]

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::RefPtr | ( | T_CppObject * | pCppObject | ) |
| inline explicit noexcept

For use only by the ::create() methods.

## ◆ RefPtr() [3/6]

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::RefPtr | ( | const RefPtr< T_CppObject > & | src | ) |
| inline noexcept

Copy constructor.

This increments the shared reference count.

## ◆ RefPtr() [4/6]

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::RefPtr | ( | RefPtr< T_CppObject > && | src | ) |
| inline noexcept

Move constructor.

## ◆ RefPtr() [5/6]

template <class T_CppObject>
template <class T_CastFrom>

Glib::RefPtr< T_CppObject >::RefPtr | ( | RefPtr< T_CastFrom > && | src | ) |
| inline noexcept

Move constructor (from different, but castable type).

## ◆ RefPtr() [6/6]

template <class T_CppObject>
template <class T_CastFrom>

Glib::RefPtr< T_CppObject >::RefPtr | ( | const RefPtr< T_CastFrom > & | src | ) |
| inline noexcept

Copy constructor (from different, but castable type).

Increments the reference count.

## Member Function Documentation

## ◆ cast_const()

template <class T_CppObject>
template <class T_CastFrom>

RefPtr Glib::RefPtr< T_CppObject >::cast_const | ( | const RefPtr< T_CastFrom > & | src | ) |
| inline static noexcept

Cast to non-const.

The RefPtr can't be cast with the usual notation so instead you can use

ptr_unconst = RefPtr<UnConstType>::cast_const(ptr_const);
Glib::RefPtr::cast_const
static RefPtr cast_const(const RefPtr< T_CastFrom > &src) noexcept
Cast to non-const.

## ◆ cast_dynamic()

template <class T_CppObject>
template <class T_CastFrom>

RefPtr Glib::RefPtr< T_CppObject >::cast_dynamic | ( | const RefPtr< T_CastFrom > & | src | ) |
| inline static noexcept

Dynamic cast to derived class.

The RefPtr can't be cast with the usual notation so instead you can use

ptr_derived = RefPtr<Derived>::cast_dynamic(ptr_base);
Glib::RefPtr::cast_dynamic
static RefPtr cast_dynamic(const RefPtr< T_CastFrom > &src) noexcept
Dynamic cast to derived class.

## ◆ cast_static()

template <class T_CppObject>
template <class T_CastFrom>

RefPtr Glib::RefPtr< T_CppObject >::cast_static | ( | const RefPtr< T_CastFrom > & | src | ) |
| inline static noexcept

Static cast to derived class.

Like the dynamic cast; the notation is

ptr_derived = RefPtr<Derived>::cast_static(ptr_base);
Glib::RefPtr::cast_static
static RefPtr cast_static(const RefPtr< T_CastFrom > &src) noexcept
Static cast to derived class.

## ◆ clear()

template <class T_CppObject>

void Glib::RefPtr< T_CppObject >::clear | ( |  | ) |
| inline noexcept

Deprecated
  Use reset() instead because this leads to confusion with clear() methods on the underlying class. For instance, people use .clear() when they mean ->clear().

## ◆ get()

template <class T_CppObject>

T_CppObject * Glib::RefPtr< T_CppObject >::get | ( |  | ) | const
| inline noexcept

Returns the stored pointer.

Since glibmm 2.56

## ◆ operator bool()

template <class T_CppObject>

Glib::RefPtr< T_CppObject >::operator bool | ( |  | ) | const
| inline explicit noexcept

Test whether the RefPtr<> points to any underlying instance.

Mimics usage of ordinary pointers:

if (ptr)
do_something();

## ◆ operator!=()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator!= | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

See operator==().

## ◆ operator->()

template <class T_CppObject>

T_CppObject * Glib::RefPtr< T_CppObject >::operator-> | ( |  | ) | const
| inline noexcept

Dereferencing.

Use the methods of the underlying instance like so: `refptr->memberfun()`.

## ◆ operator<()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator< | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

Compare based on the underlying instance address.

This is needed in code that requires an ordering on RefPtr<T_CppObject> instances, e.g. std::set<RefPtr<T_CppObject> >.

Without these, comparing two RefPtr<T_CppObject> instances is still syntactically possible, but the result is semantically wrong, as p1 REL_OP p2 is interpreted as (bool)p1 REL_OP (bool)p2.

## ◆ operator<=()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator<= | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

See operator<().

## ◆ operator=() [1/4]

template <class T_CppObject>

RefPtr & Glib::RefPtr< T_CppObject >::operator= | ( | const RefPtr< T_CppObject > & | src | ) |
| inline noexcept

Copy from another RefPtr:

## ◆ operator=() [2/4]

template <class T_CppObject>
template <class T_CastFrom>

RefPtr & Glib::RefPtr< T_CppObject >::operator= | ( | const RefPtr< T_CastFrom > & | src | ) |
| inline noexcept

Copy from different, but castable type.

Increments the reference count.

## ◆ operator=() [3/4]

template <class T_CppObject>

RefPtr & Glib::RefPtr< T_CppObject >::operator= | ( | RefPtr< T_CppObject > && | src | ) |
| inline noexcept

Move assignment operator:

## ◆ operator=() [4/4]

template <class T_CppObject>
template <class T_CastFrom>

RefPtr & Glib::RefPtr< T_CppObject >::operator= | ( | RefPtr< T_CastFrom > && | src | ) |
| inline noexcept

Move assignment operator (from different, but castable type):

## ◆ operator==()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator== | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

Tests whether the RefPtr<> point to the same underlying instance.

## ◆ operator>()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator> | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

See operator<().

## ◆ operator>=()

template <class T_CppObject>

bool Glib::RefPtr< T_CppObject >::operator>= | ( | const RefPtr< T_CppObject > & | src | ) | const
| inline noexcept

See operator<().

## ◆ release()

template <class T_CppObject>

T_CppObject * Glib::RefPtr< T_CppObject >::release | ( |  | ) |
| inline noexcept

Release the ownership of underlying instance.

RefPtr's underlying instance is set to nullptr, therefore underlying object can't be accessed through this RefPtr anymore.

Returns
  an underlying instance.

Most users should not use release(). It can spoil the automatic destruction of the managed object. A legitimate use is if you immediately give RefPtr's reference to another object.

## ◆ reset()

template <class T_CppObject>

void Glib::RefPtr< T_CppObject >::reset | ( |  | ) |
| inline noexcept

Set underlying instance to nullptr, decrementing reference count of existing instance appropriately.

Since glibmm 2.16

## ◆ swap()

template <class T_CppObject>

void Glib::RefPtr< T_CppObject >::swap | ( | RefPtr< T_CppObject > & | other | ) |
| inline noexcept

Swap the contents of two RefPtr<>.

This method swaps the internal pointers to T_CppObject. This can be done safely without involving a reference/unreference cycle and is therefore highly efficient.

## Friends And Related Symbol Documentation

## ◆ swap()

template <class T_CppObject>

void swap | ( | RefPtr< T_CppObject > & | lhs,
|  | RefPtr< T_CppObject > & | rhs )
| related