EventAnalysis
1.3.0
|
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 > | |
ObjectWrapper & | operator= (DataType *rhs) |
Assignment operator. More... | |
template<typename DataType > | |
ObjectWrapper & | operator= (DataType &rhs) |
Assignment operator. More... | |
template<typename DataType > | |
ObjectWrapper & | operator= (std::shared_ptr< DataType > rhs) |
Assignment operator. More... | |
template<typename DataType > | |
ObjectWrapper & | operator= (std::unique_ptr< DataType > rhs) |
Assignment operator. More... | |
ObjectWrapper & | operator= (const ObjectWrapper &rhs) |
Assignment operator. More... | |
ObjectWrapper & | operator= (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< ObjectWrapperBase > | content |
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... | |
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.
EA::ObjectWrapper::ObjectWrapper | ( | ) |
EA::ObjectWrapper::ObjectWrapper | ( | const ObjectWrapper & | w | ) |
copy constructor.
w | The wrapper to be copied. |
EA::ObjectWrapper::ObjectWrapper | ( | ObjectWrapper && | w | ) |
Move constructor.
w | The wrapper to be moved. |
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.
ptr | Pointer to the object to be wrapped. |
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.
ptr | Pointer to the object to be wrapped. |
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.
ptr | Pointer to the object to be wrapped. |
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.
address | The string literal to wrap. |
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.
address | The string literal to wrap. |
ObjectWrapper EA::ObjectWrapper::CloneObject | ( | ) |
Creates a clone of the wrapped object and returns a wrapper wrapping it.
std::runtime_error | if the wrapped object is non-copy-constructible. |
|
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.
|
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.
rhs | The wrapper to compare to. |
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.
rhs | Address of the object to be wrapped. |
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.
rhs | Address of the object to be wrapped. |
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.
rhs | Address of the object to be wrapped. |
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.
rhs | Address of the object to be wrapped. |
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.
rhs | The wrapper to copy from. |
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.
rhs | The wrapper to copy from. |
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).
rhs | The wrapper to compare to. |
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.
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.
address | The address of the object to be wrapped. |
type | The type of the object at the given address. |
own | If true then the wrapper will own the wrapped object. |
isPolymorhic | Set to true for objects of polymorphic classes. |
isConst | Set to true for constant objects. |
bool EA::ObjectWrapper::WrapsSomething | ( | ) |
Checks if the wrapper wraps something.
|
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.
wrapper | The data wrapper object. |
|
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.
wrapper | The data wrapper object. |
typeInfo | The type of the desired object. |
isConst | true if the object info is constant. |
|
private |