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
pat::UserData::typeId
virtual const std::type_info & typeId() const =0
Concrete type of stored data.
pat::UserData
Base class for data that users can add to pat objects.
Definition: UserData.h:23
pat::UserData::clone
virtual UserData * clone() const =0
Necessary for deep copy in OwnVector.
pat::UserData::checkDictionaries
static void checkDictionaries(const std::type_info &type)
Definition: UserData.cc:7
pat::UserHolder::UserHolder
UserHolder(const T &data)
Definition: UserData.h:70
pat::UserData::~UserData
virtual ~UserData()
Definition: UserData.h:26
pat::UserHolder::UserHolder
UserHolder()
Definition: UserData.h:69
pat::UserDataCollection
edm::OwnVector< pat::UserData > UserDataCollection
Definition: UserData.h:86
pat::UserData::typeName
virtual const std::string & typeName() const =0
Human readable name of the concrete type of stored data.
pat::UserData::UserData
UserData()
Definition: UserData.h:25
pat::UserHolder::typeName
const std::string & typeName() const override
Human readable name of the concrete type of stored data.
Definition: UserData.h:76
pat::UserData::get
const T * get() const
Extract data in a typesafe way. <T> must be the concrete type of the data.
Definition: UserData.h:41
pat::UserData::typeNameFor
static std::string typeNameFor(std::type_info const &iInfo)
Definition: UserData.cc:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pat::UserHolder::data_
const void * data_() const override
Get out the data (can't template non virtual functions)
Definition: UserData.h:79
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
value
Definition: value.py:1
pat::UserData::data_
virtual const void * data_() const =0
Get out the data (can't template non virtual functions)
pat
Definition: HeavyIon.h:7
pat::UserHolder::clone
UserHolder< T > * clone() const override
Clone.
Definition: UserData.h:72
T
long double T
Definition: Basic3DVectorLD.h:48
pat::UserData::make
static std::unique_ptr< UserData > make(const T &value, bool transientOnly=false)
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
pat::UserHolder::obj_
T obj_
Definition: UserData.h:82
pat::UserData::bareData
const void * bareData() const
Definition: UserData.h:50
OwnVector.h
pat::UserHolder::typeName_
static const std::string & typeName_()
Definition: UserData.h:99
pat::UserHolder::typeId
const std::type_info & typeId() const override
Concrete type of stored data.
Definition: UserData.h:74
edm::OwnVector< pat::UserData >
pat::UserHolder
Definition: UserData.h:67