Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-27 07:20:01

0001 #ifndef _LIBCPP_VERSION
0002 #include <cxxabi.h>
0003 #endif
0004 
0005 #include "FWCore/Reflection/interface/BaseWithDict.h"
0006 #include "FWCore/Reflection/interface/MemberWithDict.h"
0007 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0008 #include "FWCore/Reflection/interface/TypeWithDict.h"
0009 
0010 namespace edm {
0011 
0012   ObjectWithDict ObjectWithDict::byType(TypeWithDict const& type) {
0013     ObjectWithDict obj(type.construct());
0014     return obj;
0015   }
0016 
0017   class DummyVT {
0018   public:
0019     virtual ~DummyVT();
0020   };
0021 
0022   DummyVT::~DummyVT() {}
0023 
0024   // FIXME improve TypeWithDict::byTypeInfo to return by const& and return by const& here
0025   TypeWithDict ObjectWithDict::dynamicType() const {
0026     if (!type_.isVirtual()) {
0027       return type_;
0028     }
0029     // Use a dirty trick, force the typeid() operator
0030     // to consult the virtual table stored at address_.
0031     return TypeWithDict::byTypeInfo(typeid(*(DummyVT*)address_));
0032   }
0033 
0034   ObjectWithDict ObjectWithDict::get(std::string const& memberName) const {
0035     return type_.dataMemberByName(memberName).get(*this);
0036   }
0037 
0038   ObjectWithDict ObjectWithDict::castObject(TypeWithDict const& to) const {
0039     TypeWithDict from = typeOf();
0040 
0041     // Same type
0042     if (from == to) {
0043       return *this;
0044     }
0045 
0046     if (to.hasBase(from)) {  // down cast
0047 #ifndef _LIBCPP_VERSION
0048       // use the internal dynamic casting of the compiler (e.g. libstdc++.so)
0049       void* address = abi::__dynamic_cast(address_,
0050                                           static_cast<abi::__class_type_info const*>(&from.typeInfo()),
0051                                           static_cast<abi::__class_type_info const*>(&to.typeInfo()),
0052                                           -1);
0053       return ObjectWithDict(to, address);
0054 #else
0055       return ObjectWithDict(to, address_);
0056 #endif
0057     }
0058 
0059     if (from.hasBase(to)) {  // up cast
0060       size_t offset = from.getBaseClassOffset(to);
0061       size_t address = reinterpret_cast<size_t>(address_) + offset;
0062       return ObjectWithDict(to, reinterpret_cast<void*>(address));
0063     }
0064 
0065     // if everything fails return the dummy object
0066     return ObjectWithDict();
0067   }  // castObject
0068 
0069   //ObjectWithDict
0070   //ObjectWithDict::construct() const {
0071   //  TypeWithDict ty(type_);
0072   //  TClass* cl = ty.getClass();
0073   //  if (cl != nullptr) {
0074   //    return ObjectWithDict(ty, cl->New());
0075   //  }
0076   //  return ObjectWithDict(ty, new char[ty.size()]);
0077   //}
0078 
0079   void ObjectWithDict::destruct(bool dealloc) const {
0080     TClass* cl = type_.getClass();
0081     if (cl != nullptr) {
0082       cl->Destructor(address_, !dealloc);
0083       //if (dealloc) {
0084       //  address_ = nullptr;
0085       //}
0086       return;
0087     }
0088     if (dealloc) {
0089       delete[] reinterpret_cast<char*>(address_);
0090       //address_ = nullptr;
0091     }
0092   }
0093 
0094 }  // namespace edm