Belle II Software development
StoreObjPtr.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/DataStore.h>
12#include <framework/datastore/StoreAccessorBase.h>
13
14#include <stdexcept>
15
16namespace Belle2 {
95 template <class T> class StoreObjPtr : public StoreAccessorBase {
96 public:
103 explicit StoreObjPtr(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
104 StoreAccessorBase(DataStore::objectName<T>(name), durability, T::Class(), false), m_storeObjPtr(0) {}
105
110 inline bool isValid() const {ensureAttached(); return m_storeObjPtr && *m_storeObjPtr;}
111
118 template<class ...Args> bool construct(Args&& ... params)
119 {
120 T* t = new T(std::forward<Args>(params)...);
121 return assign(t, false);
122 }
123
130 template<class ...Args> bool constructAndReplace(Args&& ... params)
131 {
132 T* t = new T(std::forward<Args>(params)...);
133 return assign(t, true);
134 }
135
136 //------------------------ Imitate pointer functionality -----------------------------------------------
137 inline T& operator *() const {return *operator->();}
138#ifdef __clang_analyzer__
139 inline T* operator ->() const {ensureValid(); assert(m_storeObjPtr); return static_cast<T*>(*m_storeObjPtr);}
140#else
141 inline T* operator ->() const {ensureValid(); return static_cast<T*>(*m_storeObjPtr);}
142#endif
143 inline operator bool() const {return isValid();}
147 static std::vector<std::string> getObjectList(DataStore::EDurability durability = DataStore::c_Event)
148 {
149 return DataStore::Instance().getListOfObjects(T::Class(), durability);
150 }
151
152 private:
154 inline void ensureAttached() const
155 {
156 if (!m_storeObjPtr) {
157 const_cast<StoreObjPtr*>(this)->m_storeObjPtr = DataStore::Instance().getObject(*this);
158 }
159 }
161 inline void ensureValid() const
162 {
164 if (!m_storeObjPtr || !(*m_storeObjPtr))
165 throw std::runtime_error("Trying to access StoreObjPtr " + readableName() +
166 ", which was not created. Please check isValid() before accesses if the object is not guaranteed to be created in every event.");
167 }
169 TObject** m_storeObjPtr;
170 };
172} // end namespace Belle2
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
std::vector< std::string > getListOfObjects(const TClass *objClass, EDurability durability) const
Returns a list of names of StoreObjPtr-objects whose class is (or inherits from) objClass.
Definition: DataStore.cc:670
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
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:53
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:305
Base class for StoreObjPtr and StoreArray for easier common treatment.
std::string readableName() const
Convert this accessor into a readable string (for messages).
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
T & operator*() const
Imitate pointer functionality.
Definition: StoreObjPtr.h:137
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreObjPtr.h:154
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:110
bool constructAndReplace(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:130
T * operator->() const
Imitate pointer functionality.
Definition: StoreObjPtr.h:141
void ensureValid() const
if accesses to this object would crash, throw an std::runtime_error
Definition: StoreObjPtr.h:161
StoreObjPtr(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an object in the DataStore.
Definition: StoreObjPtr.h:103
TObject ** m_storeObjPtr
Store of actual pointer.
Definition: StoreObjPtr.h:169
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:118
static std::vector< std::string > getObjectList(DataStore::EDurability durability=DataStore::c_Event)
Return list of object names with matching type.
Definition: StoreObjPtr.h:147
Abstract base class for different kinds of events.