Belle II Software  release-08-01-10
PyStoreObj.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 <string>
15 #include <vector>
16 
17 class TClass;
18 class TObject;
19 
20 namespace Belle2 {
67  class PyStoreObj {
68  public:
70  static std::vector<std::string> list(DataStore::EDurability durability = DataStore::EDurability::c_Event);
71 
73  static void printList(DataStore::EDurability durability = DataStore::EDurability::c_Event);
74 
79  explicit PyStoreObj(const std::string& name,
80  DataStore::EDurability durability = DataStore::EDurability::c_Event);
81 
86  explicit PyStoreObj(TClass* objClass,
87  DataStore::EDurability durability = DataStore::EDurability::c_Event);
88 
94  explicit PyStoreObj(TClass* objClass,
95  const std::string& name,
96  DataStore::EDurability durability = DataStore::EDurability::c_Event);
97 
105 
113  bool registerInDataStore(const std::string& name = "",
114  DataStore::EStoreFlags storeFlags = DataStore::EStoreFlags::c_WriteOut);
115 
123  bool isRequired(const std::string& name = "");
124 
133  bool isOptional(const std::string& name = "");
134 
136  bool hasValidClass() const;
137 
139  bool isValid() const;
140 
145  operator bool() const { return isValid(); }
146 
148  TObject* obj() const { ensureAttached(); return isValid() ? m_storeEntry->ptr : nullptr; }
149 
151  TObject* operator->() const { return obj(); }
152 
159  bool assign(TObject* object, bool replace = false);
160 
161  public:
167  bool create(bool replace = false);
168 
169  private:
171  void ensureAttached() const;
172 
174  void attach() const;
175 
176  private:
179 
181  mutable StoreEntry* m_storeEntry = nullptr;
182  };
184 }
EStoreFlags
Flags describing behaviours of objects etc.
Definition: DataStore.h:69
EDurability
Durability types.
Definition: DataStore.h:58
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
TObject * operator->() const
Returns the attached DataStore object, or nullptr if no valid object exists.
Definition: PyStoreObj.h:151
StoreAccessorBase m_storeAccessor
Store accessor to retrieve the object.
Definition: PyStoreObj.h:178
static void printList(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Print list of available objects for given durability.
Definition: PyStoreObj.cc:33
PyStoreObj(const std::string &name, DataStore::EDurability durability=DataStore::EDurability::c_Event)
constructor.
Definition: PyStoreObj.cc:40
void ensureAttached() const
Ensure that contained TObject has been attached to a memory location on the DataStore.
Definition: PyStoreObj.cc:125
bool hasValidClass() const
Check whether a TClass for the contained object could be determined.
Definition: PyStoreObj.cc:97
bool isValid() const
Check whether the object was registered and created.
Definition: PyStoreObj.cc:92
bool isRequired(const std::string &name="")
Ensure this object has been registered previously.
Definition: PyStoreObj.cc:82
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: PyStoreObj.cc:87
bool registerInDataStore(DataStore::EStoreFlags storeFlags)
Register the object in the DataStore.
Definition: PyStoreObj.cc:65
bool assign(TObject *object, bool replace=false)
Assign 'object' to the accessor.
Definition: PyStoreObj.cc:140
StoreEntry * m_storeEntry
Pointer to the DataStore entry - serves as an internal cache omitting repeated look up from the DataS...
Definition: PyStoreObj.h:181
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition: PyStoreObj.cc:28
void attach() const
Lookup the store entry and cache a pointer to it.
Definition: PyStoreObj.cc:135
bool create(bool replace=false)
Create default constructed object in the DataStore.
Definition: PyStoreObj.cc:103
TObject * obj() const
Returns the attached DataStore object, or nullptr if no valid object exists.
Definition: PyStoreObj.h:148
Base class for StoreObjPtr and StoreArray for easier common treatment.
Abstract base class for different kinds of events.
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:51