EventAnalysis  1.3.0
ObjectWrapperT.hpp
Go to the documentation of this file.
1 /*
2  * ObjectWrapperT.hpp
3  *
4  * Created on: 04 Dec 2017
5  * Author: Nicola Mori
6  */
7 
8 #ifndef OBJECTWRAPPERT_HPP_
9 #define OBJECTWRAPPERT_HPP_
10 
11 #include "core/InheritsFrom.h"
12 #include "utils/SmartLog.h"
13 
14 namespace EA {
15 
16 template <typename DataType>
18  : _dataObjPtr(dataObjPtr), _dataObjSharedPtr(nullptr) {}
19 
20 template <typename DataType>
21 ObjectWrapperTImpl<DataType>::ObjectWrapperTImpl(std::shared_ptr<DataType> dataObjPtr)
22  : _dataObjPtr(dataObjPtr.get()), _dataObjSharedPtr(dataObjPtr) {}
23 
24 template <typename DataType>
25 ObjectWrapperTImpl<DataType>::ObjectWrapperTImpl(std::unique_ptr<DataType> dataObjPtr)
26  : _dataObjPtr(dataObjPtr.get()), _dataObjSharedPtr(std::shared_ptr<DataType>(std::move(dataObjPtr))) {}
27 
28 template <typename DataType>
31 
33 
34 template <typename DataType> bool ObjectWrapperTImpl<DataType>::IsPolymorphic() {
35  return std::is_polymorphic<DataType>::value;
36 }
37 
38 template <typename DataType> bool ObjectWrapperTImpl<DataType>::IsConst() { return std::is_const<DataType>::value; }
39 
40 template <typename DataType>
41 std::unique_ptr<ObjectWrapperBase>
44  return std::make_unique<ObjectWrapperT<DataType, void>>(
45  std::shared_ptr<DataType>(new DataType(*ObjectWrapperTImpl<DataType>::_dataObjPtr)));
46  } else {
47  return std::make_unique<ObjectWrapperT<DataType, void>>(
48  std::shared_ptr<DataType>(new DataType(*ObjectWrapperTImpl<DataType>::_dataObjSharedPtr)));
49  }
50 }
51 template <typename DataType>
52 bool ObjectWrapperTImpl<DataType>::WrapObject(void *address, const std::type_info &type, bool own, bool isPolymorphic,
53  bool isConst) {
54  static const std::string routineName(
55  std::string("ObjectWrapperTImpl<").append(Demangler::ClassName<DataType>()).append(">::WrapObject"));
56  if (InheritsFrom(type, typeid(DataType))) {
57  if (isPolymorphic != std::is_polymorphic<DataType>::value) {
58  COUT(ERROR) << "The value for the \"isPolymorphic\" argument does not match the one for class \""
59  << Demangler::ClassName<DataType>() << "\"" << ENDL;
60  return false;
61  }
62  if (isConst && !std::is_const<DataType>::value) {
63  COUT(ERROR) << "Can't wrap a const \"" << Demangler::ClassName(type)
64  << "\" object with a wrapper for non-const \"" << Demangler::ClassName<DataType>() << "\" objects "
65  << ENDL;
66  return false;
67  }
68  if (own)
69  _dataObjSharedPtr = std::shared_ptr<DataType>((DataType *)address);
70  else
71  _dataObjPtr = (DataType *)(address);
72  return true;
73  } else {
74  COUT(ERROR) << "Can't wrap an object of type \"" << Demangler::ClassName(type) << "\" in a wrapper for type \""
75  << Demangler::ClassName<DataType>() << "\"" << ENDL;
76  return false;
77  }
78 }
79 
80 } // namespace EA
81 
82 #endif /* OBJECTWRAPPERT_HPP_ */
bool IsConst()
Test for const wrapped object.
Definition: ObjectWrapperT.hpp:38
ObjectWrapperTImpl(observer_ptr< DataType > dataObjPtr)
Constructor.
Definition: ObjectWrapperT.hpp:17
bool IsPolymorphic()
Test for polymorphic wrapped object.
Definition: ObjectWrapperT.hpp:34
#define COUT(level)
Definition: SmartLog.h:88
std::string ClassName(const T &obj)
Function which returns the name of the class.
Definition: Demangler.h:30
Wrapper class for non-copy-constructible objects.
Definition: ObjectWrapperT.h:211
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::shared_ptr< DataType > _dataObjSharedPtr
Definition: ObjectWrapperT.h:122
bool WrapObject(void *address, const std::type_info &type, bool own, bool isPolymorphic, bool isConst)
Wrap the object at the given address.
Definition: ObjectWrapperT.hpp:52
#define ENDL
Definition: SmartLog.h:117
virtual std::unique_ptr< ObjectWrapperBase > CloneObject()=0
Clones the wrapped object and wraps it.
observer_ptr< DataType > _dataObjPtr
Definition: ObjectWrapperT.h:121
observer_ptr< DataType > GetObj()
Data object getter method.
Definition: ObjectWrapperT.hpp:32
bool InheritsFrom(const std::type_info &type, const std::type_info &baseType)
Check if a given type inherits from a given class.
Definition: InheritsFrom.cpp:20
Aliased object is an alias.
Wrapper for data objects.
Definition: ObjectWrapperT.h:26