Belle II Software  release-06-02-00
DBAccessorBase.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 #pragma once
9 
10 #include <framework/database/DBStore.h>
11 #include <framework/logging/Logger.h>
12 
13 #include <string>
14 #include <vector>
15 #include <functional>
16 
17 class TClass;
18 
19 namespace Belle2 {
27  public:
28 
36  DBAccessorBase(const std::string& name, const TClass* objClass, bool isArray, bool isRequired) :
37  m_type(DBStoreEntry::c_Object), m_name(name), m_objClass(objClass), m_isArray(isArray), m_isRequired(isRequired),
38  m_entry{DBStore::Instance().getEntry(name, objClass, isArray, isRequired)}, m_changed{isValid()}
39  {
40  if (m_entry) m_entry->registerAccessor(this);
41  }
42 
50  DBAccessorBase(DBStoreEntry::EPayloadType type, const std::string& name, bool isRequired):
51  m_type(type), m_name(name), m_objClass(nullptr), m_isArray(false), m_isRequired(isRequired),
52  m_entry{DBStore::Instance().getEntry(type, name, nullptr, false, isRequired)}, m_changed{isValid()}
53  {
54  if (m_entry) m_entry->registerAccessor(this);
55  }
56 
61  virtual ~DBAccessorBase()
62  {
63  if (m_entry) m_entry->removeAccessor(this);
64  }
65 
67  const std::string& getName() const { return m_name; }
68 
73  inline bool isValid() const {if (!ensureAttached()) return false; return (m_entry->getObject() != nullptr);}
74 
75  inline operator bool() const {return isValid();}
78  virtual bool operator==(const DBAccessorBase& other) const
79  {
80  return getName() == other.getName();
81  }
82 
84  virtual bool operator!=(const DBAccessorBase& other) const
85  {
86  return !(*this == other);
87  }
88 
90  bool hasChanged()
91  {
92  const bool ret = m_changed;
93  m_changed = false;
94  return ret;
95  }
96 
106  void addCallback(std::function<void(const std::string&)> callback, bool onDestruction = false)
107  {
108  m_callbacks.emplace_back(callback, onDestruction);
109  }
110 
119  void addCallback(std::function<void()> callback, bool onDestruction = false)
120  {
121  addCallback([callback](const std::string&) -> void { callback(); }, onDestruction);
122  }
123 
131  template<class T> void addCallback(T* object, void(T::*callback)())
132  {
133  addCallback([ = ](const std::string&) {(*object.*callback)();});
134  }
135 
137  const std::string& getGlobaltag() const { ensureAttached(); return m_entry->getGlobaltag(); }
138 
140  unsigned int getRevision() const { ensureAttached(); return m_entry->getRevision(); }
141 
144 
146  const std::string& getChecksum() const { ensureAttached(); return m_entry->getChecksum(); }
147 
149  const std::string& getFilename() const { ensureAttached(); return m_entry->getFilename(); }
150 
152  bool isRequired() const { ensureAttached(); return m_entry->isRequired(); }
153 
154  protected:
156  template<class T = TObject> const T * getObject() const
157  {
158  if (!ensureAttached()) return nullptr;
159  return reinterpret_cast<const T*>(m_entry->getObject());
160  }
161 
163  bool ensureAttached() const
164  {
165  if (!m_entry) {
166  B2DEBUG(32, "DBAccessor " << m_name << " lost connection, reattaching");
168  m_changed = true;
169  if (!m_entry) return false;
170  m_entry->registerAccessor(const_cast<DBAccessorBase*>(this));
171  }
172  return true;
173  }
174 
176  void storeEntryChanged(bool destructed)
177  {
178  // we obviously changed
179  m_changed = true;
180  // StoreEntry is destructed, remove reference
181  if (destructed) m_entry = nullptr;
182  // Now run all registered callbacks
183  // TODO: We could guard m_callbacks against insertions during callback
184  // execution to prevent exponential growth of callbacks
185  for (const auto& cb : m_callbacks) {
186  if (destructed == cb.second) cb.first(m_name);
187  }
188  }
189 
193  const std::string m_name;
195  const TClass* m_objClass;
197  const bool m_isArray;
199  const bool m_isRequired;
203  mutable bool m_changed{false};
205  std::vector<std::pair<std::function<void(const std::string&)>, bool>> m_callbacks;
207  friend class DBStoreEntry;
208  };
210 }
Base class for DBObjPtr and DBArray for easier common treatment.
bool m_changed
Internal flag whether the object has changed since we last checked.
const std::string & getFilename() const
Get the filename this object is loaded from.
virtual bool operator!=(const DBAccessorBase &other) const
Check if two store accessors point to a different object/array.
const T * getObject() const
Return a pointer to the Object already cast to the correct type.
const TClass * m_objClass
Class of the payload if type is c_Object.
DBAccessorBase(DBStoreEntry::EPayloadType type, const std::string &name, bool isRequired)
Constructor to access an object in the DBStore which is not a ROOT Object.
virtual ~DBAccessorBase()
Destructor.
std::vector< std::pair< std::function< void(const std::string &)>, bool > > m_callbacks
List of all registered callback functions.
DBAccessorBase(const std::string &name, const TClass *objClass, bool isArray, bool isRequired)
Constructor to access an object in the DBStore.
const std::string & getGlobaltag() const
Return the globaltag name (or testing payloads path) this object is loaded from.
void addCallback(std::function< void(const std::string &)> callback, bool onDestruction=false)
Add a callback method.
bool isValid() const
Check whether a valid object was obtained from the database.
const std::string m_name
Name of the payload in the database.
IntervalOfValidity getIoV() const
Return current IoV of the object.
DBStoreEntry * m_entry
Pointer to the entry in the DBStore.
const std::string & getChecksum() const
Get current checksum.
const std::string & getName() const
Return name under which the object is saved in the DBStore.
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
unsigned int getRevision() const
Return current revision of the object.
bool ensureAttached() const
Make sure we are attached to the the DBStore.
bool isRequired() const
Check whether this conditions object is required (at least one user declared it as required)
void addCallback(std::function< void()> callback, bool onDestruction=false)
Add a callback method.
void storeEntryChanged(bool destructed)
Callback function which gets called by the DBStoreEntry object if it changes.
const bool m_isArray
True if the payload is an array of objects.
void addCallback(T *object, void(T::*callback)())
Add a callback method of an object.
const DBStoreEntry::EPayloadType m_type
Type of the payload.
virtual bool operator==(const DBAccessorBase &other) const
Check if two store accessors point to the same object/array.
const bool m_isRequired
True if the payload is required, otherwise no errors will be raised if it cannot be found.
Class to hold one entry from the ConditionsDB in the DBStore.
Definition: DBStoreEntry.h:47
void removeAccessor(DBAccessorBase *object)
Deregister an Accessor object and remove it from the list of registered objects.
Definition: DBStoreEntry.h:123
const std::string & getFilename() const
get the filename for this payload
Definition: DBStoreEntry.h:101
EPayloadType
Possible Store entry types.
Definition: DBStoreEntry.h:50
const std::string & getGlobaltag() const
get the globaltag name (or testing payloads path) from which the payload is picked.
Definition: DBStoreEntry.h:94
void registerAccessor(DBAccessorBase *object)
Register an Accessor object to be notified on changes by calling DBAccessorBase::storeEntryChanged()
Definition: DBStoreEntry.h:121
IntervalOfValidity getIoV() const
get the validity of the payload
Definition: DBStoreEntry.h:99
const std::string & getChecksum() const
get the checksum of the payload.
Definition: DBStoreEntry.h:104
unsigned int getRevision() const
get the revision of the payload, this is an abitrary number which indicates the conditions version
Definition: DBStoreEntry.h:97
bool isRequired() const
check whether this payload is required for operation
Definition: DBStoreEntry.h:115
const TObject * getObject() const
get the object for this payload, can be nullptr if the payload is not loaded or not of type c_Object
Definition: DBStoreEntry.h:107
Singleton class to cache database objects.
Definition: DBStore.h:32
A class that describes the interval of experiments/runs for which an object in the database is valid.
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:26
DBStoreEntry * getEntry(DBStoreEntry::EPayloadType payloadType, const std::string &name, const TClass *objClass, bool array, bool required=true)
Returns the entry with the requested name in the DBStore.
Definition: DBStore.cc:32
Abstract base class for different kinds of events.