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#include <cassert>
16
17namespace Belle2 {
96 template <class T> class StoreObjPtr : public StoreAccessorBase {
97 public:
104 explicit StoreObjPtr(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
105 StoreAccessorBase(DataStore::objectName<T>(name), durability, T::Class(), false), m_storeObjPtr(0) {}
106
111 inline bool isValid() const {ensureAttached(); return m_storeObjPtr && *m_storeObjPtr;}
112
119 template<class ...Args> bool construct(Args&& ... params)
120 {
121 T* t = new T(std::forward<Args>(params)...);
122 return assign(t, false);
123 }
124
131 template<class ...Args> bool constructAndReplace(Args&& ... params)
132 {
133 T* t = new T(std::forward<Args>(params)...);
134 return assign(t, true);
135 }
136
137 //------------------------ Imitate pointer functionality -----------------------------------------------
138 inline T& operator *() const {return *operator->();}
139#ifdef __clang_analyzer__
140 inline T* operator ->() const {ensureValid(); assert(m_storeObjPtr); return static_cast<T*>(*m_storeObjPtr);}
141#else
142 inline T* operator ->() const {ensureValid(); return static_cast<T*>(*m_storeObjPtr);}
143#endif
144 inline operator bool() const {return isValid();}
148 static std::vector<std::string> getObjectList(DataStore::EDurability durability = DataStore::c_Event)
149 {
150 return DataStore::Instance().getListOfObjects(T::Class(), durability);
151 }
152
153 private:
155 inline void ensureAttached() const
156 {
157 if (!m_storeObjPtr) {
158 const_cast<StoreObjPtr*>(this)->m_storeObjPtr = DataStore::Instance().getObject(*this);
159 }
160 }
162 inline void ensureValid() const
163 {
165 if (!m_storeObjPtr || !(*m_storeObjPtr))
166 throw std::runtime_error("Trying to access StoreObjPtr " + readableName() +
167 ", which was not created. Please check isValid() before accesses if the object is not guaranteed to be created in every event.");
168 }
170 TObject** m_storeObjPtr;
171 };
173} // 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:671
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:54
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:306
Base class for StoreObjPtr and StoreArray for easier common treatment.
std::string readableName() const
Convert this acessor 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:96
T & operator*() const
Imitate pointer functionality.
Definition: StoreObjPtr.h:138
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreObjPtr.h:155
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
bool constructAndReplace(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:131
T * operator->() const
Imitate pointer functionality.
Definition: StoreObjPtr.h:142
void ensureValid() const
if accesses to this object would crash, throw an std::runtime_error
Definition: StoreObjPtr.h:162
StoreObjPtr(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an object in the DataStore.
Definition: StoreObjPtr.h:104
TObject ** m_storeObjPtr
Store of actual pointer.
Definition: StoreObjPtr.h:170
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:119
static std::vector< std::string > getObjectList(DataStore::EDurability durability=DataStore::c_Event)
Return list of object names with matching type.
Definition: StoreObjPtr.h:148
Abstract base class for different kinds of events.