Ocean
Ocean::SmartObjectRef< T, TBase > Class Template Reference

This template class implements a smart object reference which is a specialization of an ObjectRef object. More...

Inheritance diagram for Ocean::SmartObjectRef< T, TBase >:

Public Types

typedef ObjectRef< TBase >::ReleaseCallback ReleaseCallback
 Redefinition of the release callback function defined in ObjectRef. More...
 
- Public Types inherited from Ocean::ObjectRef< TBase >
typedef Callback< void, const TBase * > ReleaseCallback
 Definition of a release callback function. More...
 

Public Member Functions

 SmartObjectRef ()=default
 Creates a new SmartObjectRef with no internal object. More...
 
 SmartObjectRef (T *object)
 Creates a new SmartObjectRef by a given object. More...
 
 SmartObjectRef (T *object, const ReleaseCallback &releaseCallback)
 Creates a new SmartObjectRef by a given object. More...
 
 SmartObjectRef (const SmartObjectRef< T, TBase > &smartObjectRef)
 Copy constructor. More...
 
 SmartObjectRef (SmartObjectRef< T, TBase > &&smartObjectRef)
 Move constructor. More...
 
 SmartObjectRef (const ObjectRef< TBase > &objectRef)
 Creates a new SmartObjectRef by a given ObjectRef. More...
 
template<typename T2 >
 SmartObjectRef (const SmartObjectRef< T2, TBase > &smartObjectRef)
 Copies a SmartObjectRef object. More...
 
SmartObjectRef< T, TBase > & operator= (const SmartObjectRef< T, TBase > &smartObjectRef)
 Assign operator. More...
 
SmartObjectRef< T, TBase > & operator= (SmartObjectRef< T, TBase > &&smartObjectRef)
 Moves a smart object reference object to this smart object reference. More...
 
SmartObjectRef< T, TBase > & operator= (const ObjectRef< TBase > &objectRef)
 Assigns a ObjectRef to this smart object reference. 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...
 
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...
 
