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 
14  ObjectWithDict obj(type.construct());
15  return obj;
16  }
17 
18  ObjectWithDict::ObjectWithDict() : type_(), address_(nullptr) {}
19 
20  ObjectWithDict::ObjectWithDict(TypeWithDict const& type, void* address) : type_(type), address_(address) {}
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
electrons_cff.bool
bool
Definition: electrons_cff.py:372
edm::DummyVT
Definition: ObjectWithDict.cc:31
edm::ObjectWithDict::address
void * address() const
Definition: ObjectWithDict.cc:27
BaseWithDict.h
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ObjectWithDict
Definition: ObjectWithDict.h:17
edm::DummyVT::~DummyVT
virtual ~DummyVT()
Definition: ObjectWithDict.cc:36
edm::ObjectWithDict::castObject
ObjectWithDict castObject(TypeWithDict const &) const
Definition: ObjectWithDict.cc:51
to
edm::TypeWithDict::getBaseClassOffset
int getBaseClassOffset(TypeWithDict const &baseClass) const
Definition: TypeWithDict.cc:761
TypeWithDict.h
Utilities.operator
operator
Definition: Utilities.py:24
ObjectWithDict.h
GetRecoTauVFromDQM_MC_cff.cl
cl
Definition: GetRecoTauVFromDQM_MC_cff.py:38
edm::ObjectWithDict::type_
TypeWithDict type_
Definition: ObjectWithDict.h:19
edm::TypeWithDict::dataMemberByName
MemberWithDict dataMemberByName(std::string const &) const
Definition: TypeWithDict.cc:572
edm::ObjectWithDict::ObjectWithDict
ObjectWithDict()
Definition: ObjectWithDict.cc:18
edm::ObjectWithDict::address_
void * address_
Definition: ObjectWithDict.h:20
edm::ObjectWithDict::destruct
void destruct(bool dealloc) const
Definition: ObjectWithDict.cc:92
edm::MemberWithDict::get
ObjectWithDict get() const
Definition: MemberWithDict.cc:46
edm::TypeWithDict::isVirtual
bool isVirtual() const
Definition: TypeWithDict.cc:429
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::TypeWithDict
Definition: TypeWithDict.h:38
edm::ObjectWithDict::typeOf
TypeWithDict typeOf() const
Definition: ObjectWithDict.cc:29
edm::TypeWithDict::typeInfo
std::type_info const & typeInfo() const
Definition: TypeWithDict.cc:357
edm::TypeWithDict::hasBase
bool hasBase(std::string const &) const
Definition: TypeWithDict.cc:736
edm::ObjectWithDict::dynamicType
TypeWithDict dynamicType() const
Definition: ObjectWithDict.cc:38
edm::ObjectWithDict::byType
static ObjectWithDict byType(TypeWithDict const &)
Definition: ObjectWithDict.cc:13
edm::ObjectWithDict::get
ObjectWithDict get(std::string const &memberName) const
Definition: ObjectWithDict.cc:47
edm::TypeWithDict::getClass
TClass * getClass() const
Definition: TypeWithDict.cc:380
type
type
Definition: HCALResponse.h:21
edm::TypeWithDict::byTypeInfo
static TypeWithDict byTypeInfo(std::type_info const &ti)
Definition: TypeWithDict.cc:62
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
MemberWithDict.h