Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:04

0001 #ifndef DataFormats_Common_Wrapper_h
0002 #define DataFormats_Common_Wrapper_h
0003 
0004 /*----------------------------------------------------------------------
0005 
0006 Wrapper: A template wrapper around EDProducts to hold the product ID.
0007 
0008 ----------------------------------------------------------------------*/
0009 
0010 #include "DataFormats/Common/interface/WrapperBase.h"
0011 #include "DataFormats/Common/interface/WrapperDetail.h"
0012 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0013 #include "DataFormats/Provenance/interface/ProductID.h"
0014 #include "FWCore/Utilities/interface/Visibility.h"
0015 
0016 #include <algorithm>
0017 #include <cassert>
0018 #include <memory>
0019 #include <string>
0020 #include <typeinfo>
0021 
0022 namespace edm {
0023   template <typename T>
0024   class Wrapper : public WrapperBase {
0025   public:
0026     typedef T value_type;
0027     typedef T wrapped_type;  // used with the dictionary to identify Wrappers
0028     Wrapper() : WrapperBase(), present(false), obj() {}
0029     explicit Wrapper(std::unique_ptr<T> ptr);
0030     Wrapper(Wrapper<T> const& rh) = delete;             // disallow copy construction
0031     Wrapper<T>& operator=(Wrapper<T> const&) = delete;  // disallow assignment
0032 
0033     template <typename... Args>
0034     explicit Wrapper(Emplace, Args&&...);
0035     ~Wrapper() override {}
0036     T const* product() const { return (present ? &obj : nullptr); }
0037     T const* operator->() const { return product(); }
0038 
0039     T& bareProduct() { return obj; }
0040 
0041     //these are used by FWLite
0042     static std::type_info const& productTypeInfo() { return typeid(T); }
0043     static std::type_info const& typeInfo() { return typeid(Wrapper<T>); }
0044 
0045     // the constructor takes ownership of T*
0046     Wrapper(T*);
0047 
0048     //Used by ROOT storage
0049     CMS_CLASS_VERSION(3)
0050 
0051   private:
0052     bool isPresent_() const override { return present; }
0053     std::type_info const& dynamicTypeInfo_() const override { return typeid(T); }
0054     std::type_info const& wrappedTypeInfo_() const override { return typeid(Wrapper<T>); }
0055 
0056     std::type_info const& valueTypeInfo_() const override;
0057     std::type_info const& memberTypeInfo_() const override;
0058     bool isMergeable_() const override;
0059     bool mergeProduct_(WrapperBase const* newProduct) override;
0060     bool hasIsProductEqual_() const override;
0061     bool isProductEqual_(WrapperBase const* newProduct) const override;
0062     bool hasSwap_() const override;
0063     void swapProduct_(WrapperBase* newProduct) override;
0064 
0065     void do_fillView(ProductID const& id,
0066                      std::vector<void const*>& pointers,
0067                      FillViewHelperVector& helpers) const override;
0068     void do_setPtr(std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) const override;
0069     void do_fillPtrVector(std::type_info const& iToType,
0070                           std::vector<unsigned long> const& iIndices,
0071                           std::vector<void const*>& oPtr) const override;
0072 
0073     std::shared_ptr<soa::TableExaminerBase> tableExaminer_() const override;
0074 
0075   private:
0076     bool present;
0077     T obj;
0078   };
0079 
0080   template <typename T>
0081   Wrapper<T>::Wrapper(std::unique_ptr<T> ptr) : WrapperBase(), present(ptr.get() != nullptr), obj() {
0082     if (present) {
0083       obj = std::move(*ptr);
0084     }
0085   }
0086 
0087   template <typename T>
0088   template <typename... Args>
0089   Wrapper<T>::Wrapper(Emplace, Args&&... args) : WrapperBase(), present(true), obj(std::forward<Args>(args)...) {}
0090 
0091   template <typename T>
0092   Wrapper<T>::Wrapper(T* ptr) : WrapperBase(), present(ptr != 0), obj() {
0093     std::unique_ptr<T> temp(ptr);
0094     if (present) {
0095       obj = std::move(*ptr);
0096     }
0097   }
0098 
0099   template <typename T>
0100   inline std::type_info const& Wrapper<T>::valueTypeInfo_() const {
0101     return detail::getValueType<T>()();
0102   }
0103 
0104   template <typename T>
0105   inline std::type_info const& Wrapper<T>::memberTypeInfo_() const {
0106     return detail::getMemberType<T>()();
0107   }
0108 
0109   template <typename T>
0110   inline bool Wrapper<T>::isMergeable_() const {
0111     return detail::getHasMergeFunction<T>()();
0112   }
0113 
0114   template <typename T>
0115   inline bool Wrapper<T>::mergeProduct_(WrapperBase const* newProduct) {
0116     Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
0117     assert(wrappedNewProduct != nullptr);
0118     return detail::doMergeProduct<T>()(obj, wrappedNewProduct->obj);
0119   }
0120 
0121   template <typename T>
0122   inline bool Wrapper<T>::hasIsProductEqual_() const {
0123     return detail::getHasIsProductEqual<T>()();
0124   }
0125 
0126   template <typename T>
0127   inline bool Wrapper<T>::isProductEqual_(WrapperBase const* newProduct) const {
0128     Wrapper<T> const* wrappedNewProduct = dynamic_cast<Wrapper<T> const*>(newProduct);
0129     assert(wrappedNewProduct != nullptr);
0130     return detail::doIsProductEqual<T>()(obj, wrappedNewProduct->obj);
0131   }
0132 
0133   template <typename T>
0134   inline bool Wrapper<T>::hasSwap_() const {
0135     return detail::getHasSwapFunction<T>()();
0136   }
0137 
0138   template <typename T>
0139   inline void Wrapper<T>::swapProduct_(WrapperBase* newProduct) {
0140     Wrapper<T>* wrappedNewProduct = dynamic_cast<Wrapper<T>*>(newProduct);
0141     assert(wrappedNewProduct != nullptr);
0142     detail::doSwapProduct<T>()(obj, wrappedNewProduct->obj);
0143   }
0144 
0145   namespace soa {
0146     template <class T>
0147     struct MakeTableExaminer {
0148       static std::shared_ptr<edm::soa::TableExaminerBase> make(void const*) {
0149         return std::shared_ptr<edm::soa::TableExaminerBase>{};
0150       }
0151     };
0152   }  // namespace soa
0153   template <typename T>
0154   inline std::shared_ptr<edm::soa::TableExaminerBase> Wrapper<T>::tableExaminer_() const {
0155     return soa::MakeTableExaminer<T>::make(&obj);
0156   }
0157 
0158 }  // namespace edm
0159 
0160 #include "DataFormats/Common/interface/WrapperView.icc"
0161 
0162 #endif