- Public Member Functions inherited from Ocean::ObjectRef< TBase >
 ObjectRef ()=default
 Creates an empty ObjectRef object. More...
 
 ObjectRef (const ObjectRef< TBase > &objectRef)
 Copy constructor. More...
 
 ObjectRef (ObjectRef< TBase > &&object) noexcept
 Move constructor. More...
 
 ObjectRef (TBase *object)
 Creates a new ObjectRef holding a given object. More...
 
 ObjectRef (TBase *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...
 
T2 & force () const
 Returns a reference to the internal object forcing to a specified type. More...
 
TBase * operator-> () const
 Returns a point to the internal object if existing. More...
 
TBase & 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...
 
TBase * pointer () const
 Returns a pointer to the objects that is encapsulated by this wrapper. More...
 
ObjectRef< TBase > & operator= (const ObjectRef< TBase > &objectRef)
 Assign operator. More...
 
ObjectRef< TBase > & operator= (ObjectRef< TBase > &&right) noexcept
 Move operator. More...
 
bool operator== (const ObjectRef< TBase > &objectRef) const
 Returns whether two object references are holds the same internal object. More...
 
bool operator!= (const ObjectRef< TBase > &objectRef) const
 Returns whether two object references are not equal. More...
 
bool operator< (const ObjectRef< TBase > &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...
 

Protected Attributes

T * objectPointer_ = nullptr
 Pointer to the internal object. More...
 
- Protected Attributes inherited from Ocean::ObjectRef< TBase >
ObjectHolderobjectHolder_
 Pointer to the object holder. More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from Ocean::ObjectRef< TBase >
static void destroyObject (TBase *object)
 Destroys the object located in the ObjectHolder object. More...
 

Detailed Description

template<typename T, typename TBase>
class Ocean::SmartObjectRef< T, TBase >

This template class implements a smart object reference which is a specialization of an ObjectRef object.

In the following the application of the SmartObjectRef class together with the ObjectRef class is shown:

// any kind of base class
class Base
{
public:
void baseFunction();
virtual void virtualFunction();
};
// any kind of derived class
class Derived : public Base
{
public:
void derivedFunction();
virtual void virtualFunction();
};
void main()
{
// create a new object reference of type 'Base' holding an instance of type 'Base'
ObjectRef<Base> base(new Base());
// check whether 'base' really holds a valid instance
if (base)
{
// call both function of the instance
base->baseFunction();
base->virtualFunction();
}
// create another object reference of type 'Base' holding an instance of type 'Derived'
ObjectRef<Base> derived(new Derived());
// check whether 'derived' really holds a valid instance
if (derived)
{
// call the base function
derived->baseFunction();
// call the virtual function of the class 'Derived'
derived->virtualFunction();
// try to call the specific 'derivedFunction' of class 'Derived'
derived->derivedFunction(); // we will receive a compiler error as the object reference 'derived' has no knowledge about the class 'Derived'
}
// we create a specialization of 'derived' so that we can finally access the specific function of class 'Derived'
SmartObjectRef<Derived, Base> smartDerived(derived);
// check whether the provided object reference 'derived' could be specialized
if (smartDerived)
{
// three valid function calls
smartDerived->baseFunction();
smartDerived->virtualFunction();
smartDerived->derivedFunction();
}
}
Template Parameters
TType of the encapsulated object of the smart object reference, must be derived from TBase
TBaseBase type of the object to be encapsulated
See also
ObjectRef.

Member Typedef Documentation

◆ ReleaseCallback

template<typename T , typename TBase >
typedef ObjectRef<TBase>::ReleaseCallback Ocean::SmartObjectRef< T, TBase >::ReleaseCallback

Redefinition of the release callback function defined in ObjectRef.

Constructor & Destructor Documentation

◆ SmartObjectRef() [1/7]

template<typename T , typename TBase >
Ocean::SmartObjectRef< T, TBase >::SmartObjectRef ( )
default

Creates a new SmartObjectRef with no internal object.

◆ SmartObjectRef() [2/7]

template<typename T , typename TBase >
Ocean::SmartObjectRef< T, TBase >::SmartObjectRef ( T *  object)
inlineexplicit

Creates a new SmartObjectRef by a given object.

This given object will be released by the smart object reference itself.

Parameters
objectInternal object

◆ SmartObjectRef() [3/7]

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

Creates a new SmartObjectRef by a given object.

This given object will be released by the smart object reference itself.

Parameters
objectInternal object
releaseCallbackCallback function for the release event

◆ SmartObjectRef() [4/7]

template<typename T , typename TBase >
Ocean::SmartObjectRef< T, TBase >::SmartObjectRef ( const SmartObjectRef< T, TBase > &  smartObjectRef)
inline

Copy constructor.

Parameters
smartObjectRefSmartObjectRef object to copy

◆ SmartObjectRef() [5/7]

template<typename T , typename TBase >
Ocean::SmartObjectRef< T, TBase >::SmartObjectRef ( SmartObjectRef< T, TBase > &&  smartObjectRef)
inline

Move constructor.

Parameters
smartObjectRefSmartObjectRef object to move

◆ SmartObjectRef() [6/7]

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

Creates a new SmartObjectRef by a given ObjectRef.

Parameters
objectRefGiven object reference

◆ SmartObjectRef() [7/7]

template<typename T , typename TBase >
template<typename T2 >
Ocean::SmartObjectRef< T, TBase >::SmartObjectRef ( const SmartObjectRef< T2, TBase > &  smartObjectRef)
inlineexplicit

Copies a SmartObjectRef object.

Parameters
smartObjectRefSmartObjectRef object to copy
Template Parameters
T2Type of the encapsulated object of the given object, must be derived from TBase

Member Function Documentation

◆ operator*()

template<typename T , typename TBase >
T & Ocean::SmartObjectRef< T, TBase >::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 , typename TBase >
T * Ocean::SmartObjectRef< T, TBase >::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=() [1/3]

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

Assigns a ObjectRef to this smart object reference.

Parameters
objectRefObjectRef to assign
Returns
Reference to this SmartObjectRef object

◆ operator=() [2/3]

template<typename T , typename TBase >
SmartObjectRef< T, TBase > & Ocean::SmartObjectRef< T, TBase >::operator= ( const SmartObjectRef< T, TBase > &  smartObjectRef)
inline

Assign operator.

Parameters
smartObjectRefSmart object reference to assign
Returns
Reference to this object

◆ operator=() [3/3]

template<typename T , typename TBase >
SmartObjectRef< T, TBase > & Ocean::SmartObjectRef< T, TBase >::operator= ( SmartObjectRef< T, TBase > &&  smartObjectRef)
inline

Moves a smart object reference object to this smart object reference.

Parameters
smartObjectRefSmart object reference to move
Returns
Reference to this object

◆ pointer()

template<typename T , typename TBase >
T * Ocean::SmartObjectRef< T, TBase >::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 , typename TBase >
void Ocean::SmartObjectRef< T, TBase >::release
inline

Releases the internal object, if any.

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

Field Documentation

◆ objectPointer_

template<typename T , typename TBase >
T* Ocean::SmartObjectRef< T, TBase >::objectPointer_ = nullptr
protected

Pointer to the internal object.


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