openDAQ
Open data acquisition platform
ObjectPtr< T > Class Template Reference

Smart pointer wrapper for IBaseObject interface and its descendants. More...

Detailed Description

template<typename T>
class ObjectPtr< T >

Smart pointer wrapper for IBaseObject interface and its descendants.

Smart pointer is an object that wraps IBaseObject and its derived interfaces. It provides automatic reference counting for wrapped interface, data type conversion, comparison and querying for other supported interfaces.

Public Member Functions

 ObjectPtr ()
 Creates a smart pointer with no wrapped interface.
 
 ObjectPtr (std::nullptr_t)
 Creates a smart pointer with no wrapped interface.
 
 ObjectPtr (const ObjectPtr< T > &objPtr)
 Creates a smart pointer from another smart pointer. More...
 
 ObjectPtr (ObjectPtr< T > &&objPtr) noexcept
 Creates a smart pointer from another smart pointer with move semantics. More...
 
template<class U >
 ObjectPtr (const ObjectPtr< U > &objPtr)
 Creates a smart pointer from another smart pointer of a different interface type. More...
 
template<class U >
 ObjectPtr (ObjectPtr< U > &&objPtr)
 Creates a smart pointer from another smart pointer of a different interface type with move semantics. More...
 
 ObjectPtr (T *&obj)
 Creates a smart pointer from an interface pointer. More...
 
 ObjectPtr (T *&&obj)
 Creates a smart pointer from an interface pointer with move semantics. More...
 
 ObjectPtr (IWeakRef *obj)
 Creates a smart pointer from a weak reference interface pointer. More...
 
 ObjectPtr (const wchar_t *value)
 Creates a string object smart pointer from UTF16 null terminated sequence. More...
 
 ObjectPtr (ConstCharPtr value)
 Creates a string object smart pointer from UTF8 null terminated sequence. More...
 
template<typename U , std::enable_if_t< is_ct_conv< U >::value &&!std::is_enum_v< U >, int > = 0>
 ObjectPtr (const U &value)
 Creates a smart pointer wrapper from value type. More...
 
virtual ~ObjectPtr ()
 Destructor. More...
 
bool equals (ObjectPtr< IBaseObject > other) const
 Compares object to another object for equality. More...
 
T * detach ()
 Disassociates this smart pointer object from the interface that it represents. More...
 
void dispose () const
 Disposes all references held by the object. More...
 
void release ()
 Resets the wrapped interface to nullptr. More...
 
T * addRefAndReturn () const
 Increments the reference count and returns the interface. More...
 
 operator T* () const
 Casts the pointer to the wrapped interface. More...
 
bool assigned () const
 Checks if the smart pointer is not empty.
 
T * getObject () const
 Gets the pointer to the wrapped interface. More...
 
T ** addressOf ()
 Gets the address of the pointer to the wrapped interface. More...
 
T ** operator& ()
 Gets the address of the pointer to the wrapped interface. More...
 
template<class U >
U * as (bool borrow=false) const
 Casts the wrapped interface to another interface. More...
 
template<class U >
U * asOrNull (bool borrow=false) const
 Casts the wrapped interface to another interface. More...
 
template<class U , class Ptr = typename InterfaceToSmartPtr<U>::SmartPtr>
Ptr asPtr (bool borrow=false) const
 Casts the wrapped interface to another interface and wraps in a smart pointer. More...
 
template<class U , class Ptr = typename InterfaceToSmartPtr<U>::SmartPtr>
Ptr asPtrOrNull (bool borrow=false) const
 Casts the wrapped interface to another interface and wraps in a smart pointer. More...
 
template<class U >
bool supportsInterface () const
 Checks if the wrapped interface supports another interface. More...
 
bool supportsInterface (const IntfID &id) const
 Checks if the wrapped interface supports another interface using the interface ID. More...
 
CoreType getCoreType () const
 Gets the object's CoreType. More...
 
ObjectPtr< IBaseObjectconvertTo (CoreType ct) const
 Converts the object to the specified CoreType. More...
 
SizeT getHashCode () const
 Returns hash code of the object. More...
 
