CMS 3D CMS Logo

TypeID.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 
3 ----------------------------------------------------------------------*/
4 #include <cassert>
5 #include <ostream>
6 #include "tbb/concurrent_unordered_map.h"
11 
12 namespace edm {
13  void TypeID::print(std::ostream& os) const {
14  try {
15  os << className();
16  } catch (cms::Exception const&) {
17  os << typeInfo().name();
18  }
19  }
20 
21  namespace {
22 
23  TypeID const nullTypeID;
24 
25  std::string typeToClassName(std::type_info const& iType) {
26  try {
27  return typeDemangle(iType.name());
28  } catch (cms::Exception const& e) {
29  cms::Exception theError("Name Demangling Error");
30  theError << "TypeID::typeToClassName: can't demangle " << iType.name() << '\n';
31  theError.append(e);
32  throw theError;
33  }
34  }
35  } // namespace
36  struct TypeIDHasher {
37  size_t operator()(TypeID const& tid) const {
38  tbb::tbb_hash<std::string> hasher;
39  return hasher(std::string(tid.name()));
40  }
41  };
42 
43  std::string const& TypeID::className() const {
44  typedef tbb::concurrent_unordered_map<edm::TypeID, std::string, TypeIDHasher> Map;
45  static Map s_typeToName;
46 
47  auto itFound = s_typeToName.find(*this);
48  if (s_typeToName.end() == itFound) {
49  itFound = s_typeToName.insert(Map::value_type(*this, typeToClassName(typeInfo()))).first;
50  }
51  return itFound->second;
52  }
53 
55  std::string theName = className();
56  if (theName.find("edm::Wrapper") == 0) {
57  stripTemplate(theName);
58  }
59  return theName;
60  }
61 
63 
64  bool stripTemplate(std::string& theName) {
65  std::string const spec("<,>");
66  char const space = ' ';
67  std::string::size_type idx = theName.find_first_of(spec);
68  if (idx == std::string::npos) {
69  return false;
70  }
73  if (theName[idx] == '<') {
74  after = theName.rfind('>');
75  assert(after != std::string::npos);
76  first = ++idx;
77  } else {
78  theName = theName.substr(0, idx);
79  }
80  std::string::size_type idxa = after;
81  while (space == theName[--idxa])
82  --after;
84  while (space == theName[idxf++])
85  ++first;
86  theName = theName.substr(first, after - first);
87  return true;
88  }
89 
91  // Find last colon
92  std::string::size_type colonIndex = theName.rfind(':');
93  if (colonIndex == std::string::npos) {
94  // No colons, so no namespace to strip
95  return theName;
96  }
97  std::string::size_type bracketIndex = theName.rfind('>');
98  if (bracketIndex == std::string::npos || bracketIndex < colonIndex) {
99  // No '>' after last colon. Strip up to and including last colon.
100  return theName.substr(colonIndex + 1);
101  }
102  // There is a '>' after the last colon.
103  int depth = 1;
104  for (size_t index = bracketIndex; index != 0; --index) {
105  char c = theName[index - 1];
106  if (c == '>') {
107  ++depth;
108  } else if (c == '<') {
109  --depth;
110  assert(depth >= 0);
111  } else if (depth == 0 && c == ':') {
112  return theName.substr(index);
113  }
114  }
115  return theName;
116  }
117 
118  TypeID::operator bool() const { return !(*this == nullTypeID); }
119 
120  std::ostream& operator<<(std::ostream& os, TypeID const& id) {
121  id.print(os);
122  return os;
123  }
124 } // namespace edm
void print(std::ostream &os) const
Definition: TypeID.cc:13
void append(Exception const &another)
Definition: Exception.cc:153
const std::type_info & typeInfo() const
Definition: TypeIDBase.h:50
uint16_t size_type
size_t operator()(TypeID const &tid) const
Definition: TypeID.cc:37
std::string friendlyName(std::string const &iFullName)
std::string userClassName() const
Definition: TypeID.cc:54
std::string typeDemangle(char const *mangledName)
std::string stripNamespace(std::string const &theName)
Definition: TypeID.cc:90
HLT enums.
std::string const & className() const
Definition: TypeID.cc:43
T first(std::pair< T, U > const &p)
std::ostream & operator<<(std::ostream &ost, const HLTGlobalStatus &hlt)
Formatted printout of trigger tbale.
bool stripTemplate(std::string &theName)
Definition: TypeID.cc:64
const char * name() const
Definition: TypeIDBase.h:44
std::string friendlyClassName() const
Definition: TypeID.cc:62