Ocean
Ocean::ObjectRef< T > Class Template Reference

This template class implements a object reference with an internal reference counter. More...

Inheritance diagram for Ocean::ObjectRef< T >:

Data Structures

class  ObjectHolder
 This class implements a helper object for the actual object reference class. More...
 

Public Types

typedef Callback< void, const T * > ReleaseCallback
 Definition of a release callback function. More...
 

Public Member Functions

 ObjectRef ()=default
 Creates an empty ObjectRef object. More...
 
 ObjectRef (const ObjectRef< T > &objectRef)
 Copy constructor. More...
 
 ObjectRef (ObjectRef< T > &&object) noexcept
 Move constructor. More...
 
 ObjectRef (T *object)
 Creates a new ObjectRef holding a given object. More...
 
 ObjectRef (T *object, const ReleaseCallback &releaseCallback)
 Creates a new ObjectRef holding and managing a given object. More...
 
 ~ObjectRef ()
 Destructs an object reference object and releases the internal object if possible. More...
 
template<typename T2 >
T2 & force () const
 Returns a reference to the internal object forcing to a specified type. More...
 
T * operator-> () const
 Returns a point to the internal object if existing. More...
 
T & operator* () const
 Returns a reference to the internal object if existing. More...
 
bool isUnique () const
 Returns whether there is no other object reference but this one. More...
 
bool isNull () const
 Returns whether this object reference holds no internal object. More...
 
void release ()
 Releases the internal object, if any. More...
 
T * pointer () const
 Returns a pointer to the objects that is encapsulated by this wrapper. More...
 
ObjectRef< T > & operator= (const ObjectRef< T > &objectRef)
 Assign operator. More...
 
ObjectRef< T > & operator= (ObjectRef< T > &&right) noexcept
 Move operator. More...
 
bool operator== (const ObjectRef< T > &objectRef) const
 Returns whether two object references are holds the same internal object. More...
 
bool operator!= (const ObjectRef< T > &objectRef) const
 Returns whether two object references are not equal. More...
 
bool operator< (const ObjectRef< T > &objectRef) const
 Returns whether the left object is less than the right one. More...
 
 operator bool () const
 Returns whether this object reference holds an internal object. More...
 

Static Protected Member Functions

static void destroyObject (T *object)
 Destroys the object located in the ObjectHolder object. More...
 

Protected Attributes

ObjectHolderobjectHolder_ = nullptr
 Pointer to the object holder. More...
 

Friends

template<typename T2 , typename TBase >
class SmartObjectRef
 
class ObjectHolder
 

Detailed Description

template<typename T>
class Ocean::ObjectRef< T >

This template class implements a object reference with an internal reference counter.

The reference counter is thread-safe.

Further, this implementation allows to define a callback function for release events.
By application of the callback function, a manager can be implemented that allows to store several ObjectRef objects in a managed list.
The manager can used this list to provide instances of specific ObjectRef objects on demand.
Due to the callback function, the manager will be informed whenever an ObjectRef object can be removed from the managed list so that the real object (which is encapsulated by the ObjectRef) can be released automatically.
The following code snippet demonstrates the application:

// any class, struct or data type
class DataType
{
public:
// any function
int function(const double value);
};
// we create a new ObjectRef instance
const ObjectRef<DataType> object(new DataType());
if (!object.isNull())
{
const int result = object->function(5.0);
}
const ObjectRef<DataType> sameObject(object);
ocean_assert(sameObject);
const int result2 = sameObject->function(5.0);
bool isNull() const
Returns whether this object reference holds no internal object.
Definition: base/ObjectRef.h:390
Template Parameters
TType of the object to be encapsulated by this object reference
See also
SmartObjectRef.

Member Typedef Documentation

◆ ReleaseCallback

template<typename T >
typedef Callback<void, const T*> Ocean::ObjectRef< T >::ReleaseCallback

Definition of a release callback function.

The first parameter determines the object for that the release event is invoked.

Constructor & Destructor Documentation

◆ ObjectRef() [1/5]

template<typename T >
Ocean::ObjectRef< T >::ObjectRef ( )
default

Creates an empty ObjectRef object.

◆ ObjectRef() [2/5]

template<typename T >
Ocean::ObjectRef< T >::ObjectRef ( const ObjectRef< T > &  objectRef)
inline

Copy constructor.

Copies an ObjectRef object.

Parameters
objectRefObjectRef to copy

◆ ObjectRef() [3/5]

template<typename T >
Ocean::ObjectRef< T >::ObjectRef ( ObjectRef< T > &&  object)
inlinenoexcept

Move constructor.

Parameters
objectThe object to be moved

◆ ObjectRef() [4/5]

template<typename T >
Ocean::ObjectRef< T >::ObjectRef ( T *  object)
inlineexplicit

