Belle II Software development
StoreArray.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/datastore/StoreAccessorBase.h>
12#include <framework/datastore/DataStore.h>
13
14#include <framework/utilities/ArrayIterator.h>
15
16#include <TClonesArray.h>
17#include <stdexcept>
18
19namespace Belle2 {
25 namespace _StoreArrayImpl {
27 void clearRelations(const StoreAccessorBase& array);
28 };
29
112 template <class T>
114 public:
119
126 explicit StoreArray(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
127 StoreAccessorBase(DataStore::arrayName<T>(name), durability, T::Class(), true), m_storeArray(0) {}
128
129
140 template <class TO> bool registerRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
141 DataStore::EStoreFlags storeFlags = DataStore::c_WriteOut, const std::string& namedRelation = "") const
142 {
143 return DataStore::Instance().registerRelation(*this, toArray, durability, storeFlags, namedRelation);
144 }
145
155 template <class TO> bool requireRelationTo(const StoreArray<TO>& toArray,
157 const std::string& namedRelation = "") const
158 {
159 return DataStore::Instance().requireRelation(*this, toArray, durability, namedRelation);
160 }
161
172 template <class TO> bool optionalRelationTo(const StoreArray<TO>& toArray,
174 const std::string& namedRelation = "") const
175 {
176 return DataStore::Instance().optionalRelation(*this, toArray, durability, namedRelation);
177 }
178
185 template <class TO> bool hasRelationTo(const StoreArray<TO>& toArray, DataStore::EDurability durability = DataStore::c_Event,
186 const std::string& namedRelation = "") const
187 {
188 return DataStore::Instance().hasRelation(*this, toArray, durability, namedRelation);
189 }
190
197 template <class FROM> bool hasRelationFrom(const StoreArray<FROM>& fromArray,
198 DataStore::EDurability durability = DataStore::c_Event, const std::string& namedRelation = "") const
199 {
200 return DataStore::Instance().hasRelation(fromArray, *this, durability, namedRelation);
201 }
202
207 void clear() override
208 {
209 if (getEntries() != 0) {
210 (*m_storeArray)->Delete();
212 }
213 }
214
216 inline int getEntries() const { return isCreated() ? ((*m_storeArray)->GetEntriesFast()) : 0;}
217
228 inline T* operator [](int i) const
229 {
231 //At() checks for out-of-range and returns NULL in that case
232 //type was checked by DataStore, so the cast is safe.
233 T* obj = static_cast<T*>((*m_storeArray)->At(i));
234 if (obj == nullptr)
235 throw std::out_of_range("Out-of-range access in StoreArray::operator[], for " + readableName() + ", index " + std::to_string(i));
236 return obj;
237 }
238
247 inline T* appendNew() { return new (nextFreeAdress()) T(); }
248
269 template<class ...Args> T* appendNew(Args&& ... params)
270 {
271 return new (nextFreeAdress()) T(std::forward<Args>(params)...);
272 }
273
274
276 static std::vector<std::string> getArrayList(DataStore::EDurability durability = DataStore::c_Event)
277 {
278 return DataStore::Instance().getListOfArrays(T::Class(), durability);
279 }
280
281
289 inline bool isValid() const
290 {
292 return m_storeArray;
293 }
294
302 inline operator bool() const { return isValid(); }
303
312 TClonesArray* getPtr() const
313 {
315 return (m_storeArray ? *m_storeArray : nullptr);
316 }
317
322
327
328 private:
330 // cppcheck-suppress duplInheritedMember ; intentionally hides the base class member
331 bool create(bool replace = false) { return StoreAccessorBase::create(replace); }
332
337 inline T* nextFreeAdress()
338 {
340 return static_cast<T*>((*m_storeArray)->AddrAt(getEntries()));
341 }
342
344 inline void ensureAttached() const
345 {
346 if (!m_storeArray) {
347 const_cast<StoreArray*>(this)->m_storeArray = reinterpret_cast<TClonesArray**>(DataStore::Instance().getObject(*this));
348 }
349 }
350
354 inline void ensureCreated() const
355 {
356 if (!isCreated()) {
357 if (!const_cast<StoreArray*>(this)->create())
358 throw std::runtime_error("Write access to " + readableName() + " failed, did you remember to call registerInDataStore()?");
359 }
360 }
361
363 inline bool isCreated() const
364 {
366 return m_storeArray && *m_storeArray;
367 }
368
370 TClonesArray** m_storeArray;
371
372 };
373
374} // end namespace Belle2
In the store you can park objects that have to be accessed by various modules.
Definition DataStore.h:51
static bool optionalRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Register the given relation as an optional input.
Definition DataStore.cc:764
EStoreFlags
Flags describing behaviours of objects etc.
Definition DataStore.h:69
@ c_WriteOut
Object/array should be saved by output modules.
Definition DataStore.h:70
bool registerRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, EStoreFlags storeFlags, const std::string &namedRelation)
Register a relation in the DataStore map.
Definition DataStore.cc:241
std::vector< std::string > getListOfArrays(const TClass *arrayClass, EDurability durability) const
Returns a list of names of arrays which are of type (or inherit from) arrayClass.
Definition DataStore.cc:664
EDurability
Durability types.
Definition DataStore.h:58
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
bool requireRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, std::string const &namedRelation)
Produce ERROR message if no relation of given durability exists between fromArray and toArray (in tha...
Definition DataStore.cc:747
static DataStore & Instance()
Instance of singleton Store.
Definition DataStore.cc:53
bool hasRelation(const StoreAccessorBase &fromArray, const StoreAccessorBase &toArray, EDurability durability, const std::string &namedRelation)
Check for the existence of a relation in the DataStore map.
Definition DataStore.cc:268
Optimizes class to iterate over TObjArray and classes inheriting from it.
Base class for StoreObjPtr and StoreArray for easier common treatment.
StoreAccessorBase(const std::string &name, DataStore::EDurability durability, TClass *objClass, bool isArray)
Constructor to access an object or array in the DataStore.
std::string readableName() const
Convert this accessor into a readable string (for messages).
bool create(bool replace=false)
Create a default object in the data store.
T * nextFreeAdress()
Returns address of the next free position of the array.
Definition StoreArray.h:337
static std::vector< std::string > getArrayList(DataStore::EDurability durability=DataStore::c_Event)
Return list of array names with matching type.
Definition StoreArray.h:276
bool requireRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Produce error if no relation from this array to 'toArray' has been registered.
Definition StoreArray.h:155
const_iterator begin() const
Return const_iterator to first entry.
Definition StoreArray.h:324
T * appendNew()
Construct a new T object at the end of the array.
Definition StoreArray.h:247
T * operator[](int i) const
Access to the stored objects.
Definition StoreArray.h:228
void ensureAttached() const
Ensure that this object is attached.
Definition StoreArray.h:344
bool hasRelationFrom(const StoreArray< FROM > &fromArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation from the given StoreArray.
Definition StoreArray.h:197
bool isValid() const
Check whether the array was registered.
Definition StoreArray.h:289
TClonesArray ** m_storeArray
Pointer that actually holds the TClonesArray.
Definition StoreArray.h:370
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition StoreArray.h:312
ObjArrayIterator< TClonesArray, T > iterator
STL-like iterator over the T objects (not T* ).
Definition StoreArray.h:116
int getEntries() const
Get the number of objects in the array.
Definition StoreArray.h:216
T * appendNew(Args &&... params)
Construct a new T object directly at the end of the array.
Definition StoreArray.h:269
iterator end()
Return iterator to last entry +1.
Definition StoreArray.h:321
const_iterator end() const
Return const_iterator to last entry +1.
Definition StoreArray.h:326
bool hasRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Check for the existence of a relation to the given StoreArray.
Definition StoreArray.h:185
bool create(bool replace=false)
Creating StoreArrays is unnecessary, only used internally.
Definition StoreArray.h:331
StoreArray(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an array in the DataStore.
Definition StoreArray.h:126
iterator begin()
Return iterator to first entry.
Definition StoreArray.h:319
void ensureCreated() const
Ensure that the array has been created.
Definition StoreArray.h:354
void clear() override
Delete all entries in this array.
Definition StoreArray.h:207
ObjArrayIterator< const TClonesArray, const T > const_iterator
STL-like const_iterator over the T objects (not T* ).
Definition StoreArray.h:118
bool optionalRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, const std::string &namedRelation="") const
Tell the data store about a relation that we could make use of.
Definition StoreArray.h:172
bool isCreated() const
Check whether the array object was created.
Definition StoreArray.h:363
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition StoreArray.h:140
hide some implementation details.
Definition StoreArray.h:25
void clearRelations(const StoreAccessorBase &array)
clear all relations touching the given array.
Definition StoreArray.cc:16
Abstract base class for different kinds of events.