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 
15  return obj;
16  }
17 
19 
21 
22  ObjectWithDict::ObjectWithDict(std::type_info const& ti, void* address)
23  : type_(TypeWithDict(ti)), address_(address) {}
24 
25  ObjectWithDict::operator bool() const { return bool(type_) && (address_ != nullptr); }
26 
27  void* ObjectWithDict::address() const { return address_; }
28 
30 
31  class DummyVT {
32  public:
33  virtual ~DummyVT();
34  };
35 
37 
39  if (!type_.isVirtual()) {
40  return type_;
41  }
42  // Use a dirty trick, force the typeid() operator
43  // to consult the virtual table stored at address_.
44  return TypeWithDict::byTypeInfo(typeid(*(DummyVT*)address_));
45  }
46 
47  ObjectWithDict ObjectWithDict::get(std::string const& memberName) const {
48  return type_.dataMemberByName(memberName).get(*this);
49  }
50 
52  TypeWithDict from = typeOf();
53 
54  // Same type
55  if (from == to) {
56  return *this;
57  }
58 
59  if (to.hasBase(from)) { // down cast
60 #ifndef _LIBCPP_VERSION
61  // use the internal dynamic casting of the compiler (e.g. libstdc++.so)
62  void* address = abi::__dynamic_cast(address_,
63  static_cast<abi::__class_type_info const*>(&from.typeInfo()),
64  static_cast<abi::__class_type_info const*>(&to.typeInfo()),
65  -1);
66  return ObjectWithDict(to, address);
67 #else
68  return ObjectWithDict(to, address_);
69 #endif
70  }
71 
72  if (from.hasBase(to)) { // up cast
73  size_t offset = from.getBaseClassOffset(to);
74  size_t address = reinterpret_cast<size_t>(address_) + offset;
75  return ObjectWithDict(to, reinterpret_cast<void*>(address));
76  }
77 
78  // if everything fails return the dummy object
79  return ObjectWithDict();
80  } // castObject
81 
82  //ObjectWithDict
83  //ObjectWithDict::construct() const {
84  // TypeWithDict ty(type_);
85  // TClass* cl = ty.getClass();
86  // if (cl != nullptr) {
87  // return ObjectWithDict(ty, cl->New());
88  // }
89  // return ObjectWithDict(ty, new char[ty.size()]);
90  //}
91 
92  void ObjectWithDict::destruct(bool dealloc) const {
93  TClass* cl = type_.getClass();
94  if (cl != nullptr) {
95  cl->Destructor(address_, !dealloc);
96  //if (dealloc) {
97  // address_ = nullptr;
98  //}
99  return;
100  }
101  if (dealloc) {
102  delete[] reinterpret_cast<char*>(address_);
103  //address_ = nullptr;
104  }
105  }
106 
107 } // namespace edm
type
Definition: HCALResponse.h:21
static ObjectWithDict byType(TypeWithDict const &)
void * address() const
ObjectWithDict castObject(TypeWithDict const &) const
#define nullptr
virtual ~DummyVT()
TypeWithDict dynamicType() const
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
static TypeWithDict byTypeInfo(std::type_info const &ti)
Definition: TypeWithDict.cc:62
int getBaseClassOffset(TypeWithDict const &baseClass) const
HLT enums.
ObjectWithDict construct() const
TypeWithDict type_
ObjectWithDict get(std::string const &memberName) const