Fawkes API  Fawkes Development Version
fawkes::RefPtr< T_CppObject > Class Template Reference

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

#include <refptr.h>

Inheritance diagram for fawkes::RefPtr< T_CppObject >:

Public Member Functions

 RefPtr ()
 Default constructor. More...
 
 ~RefPtr ()
 Destructor - decrements reference count. More...
 
 RefPtr (T_CppObject *cpp_object)
 Constructor that takes ownership. More...
 
 RefPtr (const RefPtr< T_CppObject > &src)
 Copy constructor This increments the shared reference count. 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...
 
RefPtr< T_CppObject > & operator= (T_CppObject *ptr)
 Assign object and claim ownership. 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
 Tests whether the RefPtr<> do not point to the same underlying instance. More...
 
T_CppObject * operator-> () const
 Dereferencing. More...
 
T_CppObject * operator* () const
 Get underlying pointer. More...
 
 operator bool () const
 Test whether the RefPtr<> points to any underlying instance. More...
 
void clear ()
 Set underlying instance to 0, decrementing reference count of existing instance appropriately. More...
 
void reset ()
 Reset pointer. More...
 
 RefPtr (T_CppObject *cpp_object, int *refcount, Mutex *refmutex)
 For use only in the internal implementation of sharedptr. More...
 
int * refcount_ptr () const
 For use only in the internal implementation of sharedptr. More...
 
int use_count () const
 Get current reference count. More...
 
Mutexrefmutex_ptr () const
 For use only in the internal implementation of sharedptr. 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 > &lrp, RefPtr< T_CppObject > &rrp)
 Swap refptr instances. More...
 

Detailed Description

template<class T_CppObject>
class fawkes::RefPtr< T_CppObject >

RefPtr<> is a reference-counting shared smartpointer.

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

Fawkes uses RefPtr so that you don't need to remember to delete the object explicitly, or know when a method expects you to delete the object that it returns.

Note that RefPtr is thread-safe.

Definition at line 49 of file refptr.h.

Constructor & Destructor Documentation

◆ RefPtr() [1/5]

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

Default constructor.

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

Definition at line 263 of file refptr.h.

Referenced by fawkes::RefPtr< Roomba500 >::cast_const().

◆ ~RefPtr()

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

Destructor - decrements reference count.

Definition at line 271 of file refptr.h.

◆ RefPtr() [2/5]

template<class T_CppObject>
fawkes::RefPtr< T_CppObject >::RefPtr ( T_CppObject *  cpp_object)
inlineexplicit

Constructor that takes ownership.

This takes ownership of cpp_object, so it will be deleted when the last RefPtr is deleted, for instance when it goes out of scope.

Parameters
cpp_objectC++ object to take ownership of

Definition at line 299 of file refptr.h.

◆ RefPtr() [3/5]

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

Copy constructor This increments the shared reference count.

Parameters
srcrefptr to copy

Definition at line 329 of file refptr.h.

◆ RefPtr() [4/5]

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

Copy constructor (from different, but castable type).

Increments the reference count.

Parameters
srcrefptr to copy

Definition at line 349 of file refptr.h.

◆ RefPtr() [5/5]

template<class T_CppObject>
fawkes::RefPtr< T_CppObject >::RefPtr ( T_CppObject *  cpp_object,
int *  refcount,
Mutex refmutex 
)
inlineexplicit

For use only in the internal implementation of sharedptr.

Parameters
cpp_objectC++ object to wrap
refcountreference count
refmutexreference count mutex

Definition at line 315 of file refptr.h.

Member Function Documentation

◆ cast_const()

template<class T_CppObject>
template<class T_CastFrom >
static RefPtr<T_CppObject> fawkes::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);
Parameters
srcsource refptr to cast
Returns
refptr to object casted to given type

Definition at line 204 of file refptr.h.

◆ cast_dynamic()

template<class T_CppObject>
template<class T_CastFrom >
static RefPtr<T_CppObject> fawkes::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);
Parameters
srcsource refptr to cast
Returns
refptr to object casted to given type

Definition at line 167 of file refptr.h.

◆ cast_static()

template<class T_CppObject>
template<class T_CastFrom >
static RefPtr<T_CppObject> fawkes::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);
Parameters
srcsource refptr to cast
Returns
refptr to object casted to given type

Definition at line 187 of file refptr.h.

