CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 //
22 // constants, enums and typedefs
23 //
24 
25 //
26 // static data member definitions
27 //
28 //This class hides the use of map from all classes that use HCTypeTag
29 namespace {
30  struct StringEqual {
31  bool operator()(const char* iLHS, const char* iRHS) const {
32  return (std::strcmp(iLHS, iRHS) == 0);
33  }
34 
35  };
36 
37  //NOTE: The following hash calculation was taken from
38  // the hash calculation for std::string in tbb
39 
41  template <unsigned u, unsigned long long ull >
42  struct select_size_t_constant {
43  //Explicit cast is needed to avoid compiler warnings about possible truncation.
44  //The value of the right size, which is selected by ?:, is anyway not truncated or promoted.
45  static constexpr size_t value = (size_t)((sizeof(size_t)==sizeof(u)) ? u : ull);
46  };
47 
49 
50  struct StringHash {
51  inline size_t operator()( const char* s) const {
52  size_t h = 0;
53  for( const char* c = s; *c; ++c )
54  h = static_cast<size_t>(*c) ^ (h * hash_multiplier);
55  return h;
56  }
57  };
58 
59  //NOTE: the use of const char* does not lead to a memory leak because the data
60  // for the strings are assigned at compile time via a macro call
61  using TypeNameToValueMap = tbb::concurrent_unordered_map<const char*, const std::type_info*, StringHash, StringEqual>;
62 
63  static TypeNameToValueMap& typeNameToValueMap() {
64  static TypeNameToValueMap s_map;
65  return s_map;
66  }
67 }
68 
69 edm::typelookup::NameRegistrar::NameRegistrar(const char* iTypeName,const std::type_info& iInfo)
70 {
71  typeNameToValueMap().insert(std::pair<const char*, const std::type_info*>(iTypeName,&iInfo));
72 }
73 
74 
75 std::pair<const char*, const std::type_info*>
76 edm::typelookup::findType(const char* iTypeName) {
77 
78  auto itFind = typeNameToValueMap().find(iTypeName);
79 
80  if(itFind == typeNameToValueMap().end()) {
81  return std::make_pair(static_cast<const char*>(0), static_cast<std::type_info*> (0));
82  }
83 
84  return (*itFind);
85 }
std::pair< const char *, const std::type_info * > findType(const char *iClassName)
Definition: typelookup.cc:76
#define constexpr
NameRegistrar(const char *iTypeName, const std::type_info &iInfo)
Definition: typelookup.cc:69
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
#define end
Definition: vmac.h:37