ATLAS Offline Software
TransformMap.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef GEOMODELUTILITIES_TRANSFORMMAP_ICC
6 #define GEOMODELUTILITIES_TRANSFORMMAP_ICC
7 
8 #include "GeoModelHelpers/throwExcept.h"
9 #include "CxxUtils/inline_hints.h"
10 
11 
12 template <typename T, typename X> bool TransformMap<T, X>::setTransform(const T* obj, std::shared_ptr<const X> xf) const {
13  if (m_locked) {
14  THROW_EXCEPTION("The transform map is already locked ");
15  }
16  std::unique_lock guard{m_mutex};
17  typename ConCurrentMap_t::iterator itr = m_mutableCont->find(obj);
18  if (itr !=m_mutableCont->end()) {
19  itr->second = std::move(xf);
20  return false;
21  }
22  return m_mutableCont->emplace(obj, xf).second;
23 }
24 template <typename T, typename X>
25 bool TransformMap<T, X>::setTransform(const T* obj, std::shared_ptr<const X> xf) {
26  std::unique_lock guardThis{m_mutex};
27  if (m_locked) {
28  std::shared_ptr<const X>& newObj = m_container[obj];
29  bool good = !newObj;
30  newObj = std::move(xf);
31  return good;
32  }
33  typename ConCurrentMap_t::iterator itr = m_mutableCont->find(obj);
34  if (itr !=m_mutableCont->end()) {
35  itr->second = std::move(xf);
36  return false;
37  }
38  return m_mutableCont->emplace(obj, xf).second;
39 }
40 
41 template <typename T, typename X> bool TransformMap<T, X>::setTransform(const T *obj, const X &xf) const {
42  return setTransform(obj, std::make_shared<const X>(xf));
43 }
44 template <typename T, typename X> bool TransformMap<T, X>::setTransform(const T *obj, const X &xf) {
45  return setTransform(obj, std::make_shared<const X>(xf));
46 }
47 
48 
49 template <typename T, typename X>
50 ATH_FLATTEN
51 const X *TransformMap<T, X>::getTransform(const T *obj) const {
52  if (m_locked) {
53  auto itr = m_container.find(obj);
54  return itr != m_container.end() ? itr->second.get() : nullptr;
55  }
56  auto itr = m_mutableCont->find(obj);
57  return itr != m_mutableCont->end() ? itr->second.get() : nullptr;
58 }
59 
60 template <typename T, typename X> bool TransformMap<T, X>::append(const TransformMap &other) {
61  bool allGood{true};
62  std::shared_lock guardOther{other.m_mutex};
63  std::unique_lock guardThis{m_mutex};
64  if (other.m_mutableCont) {
65  for (const auto&[key, value]: *other.m_mutableCont){
66  allGood &= setTransform(key, value);
67  }
68  } else {
69  for (const auto &[key, value] : other.m_container) {
70  allGood &= setTransform(key, value);
71  }
72  }
73  return allGood;
74 }
75 
76 template <typename T, typename X> void TransformMap<T, X>::clear() {
77  std::unique_lock guardThis{m_mutex};
78  m_container.clear();
79  m_mutableCont = std::make_unique<ConCurrentMap_t>(typename ConCurrentMap_t::Updater_t());
80  m_locked = false;
81 }
82 
83 template <typename T, typename X> void TransformMap<T, X>::lock() {
84  std::unique_lock guardThis{m_mutex};
85  if (m_locked) return;
86  for (auto& [key, value] : *m_mutableCont) {
87  m_container.insert(std::make_pair(key, value));
88  }
89  m_locked = true;
90  m_mutableCont.reset();
91 }
92 template <typename T, typename X>
93 std::vector<const T*> TransformMap<T, X>::getStoredKeys() const {
94  std::vector<const T*> keys{};
95  if (m_mutableCont) {
96  keys.reserve(m_mutableCont->size());
97  for (const auto& key_value: *m_mutableCont) {
98  keys.push_back(key_value.first);
99  }
100  } else {
101  keys.reserve(m_container.size());
102  for (const auto& key_value: m_container) {
103  keys.push_back(key_value.first);
104  }
105  }
106  return keys;
107 }
108 #endif