EventAnalysis  1.3.0
Public Member Functions | Private Attributes | Friends | List of all members
EA::ObjectWrapper Class Reference

Generic wrapper class. More...

#include <ObjectWrapper.h>

Public Member Functions

 ObjectWrapper ()
 Default constructor. More...
 
 ObjectWrapper (const ObjectWrapper &w)
 copy constructor. More...
 
 ObjectWrapper (ObjectWrapper &&w)
 Move constructor. More...
 
template<typename DataType >
 ObjectWrapper (observer_ptr< DataType > ptr)
 Constructor. More...
 
template<typename DataType >
 ObjectWrapper (std::shared_ptr< DataType > ptr)
 Constructor. More...
 
template<typename DataType >
 ObjectWrapper (std::unique_ptr< DataType > ptr)
 Constructor. More...
 
 ObjectWrapper (const char *address)
 Constructor. Overload for converting a const char * string literal to std::string before wrapping (needed to circumvent troubles with template substitution and type mismatch). The wrapped type will be set to std::string. More...
 
 ObjectWrapper (char *address)
 Constructor. Overload for converting a char * string literal to std::string before wrapping (needed to circumvent troubles with template substitution and type mismatch). The wrapped type will be set to std::string. More...
 
template<typename DataType >
ObjectWrapperoperator= (DataType *rhs)
 Assignment operator. More...
 
template<typename DataType >
ObjectWrapperoperator= (DataType &rhs)
 Assignment operator. More...
 
template<typename DataType >
ObjectWrapperoperator= (std::shared_ptr< DataType > rhs)
 Assignment operator. More...
 
template<typename DataType >
ObjectWrapperoperator= (std::unique_ptr< DataType > rhs)
 Assignment operator. More...
 
ObjectWrapperoperator= (const ObjectWrapper &rhs)
 Assignment operator. More...
 
ObjectWrapperoperator= (ObjectWrapper &rhs)
 Assignment operator. More...
 
bool operator== (const ObjectWrapper &rhs) const
 Equal-to operator. More...
 
bool operator!= (const ObjectWrapper &rhs) const
 Not-equal-to operator. More...
 
void WrapNothing ()
 Remove the wrapped object. More...
 
bool WrapsSomething ()
 Checks if the wrapper wraps something. More...
 
ObjectWrapper CloneObject ()
 Creates a clone of the wrapped object and returns a wrapper wrapping it. More...
 
const std::type_info & GetObjectType ()
 Get the wrapped type. More...
 
bool WrapObject (void *address, const std::type_info &type, bool own, bool isPolymorphic, bool isConst)
 Wrap the object at the given address. More...
 

Private Attributes

std::unique_ptr< ObjectWrapperBasecontent
 

Friends

template<typename DataType >
observer_ptr< DataType > wrapper_cast (ObjectWrapper &)
 Cast function to extract wrapped pointer from wrapper in a type-safe way. More...
 
void * wrapper_cast (ObjectWrapper &, const std::type_info &, bool)
 Cast function to extract wrapped pointer from wrapper in a type-safe way. More...
 

Detailed Description

Generic wrapper class.

This class wraps any kind of object in a type-safe way, and is useful to implement generic containers not tied to an inheritance line. Type safety is achieved by storing the wrapped type, i.e. the type of the wrapped object, which is then checked by wrapper_cast at the moment of obtaining the wrapped object from the wrapper. Once set, the wrapped type is preserved even if the wrapped object is "unwrapped" (see WrapNothing).

The class wraps objects by wrapping their addresses, so no object copy is done. It may or may not own the wrapped objects.

Constructor & Destructor Documentation

◆ ObjectWrapper() [1/8]

EA::ObjectWrapper::ObjectWrapper ( )

Default constructor.

Constructs a wrapper wrapping nothing. The wrapped object can be subsequently set by operator= . The default constructor does not set the wrapped type: it will be set when setting the wrapped object with operator=

◆ ObjectWrapper() [2/8]

EA::ObjectWrapper::ObjectWrapper ( const ObjectWrapper w)

copy constructor.

Parameters
wThe wrapper to be copied.

◆ ObjectWrapper() [3/8]

EA::ObjectWrapper::ObjectWrapper ( ObjectWrapper &&  w)

Move constructor.

Parameters
wThe wrapper to be moved.

◆ ObjectWrapper() [4/8]

template<typename DataType >
EA::ObjectWrapper::ObjectWrapper ( observer_ptr< DataType >  ptr)