◆ clear()

template<class T_CppObject >
void fawkes::RefPtr< T_CppObject >::clear ( )
inline

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

Definition at line 457 of file refptr.h.

Referenced by Roomba500Thread::finalize(), Roomba500Thread::init(), fawkes::OpenRaveEnvironment::run_graspplanning(), JacoOpenraveThread::set_target(), JacoOpenraveThread::set_target_ang(), and JacoGotoThread::stop().

◆ operator bool()

template<class T_CppObject >
fawkes::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();

Definition at line 451 of file refptr.h.

◆ operator!=()

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

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

Parameters
srcrefptr to compare to
Returns
true if both refptrs do not point to the same instance.

Definition at line 445 of file refptr.h.

◆ operator*()

template<class T_CppObject >
T_CppObject * fawkes::RefPtr< T_CppObject >::operator* ( ) const
inline

Get underlying pointer.

Use with care!

Returns
pointer to encapsulated object

Definition at line 257 of file refptr.h.

◆ operator->()

template<class T_CppObject >
T_CppObject * fawkes::RefPtr< T_CppObject >::operator-> ( ) const
inline

Dereferencing.

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

Returns
pointer to encapsulated object

Definition at line 250 of file refptr.h.

◆ operator=() [1/3]

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

Copy from another RefPtr.

Parameters
srcrefptr to copy from
Returns
reference to this instance

Definition at line 384 of file refptr.h.

◆ operator=() [2/3]

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

Copy from different, but castable type).

Increments the reference count.

Parameters
srcrefptr to copy from
Returns
reference to this instance

Definition at line 429 of file refptr.h.

◆ operator=() [3/3]

template<class T_CppObject>
RefPtr< T_CppObject > & fawkes::RefPtr< T_CppObject >::operator= ( T_CppObject *  ptr)
inline

Assign object and claim ownership.

Parameters
ptrpointer to object, this refptr will claim ownership of the src!
Returns
reference to this instance

Definition at line 417 of file refptr.h.

◆ operator==()

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

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

Parameters
srcrefptr to compare to
Returns
true if both refptrs point to the same instance.

Definition at line 438 of file refptr.h.

◆ refcount_ptr()

template<class T_CppObject>
int* fawkes::RefPtr< T_CppObject >::refcount_ptr ( ) const
inline

For use only in the internal implementation of sharedptr.

Get reference count pointer. Warning: This is for internal use only. Do not manually modify the reference count with this pointer.

Returns
pointer to refcount integer

Definition at line 224 of file refptr.h.

Referenced by fawkes::RefPtr< Roomba500 >::cast_const(), fawkes::RefPtr< Roomba500 >::cast_dynamic(), and fawkes::RefPtr< Roomba500 >::cast_static().

◆ refmutex_ptr()

template<class T_CppObject>
Mutex* fawkes::RefPtr< T_CppObject >::refmutex_ptr ( ) const
inline

For use only in the internal implementation of sharedptr.

Get reference mutex.

Returns
pointer to refcount mutex

Definition at line 235 of file refptr.h.

Referenced by fawkes::RefPtr< Roomba500 >::cast_const(), fawkes::RefPtr< Roomba500 >::cast_dynamic(), and fawkes::RefPtr< Roomba500 >::cast_static().

◆ reset()

template<class T_CppObject >
void fawkes::RefPtr< T_CppObject >::reset ( )
inline

◆ swap()

template<class T_CppObject>
void fawkes::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.

Parameters
otherother instance to swap with.

Definition at line 367 of file refptr.h.

Referenced by fawkes::RefPtr< Roomba500 >::swap().

◆ use_count()

template<class T_CppObject>
int fawkes::RefPtr< T_CppObject >::use_count ( ) const
inline

Get current reference count.

Returns
current number of owners referencing this RefPtr.

Definition at line 229 of file refptr.h.

Referenced by OpenNiPclOnlyThread::loop(), and Bumblebee2Thread::loop().

Friends And Related Function Documentation

◆ swap()

template<class T_CppObject >
void swap ( RefPtr< T_CppObject > &  lrp,
RefPtr< T_CppObject > &  rrp 
)
related

Swap refptr instances.

Parameters
lrp"left" refptr
rrp"right" refptr

Definition at line 478 of file refptr.h.


The documentation for this class was generated from the following file: