ATLAS Offline Software
TestStore.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
4 */
13 #ifndef SGTOOLS_TESTSTORE_H
14 #define SGTOOLS_TESTSTORE_H
15 
16 
18 #include "SGTools/StringPool.h"
19 #include "SGTools/DataProxy.h"
22 #include <unordered_map>
23 #include <mutex>
24 
25 
26 namespace SGTest {
27 
28 
30 
31 
33 {
34  TestStoreRemap (SG::sgkey_t the_key=0, size_t the_index = 0)
35  : key (the_key), index (the_index) {}
36  bool operator== (const TestStoreRemap& other) const
37  { return other.key == key && other.index == index; }
39  size_t index;
40 };
41 
42 
44 {
45  size_t operator() (const TestStoreRemap& m) const
46  { return m.key + m.index; }
47 };
48 
49 
50 class TestStore
51  : virtual public IProxyDict
52 {
53 public:
54  // These are unimplemented and will abort.
55  virtual unsigned long addRef() override;
56  virtual unsigned long release() override;
57  virtual StatusCode queryInterface(const InterfaceID &/*ti*/, void** /*pp*/) override;
58  virtual std::vector<const SG::DataProxy*> proxies() const override;
59  virtual const std::string* keyToString (sgkey_t /*key*/) const override;
60  virtual void registerKey (sgkey_t /*key*/,
61  const std::string& /*str*/,
62  CLID /*clid*/) override;
63 
64 
65  // These have dummy implementations.
66  virtual const std::string& name() const override;
67  virtual SG::DataProxy* proxy(const void* const pTransient) const override;
68  virtual SG::DataProxy* proxy(const CLID& id, const std::string& key) const override;
69  virtual SG::DataProxy* proxy_exact (SG::sgkey_t sgkey) const override;
70  virtual sgkey_t stringToKey (const std::string& str, CLID clid) override;
71  virtual const std::string* keyToString (sgkey_t key, CLID& clid) const override;
72  virtual bool tryELRemap (sgkey_t sgkey_in, size_t index_in,
73  sgkey_t& sgkey_out, size_t& index_out) override;
74  virtual StatusCode addToStore (CLID /*id*/, SG::DataProxy* proxy) override;
75  virtual void boundHandle (IResetable* handle) override;
76  virtual void unboundHandle (IResetable* handle) override;
77 
79  const std::string& key,
80  bool allowMods,
81  bool returnExisting) override;
82 
83 
84  template <class T>
85  void record (const T* p, const std::string& key)
86  {
87  lock_t lock (m_mutex);
88  T* p_nc ATLAS_THREAD_SAFE = const_cast<T*>(p);
89  DataObject* obj = SG::asStorable<T>(p_nc);
90  CLID clid = ClassID_traits<T>::ID();
91  record1 (lock, p, obj, clid, key);
92  }
93 
94 
95  template <class T>
96  void record (std::unique_ptr<T> up, const std::string& key)
97  {
98  lock_t lock (m_mutex);
99  T* p = up.get();
100  DataObject* obj = SG::asStorable<T>(std::move (up));
101  CLID clid = ClassID_traits<T>::ID();
102  record1 (lock, p, obj, clid, key);
103  }
104 
105 
106  void remap (sgkey_t sgkey_in, sgkey_t sgkey_out,
107  size_t index_in = 0, size_t index_out = 0);
108 
109 
110  template <class T>
111  void remap (const std::string& key_in, const std::string& key_out,
112  size_t index_in = 0, size_t index_out = 0)
113  {
114  CLID clid = ClassID_traits<T>::ID();
115  sgkey_t sgkey_in = stringToKey (key_in, clid);
116  sgkey_t sgkey_out = stringToKey (key_out, clid);
117  remap (sgkey_in, sgkey_out, index_in, index_out);
118  }
119 
120  void alias (SG::DataProxy* proxy, const std::string& newKey);
121 
122  typedef std::unordered_map<const void*, SG::DataProxy*> tmap_t;
124 
127 
128  typedef std::unordered_map<TestStoreRemap, TestStoreRemap, TestStoreRemapHash> remap_t;
130 
131  mutable SG::StringPool m_stringPool ATLAS_THREAD_SAFE;
132 
133  std::vector<IResetable*> m_boundHandles;
134 
135  // Log failed calls to proxy(CLID, std::string).
136  mutable std::vector<std::pair<CLID, std::string> > m_missedProxies ATLAS_THREAD_SAFE;
137 
139  typedef std::lock_guard<mutex_t> lock_t;
140  mutable mutex_t m_mutex;
141 
142  SG::DataProxy* proxy(lock_t&, const void* const pTransient) const;
143  SG::DataProxy* proxy(lock_t&, const CLID& id, const std::string& key) const;
144  SG::DataProxy* record1 (lock_t&, const void* p, DataObject* obj,
145  CLID clid, const std::string& key);
146 };
147 
148 
149 // Initialize the global, static test store.
150 extern TestStore store;
151 void initTestStore ATLAS_NOT_THREAD_SAFE ();
152 
153 
154 // Create a new test store and install it.
155 std::unique_ptr<TestStore> getTestStore();
156 
157 
158 } // namespace SGTest
159 
160 
161 #endif // not SGTOOLS_TESTSTORE_H
SGTest::TestStore::mutex_t
std::mutex mutex_t
Definition: TestStore.h:138
SGTest::store
TestStore store
Definition: TestStore.cxx:23
common.sgkey
def sgkey(tool)
Definition: common.py:1028
CurrentEventStore.h
Hold a pointer to the current event store.
SGTest
Definition: TestStore.h:26
SGTest::TestStoreRemap::TestStoreRemap
TestStoreRemap(SG::sgkey_t the_key=0, size_t the_index=0)
Definition: TestStore.h:34
SGTest::TestStore::release
virtual unsigned long release() override
Definition: TestStore.cxx:32
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SGTest::TestStore::boundHandle
virtual void boundHandle(IResetable *handle) override
Tell the store that a handle has been bound to a proxy.
Definition: TestStore.cxx:204
SGTest::TestStore::record1
SG::DataProxy * record1(lock_t &, const void *p, DataObject *obj, CLID clid, const std::string &key)
Definition: TestStore.cxx:221
SG::StringPool
Definition: StringPool.h:35
SGTest::TestStore::lock_t
std::lock_guard< mutex_t > lock_t
Definition: TestStore.h:139
index
Definition: index.py:1
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
SGTest::TestStoreRemap::operator==
bool operator==(const TestStoreRemap &other) const
Definition: TestStore.h:36
SGTest::TestStore::m_remap
remap_t m_remap
Definition: TestStore.h:129
SGTest::TestStore::proxy_exact
virtual SG::DataProxy * proxy_exact(SG::sgkey_t sgkey) const override
Get proxy given a hashed key+clid.
Definition: TestStore.cxx:158
SGTest::TestStore::tryELRemap
virtual bool tryELRemap(sgkey_t sgkey_in, size_t index_in, sgkey_t &sgkey_out, size_t &index_out) override
Test to see if the target of an ElementLink has moved.
Definition: TestStore.cxx:182
SGTest::TestStore::stringToKey
virtual sgkey_t stringToKey(const std::string &str, CLID clid) override
Find the key for a string/CLID pair.
Definition: TestStore.cxx:168
IStringPool::sgkey_t
SG::sgkey_t sgkey_t
Type of the keys.
Definition: IStringPool.h:34
SGTest::TestStore::remap
void remap(sgkey_t sgkey_in, sgkey_t sgkey_out, size_t index_in=0, size_t index_out=0)
Definition: TestStore.cxx:245
IProxyDict
A proxy dictionary.
Definition: AthenaKernel/AthenaKernel/IProxyDict.h:51
SGTest::TestStoreRemap::key
SG::sgkey_t key
Definition: TestStore.h:38
SGTest::TestStore
Definition: TestStore.h:52
SGTest::TestStoreRemapHash
Definition: TestStore.h:44
SGTest::TestStore::remap
void remap(const std::string &key_in, const std::string &key_out, size_t index_in=0, size_t index_out=0)
Definition: TestStore.h:111
SGTest::TestStore::addToStore
virtual StatusCode addToStore(CLID, SG::DataProxy *proxy) override
Add a new proxy to the store.
Definition: TestStore.cxx:194
SGTest::TestStore::proxy
virtual SG::DataProxy * proxy(const void *const pTransient) const override
Get a proxy referencing a given transient object.
Definition: TestStore.cxx:122
ClassID_traits::ID
static const CLID & ID()
the CLID of T
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:50
SGTest::TestStore::m_mutex
mutex_t m_mutex
Definition: TestStore.h:140
IResetable
a resetable object (e.g. a SG DataHandle)
Definition: IResetable.h:15
SGTest::TestStore::addRef
virtual unsigned long addRef() override
Definition: TestStore.cxx:26
SGTest::TestStore::ATLAS_THREAD_SAFE
std::vector< std::pair< CLID, std::string > > m_missedProxies ATLAS_THREAD_SAFE
Definition: TestStore.h:136
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SGTest::TestStoreRemapHash::operator()
size_t operator()(const TestStoreRemap &m) const
Definition: TestStore.h:45
SGTest::TestStore::m_boundHandles
std::vector< IResetable * > m_boundHandles
Definition: TestStore.h:133
SGTest::TestStore::proxies
virtual std::vector< const SG::DataProxy * > proxies() const override
Return the list of all current proxies in store.
Definition: TestStore.cxx:44
SGTest::sgkey_t
SG::sgkey_t sgkey_t
Definition: TestStore.h:29
CalibCoolCompareRT.up
up
Definition: CalibCoolCompareRT.py:109
StringPool.h
Maintain a mapping of strings to 64-bit ints.
SGTest::TestStore::m_tmap
tmap_t m_tmap
Definition: TestStore.h:123
SGTest::TestStore::name
virtual const std::string & name() const override
Definition: TestStore.cxx:115
SGTest::TestStore::unboundHandle
virtual void unboundHandle(IResetable *handle) override
Tell the store that a handle has been unbound from a proxy.
Definition: TestStore.cxx:211
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
IProxyDict.h
SGTest::ATLAS_NOT_THREAD_SAFE
void initTestStore ATLAS_NOT_THREAD_SAFE()
Definition: TestStore.cxx:265
SGTest::TestStore::remap_t
std::unordered_map< TestStoreRemap, TestStoreRemap, TestStoreRemapHash > remap_t
Definition: TestStore.h:128
SGTest::TestStore::keyToString
virtual const std::string * keyToString(sgkey_t) const override
Find the string corresponding to a given key.
Definition: TestStore.cxx:50
SGTest::TestStore::registerKey
virtual void registerKey(sgkey_t, const std::string &, CLID) override
Remember an additional mapping from key to string/CLID.
Definition: TestStore.cxx:56
SGTest::TestStore::kmap_t
SG::SGKeyMap< SG::DataProxy * > kmap_t
Definition: TestStore.h:125
SGTest::TestStore::record
void record(const T *p, const std::string &key)
Definition: TestStore.h:85
SG::sgkey_t
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition: CxxUtils/CxxUtils/sgkey_t.h:32
SGTest::TestStoreRemap::index
size_t index
Definition: TestStore.h:39
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
SGTest::TestStore::ATLAS_THREAD_SAFE
SG::StringPool m_stringPool ATLAS_THREAD_SAFE
Definition: TestStore.h:131
SGTest::TestStore::queryInterface
virtual StatusCode queryInterface(const InterfaceID &, void **) override
Definition: TestStore.cxx:38
SG::SGKeyMap
std::unordered_map< sgkey_t, T > SGKeyMap
A map using sgkey_t as a key.
Definition: CxxUtils/CxxUtils/sgkey_t.h:93
SGTest::getTestStore
std::unique_ptr< TestStore > getTestStore()
Definition: TestStore.cxx:271
SGTest::TestStore::m_kmap
kmap_t m_kmap
Definition: TestStore.h:126
SGTest::TestStore::recordObject
virtual SG::DataProxy * recordObject(SG::DataObjectSharedPtr< DataObject > obj, const std::string &key, bool allowMods, bool returnExisting) override
Record an object in the store.
Definition: TestStore.cxx:64
str
Definition: BTagTrackIpAccessor.cxx:11
SGTest::TestStoreRemap
Definition: TestStore.h:33
SG::DataObjectSharedPtr
Smart pointer to manage DataObject reference counts.
Definition: DataObjectSharedPtr.h:46
SGTest::TestStore::tmap_t
std::unordered_map< const void *, SG::DataProxy * > tmap_t
Definition: TestStore.h:122
checker_macros.h
Define macros for attributes used to control the static checker.
python.PyAthena.obj
obj
Definition: PyAthena.py:135
SG::DataProxy
Definition: DataProxy.h:44
SGTest::TestStore::record
void record(std::unique_ptr< T > up, const std::string &key)
Definition: TestStore.h:96
SGTest::TestStore::alias
void alias(SG::DataProxy *proxy, const std::string &newKey)
Definition: TestStore.cxx:254
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
DataProxy.h