ObjectPtr< IStringtoString () const
 Converts the object to the string object. More...
 
void freeze () const
 Freezes the object. More...
 
Bool isFrozen () const
 Checks if the object is frozen. More...
 
void dispatch (const ObjectPtr< IBaseObject > &params) const
 Calls the stored callback. More...
 
void dispatch () const
 Calls the stored callback without any parameters. More...
 
void execute () const
 Calls the stored callback without any parameters. More...
 
template<typename... Params>
void execute (Params... params) const
 Calls the stored callback. More...
 
template<typename... Params>
ObjectPtr< IBaseObjectcall (Params... params) const
 Calls the stored callback and returns the result. More...
 
bool isBorrowed () const
 Checks if the reference is borrowed. More...
 

Static Public Member Functions

template<class U = T>
static InterfaceToSmartPtr< U >::SmartPtr Borrow (T *&obj)
 Borrows an interface. More...
 
template<typename U = T>
static InterfaceToSmartPtr< U >::SmartPtr Adopt (T *obj)
 Adopts an interface. More...
 
template<class U , class V = T, std::enable_if_t< std::is_base_of_v< IBaseObject, U >, int > = 0>
static InterfaceToSmartPtr< V >::SmartPtr Borrow (U *&obj)
 Borrows an interface from the interface of another type. More...
 
static Bool Equals (const ObjectPtr< IBaseObject > &a, const ObjectPtr< IBaseObject > &b)
 Value based comparison of two objects. If both objects are nullptr, they are considered to be equal. More...
 

Constructor & Destructor Documentation

◆ ObjectPtr() [1/10]

ObjectPtr ( const ObjectPtr< T > &  objPtr)

Creates a smart pointer from another smart pointer.

Parameters
objPtrThe source smart pointer with the same type of wrapped interface.

◆ ObjectPtr() [2/10]

ObjectPtr ( ObjectPtr< T > &&  objPtr)
noexcept

Creates a smart pointer from another smart pointer with move semantics.

Parameters
objPtrThe source smart pointer with the same type of wrapped interface.

Source smart pointer is moved into the smart pointer. The Reference count is not incremented.

◆ ObjectPtr() [3/10]

ObjectPtr ( const ObjectPtr< U > &  objPtr)

Creates a smart pointer from another smart pointer of a different interface type.

Parameters
objPtrThe source smart pointer with a different type of wrapped interface.

◆ ObjectPtr() [4/10]

ObjectPtr ( ObjectPtr< U > &&  objPtr)

Creates a smart pointer from another smart pointer of a different interface type with move semantics.

Parameters
objPtrThe source smart pointer with a different type of wrapped interface.

Source smart pointer is moved into the smart pointer. The Reference count is not incremented.

◆ ObjectPtr() [5/10]

ObjectPtr ( T *&  obj)

Creates a smart pointer from an interface pointer.

Parameters
objThe interface pointer.

The reference count is incremented.

◆ ObjectPtr() [6/10]

ObjectPtr ( T *&&  obj)

Creates a smart pointer from an interface pointer with move semantics.

Parameters
objThe interface pointer.

The reference count is not incremented.

◆ ObjectPtr() [7/10]

ObjectPtr ( IWeakRef obj)

Creates a smart pointer from a weak reference interface pointer.

Parameters
objThe weak reference interface pointer.

If the object that the weak reference interface pointer represents does not exist anymore, the smart pointer sets the wrapped interface to nullptr.

◆ ObjectPtr() [8/10]

ObjectPtr ( const wchar_t *  value)

Creates a string object smart pointer from UTF16 null terminated sequence.

Parameters
valueThe UTF16 null terminated sequence.

◆ ObjectPtr() [9/10]

ObjectPtr ( ConstCharPtr  value)

Creates a string object smart pointer from UTF8 null terminated sequence.

Parameters
valueThe UTF8 null terminated sequence.

◆ ObjectPtr() [10/10]

ObjectPtr ( const U &  value)

Creates a smart pointer wrapper from value type.

Parameters
valueThe value.

