fawkes::RefPtr< T_CppObject > Class Template Reference
[Fawkes Core Library]

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

#include <refptr.h>

Inheritance diagram for fawkes::RefPtr< T_CppObject >:

[legend]
List of all members.

Public Member Functions

 RefPtr ()
 Default constructor.
 ~RefPtr ()
 Destructor - decrements reference count.
 RefPtr (T_CppObject *cpp_object)
 Constructor that takes ownership.
 RefPtr (const RefPtr< T_CppObject > &src)
 Copy constructor This increments the shared reference count.
template<class T_CastFrom>
 RefPtr (const RefPtr< T_CastFrom > &src)
 Copy constructor (from different, but castable type).
void swap (RefPtr< T_CppObject > &other)
 Swap the contents of two RefPtr<>.
RefPtr< T_CppObject > & operator= (const RefPtr< T_CppObject > &src)
 Copy from another RefPtr.
template<class T_CastFrom>
RefPtr< T_CppObject > & operator= (const RefPtr< T_CastFrom > &src)
 Copy from different, but castable type).
RefPtr< T_CppObject > & operator= (T_CppObject *ptr)
 Assign object and claim ownership.
bool operator== (const RefPtr< T_CppObject > &src) const
 Tests whether the RefPtr<> point to the same underlying instance.
bool operator!= (const RefPtr< T_CppObject > &src) const
 Tests whether the RefPtr<> do not point to the same underlying instance.
T_CppObject * operator-> () const
 Dereferencing.
 operator bool () const
 Test whether the RefPtr<> points to any underlying instance.
void clear ()
 Set underlying instance to 0, decrementing reference count of existing instance appropriately.
 RefPtr (T_CppObject *cpp_object, int *refcount, Mutex *refmutex)
 For use only in the internal implementation of sharedptr.
int * refcount_ptr () const
 For use only in the internal implementation of sharedptr.
Mutexrefmutex_ptr () const
 For use only in the internal implementation of sharedptr.

Static Public Member Functions

template<class T_CastFrom>
static RefPtr< T_CppObject > cast_dynamic (const RefPtr< T_CastFrom > &src)
 Dynamic cast to derived class.
template<class T_CastFrom>
static RefPtr< T_CppObject > cast_static (const RefPtr< T_CastFrom > &src)
 Static cast to derived class.
template<class T_CastFrom>
static RefPtr< T_CppObject > cast_const (const RefPtr< T_CastFrom > &src)
 Cast to non-const.

Related Functions

(Note that these are not member functions.)

void swap (RefPtr< T_CppObject > &lrp, RefPtr< T_CppObject > &rrp)
 Swap refptr instances.

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, and to prevent any need to manually reference and unreference cairo objects.

Note that RefPtr is thread-safe.

Definition at line 50 of file refptr.h.


Constructor & Destructor Documentation

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 221 of file refptr.h.

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

Destructor - decrements reference count.

Definition at line 229 of file refptr.h.

References fawkes::Mutex::lock(), and fawkes::Mutex::unlock().

template<class T_CppObject>
fawkes::RefPtr< T_CppObject >::RefPtr ( T_CppObject *  cpp_object  )  [inline, explicit]

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_object C++ object to take ownership of

Definition at line 257 of file refptr.h.

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

Copy constructor This increments the shared reference count.

Parameters:
src refptr to copy

Definition at line 287 of file refptr.h.

References fawkes::Mutex::lock(), and fawkes::Mutex::unlock().

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:
src refptr to copy

Definition at line 307 of file refptr.h.

References fawkes::Mutex::lock(), and fawkes::Mutex::unlock().

template<class T_CppObject>
fawkes::RefPtr< T_CppObject >::RefPtr ( T_CppObject *  cpp_object,
int *  refcount,
Mutex refmutex 
) [inline, explicit]

For use only in the internal implementation of sharedptr.

Parameters:
cpp_object C++ object to wrap
refcount reference count
refmutex reference count mutex

Definition at line 273 of file refptr.h.

References fawkes::Mutex::lock(), and fawkes::Mutex::unlock().


Member Function Documentation

template<class T_CppObject>
template<class T_CastFrom>
RefPtr< T_CppObject > fawkes::RefPtr< T_CppObject >::cast_const ( const RefPtr< T_CastFrom > &  src  )  [inline, static]

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:
src source refptr to cast
Returns:
refptr to object casted to given type

Definition at line 450 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::refcount_ptr(), and fawkes::RefPtr< T_CppObject >::refmutex_ptr().

template<class T_CppObject>
template<class T_CastFrom>
RefPtr< T_CppObject > fawkes::RefPtr< T_CppObject >::cast_dynamic ( const RefPtr< T_CastFrom > &  src  )  [inline, static]

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:
src source refptr to cast
Returns:
refptr to object casted to given type

Definition at line 425 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::refcount_ptr(), and fawkes::RefPtr< T_CppObject >::refmutex_ptr().

template<class T_CppObject>
template<class T_CastFrom>
RefPtr< T_CppObject > fawkes::RefPtr< T_CppObject >::cast_static ( const RefPtr< T_CastFrom > &  src  )  [inline, static]

Static cast to derived class.

Like the dynamic cast; the notation is

   ptr_derived = RefPtr<Derived>::cast_static(ptr_base);
Parameters:
src source refptr to cast
Returns:
refptr to object casted to given type

Definition at line 439 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::refcount_ptr(), and fawkes::RefPtr< T_CppObject >::refmutex_ptr().

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 415 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::swap().

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 409 of file refptr.h.

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:
src refptr to compare to
Returns:
true if both refptrs do not point to the same instance.

Definition at line 403 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::__cpp_object.

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 215 of file refptr.h.

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

Assign object and claim ownership.

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

Definition at line 375 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::swap().

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:
src refptr to copy from
Returns:
reference to this instance

Definition at line 387 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::swap().

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

Copy from another RefPtr.

Parameters:
src refptr to copy from
Returns:
reference to this instance

Definition at line 342 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::swap().

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:
src refptr to compare to
Returns:
true if both refptrs point to the same instance.

Definition at line 396 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::__cpp_object.

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 194 of file refptr.h.

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

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 200 of file refptr.h.

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

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:
other other instance to swap with.

Definition at line 325 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::__cpp_object, fawkes::RefPtr< T_CppObject >::__ref_count, and fawkes::RefPtr< T_CppObject >::__ref_mutex.

Referenced by fawkes::RefPtr< T_CppObject >::clear(), fawkes::RefPtr< T_CppObject >::operator=(), and fawkes::RefPtr< T_CppObject >::swap().


Friends And Related Function Documentation

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 465 of file refptr.h.

References fawkes::RefPtr< T_CppObject >::swap().


The documentation for this class was generated from the following file:
Generated on Tue Feb 22 13:31:39 2011 for Fawkes API by  doxygen 1.4.7