LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TypeID.cc
Go to the documentation of this file.
2 
6 #include "tbb/concurrent_unordered_map.h"
7 
8 #include <cstddef>
9 #include <ostream>
10 #include <string>
11 
12 using namespace std;
13 
14 void
15 art::TypeID::print(ostream& os) const
16 {
17  os << className();
18 }
19 
20 string
22 {
23  static tbb::concurrent_unordered_map<size_t, string> s_nameMap;
24  auto hash_code = typeInfo().hash_code();
25  auto entry = s_nameMap.find(hash_code);
26  if (entry == s_nameMap.end()) {
27  entry = s_nameMap.emplace(hash_code, uniform_type_name(typeInfo())).first;
28  }
29  return entry->second;
30 }
31 
32 string
34 {
35  return friendlyname::friendlyName(className());
36 }
37 
38 ostream&
39 art::operator<<(ostream& os, TypeID const& tid)
40 {
41  tid.print(os);
42  return os;
43 }
44 
45 std::string
46 art::name_of_template_arg(std::string const& template_instance,
47  size_t desired_arg)
48 {
49  std::string result;
50  auto comma_count = 0ul;
51  auto template_level = 0ul;
52  auto arg_start = string::npos;
53  auto pos = 0ul;
54  pos = template_instance.find_first_of("<>,", pos);
55  while (pos != string::npos) {
56  switch (template_instance[pos]) {
57  case '<':
58  ++template_level;
59  if ((desired_arg == 0ul) && (template_level == 1ul)) {
60  // Found the begin of the desired template arg.
61  arg_start = pos + 1;
62  }
63  break;
64  case '>':
65  --template_level;
66  if ((desired_arg == comma_count) && (template_level == 0ul)) {
67  // Found the end of the desired template arg -- trim trailing
68  // whitespace
69  auto const arg_end =
70  template_instance.find_last_not_of(" \t", pos - 1) + 1;
71  result = template_instance.substr(arg_start, arg_end - arg_start);
72  return result;
73  }
74  break;
75  case ',':
76  if (template_level != 1ul) {
77  // Ignore arguments not at the first level.
78  break;
79  }
80  if (comma_count == desired_arg) {
81  // Found the end of the desired template arg.
82  result = template_instance.substr(arg_start, pos - arg_start);
83  return result;
84  }
85  ++comma_count;
86  if (comma_count == desired_arg) {
87  // Found the begin of the desired template arg.
88  arg_start = pos + 1;
89  }
90  break;
91  }
92  ++pos;
93  pos = template_instance.find_first_of("<>,", pos);
94  }
95  return result;
96 }
97 
98 std::string
99 art::name_of_assns_partner(std::string assns_type_name)
100 {
101  std::string result;
102  if (!is_assns(assns_type_name)) {
103  return result;
104  }
105  static std::string const assns_start = "art::Assns<"s;
106  auto const arg0 = name_of_template_arg(assns_type_name, 0);
107  auto const arg1 = name_of_template_arg(assns_type_name, 1);
108  auto const arg2 = name_of_template_arg(assns_type_name, 2);
109  result = assns_start + arg1 + ',' + arg0 + ',' + arg2 + '>';
110  return result;
111 }
112 
113 std::string
114 art::name_of_assns_base(std::string assns_type_name)
115 {
116  std::string result;
117  if (!is_assns(assns_type_name)) {
118  return result;
119  }
120  using namespace std::string_literals;
121  static std::string const assns_start = "art::Assns<"s;
122  if (name_of_template_arg(assns_type_name, 2) == "void"s) {
123  // Doesn't have the base we're looking for.
124  return result;
125  }
126  result = assns_start + name_of_template_arg(assns_type_name, 0) + ',' +
127  name_of_template_arg(assns_type_name, 1) + ",void>";
128  return result;
129 }
130 
131 std::string
132 art::name_of_unwrapped_product(std::string const& wrapped_name)
133 {
134  using namespace std::string_literals;
135  if (!is_instantiation_of(wrapped_name, "art::Wrapper"s)) {
136  throw Exception(errors::LogicError, "Can't unwrap"s)
137  << "-- attempted to get unwrapped product from non-instance of art::Wrapper."s;
138  }
139  return name_of_template_arg(wrapped_name, 0);
140 }
std::ostream & operator<<(std::ostream &os, EDAnalyzer::Table< T > const &t)
Definition: EDAnalyzer.h:184
Float_t s
Definition: plot.C:23
std::string friendlyClassName() const
Definition: TypeID.cc:33
STL namespace.
std::string name_of_assns_partner(std::string assns_type_name)
Definition: TypeID.cc:99
void print(std::ostream &) const
Definition: TypeID.cc:15
std::string uniform_type_name(std::type_info const &tid)
std::string name_of_unwrapped_product(std::string const &wrapped_name)
Definition: TypeID.cc:132
std::string name_of_assns_base(std::string assns_type_name)
Definition: TypeID.cc:114
bool is_instantiation_of(std::string const &type_name, std::string const &template_name)
Definition: TypeID.h:158
bool is_assns(TypeID const &tid)
Definition: TypeID.h:145
std::string className() const
Definition: TypeID.cc:21
std::string friendlyName(std::string const &iFullName)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string name_of_template_arg(std::string const &template_instance, size_t desired_arg)
Definition: TypeID.cc:46