KFoundation  v2.1
A Better Foundation Library for C++
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
Public Member Functions | List of all members
kfoundation::Ref< T > Class Template Reference

Reference to a class of given template type. More...

#include <kfoundation/Ref.h>

Inheritance diagram for kfoundation::Ref< T >:
Inheritance graph
[legend]

Public Member Functions

 Ref ()
 Constructs a NULL reference to the given template class type.
 
 Ref (const T *cptr)
 Constructs a reference to the object pointed by the given argument.
 
template<typename TT >
Ref< TT > cast () const
 Casts this reference to the given type. More...
 
T & operator* () const
 Dereference operator. More...
 
T * operator-> () const
 Structure dereference operator. More...
 
Ref< T > & operator= (const Ref< T > &other)
 Replaces the referenced object with a new one. More...
 
Ref< T > & operator= (const T *cptr)
 Replaces the referenced object with a new one. More...
 
- Public Member Functions inherited from kfoundation::RefBase
kf_uref_t get () const
 Returns the value of reference.
 
int getRetainCount () const
 Returns the retain count for the pointed object. More...
 
bool isNull () const
 Checks if this is a NULL-pointer.
 
bool isValid () const
 Checks if this is a valid pointed. More...
 
template<typename TT >
bool isa () const
 Checks if the referenced object is a subclass of the given template argument.
 
void serialize (Ref< ObjectSerializer > builder) const
 Serializing method.
 
RefConst< UString > toString () const
 Generates a description of this reference.
 
bool operator== (const RefBase &other) const
 Equality operator.
 
bool operator== (const KFObject *const other) const
 Equality operator between a managed pointer and a classic pointer.
 
bool operator!= (const RefBase &other) const
 Inequality operator.
 
bool operator!= (const KFObject *const other) const
 Inequality operator between a managed pointer and a class pointer.
 

Detailed Description

template<typename T>
class kfoundation::Ref< T >

Reference to a class of given template type.

The template type should be a subclass of KFObject. To use, try

Ref<MyClass> myObject = new MyClass();

To create a null pointer, try

Ref<MyClass> myObject = NULL;

or just

Ref<MyClass> myObject;

After this, it can be used just like an ordinary poitner.

myObject->myMethod();

Ref prevents segmentation fault situations to happen. If the object being dereferenced is NULL, a NullPointerException will be thrown. If the object is invalid, i.e. it is previously deconstructed, an InvalidPointerException will be thrown.

You can also manually check the validity of the pointer using isValid() method. isValid() will also return false if the pointer is NULL. To check the for NULL pointer, use isNull() method.

There is an elegant way to type-case a managed pointer:

Ref<MySuperClass> myCastedObject = myObject.AS(MySuperClass);

Similiarily, there is an elegant way to check the type of a managed pointer.

if(myObject.ISA(MySuperClass)) {
  ...
}

Referenced objects do not need to be explicitly destructed. Ref will automatically retain and release the instance of the object it is pointing to whenever necessary, and calls the destructor when the object instance is no longer needed.

You can check the retain count using getRetainCount() method. Use toString() method to print a description of this reference, plus the corresponding retain count.

LOG << myObject.toString() << OVER;

If you mean to print the content of the object being pointed to, rather than the content of the pointer, make sure to dereference it.

LOG << *myObject << OVER;

KFoundation managed pointers are designed to be fast and efficient. The size of Ref is exactly 8 bytes — the same as normal pointers on most platforms. To make it safe, the validity of the pointer is checked against the memory manager's registery on each access. To make it fast, a novel fast algorithm with O(1) time complexity is developed to do the task.

See Also
RefConst - Reference to constant
StaticRef - Static reference
StaticRefConst - Static reference to constant

Member Function Documentation

template<typename T >
template<typename TT >
Ref< TT > kfoundation::Ref< T >::cast ( ) const

Casts this reference to the given type.

Throws KFException if the referenced object is of a type not compatible with the given template argument.

template<typename T >
T & kfoundation::Ref< T >::operator* ( ) const
inline

Dereference operator.

Functions the same as normal C/C++ dereference operator.

template<typename T >
T * kfoundation::Ref< T >::operator-> ( ) const
inline

Structure dereference operator.

Functions the same as normal C/C++ equivalant.

template<typename T>
Ref< T > & kfoundation::Ref< T >::operator= ( const Ref< T > &  other)
inline

Replaces the referenced object with a new one.

Returns
Self
template<typename T>
Ref< T > & kfoundation::Ref< T >::operator= ( const T *  obj)
inline

Replaces the referenced object with a new one.

Returns
Self

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