CMS 3D CMS Logo

typelookup.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Utilities
4 // Class : typelookup
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Wed Jan 20 14:26:49 CST 2010
11 //
12 
13 // system include files
14 #include <cstring>
15 #include "tbb/concurrent_unordered_map.h"
16 
17 // user include files
19 
20 //
21 // constants, enums and typedefs
22 //
23 
24 //
25 // static data member definitions
26 //
27 //This class hides the use of map from all classes that use HCTypeTag
28 namespace {
29  struct StringEqual {
30  bool operator()(const char* iLHS, const char* iRHS) const { return (std::strcmp(iLHS, iRHS) == 0); }
31  };
32 
33  //NOTE: The following hash calculation was taken from
34  // the hash calculation for std::string in tbb
35 
37  template <unsigned u, unsigned long long ull>
38  struct select_size_t_constant {
39  //Explicit cast is needed to avoid compiler warnings about possible truncation.
40  //The value of the right size, which is selected by ?:, is anyway not truncated or promoted.
41  static constexpr size_t value = (size_t)((sizeof(size_t) == sizeof(u)) ? u : ull);
42  };
43 
45 
46  struct StringHash {
47  inline size_t operator()(const char* s) const {
48  size_t h = 0;
49  for (const char* c = s; *c; ++c)
50  h = static_cast<size_t>(*c) ^ (h * hash_multiplier);
51  return h;
52  }
53  };
54 
55  //NOTE: the use of const char* does not lead to a memory leak because the data
56  // for the strings are assigned at compile time via a macro call
57  using TypeNameToValueMap = tbb::concurrent_unordered_map<const char*, const std::type_info*, StringHash, StringEqual>;
58 
59  TypeNameToValueMap& typeNameToValueMap() {
60  static TypeNameToValueMap s_map;
61  return s_map;
62  }
63 } // namespace
64 
65 edm::typelookup::NameRegistrar::NameRegistrar(const char* iTypeName, const std::type_info& iInfo) {
66  typeNameToValueMap().insert(std::pair<const char*, const std::type_info*>(iTypeName, &iInfo));
67 }
68 
69 std::pair<const char*, const std::type_info*> edm::typelookup::findType(const char* iTypeName) {
70  auto itFind = typeNameToValueMap().find(iTypeName);
71 
72  if (itFind == typeNameToValueMap().end()) {
73  return std::make_pair(static_cast<const char*>(nullptr), static_cast<std::type_info*>(nullptr));
74  }
75 
76  return (*itFind);
77 }
std::pair< const char *, const std::type_info * > findType(const char *iClassName)
Definition: typelookup.cc:69
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
NameRegistrar(const char *iTypeName, const std::type_info &iInfo)
Definition: typelookup.cc:65
#define end
Definition: vmac.h:39
Definition: value.py:1
#define constexpr