CMS 3D CMS Logo

UserData.h
Go to the documentation of this file.
1 #ifndef DataFormats_PatCandidates_UserData_h
2 #define DataFormats_PatCandidates_UserData_h
3 
16 #include <string>
17 #include <vector>
18 #include <typeinfo>
20 
21 namespace pat {
22 
23  class UserData {
24  public:
25  UserData() {}
26  virtual ~UserData() {}
27 
29  virtual UserData *clone() const = 0;
30 
32  virtual const std::type_info &typeId() const = 0;
34  virtual const std::string &typeName() const = 0;
35 
37  /* I don't think there is an easy way to get it working with generic type T,
38  barrying the use of ROOT::Reflex and all the edm::Ptr technicalities.
39  I'd say we can live without polymorphic storage of polymorphic data */
40  template <typename T>
41  const T *get() const {
42  if (typeid(T) != typeId())
43  return nullptr;
44  return static_cast<const T *>(data_());
45  }
46 
49  // Really needed for CINT? I would really like to avoid this
50  const void *bareData() const { return data_(); }
51 
54  template <typename T>
55  static std::unique_ptr<UserData> make(const T &value, bool transientOnly = false);
56 
57  protected:
59  virtual const void *data_() const = 0;
60  static std::string typeNameFor(std::type_info const &iInfo);
61 
62  private:
63  static void checkDictionaries(const std::type_info &type);
64  };
65 
66  template <typename T>
67  class UserHolder : public UserData {
68  public:
69  UserHolder() : obj_() {}
70  UserHolder(const T &data) : obj_(data) {}
72  UserHolder<T> *clone() const override { return new UserHolder<T>(*this); }
74  const std::type_info &typeId() const override { return typeid(T); }
76  const std::string &typeName() const override { return typeName_(); }
77 
78  protected:
79  const void *data_() const override { return &obj_; }
80 
81  private:
83  static const std::string &typeName_();
84  };
85 
87 } // namespace pat
88 
89 template <typename T>
90 std::unique_ptr<pat::UserData> pat::UserData::make(const T &value, bool transientOnly) {
91  if (!transientOnly) {
92  checkDictionaries(typeid(T));
94  }
95  return std::unique_ptr<UserData>(new pat::UserHolder<T>(value));
96 }
97 
98 template <typename T>
100  static const std::string name(typeNameFor(typeid(T)));
101  return name;
102 }
103 
104 #endif
type
Definition: HCALResponse.h:21
UserHolder(const T &data)
Definition: UserData.h:70
UserHolder< T > * clone() const override
Clone.
Definition: UserData.h:72
static std::unique_ptr< UserData > make(const T &value, bool transientOnly=false)
Definition: HeavyIon.h:7
const std::string & typeName() const override
Human readable name of the concrete type of stored data.
Definition: UserData.h:76
virtual const std::type_info & typeId() const =0
Concrete type of stored data.
virtual const void * data_() const =0
Get out the data (can&#39;t template non virtual functions)
virtual const std::string & typeName() const =0
Human readable name of the concrete type of stored data.
const std::type_info & typeId() const override
Concrete type of stored data.
Definition: UserData.h:74
Definition: value.py:1
virtual UserData * clone() const =0
Necessary for deep copy in OwnVector.
static std::string typeNameFor(std::type_info const &iInfo)
Definition: UserData.cc:28
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
const void * data_() const override
Get out the data (can&#39;t template non virtual functions)
Definition: UserData.h:79
edm::OwnVector< pat::UserData > UserDataCollection
Definition: UserData.h:86
Base class for data that users can add to pat objects.
Definition: UserData.h:23
static const std::string & typeName_()
Definition: UserData.h:99
long double T
virtual ~UserData()
Definition: UserData.h:26
const void * bareData() const
Definition: UserData.h:50
static void checkDictionaries(const std::type_info &type)
Definition: UserData.cc:7