Creates a new ObjectRef holding a given object.

Beware: The given object will be released by this object reference!

Parameters
objectThe object to store

◆ ObjectRef() [5/5]

template<typename T >
Ocean::ObjectRef< T >::ObjectRef ( T *  object,
const ReleaseCallback releaseCallback 
)
inline

Creates a new ObjectRef holding and managing a given object.

This constructor also requests a release callback event.
This callback event will be invoked after the internal reference counter of this object has been decremented and is equal to 1 afterwards.
Thus, the release callback provides an information that in the moment of the callback only one instance of the ObjectRef exists.
Therefore, this callback can be used to release an ObjectRef object which is stored in e.g. a managed list so that finally the reference counter will be zero and the stored object will be released.
Beware: The given object will be released by this object reference!

Parameters
objectThe object to store
releaseCallbackCallback function which will be invoked if only one ObjectRef instance managing a specific object is left (if more than one existed before)

◆ ~ObjectRef()

template<typename T >
Ocean::ObjectRef< T >::~ObjectRef
inline

Destructs an object reference object and releases the internal object if possible.

Member Function Documentation

◆ destroyObject()

template<typename T >
void Ocean::ObjectRef< T >::destroyObject ( T *  object)
inlinestaticprotected

Destroys the object located in the ObjectHolder object.

This function is used to get access to the protected delete operator of the internal encapsulated object.

Parameters
objectthe object to destroy

◆ force()

template<typename T >
template<typename T2 >
T2 & Ocean::ObjectRef< T >::force
inline

Returns a reference to the internal object forcing to a specified type.

Beware: Check whether this reference holds a valid internal object before calling this function!
Beware: Make sure the forced type is matching the internal object!

Returns
Internal object with the forced type

◆ isNull()

template<typename T >
bool Ocean::ObjectRef< T >::isNull
inline

Returns whether this object reference holds no internal object.

Returns
True, if so

◆ isUnique()

template<typename T >
bool Ocean::ObjectRef< T >::isUnique
inline

Returns whether there is no other object reference but this one.

Returns
True, if so

◆ operator bool()

template<typename T >
Ocean::ObjectRef< T >::operator bool
inlineexplicit

Returns whether this object reference holds an internal object.

Returns
True, if so

◆ operator!=()

template<typename T >
bool Ocean::ObjectRef< T >::operator!= ( const ObjectRef< T > &  objectRef) const
inline

Returns whether two object references are not equal.

Parameters
objectRefRight object reference
Returns
True, if so

◆ operator*()

template<typename T >
T & Ocean::ObjectRef< T >::operator*
inline

Returns a reference to the internal object if existing.

Beware: Check whether this reference holds an internal object before calling this function!

Returns
Reference to the internal object

◆ operator->()

template<typename T >
T * Ocean::ObjectRef< T >::operator->
inline

Returns a point to the internal object if existing.

Beware: Check whether this reference holds an internal object before calling this function!

Returns
Pointer to the internal object

◆ operator<()

template<typename T >
bool Ocean::ObjectRef< T >::operator< ( const ObjectRef< T > &  objectRef) const
inline

Returns whether the left object is less than the right one.

Parameters
objectRefRight operand
Returns
True, if so

◆ operator=() [1/2]

template<typename T >
ObjectRef< T > & Ocean::ObjectRef< T >::operator= ( const ObjectRef< T > &  objectRef)
inline

Assign operator.

Parameters
objectRefRight object reference to assign
Returns
Reference to this object reference

◆ operator=() [2/2]

template<typename T >
ObjectRef< T > & Ocean::ObjectRef< T >::operator= ( ObjectRef< T > &&  right)
inlinenoexcept

Move operator.

Parameters
rightThe right object to assign
Returns
Reference to this object

◆ operator==()

template<typename T >
bool Ocean::ObjectRef< T >::operator== ( const ObjectRef< T > &  objectRef) const
inline

Returns whether two object references are holds the same internal object.

Parameters
objectRefRight object reference
Returns
True, if so

◆ pointer()

template<typename T >
T * Ocean::ObjectRef< T >::pointer
inline

Returns a pointer to the objects that is encapsulated by this wrapper.

Returns
Pointer to the object or nullptr if no object is encapsulated

◆ release()

template<typename T >
void Ocean::ObjectRef< T >::release
inline

Releases the internal object, if any.

Beware: After the release the object can not be accessed anymore!

Friends And Related Function Documentation

◆ ObjectHolder

template<typename T >
friend class ObjectHolder
friend

◆ SmartObjectRef

template<typename T >
template<typename T2 , typename TBase >
friend class SmartObjectRef
friend

Field Documentation

◆ objectHolder_

template<typename T >
ObjectHolder* Ocean::ObjectRef< T >::objectHolder_ = nullptr
protected

Pointer to the object holder.


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