Belle II Software  release-05-02-19
StoreObjPtr.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Heck *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreAccessorBase.h>
15 
16 #include <stdexcept>
17 
18 namespace Belle2 {
97  template <class T> class StoreObjPtr : public StoreAccessorBase {
98  public:
105  explicit StoreObjPtr(const std::string& name = "", DataStore::EDurability durability = DataStore::c_Event):
106  StoreAccessorBase(DataStore::objectName<T>(name), durability, T::Class(), false), m_storeObjPtr(0) {}
107 
112  inline bool isValid() const {ensureAttached(); return m_storeObjPtr && *m_storeObjPtr;}
113 
120  template<class ...Args> bool construct(Args&& ... params)
121  {
122  T* t = new T(std::forward<Args>(params)...);
123  return assign(t, false);
124  }
125 
132  template<class ...Args> bool constructAndReplace(Args&& ... params)
133  {
134  T* t = new T(std::forward<Args>(params)...);
135  return assign(t, true);
136  }
137 
138  //------------------------ Imitate pointer functionality -----------------------------------------------
139  inline T& operator *() const {return *operator->();}
140  inline T* operator ->() const {ensureValid(); return static_cast<T*>(*m_storeObjPtr);}
141  inline operator bool() const {return isValid();}
145  static std::vector<std::string> getObjectList(DataStore::EDurability durability = DataStore::c_Event)
146  {
147  return DataStore::Instance().getListOfObjects(T::Class(), durability);
148  }
149 
150  private:
152  inline void ensureAttached() const
153  {
154  if (!m_storeObjPtr) {
155  const_cast<StoreObjPtr*>(this)->m_storeObjPtr = DataStore::Instance().getObject(*this);
156  }
157  }
159  inline void ensureValid() const
160  {
161  ensureAttached();
162  if (!m_storeObjPtr || !(*m_storeObjPtr))
163  throw std::runtime_error("Trying to access StoreObjPtr " + readableName() +
164  ", which was not created. Please check isValid() before accesses if the object is not guaranteed to be created in every event.");
165  }
167  TObject** m_storeObjPtr;
168  };
170 } // end namespace Belle2
Belle2::StoreObjPtr::getObjectList
static std::vector< std::string > getObjectList(DataStore::EDurability durability=DataStore::c_Event)
Return list of object names with matching type.
Definition: StoreObjPtr.h:153
Belle2::DataStore::Instance
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
Belle2::StoreObjPtr::constructAndReplace
bool constructAndReplace(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:140
Belle2::DataStore::getObject
TObject ** getObject(const StoreAccessorBase &accessor)
Get a pointer to a pointer of an object in the DataStore.
Definition: DataStore.cc:306
Belle2::operator*
B2Vector3< DataType > operator*(DataType a, const B2Vector3< DataType > &p)
non-memberfunction Scaling of 3-vectors with a real number
Definition: B2Vector3.h:528
Belle2::DataStore::getListOfObjects
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:672
Belle2::StoreObjPtr::StoreObjPtr
StoreObjPtr(const std::string &name="", DataStore::EDurability durability=DataStore::c_Event)
Constructor to access an object in the DataStore.
Definition: StoreObjPtr.h:113
Belle2::StoreObjPtr::construct
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:128
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::StoreObjPtr::ensureValid
void ensureValid() const
if accesses to this object would crash, throw an std::runtime_error
Definition: StoreObjPtr.h:167
Belle2::StoreObjPtr::m_storeObjPtr
TObject ** m_storeObjPtr
Store of actual pointer.
Definition: StoreObjPtr.h:175
Belle2::StoreObjPtr::ensureAttached
void ensureAttached() const
Ensure that this object is attached.
Definition: StoreObjPtr.h:160
Belle2::DataStore::c_Event
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:61
Belle2::DataStore::EDurability
EDurability
Durability types.
Definition: DataStore.h:60
Belle2::StoreObjPtr::isValid
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:120