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 expandtab :
4 
5 //
6 // TypeID: A unique identifier for a C++ type.
7 //
8 // The identifier is unique within an entire program (not really,
9 // it is supposed to be, but gcc violates the standard requirement
10 // that a type_info should have a unique address for performance
11 // reasons) but cannot be persisted across invocations of the program.
12 //
13 
14 #include "hep_concurrency/RecursiveMutex.h"
15 #include "hep_concurrency/tsan.h"
16 
17 #include <iosfwd>
18 #include <map>
19 #include <string>
20 #include <typeinfo>
21 #include <utility>
22 
23 namespace art {
24 
25  class TypeID {
26 
27  // STATIC MEMBER FUNCTIONS
28  public:
29  static void startup();
30  static void shutdown();
31 
32  // STATIC DATA MEMBERS
33  private:
34  static hep::concurrency::RecursiveMutex* s_mutex;
35  static std::map<size_t, std::string>* s_nameMap;
36 
37  // MEMBER FUNCTIONS -- Special Member Functions
38  public:
39  ~TypeID() noexcept;
40  TypeID() noexcept;
41  explicit TypeID(std::type_info const&) noexcept;
42  explicit TypeID(std::type_info const*) noexcept;
43  template <typename T>
44  explicit TypeID(T const& val) noexcept;
45  TypeID(TypeID const&) noexcept;
46  TypeID(TypeID&) noexcept;
47  TypeID& operator=(TypeID const&) noexcept;
48  TypeID& operator=(TypeID&) noexcept;
49 
50  // MEMBER FUNCTIONS -- API for the user
51  public:
52  std::type_info const& typeInfo() const;
53  char const* name() const;
54  std::string className() const;
55  std::string friendlyClassName() const;
56  bool operator<(TypeID const&) const;
57  bool operator==(TypeID const&) const;
58  explicit operator bool() const;
59  void swap(TypeID&);
60  void print(std::ostream&) const;
61 
62  // MEMBER DATA
63  private:
64  std::type_info const* ti_{nullptr};
65  };
66 
67  template <typename T>
68  TypeID::TypeID(T const& val) noexcept : ti_{&typeid(val)}
69  {}
70 
71  inline bool
72  is_instantiation_of(std::string const& type_name,
73  std::string const& template_name)
74  {
75  return type_name.find(template_name + '<') == 0ull;
76  }
77 
78  inline bool
79  is_instantiation_of(TypeID const& tid, std::string const& template_name)
80  {
81  return is_instantiation_of(tid.className(), template_name);
82  }
83 
84  inline bool
85  is_assns(std::string const& type_name)
86  {
87  using namespace std::string_literals;
88  return is_instantiation_of(type_name, "art::Assns"s);
89  }
90 
91  inline bool
92  is_assns(TypeID const& tid)
93  {
94  return is_assns(tid.className());
95  }
96 
97  bool operator>(TypeID const&, TypeID const&);
98  bool operator!=(TypeID const&, TypeID const&);
99  std::ostream& operator<<(std::ostream&, TypeID const&);
100  void swap(TypeID&, TypeID&);
101  std::string name_of_template_arg(std::string const& template_instance,
102  size_t desired_arg);
104  std::string name_of_assns_base(std::string assns_type_name);
106 
107 } // namespace art
108 
109 // Local Variables:
110 // mode: c++
111 // End:
112 #endif /* canvas_Utilities_TypeID_h */
const XML_Char * name
Definition: expat.h:151
static hep::concurrency::RecursiveMutex * s_mutex
Definition: TypeID.h:34
std::string friendlyClassName() const
bool operator>(ScheduleID left, ScheduleID right) noexcept
std::string className() const
std::string name_of_assns_partner(std::string assns_type_name)
TypeID() noexcept
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
void print(std::ostream &) const
const XML_Char * s
Definition: expat.h:262
std::string name_of_unwrapped_product(std::string const &wrapped_name)
std::ostream & operator<<(std::ostream &os, const GroupSelector &gs)
std::string name_of_assns_base(std::string assns_type_name)
bool is_instantiation_of(std::string const &type_name, std::string const &template_name)
Definition: TypeID.h:72
static void shutdown()
void swap(TypeID &)
static std::map< size_t, std::string > * s_nameMap
Definition: TypeID.h:35
static void startup()
std::type_info const & typeInfo() const
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)
bool is_assns(std::string const &type_name)
Definition: TypeID.h:85
std::type_info const * ti_
Definition: TypeID.h:64
~TypeID() noexcept
enum BeamMode string