Value can be any type that has interface wrapper, such as Int, Bool, Float, uint16_t, float, double, etc...

◆ ~ObjectPtr()

~ObjectPtr
virtual

Destructor.

If the interface is not borrowed it releases the reference.

Member Function Documentation

◆ addRefAndReturn()

T * addRefAndReturn

Increments the reference count and returns the interface.

Call addRefAndReturn to return internally stored interface as the function return value. The caller is responsible for releasing returned interface.

Example:

class SomeClass
{
public:
}
IBaseObject* SomeClass::createObject()
{
return obj.addRefAndReturn();
}

◆ addressOf()

T ** addressOf

Gets the address of the pointer to the wrapped interface.

Returns
The address of the pointer to the interface.

◆ Adopt()

InterfaceToSmartPtr< U >::SmartPtr Adopt ( T *  obj)
static

Adopts an interface.

Parameters
objThe source interface.

The smart pointer does not increment the reference count when it adopts the interface. However, the smart pointer takes ownership of the interface and releases the interface when it is destroyed. After the interface is adopted, the client should not call any methods directly on the interface.

◆ as()

U * as ( bool  borrow = false) const

Casts the wrapped interface to another interface.

Parameters
borrowIncrements the reference count if false, keeps the reference count otherwise.

Throws an exception if cast fails.

◆ asOrNull()

U * asOrNull ( bool  borrow = false) const

Casts the wrapped interface to another interface.

Parameters
borrowIncrements the reference count if false, keeps the reference count otherwise.

If cast fails, the wrapped interface set to nullptr.

◆ asPtr()

Ptr asPtr ( bool  borrow = false) const

Casts the wrapped interface to another interface and wraps in a smart pointer.

Parameters
borrowIncrements the reference count if false, keeps the reference count otherwise.

Throws an exception if cast fails.

◆ asPtrOrNull()

Ptr asPtrOrNull ( bool  borrow = false) const

Casts the wrapped interface to another interface and wraps in a smart pointer.

Parameters
borrowIncrements the reference count if false, keeps the reference count otherwise.

If cast fails, the wrapped interface set to nullptr.

◆ Borrow() [1/2]

InterfaceToSmartPtr< U >::SmartPtr Borrow ( T *&  obj)
static

Borrows an interface.

Parameters
objThe source interface.

The smart pointer does not increment the reference count when it borrows the interface. It also does not decrement the reference count when the smart pointer is destroyed. The client is responsible that it keeps the object alive during the lifetime of the smart pointer.

◆ Borrow() [2/2]

InterfaceToSmartPtr< V >::SmartPtr Borrow ( U *&  obj)
static

Borrows an interface from the interface of another type.

Parameters
objThe source interface.

The smart pointer does not increment the reference count when it borrows the interface. It also does not decrement the reference count when the smart pointer is destroyed. The client is responsible that it keeps the object alive during the lifetime of the smart pointer.

◆ call()

ObjectPtr< IBaseObject > call ( Params...  params) const

Calls the stored callback and returns the result.

Parameters
paramsParameters passed to the callback.
Returns
Result wrapped in a smart pointer.

If it does not support the IFunction interface, it throws an exception.

Params are of supported Core types type which is internally converted to it's object representation.

◆ convertTo()

ObjectPtr< IBaseObject > convertTo ( CoreType  ct) const

Converts the object to the specified CoreType.

Returns
ct CoreType of the converted object.

◆ detach()

T * detach

Disassociates this smart pointer object from the interface that it represents.

The reference count is not decremented nor incremented.

Use this function when the object is created in a function and the function returns the interface to the object.

If the object is created outside of the function and the function returns the interface, use addRefAndReturn

Example:

IBaseObject* createObject()
{
// create smart pointer with wrapped IBaseObject interface
auto obj = BaseObject();
// return wrapped interface, the reference count is not decremented
return obj.detach();
}

◆ dispatch() [1/2]

void dispatch

Calls the stored callback without any parameters.

If it does not support the IProcedure interface, it throws an exception.

◆ dispatch() [2/2]

