CMS 3D CMS Logo

ObjectWithDict.cc
Go to the documentation of this file.
2 
6 
7 #ifndef _LIBCPP_VERSION
8 #include <cxxabi.h>
9 #endif
10 
11 namespace edm {
12 
13  ObjectWithDict
16  return obj;
17  }
18 
20  }
21 
23  type_(type),
24  address_(address) {
25  }
26 
27  ObjectWithDict::ObjectWithDict(std::type_info const& ti, void* address) :
28  type_(TypeWithDict(ti)),
29  address_(address) {
30  }
31 
33  return bool(type_) && (address_ != nullptr);
34  }
35 
36  void*
38  return address_;
39  }
40 
43  return type_;
44  }
45 
46  class DummyVT {
47  public:
48  virtual ~DummyVT();
49  };
50 
52  }
53 
56  if (!type_.isVirtual()) {
57  return type_;
58  }
59  // Use a dirty trick, force the typeid() operator
60  // to consult the virtual table stored at address_.
61  return TypeWithDict(typeid(*(DummyVT*)address_));
62  }
63 
65  ObjectWithDict::get(std::string const& memberName) const {
66  return type_.dataMemberByName(memberName).get(*this);
67  }
68 
71  TypeWithDict from = typeOf();
72 
73  // Same type
74  if (from == to) {
75  return *this;
76  }
77 
78  if (to.hasBase(from)) { // down cast
79 #ifndef _LIBCPP_VERSION
80  // use the internal dynamic casting of the compiler (e.g. libstdc++.so)
81  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);
82  return ObjectWithDict(to, address);
83 #else
84  return ObjectWithDict(to, address_);
85 #endif
86  }
87 
88  if (from.hasBase(to)) { // up cast
89  size_t offset = from.getBaseClassOffset(to);
90  size_t address = reinterpret_cast<size_t>(address_) + offset;
91  return ObjectWithDict(to, reinterpret_cast<void*>(address));
92  }
93 
94  // if everything fails return the dummy object
95  return ObjectWithDict();
96  } // castObject
97 
98  //ObjectWithDict
99  //ObjectWithDict::construct() const {
100  // TypeWithDict ty(type_);
101  // TClass* cl = ty.getClass();
102  // if (cl != nullptr) {
103  // return ObjectWithDict(ty, cl->New());
104  // }
105  // return ObjectWithDict(ty, new char[ty.size()]);
106  //}
107 
108  void
109  ObjectWithDict::destruct(bool dealloc) const {
110  TClass* cl = type_.getClass();
111  if (cl != nullptr) {
112  cl->Destructor(address_, !dealloc);
113  //if (dealloc) {
114  // address_ = nullptr;
115  //}
116  return;
117  }
118  if (dealloc) {
119  delete[] reinterpret_cast<char*>(address_);
120  //address_ = nullptr;
121  }
122  }
123 
124 } // 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
HLT enums.
ObjectWithDict construct() const
TypeWithDict type_
ObjectWithDict get(std::string const &memberName) const