[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
KLFRefPtr< T > Class Template Reference

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

#include <klfutil.h>

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
void setAutoDelete(bool on)
Definition klfutil.h:640
KLFRefPtr()
Definition klfutil.h:619

The pointed/referenced object must:

  • provide a ref() function (no arguments, return type unimportant)
  • provide a deref() function (no arguments). Its return type, when cast into an int, is strictly positive as long as the object is still referenced.

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() {
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) { ... }
T * ptr()
Definition klfutil.h:636
int size() const

Definition at line 613 of file klfutil.h.

Member Typedef Documentation

◆ Pointer

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

◆ KLFRefPtr() [1/2]

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

Definition at line 619 of file klfutil.h.

References KLF_DEBUG_BLOCK.

Referenced by KLFRefPtr(), operator!=(), operator=(), operator=(), operator=(), and operator==().

◆ KLFRefPtr() [2/2]

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

Definition at line 624 of file klfutil.h.

References KLF_DEBUG_BLOCK, and KLFRefPtr().

◆ ~KLFRefPtr()

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

Definition at line 630 of file klfutil.h.

References KLF_DEBUG_BLOCK.

Member Function Documentation

◆ autoDelete()

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

Definition at line 639 of file klfutil.h.

◆ dyn_cast() [1/2]

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

Definition at line 717 of file klfutil.h.

◆ dyn_cast() [2/2]

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

Definition at line 720 of file klfutil.h.

◆ operator const T *()

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

Definition at line 698 of file klfutil.h.

◆ operator T*()

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

Definition at line 696 of file klfutil.h.

◆ operator!=() [1/2]

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

Definition at line 731 of file klfutil.h.

References KLFRefPtr(), and operator==().

◆ operator!=() [2/2]

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

Definition at line 728 of file klfutil.h.

References operator==().

◆ operator()() [1/2]

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

Definition at line 701 of file klfutil.h.

◆ operator()() [2/2]

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

Definition at line 703 of file klfutil.h.

◆ operator->() [1/2]

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

Definition at line 711 of file klfutil.h.

◆ operator->() [2/2]

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

Definition at line 713 of file klfutil.h.

◆ operator=() [1/3]

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

Definition at line 656 of file klfutil.h.

References KLF_DEBUG_BLOCK, KLFRefPtr(), and setPointer().

◆ operator=() [2/3]

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

Definition at line 663 of file klfutil.h.

References KLF_DEBUG_BLOCK, KLFRefPtr(), and setPointer().

◆ operator=() [3/3]

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

Definition at line 670 of file klfutil.h.

References KLF_DEBUG_BLOCK, KLFRefPtr(), and setPointer().

◆ operator==() [1/2]

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

Definition at line 725 of file klfutil.h.

References KLFRefPtr().

◆ operator==() [2/2]

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

Definition at line 722 of file klfutil.h.

Referenced by operator!=(), and operator!=().

◆ ptr() [1/2]

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

Definition at line 636 of file klfutil.h.

◆ ptr() [2/2]

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

Definition at line 637 of file klfutil.h.

◆ setAutoDelete()

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

Definition at line 640 of file klfutil.h.

◆ setNull()

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

Definition at line 651 of file klfutil.h.

References setPointer().

◆ setPointer()

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

Definition at line 642 of file klfutil.h.

References klfDbg.

Referenced by operator=(), operator=(), operator=(), and setNull().


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

Generated by doxygen 1.14.0