CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectWithDict.cc
Go to the documentation of this file.
2 
6 
7 #include <cxxabi.h>
8 
9 namespace edm {
10 
11  ObjectWithDict
14  return obj;
15  }
16 
17  ObjectWithDict::ObjectWithDict() : type_(), address_(nullptr) {
18  }
19 
21  type_(type),
22  address_(address) {
23  }
24 
25  ObjectWithDict::ObjectWithDict(std::type_info const& ti, void* address) :
26  type_(TypeWithDict(ti)),
27  address_(address) {
28  }
29 
30  ObjectWithDict::operator bool() const {
31  return bool(type_) && (address_ != nullptr);
32  }
33 
34  void*
36  return address_;
37  }
38 
41  return type_;
42  }
43 
44  class DummyVT {
45  public:
46  virtual ~DummyVT();
47  };
48 
50  }
51 
54  if (!type_.isVirtual()) {
55  return type_;
56  }
57  // Use a dirty trick, force the typeid() operator
58  // to consult the virtual table stored at address_.
59  return TypeWithDict(typeid(*(DummyVT*)address_));
60  }
61 
63  ObjectWithDict::get(std::string const& memberName) const {
64  return type_.dataMemberByName(memberName).get(*this);
65  }
66 
69  TypeWithDict from = typeOf();
70 
71  // Same type
72  if (from == to) {
73  return *this;
74  }
75 
76  if (to.hasBase(from)) { // down cast
77  // use the internal dynamic casting of the compiler (e.g. libstdc++.so)
78  void* address = abi::__dynamic_cast(address_, static_cast<abi::__class_type_info const*>(&from.typeInfo()), static_cast<abi::__class_type_info const*>(&to.typeInfo()), -1);
79  return ObjectWithDict(to, address);
80  }
81 
82  if (from.hasBase(to)) { // up cast
83  size_t offset = from.getBaseClassOffset(to);
84  size_t address = reinterpret_cast<size_t>(address_) + offset;
85  return ObjectWithDict(to, reinterpret_cast<void*>(address));
86  }
87 
88  // if everything fails return the dummy object
89  return ObjectWithDict();
90  } // castObject
91 
92  //ObjectWithDict
93  //ObjectWithDict::construct() const {
94  // TypeWithDict ty(type_);
95  // TClass* cl = ty.getClass();
96  // if (cl != nullptr) {
97  // return ObjectWithDict(ty, cl->New());
98  // }
99  // return ObjectWithDict(ty, new char[ty.size()]);
100  //}
101 
102  void
103  ObjectWithDict::destruct(bool dealloc) const {
104  TClass* cl = type_.getClass();
105  if (cl != nullptr) {
106  cl->Destructor(address_, !dealloc);
107  //if (dealloc) {
108  // address_ = nullptr;
109  //}
110  return;
111  }
112  if (dealloc) {
113  delete[] reinterpret_cast<char*>(address_);
114  //address_ = nullptr;
115  }
116  }
117 
118 } // namespace edm
type
Definition: HCALResponse.h:21
static ObjectWithDict byType(TypeWithDict const &)
void * address() const
ObjectWithDict castObject(TypeWithDict const &) const
virtual ~DummyVT()
TypeWithDict dynamicType() const
#define nullptr
bool hasBase(std::string const &) const
MemberWithDict dataMemberByName(std::string const &) const
TypeWithDict typeOf() const
TClass * getClass() const
bool isVirtual() const
ObjectWithDict get() const
std::type_info const & typeInfo() const
void destruct(bool dealloc) const
int getBaseClassOffset(TypeWithDict const &baseClass) const
ObjectWithDict construct() const
TypeWithDict type_
ObjectWithDict get(std::string const &memberName) const