[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
Public Types | Public Member Functions
KLFRefPtr< T > Class Template Reference

Stores a pointer to an object with refcount. More...

#include <klfutil.h>

List of all members.

Public Types

typedef T * Pointer

Public Member Functions

 KLFRefPtr ()
 KLFRefPtr (const KLFRefPtr &copy)
 ~KLFRefPtr ()
T * ptr ()
const T * ptr () const
bool autoDelete () const
void setAutoDelete (bool on)
void setPointer (Pointer newptr)
void setNull ()
KLFRefPtr< T > & operator= (const KLFRefPtr< T > &other)
template<class OtherPtr >
KLFRefPtr< T > & operator= (OtherPtr aptr)
KLFRefPtr< T > & operator= (Pointer newptr)
 operator T * ()
 operator const T * () const
T * operator() ()
const T * operator() () const
Pointer operator-> ()
Pointer operator-> () const
template<class OtherPtr >
OtherPtr dyn_cast ()
template<class OtherPtr >
const OtherPtr dyn_cast () const
bool operator== (void *otherptr) const
bool operator== (const KLFRefPtr< T > &otherptr) const
bool operator!= (void *otherptr) const
bool operator!= (const KLFRefPtr< T > &otherptr) const

Detailed Description

template<class T>
class KLFRefPtr< T >

Stores a pointer to an object with refcount.

This class provides a normal datatype (with default constructor, copy constructor, assignment, ...) that stores a pointer to a structure that provides the functions ref() and deref() to track reference counts.

When this object is copied, or a pointer is assigned, then the pointed object's ref() function is called. When another pointer is assigned, or when this object is destroyed, then the function deref() is called on the previously pointed object, and the object is possibly delete'd if needed and required.

Automatic object deletion upon zero refcount is optional, see autoDelete() and setAutoDelete(). It is on by default.

When constructed with the default constructor, pointers are initialized to NULL.

The copy constructor and the assignment operator also preserves autodelete setting, eg.

  KLFRefPtr<Obj> ptr1 = ...;
  ptr1.setAutoDelete(false);
  KLFRefPtr<Obj> ptr2(ptr1);
  // ptr2.autoDelete() == false

The pointed/referenced object must:

Example:

 class MyObj {
   QString name;
   int refcount;
 public:
   MyObj(const QString& s) : name(s), refcount(0) { }
   int ref() { return ++refcount; }
   int deref() { return --refcount; }
   ...
   QString objname() const { return name; }
 };
 
 int main() {
   KLFRefPtr<MyObj> ptr;
   KLFRefPtr<MyObj> ptr2;
   ptr = new MyObj("Alice");
   ptr2 = ptr;  // this will increase "Alice"'s refcount by one
   ptr = new MyObj("Bob"); // "Alice" is still pointed by ptr2, so it is not yet deleted.
   ptr2 = NULL; // now "Alice" is deleted (refcount reached zero)
   ptr = obj_random_name();
   ptr2 = obj_random_name();
   do_something(ptr):
   do_something_2(ptr2);
   // ptr->field works as expected:
   printf("ptr points on object name=%s\n", qPrintable(ptr->objname()));
 }
 KLFRefPtr<MyObj> obj_random_name() {
   static QStringList names
      = QStringList()<<"Marty"<<"Jane"<<"Don"<<"John"<<"Phoebe"<<"Matthew"<<"Melissa"<<"Jessica"
   KLFRefPtr<MyObj> o = new MyObj(names[rand()%names.size()]);
   // the MyObj instance survives the `return' statement because of refcount increase in copy constructor
   return o;
 }
 void do_something(MyObj *object)  {  ...  }
 void do_something_2(KLFRefPtr<MyObj> ptr)  {  ...  }

Definition at line 613 of file klfutil.h.


Member Typedef Documentation

template<class T>
typedef T* KLFRefPtr< T >::Pointer

The pointer type. Alias for T *.

Definition at line 617 of file klfutil.h.


Constructor & Destructor Documentation

template<class T>
KLFRefPtr< T >::KLFRefPtr ( ) [inline]

Definition at line 619 of file klfutil.h.

template<class T>
KLFRefPtr< T >::KLFRefPtr ( const KLFRefPtr< T > &  copy) [inline]

Definition at line 624 of file klfutil.h.

template<class T>
KLFRefPtr< T >::~KLFRefPtr ( ) [inline]

Definition at line 630 of file klfutil.h.


Member Function Documentation

template<class T>
bool KLFRefPtr< T >::autoDelete ( ) const [inline]

Definition at line 639 of file klfutil.h.

template<class T>
template<class OtherPtr >
OtherPtr KLFRefPtr< T >::dyn_cast ( ) [inline]

Definition at line 717 of file klfutil.h.

Referenced by KLFPosSearchable::Pos::data().

template<class T>
template<class OtherPtr >
const OtherPtr KLFRefPtr< T >::dyn_cast ( ) const [inline]

Definition at line 720 of file klfutil.h.

template<class T>
KLFRefPtr< T >::operator const T * ( ) const [inline]

Definition at line 698 of file klfutil.h.

template<class T>
KLFRefPtr< T >::operator T * ( ) [inline]

Definition at line 696 of file klfutil.h.

template<class T>
bool KLFRefPtr< T >::operator!= ( void *  otherptr) const [inline]

Definition at line 728 of file klfutil.h.

template<class T>
bool KLFRefPtr< T >::operator!= ( const KLFRefPtr< T > &  otherptr) const [inline]

Definition at line 731 of file klfutil.h.

template<class T>
T* KLFRefPtr< T >::operator() ( ) [inline]

Definition at line 701 of file klfutil.h.

template<class T>
const T* KLFRefPtr< T >::operator() ( ) const [inline]

Definition at line 703 of file klfutil.h.

template<class T>
Pointer KLFRefPtr< T >::operator-> ( ) [inline]

Definition at line 711 of file klfutil.h.

template<class T>
Pointer KLFRefPtr< T >::operator-> ( ) const [inline]

Definition at line 713 of file klfutil.h.

template<class T>
KLFRefPtr<T>& KLFRefPtr< T >::operator= ( const KLFRefPtr< T > &  other) [inline]

Definition at line 656 of file klfutil.h.

template<class T>
template<class OtherPtr >
KLFRefPtr<T>& KLFRefPtr< T >::operator= ( OtherPtr  aptr) [inline]

Definition at line 663 of file klfutil.h.

template<class T>
KLFRefPtr<T>& KLFRefPtr< T >::operator= ( Pointer  newptr) [inline]

Definition at line 670 of file klfutil.h.

template<class T>
bool KLFRefPtr< T >::operator== ( void *  otherptr) const [inline]

Definition at line 722 of file klfutil.h.

Referenced by KLFRefPtr< KLFPointerGuard >::operator!=().

template<class T>
bool KLFRefPtr< T >::operator== ( const KLFRefPtr< T > &  otherptr) const [inline]

Definition at line 725 of file klfutil.h.

template<class T>
T* KLFRefPtr< T >::ptr ( ) [inline]

Definition at line 636 of file klfutil.h.

template<class T>
const T* KLFRefPtr< T >::ptr ( ) const [inline]

Definition at line 637 of file klfutil.h.

template<class T>
void KLFRefPtr< T >::setAutoDelete ( bool  on) [inline]

Definition at line 640 of file klfutil.h.

template<class T>
void KLFRefPtr< T >::setNull ( ) [inline]

Definition at line 651 of file klfutil.h.

Referenced by KLFSyncGuardPtr< T >::reset().

template<class T>
void KLFRefPtr< T >::setPointer ( Pointer  newptr) [inline]

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

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net