TypeID.h
Go to the documentation of this file.
1 #ifndef canvas_Utilities_TypeID_h
2 #define canvas_Utilities_TypeID_h
3 // vim: set sw=2:
4 
5 //
6 // TypeID: A unique identifier for a C++ type.
7 //
8 // The identifier is unique within an entire program, but cannot be
9 // persisted across invocations of the program.
10 //
11 
12 #include "canvas/Utilities/fwd.h"
13 #include <iosfwd>
14 #include <string>
15 #include <typeinfo>
16 
17 namespace art {
18  class TypeID;
19 
20  bool operator>(TypeID const&, TypeID const&);
21  bool operator!=(TypeID const&, TypeID const&);
22  std::ostream& operator<<(std::ostream&, TypeID const&);
23  void swap(TypeID&, TypeID&);
24 
25  bool is_assns(TypeID const& tid);
26  bool is_assns(std::string const& type_name);
27 
28  bool is_instantiation_of(std::string const& type_name,
29  std::string const& template_name);
30  bool is_instantiation_of(TypeID const& tid, std::string const& template_name);
31 
32  std::string name_of_template_arg(std::string const& template_instance,
33  size_t desired_arg);
37 }
38 
39 class art::TypeID {
40 public:
41  TypeID() = default;
42 
43  explicit TypeID(std::type_info const&);
44 
45  explicit TypeID(std::type_info const*);
46 
47  template <typename T>
48  explicit TypeID(T const& val);
49 
50  // Print out the name of the type, using the reflection class name.
51  void print(std::ostream&) const;
52 
53  // Returned C-style string owned by system; do not delete[] it.
54  // This is the (horrible, mangled, platform-dependent) name of the type.
55  char const* name() const;
56 
57  std::string className() const;
58 
60 
61  bool operator<(TypeID const& rhs) const;
62 
63  bool operator==(TypeID const& rhs) const;
64 
65  // Are we valid?
66  explicit operator bool() const;
67 
68  // Access the typeinfo.
69  std::type_info const& typeInfo() const;
70 
71  void swap(TypeID& other);
72 
73 private:
74  // NOTE: since (a) the compiler generates the type_infos, and (b)
75  // they have a lifetime good for the entire application, we do not
76  // have to delete it.
77  // We use a pointer rather than a reference so that assignment will
78  // work
79  std::type_info const* ti_{nullptr};
80 };
81 
82 inline art::TypeID::TypeID(std::type_info const& ti) : ti_{&ti} {}
83 
84 inline art::TypeID::TypeID(std::type_info const* ti) : ti_{ti} {}
85 
86 template <typename T>
87 inline art::TypeID::TypeID(T const& val) : ti_{&typeid(val)}
88 {}
89 
90 inline char const*
92 {
93  return ti_->name();
94 }
95 
96 inline bool
97 art::TypeID::operator<(TypeID const& rhs) const
98 {
99  return ti_->before(*rhs.ti_);
100 }
101 
102 inline bool
104 {
105  return *ti_ == *rhs.ti_;
106 }
107 
108 inline art::TypeID::operator bool() const
109 {
110  return ti_ != nullptr;
111 }
112 
113 inline std::type_info const&
115 {
116  return *ti_;
117 }
118 
119 inline void
121 {
122  using std::swap;
123  swap(ti_, other.ti_);
124 }
125 
126 inline bool
127 art::operator>(TypeID const& a, TypeID const& b)
128 {
129  return b < a;
130 }
131 
132 inline bool
133 art::operator!=(TypeID const& a, TypeID const& b)
134 {
135  return !(a == b);
136 }
137 
138 inline void
140 {
141  left.swap(right);
142 }
143 
144 inline bool
146 {
147  return is_assns(tid.className());
148 }
149 
150 inline bool
151 art::is_assns(std::string const& type_name)
152 {
153  using namespace std::string_literals;
154  return is_instantiation_of(type_name, "art::Assns"s);
155 }
156 
157 inline bool
159  std::string const& template_name)
160 {
161  return type_name.find(template_name + '<') == 0ull;
162 }
163 
164 inline bool
165 art::is_instantiation_of(TypeID const& tid, std::string const& template_name)
166 {
167  return is_instantiation_of(tid.className(), template_name);
168 }
169 
170 // Local Variables:
171 // mode: c++
172 // End:
173 #endif /* canvas_Utilities_TypeID_h */
std::ostream & operator<<(std::ostream &os, EDAnalyzer::Table< T > const &t)
Definition: EDAnalyzer.h:184
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
std::string friendlyClassName() const
std::string className() const
std::string name_of_assns_partner(std::string assns_type_name)
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
bool operator>(ScheduleID left, ScheduleID right)
Definition: ScheduleID.h:136
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
void print(std::ostream &) const
const XML_Char * s
Definition: expat.h:262
std::string name_of_unwrapped_product(std::string const &wrapped_name)
void swap(Handle< T > &a, Handle< T > &b)
const double a
std::string name_of_assns_base(std::string assns_type_name)
bool operator<(TypeID const &rhs) const
Definition: TypeID.h:97
bool is_instantiation_of(std::string const &type_name, std::string const &template_name)
Definition: TypeID.h:158
bool operator==(TypeID const &rhs) const
Definition: TypeID.h:103
std::type_info const & typeInfo() const
Definition: TypeID.h:114
char const * name() const
Definition: TypeID.h:91
bool is_assns(TypeID const &tid)
Definition: TypeID.h:145
TypeID()=default
void swap(TypeID &other)
Definition: TypeID.h:120
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
const hit & b
Definition: hits.cxx:21
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
double T
Definition: Xdiff_gwt.C:5
std::string name_of_template_arg(std::string const &template_instance, size_t desired_arg)
std::type_info const * ti_
Definition: TypeID.h:79