void dispatch ( const ObjectPtr< IBaseObject > &  params) const

Calls the stored callback.

Parameters
paramsParameters passed to the callback.

If it does not support the IProcedure interface, it throws an exception.

If the callback expects no parameters, the params parameter has to be nullptr. If it expects a single parameter, pass any openDAQ object as the params parameter. If it expects multiple parameters, pass a list of openDAQ objects as the params parameter.

◆ dispose()

void dispose

Disposes all references held by the object.

An object that holds references to other interfaces must reset these references to null in dispose. This method is called automatically when the reference count drops to zero.

Call this method manually to break cycle references. If two objects have reference to each other, calling dispose will break cycle references.

After dispose is called, the object should not be accessed again, except by calling releaseRef function.

◆ Equals()

Bool Equals ( const ObjectPtr< IBaseObject > &  a,
const ObjectPtr< IBaseObject > &  b 
)
inlinestatic

Value based comparison of two objects. If both objects are nullptr, they are considered to be equal.

Parameters
aObject to compare.
bObject to compare.

◆ equals()

bool equals ( ObjectPtr< IBaseObject other) const

Compares object to another object for equality.

Parameters
otherInterface to another object for comparison.
Returns
The value true indicating if the other is equivalent to this one otherwise false.

For reference objects, it compares the address of the object. For value objects, such as numbers and strings, it compares values.

ImplementationOf which provides default implementation of IBaseObject uses pointer address to compare for equality.

◆ execute() [1/2]

void execute

Calls the stored callback without any parameters.

If it does not support the IProcedure interface, it throws an exception.

◆ execute() [2/2]

void execute ( Params...  params) const

Calls the stored callback.

Parameters
paramsParameters passed to the callback.

If it does not support the IProcedure interface, it throws an exception.

Params are of supported Core types type which are internally converted to their object representation.

◆ freeze()

void freeze

Freezes the object.

Function freezes the object if it supports the IFreezable interface. Otherwise, it throws an exception.

Freezing the object makes it immutable.

◆ getCoreType()

CoreType getCoreType

Gets the object's CoreType.

Returns
CoreType of the object.

If the object does not support ICoreType interface, ctObject is returned as CoreType.

◆ getHashCode()

SizeT getHashCode

Returns hash code of the object.

Returns
Hash code Hash code.

◆ getObject()

T * getObject

Gets the pointer to the wrapped interface.

Returns
The pointer to the interface.

◆ isBorrowed()

bool isBorrowed

Checks if the reference is borrowed.

Returns
Result True if it is a borrowed reference.

◆ isFrozen()

Bool isFrozen

Checks if the object is frozen.

Return values
TrueThe object is frozen.
FalseThe object is not frozen.

Function freezes the object if it supports the IFreezable interface. Otherwise, it throws an exception.

If it does not support the IFreezable interface, it throws an exception.

◆ operator T*()

operator T*

Casts the pointer to the wrapped interface.

Returns
The pointer to the interface.

◆ operator&()

T ** operator&

Gets the address of the pointer to the wrapped interface.

Returns
The address of the pointer to the interface.

◆ release()

void release

Resets the wrapped interface to nullptr.

If the smart pointer does not borrow the interface, calling release decrements the reference count. If the reference count equals 0 after calling release, it destroys the object that implements the interface.

The wrapped interface is set to nullptr.

Smart pointer destructor does the same thing as release.

◆ supportsInterface() [1/2]

bool supportsInterface

Checks if the wrapped interface supports another interface.

Template Parameters
UThe interface to check for support.
Returns
True if the interface is supported, False otherwise.

◆ supportsInterface() [2/2]

bool supportsInterface ( const IntfID &  id) const

Checks if the wrapped interface supports another interface using the interface ID.

Parameters
idThe ID of the interface to check for support.
Returns
True if the interface is supported, False otherwise.

◆ toString()

ObjectPtr< IString > toString

Converts the object to the string object.

Returns
The Converted smart pointer wrapped to the IString interface.
IBaseObject
Definition: baseobject.h:117
ObjectPtr< IBaseObject >