Constructor.

Builds a wrapper wrapping the object pointed by #ptr. The wrapper will not own the wrapped object. The wrapped type will be set to #DataType.

Parameters
ptrPointer to the object to be wrapped.

◆ ObjectWrapper() [5/8]

template<typename DataType >
EA::ObjectWrapper::ObjectWrapper ( std::shared_ptr< DataType >  ptr)

Constructor.

Builds a wrapper wrapping the object pointed by #ptr. The wrapper will share the ownership of the wrapped object. The wrapped type will be set to #DataType.

Parameters
ptrPointer to the object to be wrapped.

◆ ObjectWrapper() [6/8]

template<typename DataType >
EA::ObjectWrapper::ObjectWrapper ( std::unique_ptr< DataType >  ptr)

Constructor.

Builds a wrapper wrapping the object pointed by #ptr. The wrapper will exclusively own the wrapped object. The wrapped type will be set to #DataType.

Parameters
ptrPointer to the object to be wrapped.

◆ ObjectWrapper() [7/8]

EA::ObjectWrapper::ObjectWrapper ( const char *  address)

Constructor. Overload for converting a const char * string literal to std::string before wrapping (needed to circumvent troubles with template substitution and type mismatch). The wrapped type will be set to std::string.

Parameters
addressThe string literal to wrap.

◆ ObjectWrapper() [8/8]

EA::ObjectWrapper::ObjectWrapper ( char *  address)

Constructor. Overload for converting a char * string literal to std::string before wrapping (needed to circumvent troubles with template substitution and type mismatch). The wrapped type will be set to std::string.

Parameters
addressThe string literal to wrap.

Member Function Documentation

◆ CloneObject()

ObjectWrapper EA::ObjectWrapper::CloneObject ( )

Creates a clone of the wrapped object and returns a wrapper wrapping it.

Exceptions
std::runtime_errorif the wrapped object is non-copy-constructible.
Returns
A wrapper wrapping a clone of the originally wrapped object.

◆ GetObjectType()

const std::type_info& EA::ObjectWrapper::GetObjectType ( )
inline

Get the wrapped type.

The wrapped type is the type of the wrapped object. It is set at the moment of actually wrapping an object (i.e. when constructing the wrapper with a non-default constructor or when assigning a wrapped object with operator= ) and is preserved when "unwrapping" it with WrapNothing.

Returns
The type of the wrapped object, or typeid(void) if the wrapper has been default-constructed and no wrapped object has ever been set.

◆ operator!=()

bool EA::ObjectWrapper::operator!= ( const ObjectWrapper rhs) const
inline

Not-equal-to operator.

Compares two wrappers. Returns true if the two wrappers don't wrap the same object or if they wrap different types.

Parameters
rhsThe wrapper to compare to.
Returns
true if the wrappers wrap different objects or different types.

◆ operator=() [1/6]

template<typename DataType >
ObjectWrapper & EA::ObjectWrapper::operator= ( DataType *  rhs)

Assignment operator.

Wraps the object at the specified address. The wrapper will not own the object. An eventual previously wrapped object will be deleted if the object is owned by the wrapper, and the wrapped type will be updated to the type of the new wrapped object.

Parameters
rhsAddress of the object to be wrapped.
Returns
this object wrapper.

◆ operator=() [2/6]

template<typename DataType >
ObjectWrapper & EA::ObjectWrapper::operator= ( DataType &  rhs)

Assignment operator.

Wraps the given object. The wrapper will not own the object. An eventual previously wrapped object will be deleted if the object is owned by the wrapper, and the wrapped type will be updated to the type of the new wrapped object.

Parameters
rhsAddress of the object to be wrapped.
Returns
this object wrapper.

◆ operator=() [3/6]

template<typename DataType >
ObjectWrapper & EA::ObjectWrapper::operator= ( std::shared_ptr< DataType >  rhs)

Assignment operator.

Wraps the object pointed by the given pointer. The wrapper will own the object. An eventual previously wrapped object will be deleted if the object is owned by the wrapper, and the wrapped type will be updated to the type of the new wrapped object.

Parameters
rhsAddress of the object to be wrapped.
Returns
this object wrapper.

◆ operator=() [4/6]

template<typename DataType >
ObjectWrapper & EA::ObjectWrapper::operator= ( std::unique_ptr< DataType >  rhs)

Assignment operator.

