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 <iosfwd>
15 #include <string>
16 #include <typeinfo>
17 
18 namespace art {
19 
20  class TypeID {
21  public:
22  ~TypeID() noexcept;
23  TypeID() noexcept;
24  explicit TypeID(std::type_info const&) noexcept;
25  explicit TypeID(std::type_info const*) noexcept;
26 
27  template <typename T>
28  explicit TypeID(T const& val) noexcept;
29  TypeID(TypeID const&) noexcept;
30  TypeID(TypeID&) noexcept;
31  TypeID& operator=(TypeID const&) noexcept;
32  TypeID& operator=(TypeID&) noexcept;
33 
34  std::type_info const& typeInfo() const;
35  char const* name() const;
36  std::string className() const;
38  bool operator<(TypeID const&) const;
39  bool operator==(TypeID const&) const;
40  explicit operator bool() const;
41  void swap(TypeID&);
42  void print(std::ostream&) const;
43 
44  private:
45  std::type_info const* ti_{nullptr};
46  };
47 
48  template <typename T>
49  TypeID::TypeID(T const& val) noexcept : ti_{&typeid(val)}
50  {}
51 
52  inline bool
53  is_instantiation_of(std::string const& type_name,
54  std::string const& template_name)
55  {
56  return type_name.find(template_name + '<') == 0ull;
57  }
58 
59  inline bool
60  is_instantiation_of(TypeID const& tid, std::string const& template_name)
61  {
62  return is_instantiation_of(tid.className(), template_name);
63  }
64 
65  inline bool
66  is_assns(std::string const& type_name)
67  {
68  using namespace std::string_literals;
69  return is_instantiation_of(type_name, "art::Assns"s);
70  }
71 
72  inline bool
73  is_assns(TypeID const& tid)
74  {
75  return is_assns(tid.className());
76  }
77 
78  bool operator>(TypeID const&, TypeID const&);
79  bool operator!=(TypeID const&, TypeID const&);
80  std::ostream& operator<<(std::ostream&, TypeID const&);
81  void swap(TypeID&, TypeID&);
82  std::string name_of_template_arg(std::string const& template_instance,
83  size_t desired_arg);
87 
88 } // namespace art
89 
90 // Local Variables:
91 // mode: c++
92 // End:
93 #endif /* canvas_Utilities_TypeID_h */
TypeID & operator=(TypeID const &) noexcept
bool operator>(ScheduleID const left, ScheduleID const right) noexcept
Definition: ScheduleID.cc:53
std::string friendlyClassName() const
Definition: TypeID.cc:61
std::string string
Definition: nybbler.cc:12
bool operator==(TypeID const &) const
Definition: TypeID.cc:73
bool operator<(TypeID const &) const
Definition: TypeID.cc:67
TypeID() noexcept
string name_of_assns_partner(string assns_type_name)
Definition: TypeID.cc:170
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
string name_of_unwrapped_product(string const &wrapped_name)
Definition: TypeID.cc:203
void print(std::ostream &) const
Definition: TypeID.cc:87
std::ostream & operator<<(std::ostream &os, const GroupSelector &gs)
string name_of_assns_base(string assns_type_name)
Definition: TypeID.cc:185
bool is_instantiation_of(std::string const &type_name, std::string const &template_name)
Definition: TypeID.h:53
char const * name() const
Definition: TypeID.cc:42
std::string className() const
Definition: TypeID.cc:48
string name_of_template_arg(string const &template_instance, size_t desired_arg)
Definition: TypeID.cc:118
void swap(TypeID &)
Definition: TypeID.cc:81
int bool
Definition: qglobal.h:345
static QCString * s
Definition: config.cpp:1042
bool is_assns(std::string const &type_name)
Definition: TypeID.h:66
std::type_info const & typeInfo() const
Definition: TypeID.cc:36
std::type_info const * ti_
Definition: TypeID.h:45
~TypeID() noexcept