ATLAS Offline Software
normalizedTypeinfoName.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
18 #include "CxxUtils/ClassName.h"
20 #include <unordered_map>
21 #include <functional>
22 
23 
24 namespace {
25 
26 
28 {
30 
31  // STL container types that Reflex knows about.
32  rules.add ("std::deque<$T, std::allocator<$T> >",
33  "std::deque<$T>");
34  rules.add ("std::list<$T, std::allocator<$T> >",
35  "std::list<$T>");
36  rules.add ("std::vector<$T, std::allocator<$T> >",
37  "std::vector<$T>");
38  rules.add ("std::map<$KEY, $T, std::less<$KEY>, std::allocator<std::pair<const $KEY, $T> > >",
39  "std::map<$KEY, $T>");
40  rules.add ("std::multimapmap<$KEY, $T, std::less<$KEY>, std::allocator<std::pair<const $KEY, $T> > >",
41  "std::multimapmap<$KEY, $T>");
42  rules.add ("std::set<$KEY, std::less<$KEY>, std::allocator<$KEY> >",
43  "std::set<$KEY>");
44  rules.add ("std::multiset<$KEY, std::less<$KEY>, std::allocator<$KEY> >",
45  "std::multiset<$KEY>");
46  rules.add ("std::unordered_map<$KEY, $T, std::hash<$KEY>, std::equal_to<$KEY>, std::allocator<std::pair<const $KEY, $T> > >",
47  "std::unordered_map<$KEY, $T>");
48  rules.add ("std::unordered_multimap<$KEY, $T, std::hash<$KEY>, std::equal_to<$KEY>, std::allocator<std::pair<const $KEY, $T> > >",
49  "std::unordered_multimap<$KEY, $T>");
50  rules.add ("std::unordered_set<$KEY, std::hash<$KEY>, std::equal_to<$KEY>, std::allocator<$KEY> >",
51  "std::unordered_set<$KEY>");
52  rules.add ("std::unordered_multiset<$KEY, std::hash<$KEY>, std::equal_to<$KEY>, std::allocator<$KEY> >",
53  "std::unordered_multiset<$KEY>");
54  rules.add ("std::queue<$T, std::deque<$T> >",
55  "std::queue<$T>");
56  rules.add ("std::stack<$T, std::deque<$T> >",
57  "std::stack<$T>");
58 
59  // These container types are also defined in the C++11 standard,
60  // but root5 reflex doesn't know about them. List them here anyway.
61  rules.add ("std::forward_list<$T, std::allocator<$T> >",
62  "std::forward_list<$T>");
63  rules.add ("std::priority_queue<$T, std::vector<$T>, std::less<$T> >",
64  "std::priority_queue<$T>");
65 
66  // Reflex also handles the non-standard containers hash_map, etc.
67  // Don't think atlas is using those anywhere, so skip that.
68 
69  // Reflex handles the basic_string template.
70  // I actually think that demangling will produce `std::string' rather
71  // than `std::basic_string' with default arguments; however, add it
72  // for completeness.
73  rules.add ("std::basic_string<$T, std::char_traits<$T>, std::allocator<$T> >",
74  "std::string");
75 
76  // Atlas-specific mappings.
77  rules.add ("DataVector<$T, $U>", "DataVector<$T>");
78 
79  // Handle gcc's C++11 ABI.
80  rules.add ("std::__cxx11", "std");
81 
82  // This works around a bug in Gaudi's typeinfoName, which substitutes
83  // 'std::basic_string<...> ?' with `std::string' --- but including the
84  // optional trailing space in the pattern to replace means that we can
85  // end up with `std::stringconst'.
86  rules.add ("std::stringconst", "const std::string");
87 
88  // Needed for macos?
89  rules.add ("std::__1", "std");
90 
91  return rules;
92 }
93 
94 
95 } // anonymous namespace
96 
97 
98 namespace SG {
99 
100 
120 std::string normalizedTypeinfoName (const std::type_info& info)
121 {
122  // Maintain a cache of mappings, protected with an ordinary mutex.
123  // We originally did this with an upgrading mutex.
124  // However, these mutexes have been observed to have considerable overhead,
125  // so are probably not worthwhile if the read critical section is short.
126  // If this lock becomes a performance issue, consider a concurrent hashmap
127  // or rcu-style solution.
128  typedef std::unordered_map<const std::type_info*, std::string> typemap_t;
129  static typemap_t normalizedTypemap ATLAS_THREAD_SAFE;
130  typedef AthContainers_detail::mutex mutex_t;
131  static mutex_t normalizedTypemapMutex;
132  AthContainers_detail::lock_guard<mutex_t> lock (normalizedTypemapMutex);
133 
134  // Test to see if we already have the mapping.
135  typemap_t::iterator it = normalizedTypemap.find (&info);
136  if (it != normalizedTypemap.end()) {
137  return it->second;
138  }
139 
140  // Didn't find it. Apply the rules.
141  static const CxxUtils::ClassName::Rules normalizeRules = makeRules();
142  std::string tiname = AthContainers_detail::typeinfoName (info);
143  std::string normalizedName = normalizeRules.apply (tiname);
144 
145  // Remember this mapping.
146  normalizedTypemap[&info] = normalizedName;
147  return normalizedName;
148 }
149 
150 
151 } // namespace SG
grepfile.info
info
Definition: grepfile.py:38
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ClassName.h
Recursively separate out template arguments in a C++ class name.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
CxxUtils::ClassName::Rules::add
void add(const ClassName &pattern, const ClassName &replacement)
Add a new transformation rule.
Definition: ClassName.cxx:60
SG::normalizedTypeinfoName
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
Definition: normalizedTypeinfoName.cxx:120
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
skel.it
it
Definition: skel.GENtoEVGEN.py:423
CxxUtils::ClassName::Rules::apply
std::string apply(const std::string &name) const
Apply transformations to a class name.
Definition: ClassName.cxx:127
error.h
Helper for emitting error messages.
CxxUtils::ClassName::Rules
A set of transformation rules to use with ClassName.
Definition: CxxUtils/CxxUtils/ClassName.h:130
normalizedTypeinfoName.h
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
threading.h
Threading definitions.
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
checker_macros.h
Define macros for attributes used to control the static checker.