Wraps the object pointed by the given pointer. The wrapper will own the object. An eventual previously wrapped object will be deleted if the object is owned by the wrapper, and the wrapped type will be updated to the type of the new wrapped object.

Parameters
rhsAddress of the object to be wrapped.
Returns
this object wrapper.

◆ operator=() [5/6]

ObjectWrapper & EA::ObjectWrapper::operator= ( const ObjectWrapper rhs)

Assignment operator.

Makes the wrapper wrap the same object wrapped by rhs. This overload is necessary to avoid that the call of operator= with a const ObjectWrapper argument results in calling the template version operator=(DataType &rhs), that would result in making the wrapper wrap the rhs wrapper instead of the object wrapped by the rhs wrapper.

Parameters
rhsThe wrapper to copy from.
Returns
this object wrapper.

◆ operator=() [6/6]

ObjectWrapper & EA::ObjectWrapper::operator= ( ObjectWrapper rhs)

Assignment operator.

Makes the wrapper wrap the same object wrapped by rhs. This overload is necessary to avoid that the call of operator= with a non-const ObjectWrapper argument results in calling the template version operator=(DataType &rhs), that would result in making the wrapper wrap the rhs wrapper instead of the object wrapped by the rhs wrapper.

Parameters
rhsThe wrapper to copy from.
Returns
this object wrapper.

◆ operator==()

bool EA::ObjectWrapper::operator== ( const ObjectWrapper rhs) const

Equal-to operator.

Compares two wrappers. Returns true if both wrappers wrap the same object (i.e. the same memory address and type).

Parameters
rhsThe wrapper to compare to.
Returns
true if both wrappers wrap the same object.

◆ WrapNothing()

void EA::ObjectWrapper::WrapNothing ( )

Remove the wrapped object.

Makes the wrapper wrap no object. An eventual previously wrapped object will be deleted if the object is owned by the wrapper. The wrapped type is preserved.

◆ WrapObject()

bool EA::ObjectWrapper::WrapObject ( void *  address,
const std::type_info &  type,
bool  own,
bool  isPolymorphic,
bool  isConst 
)

Wrap the object at the given address.

Set the wrapped object. This method should be used only in those rare cases where the object is available only through a void*. Type check is enforced through the #type argument.

Parameters
addressThe address of the object to be wrapped.
typeThe type of the object at the given address.
ownIf true then the wrapper will own the wrapped object.
isPolymorhicSet to true for objects of polymorphic classes.
isConstSet to true for constant objects.
Returns
true if the object at the given address has been correctly wrapped.

◆ WrapsSomething()

bool EA::ObjectWrapper::WrapsSomething ( )

Checks if the wrapper wraps something.

Returns
true if the wrapped pointer is pointing to something.

Friends And Related Function Documentation

◆ wrapper_cast [1/2]

template<typename DataType >
observer_ptr<DataType> wrapper_cast ( ObjectWrapper wrapper)
friend

Cast function to extract wrapped pointer from wrapper in a type-safe way.

The wrapped object is returned if DataType corresponds to its class or to a mother class. In case DataType is the type of a mother class, then the wrapped objects must be polymorphic (i.e. have at least one virtual method) in order for the base classes to be checked. For non-polymorphic objects this method returns a non-null value only if the type match between the wrapped object and DataType is exact.

Parameters
wrapperThe data wrapper object.
Returns
pointer to the wrapped object (nullptr if there is a type mismatch).

◆ wrapper_cast [2/2]

void* wrapper_cast ( ObjectWrapper wrapper,
const std::type_info &  typeInfo,
bool  isConst 
)
friend

Cast function to extract wrapped pointer from wrapper in a type-safe way.

This overload for wrapper_cast is to allow for a type_safe extraction of the wrapped object when the type of the requested object is unknown until runtime (e.g. in PersistenceService methods for generic persistence, which are virtual and thus cannot be template but still needs to be generic).

The function returns a void* since in principle there is no way to know the object type at compile time. It is user responsibility to cast the returned pointer to the appropriate type.

Parameters
wrapperThe data wrapper object.
typeInfoThe type of the desired object.
isConsttrue if the object info is constant.
Returns
a pointer to the wrapped object (nullptr if the wrapper wraps nothing or if the type of wrapped object does not match #typeInfo).

Member Data Documentation

◆ content

std::unique_ptr<ObjectWrapperBase> EA::ObjectWrapper::content
private

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