Belle II Software  release-06-02-00
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 
16 namespace 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  inline T* operator ->() const {ensureValid(); return static_cast<T*>(*m_storeObjPtr);}
139  inline operator bool() const {return isValid();}
143  static std::vector<std::string> getObjectList(DataStore::EDurability durability = DataStore::c_Event)
144  {
145  return DataStore::Instance().getListOfObjects(T::Class(), durability);
146  }
147 
148  private:
150  inline void ensureAttached() const
151  {
152  if (!m_storeObjPtr) {
153  const_cast<StoreObjPtr*>(this)->m_storeObjPtr = DataStore::Instance().getObject(*this);
154  }
155  }
157  inline void ensureValid() const
158  {
159  ensureAttached();
160  if (!m_storeObjPtr || !(*m_storeObjPtr))
161  throw std::runtime_error("Trying to access StoreObjPtr " + readableName() +
162  ", which was not created. Please check isValid() before accesses if the object is not guaranteed to be created in every event.");
163  }
165  TObject** m_storeObjPtr;
166  };
168 } // 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:669
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:52
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:304
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:95
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreObjPtr.h:150
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:110
T * operator->() const
Imitate pointer functionality.
Definition: StoreObjPtr.h:138
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:137
void ensureValid() const
if accesses to this object would crash, throw an std::runtime_error
Definition: StoreObjPtr.h:157
static std::vector< std::string > getObjectList(DataStore::EDurability durability=DataStore::c_Event)
Return list of object names with matching type.
Definition: StoreObjPtr.h:143
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:165
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:118
Abstract base class for different kinds of events.