glibmm: Glib::RefPtr< T_CppObject > Class Template Reference

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

#include <glibmm/refptr.h>

Public Member Functions

 RefPtr ()
 Default constructor. More...

 
 ~RefPtr ()
 Destructor - decrements reference count. More...

 
 RefPtr (T_CppObject* pCppObject)
 For use only by the ::create() methods. More...

 
 RefPtr (const RefPtr< T_CppObject >& src)
 Copy constructor. More...

 
template<class T_CastFrom >
 RefPtr (const RefPtr< T_CastFrom >& src)
 Copy constructor (from different, but castable type). More...

 
void swap (RefPtr< T_CppObject >& other)
 Swap the contents of two RefPtr<>. More...

 
RefPtr< T_CppObject >& operator= (const RefPtr< T_CppObject >& src)
 Copy from another RefPtr: More...

 
template<class T_CastFrom >
RefPtr< T_CppObject >& operator= (const RefPtr< T_CastFrom >& src)
 Copy from different, but castable type). More...

 
bool operator== (const RefPtr< T_CppObject >& src) const
 Tests whether the RefPtr<> point to the same underlying instance. More...

 
bool operator!= (const RefPtr< T_CppObject >& src) const
 See operator==(). More...

 
T_CppObject* operator-> () const
 Dereferencing. More...

 
 operator bool () const
 Test whether the RefPtr<> points to any underlying instance. More...

 
void clear ()
 
void reset ()
 Set underlying instance to 0, decrementing reference count of existing instance appropriately. More...

 
bool operator< (const RefPtr< T_CppObject >& src) const
 Compare based on the underlying instance address. More...

 
bool operator<= (const RefPtr< T_CppObject >& src) const
 See operator<(). More...

 
bool operator> (const RefPtr< T_CppObject >& src) const
 See operator<(). More...

 
bool operator>= (const RefPtr< T_CppObject >& src) const
 See operator<(). More...

 

Static Public Member Functions

template<class T_CastFrom >
static RefPtr< T_CppObject > cast_dynamic (const RefPtr< T_CastFrom >& src)
 Dynamic cast to derived class. More...

 
template<class T_CastFrom >
static RefPtr< T_CppObject > cast_static (const RefPtr< T_CastFrom >& src)
 Static cast to derived class. More...

 
template<class T_CastFrom >
static RefPtr< T_CppObject > cast_const (const RefPtr< T_CastFrom >& src)
 Cast to non-const. More...

 

Related Functions

(Note that these are not member functions.)

template<class T_CppObject >
void swap (RefPtr< T_CppObject >& lhs, RefPtr< T_CppObject >& rhs)
 

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. In gtkmm, that is anything derived from Glib::ObjectBase, such as Gdk::Pixmap.

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

Examples:
thread/dispatcher.cc.

Constructor & Destructor Documentation

template <class T_CppObject>
Glib::RefPtr< T_CppObject >::RefPtr ( )
inline

Default constructor.

Afterwards it will be null and use of -> will cause a segmentation fault.

template <class T_CppObject>
Glib::RefPtr< T_CppObject >::~RefPtr ( )
inline

Destructor - decrements reference count.

template <class T_CppObject>
Glib::RefPtr< T_CppObject >::RefPtr ( T_CppObject *  pCppObject)
inlineexplicit

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

template <class T_CppObject>
Glib::RefPtr< T_CppObject >::RefPtr ( const RefPtr< T_CppObject >&  src)
inline

Copy constructor.

This increments the shared reference count.

template <class T_CppObject>
template <class T_CastFrom >
Glib::RefPtr< T_CppObject >::RefPtr ( const RefPtr< T_CastFrom >&  src)
inline

Copy constructor (from different, but castable type).

Increments the reference count.

Member Function Documentation

template <class T_CppObject>
template <class T_CastFrom >
static RefPtr<T_CppObject> Glib::RefPtr< T_CppObject >::cast_const ( const RefPtr< T_CastFrom >&  src)
inlinestatic

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);
template <class T_CppObject>
template <class T_CastFrom >
static RefPtr<T_CppObject> Glib::RefPtr< T_CppObject >::cast_dynamic ( const RefPtr< T_CastFrom >&  src)
inlinestatic

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);
template <class T_CppObject>
template <class T_CastFrom >
static RefPtr<T_CppObject> Glib::RefPtr< T_CppObject >::cast_static ( const RefPtr< T_CastFrom >&  src)
inlinestatic

Static cast to derived class.

Like the dynamic cast; the notation is

ptr_derived = RefPtr<Derived>::cast_static(ptr_base);
template <class T_CppObject>
void Glib::RefPtr< T_CppObject >::clear ( )
inline
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().
template <class T_CppObject>
Glib::RefPtr< T_CppObject >::operator bool ( ) const
inline

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

Mimics usage of ordinary pointers:

if (ptr)
do_something();
template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator!= ( const RefPtr< T_CppObject >&  src) const
inline
template <class T_CppObject>
T_CppObject* Glib::RefPtr< T_CppObject >::operator-> ( ) const
inline

Dereferencing.

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

template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator< ( const RefPtr< T_CppObject >&  src) const
inline

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.

template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator<= ( const RefPtr< T_CppObject >&  src) const
inline
template <class T_CppObject>
RefPtr<T_CppObject>& Glib::RefPtr< T_CppObject >::operator= ( const RefPtr< T_CppObject >&  src)
inline

Copy from another RefPtr:

template <class T_CppObject>
template <class T_CastFrom >
RefPtr<T_CppObject>& Glib::RefPtr< T_CppObject >::operator= ( const RefPtr< T_CastFrom >&  src)
inline

Copy from different, but castable type).

Increments the reference count.

template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator== ( const RefPtr< T_CppObject >&  src) const
inline

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

template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator> ( const RefPtr< T_CppObject >&  src) const
inline
template <class T_CppObject>
bool Glib::RefPtr< T_CppObject >::operator>= ( const RefPtr< T_CppObject >&  src) const
inline
template <class T_CppObject>
void Glib::RefPtr< T_CppObject >::reset ( )
inline

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

Since glibmm 2.16:
template <class T_CppObject>
void Glib::RefPtr< T_CppObject >::swap ( RefPtr< T_CppObject >&  other)
inline

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 Function Documentation

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