Smart pointer wrapper for IBaseObject interface and its descendants. More...
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< IBaseObject > | convertTo (CoreType ct) const |
Converts the object to the specified CoreType . More... | |
SizeT | getHashCode () const |
Returns hash code of the object. More... | |
ObjectPtr< IString > | toString () 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 > ¶ms) 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< IBaseObject > | call (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... | |
Creates a smart pointer from another smart pointer.
objPtr | The source smart pointer with the same type of wrapped interface. |
Creates a smart pointer from another smart pointer with move semantics.
objPtr | The 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.
Creates a smart pointer from another smart pointer of a different interface type.
objPtr | The source smart pointer with a different type of wrapped interface. |
Creates a smart pointer from another smart pointer of a different interface type with move semantics.
objPtr | The 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 | ( | T *& | obj | ) |
Creates a smart pointer from an interface pointer.
obj | The interface pointer. |
The reference count is incremented.
ObjectPtr | ( | T *&& | obj | ) |
Creates a smart pointer from an interface pointer with move semantics.
obj | The interface pointer. |
The reference count is not incremented.
Creates a smart pointer from a weak reference interface pointer.
obj | The 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 | ( | const wchar_t * | value | ) |
Creates a string object smart pointer from UTF16 null terminated sequence.
value | The UTF16 null terminated sequence. |
ObjectPtr | ( | ConstCharPtr | value | ) |
Creates a string object smart pointer from UTF8 null terminated sequence.
value | The UTF8 null terminated sequence. |
ObjectPtr | ( | const U & | value | ) |
Creates a smart pointer wrapper from value type.
value | The value. |
Value can be any type that has interface wrapper, such as Int, Bool, Float, uint16_t, float, double, etc...
|
virtual |
Destructor.
If the interface is not borrowed it releases the reference.
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:
T ** addressOf |
Gets the address of the pointer to the wrapped interface.
|
static |
Adopts an interface.
obj | The 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.
U * as | ( | bool | borrow = false | ) | const |
Casts the wrapped interface to another interface.
borrow | Increments the reference count if false, keeps the reference count otherwise. |
Throws an exception if cast fails.
U * asOrNull | ( | bool | borrow = false | ) | const |
Casts the wrapped interface to another interface.
borrow | Increments the reference count if false, keeps the reference count otherwise. |
If cast fails, the wrapped interface set to nullptr.
Ptr asPtr | ( | bool | borrow = false | ) | const |
Casts the wrapped interface to another interface and wraps in a smart pointer.
borrow | Increments the reference count if false, keeps the reference count otherwise. |
Throws an exception if cast fails.
Ptr asPtrOrNull | ( | bool | borrow = false | ) | const |
Casts the wrapped interface to another interface and wraps in a smart pointer.
borrow | Increments the reference count if false, keeps the reference count otherwise. |
If cast fails, the wrapped interface set to nullptr.
|
static |
Borrows an interface.
obj | The 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.
|
static |
Borrows an interface from the interface of another type.
obj | The 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.
ObjectPtr< IBaseObject > call | ( | Params... | params | ) | const |
Calls the stored callback and returns the result.
params | Parameters passed to the callback. |
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.
ObjectPtr< IBaseObject > convertTo | ( | CoreType | ct | ) | const |
Converts the object to the specified CoreType
.
CoreType
of the converted object. 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:
void dispatch |
Calls the stored callback without any parameters.
If it does not support the IProcedure interface, it throws an exception.
void dispatch | ( | const ObjectPtr< IBaseObject > & | params | ) | const |
Calls the stored callback.
params | Parameters 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.
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.
|
inlinestatic |
Value based comparison of two objects. If both objects are nullptr, they are considered to be equal.
a | Object to compare. |
b | Object to compare. |
bool equals | ( | ObjectPtr< IBaseObject > | other | ) | const |
Compares object to another object for equality.
other | Interface to another object for comparison. |
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.
void execute |
Calls the stored callback without any parameters.
If it does not support the IProcedure
interface, it throws an exception.
void execute | ( | Params... | params | ) | const |
Calls the stored callback.
params | Parameters 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.
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.
CoreType getCoreType |
Gets the object's CoreType
.
If the object does not support ICoreType
interface, ctObject is returned as CoreType.
SizeT getHashCode |
Returns hash code of the object.
T * getObject |
Gets the pointer to the wrapped interface.
bool isBorrowed |
Checks if the reference is borrowed.
Bool isFrozen |
Checks if the object is frozen.
True | The object is frozen. |
False | The 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* |
Casts the pointer to the wrapped interface.
T ** operator& |
Gets the address of the pointer to the wrapped interface.
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
.
bool supportsInterface |
Checks if the wrapped interface supports another interface.
U | The interface to check for support. |
bool supportsInterface | ( | const IntfID & | id | ) | const |
Checks if the wrapped interface supports another interface using the interface ID.
id | The ID of the interface to check for support. |