Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:19

0001 #ifndef FWCore_Utilities_OffsetToBase_h
0002 #define FWCore_Utilities_OffsetToBase_h
0003 #include <cstddef>
0004 #include <typeinfo>
0005 
0006 /*
0007  * For any class used in Views, RefToBase, or Ptr,
0008  * class template OffsetToBase must be specialized for any class
0009  * with multiple inheritance (i.e. with non-zero offset to
0010  * at least one base class).
0011  * A specialization would look something like this
0012  * (in YourClass.h"
0013  *
0014  * #include "FWCore/Utilities/interface/OffsetToBase.h"
0015  * namespace edm {
0016  *   template<>
0017  *   class OffsetToBase<YourClass> {
0018  *     public OffsetToBase() {}
0019  *     size_t offsetToBase(std::type_info const& baseTypeInfo) const {
0020  *       int const dummy = 0;
0021  *       YourClass const* object = reinterpret_cast<YourClass const*>(&dummy);
0022  *       void const* objectPtr = object;
0023  *       if(baseTypeInfo == typeid(BaseClass1)) {
0024  *          BaseClass1 const* base = object;
0025  *          void const* basePtr = base;
0026  *          return static_cast<char const*>(basePtr) - static_cast<char const*>(objectPtr);
0027  *       }
0028  *       if(baseTypeInfo == typeid(BaseClass2)) {
0029  *          ...
0030  *       }
0031  *       etc.
0032  *     }
0033  *   };
0034  * }
0035  *
0036 */
0037 
0038 namespace edm {
0039   template <typename T>
0040   class OffsetToBase {
0041   public:
0042     OffsetToBase() {}
0043     size_t offsetToBase(std::type_info const& baseTypeInfo) const { return 0; }
0044   };
0045 
0046   template <typename T>
0047   void const* pointerToBase(std::type_info const& baseTypeInfo, T const* address) {
0048     OffsetToBase<T> offsetToBase;
0049     int offset = offsetToBase.offsetToBase(baseTypeInfo);
0050     void const* ptr = address;
0051     return static_cast<char const*>(ptr) + offset;
0052   }
0053 }  // namespace edm
0054 #endif