Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:54

0001 // -*- C++ -*-
0002 //
0003 // Package:     Utilities
0004 // Class  :     typelookup
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Wed Jan 20 14:26:49 CST 2010
0011 //
0012 
0013 // system include files
0014 #include <cstring>
0015 #include "oneapi/tbb/concurrent_unordered_map.h"
0016 
0017 // user include files
0018 #include "FWCore/Utilities/interface/typelookup.h"
0019 
0020 //
0021 // constants, enums and typedefs
0022 //
0023 
0024 //
0025 // static data member definitions
0026 //
0027 //This class hides the use of map from all classes that use HCTypeTag
0028 namespace {
0029   struct StringEqual {
0030     bool operator()(const char* iLHS, const char* iRHS) const { return (std::strcmp(iLHS, iRHS) == 0); }
0031   };
0032 
0033   //NOTE: The following hash calculation was taken from
0034   // the hash calculation for std::string in tbb
0035 
0036   //! A template to select either 32-bit or 64-bit constant as compile time, depending on machine word size.
0037   template <unsigned u, unsigned long long ull>
0038   struct select_size_t_constant {
0039     //Explicit cast is needed to avoid compiler warnings about possible truncation.
0040     //The value of the right size,   which is selected by ?:, is anyway not truncated or promoted.
0041     static constexpr size_t value = (size_t)((sizeof(size_t) == sizeof(u)) ? u : ull);
0042   };
0043 
0044   constexpr size_t hash_multiplier = select_size_t_constant<2654435769U, 11400714819323198485ULL>::value;
0045 
0046   struct StringHash {
0047     inline size_t operator()(const char* s) const {
0048       size_t h = 0;
0049       for (const char* c = s; *c; ++c)
0050         h = static_cast<size_t>(*c) ^ (h * hash_multiplier);
0051       return h;
0052     }
0053   };
0054 
0055   //NOTE: the use of const char* does not lead to a memory leak because the data
0056   // for the strings are assigned at compile time via a macro call
0057   using TypeNameToValueMap =
0058       oneapi::tbb::concurrent_unordered_map<const char*, const std::type_info*, StringHash, StringEqual>;
0059 
0060   TypeNameToValueMap& typeNameToValueMap() {
0061     static TypeNameToValueMap s_map;
0062     return s_map;
0063   }
0064 }  // namespace
0065 
0066 edm::typelookup::NameRegistrar::NameRegistrar(const char* iTypeName, const std::type_info& iInfo) {
0067   typeNameToValueMap().insert(std::pair<const char*, const std::type_info*>(iTypeName, &iInfo));
0068 }
0069 
0070 std::pair<const char*, const std::type_info*> edm::typelookup::findType(const char* iTypeName) {
0071   auto itFind = typeNameToValueMap().find(iTypeName);
0072 
0073   if (itFind == typeNameToValueMap().end()) {
0074     return std::make_pair(static_cast<const char*>(nullptr), static_cast<std::type_info*>(nullptr));
0075   }
0076 
0077   return (*itFind);
0078 }