CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
14  TypeID::print(std::ostream& os) const {
15  try {
16  os << className();
17  } catch (cms::Exception const&) {
18  os << typeInfo().name();
19  }
20  }
21 
22 namespace {
23 
24  TypeID const nullTypeID;
25 
26  std::string typeToClassName(std::type_info const& iType) {
27  try {
28  return typeDemangle(iType.name());
29  } catch (cms::Exception const& e) {
30  cms::Exception theError("Name Demangling Error");
31  theError << "TypeID::typeToClassName: can't demangle " << iType.name() << '\n';
32  theError.append(e);
33  throw theError;
34  }
35  }
36 }
37  struct TypeIDHasher {
38  size_t operator()(TypeID const& tid) const {
39  tbb::tbb_hash<std::string> hasher;
40  return hasher(std::string(tid.name()));
41  }
42  };
43 
44 
45  std::string const&
47  typedef tbb::concurrent_unordered_map<edm::TypeID, std::string, TypeIDHasher> Map;
48  static Map s_typeToName;
49 
50  auto itFound = s_typeToName.find(*this);
51  if(s_typeToName.end() == itFound) {
52  itFound = s_typeToName.insert(Map::value_type(*this, typeToClassName(typeInfo()))).first;
53  }
54  return itFound->second;
55  }
56 
59  std::string theName = className();
60  if (theName.find("edm::Wrapper") == 0) {
61  stripTemplate(theName);
62  }
63  return theName;
64  }
65 
69  }
70 
71  bool
73  std::string const spec("<,>");
74  char const space = ' ';
75  std::string::size_type idx = theName.find_first_of(spec);
76  if (idx == std::string::npos) {
77  return false;
78  }
81  if (theName[idx] == '<') {
82  after = theName.rfind('>');
83  assert (after != std::string::npos);
84  first = ++idx;
85  } else {
86  theName = theName.substr(0, idx);
87  }
88  std::string::size_type idxa = after;
89  while (space == theName[--idxa]) --after;
91  while (space == theName[idxf++]) ++first;
92  theName = theName.substr(first, after - first);
93  return true;
94  }
95 
97  stripNamespace(std::string const& theName) {
98  // Find last colon
99  std::string::size_type colonIndex = theName.rfind(':');
100  if(colonIndex == std::string::npos) {
101  // No colons, so no namespace to strip
102  return theName;
103  }
104  std::string::size_type bracketIndex = theName.rfind('>');
105  if(bracketIndex == std::string::npos || bracketIndex < colonIndex) {
106  // No '>' after last colon. Strip up to and including last colon.
107  return theName.substr(colonIndex+1);
108  }
109  // There is a '>' after the last colon.
110  int depth = 1;
111  for(size_t index = bracketIndex; index != 0; --index) {
112  char c = theName[index - 1];
113  if(c == '>') {
114  ++depth;
115  } else if(c == '<') {
116  --depth;
117  assert(depth >= 0);
118  } else if(depth == 0 && c == ':') {
119  return theName.substr(index);
120  }
121  }
122  return theName;
123  }
124 
125  TypeID::operator bool() const {
126  return !(*this == nullTypeID);
127  }
128 
129 
130  std::ostream&
131  operator<<(std::ostream& os, TypeID const& id) {
132  id.print(os);
133  return os;
134  }
135 }
136 
void print(std::ostream &os) const
Definition: TypeID.cc:14
assert(m_qm.get())
const std::type_info & typeInfo() const
Definition: TypeIDBase.h:58
uint16_t size_type
size_t operator()(TypeID const &tid) const
Definition: TypeID.cc:38
std::string friendlyName(std::string const &iFullName)
Container::value_type value_type
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
std::string userClassName() const
Definition: TypeID.cc:58
std::string typeDemangle(char const *mangledName)
std::string stripNamespace(std::string const &theName)
Definition: TypeID.cc:97
std::string const & className() const
Definition: TypeID.cc:46
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:72
const char * name() const
Definition: TypeIDBase.h:52
std::string friendlyClassName() const
Definition: TypeID.cc:67