Belle II Software  release-05-02-19
DBAccessorBase.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr, Marko Staric, Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/database/DBStore.h>
13 #include <framework/logging/Logger.h>
14 
15 #include <string>
16 #include <vector>
17 #include <functional>
18 
19 class TClass;
20 
21 namespace Belle2 {
29  public:
30 
38  DBAccessorBase(const std::string& name, const TClass* objClass, bool isArray, bool isRequired) :
39  m_type(DBStoreEntry::c_Object), m_name(name), m_objClass(objClass), m_isArray(isArray), m_isRequired(isRequired),
40  m_entry{DBStore::Instance().getEntry(name, objClass, isArray, isRequired)}, m_changed{isValid()}
41  {
42  if (m_entry) m_entry->registerAccessor(this);
43  }
44 
52  DBAccessorBase(DBStoreEntry::EPayloadType type, const std::string& name, bool isRequired):
53  m_type(type), m_name(name), m_objClass(nullptr), m_isArray(false), m_isRequired(isRequired),
54  m_entry{DBStore::Instance().getEntry(type, name, nullptr, false, isRequired)}, m_changed{isValid()}
55  {
56  if (m_entry) m_entry->registerAccessor(this);
57  }
58 
63  virtual ~DBAccessorBase()
64  {
65  if (m_entry) m_entry->removeAccessor(this);
66  }
67 
69  const std::string& getName() const { return m_name; }
70 
75  inline bool isValid() const {if (!ensureAttached()) return false; return (m_entry->getObject() != nullptr);}
76 
77  inline operator bool() const {return isValid();}
80  virtual bool operator==(const DBAccessorBase& other) const
81  {
82  return getName() == other.getName();
83  }
84 
86  virtual bool operator!=(const DBAccessorBase& other) const
87  {
88  return !(*this == other);
89  }
90 
92  bool hasChanged()
93  {
94  const bool ret = m_changed;
95  m_changed = false;
96  return ret;
97  }
98 
108  void addCallback(std::function<void(const std::string&)> callback, bool onDestruction = false)
109  {
110  m_callbacks.emplace_back(callback, onDestruction);
111  }
112 
121  void addCallback(std::function<void()> callback, bool onDestruction = false)
122  {
123  addCallback([callback](const std::string&) -> void { callback(); }, onDestruction);
124  }
125 
133  template<class T> void addCallback(T* object, void(T::*callback)())
134  {
135  addCallback([ = ](const std::string&) {(*object.*callback)();});
136  }
137 
139  unsigned int getRevision() const { ensureAttached(); return m_entry->getRevision(); }
140 
143 
145  const std::string& getChecksum() const { ensureAttached(); return m_entry->getChecksum(); }
146 
148  const std::string& getFilename() const { ensureAttached(); return m_entry->getFilename(); }
149 
151  bool isRequired() const { ensureAttached(); return m_entry->isRequired(); }
152 
153  protected:
155  template<class T = TObject> const T * getObject() const
156  {
157  if (!ensureAttached()) return nullptr;
158  return reinterpret_cast<const T*>(m_entry->getObject());
159  }
160 
162  bool ensureAttached() const
163  {
164  if (!m_entry) {
165  B2DEBUG(32, "DBAccessor " << m_name << " lost connection, reattaching");
167  m_changed = true;
168  if (!m_entry) return false;
169  m_entry->registerAccessor(const_cast<DBAccessorBase*>(this));
170  }
171  return true;
172  }
173 
175  void storeEntryChanged(bool destructed)
176  {
177  // we obviously changed
178  m_changed = true;
179  // StoreEntry is destructed, remove reference
180  if (destructed) m_entry = nullptr;
181  // Now run all registered callbacks
182  // TODO: We could guard m_callbacks against insertions during callback
183  // execution to prevent exponential growth of callbacks
184  for (const auto& cb : m_callbacks) {
185  if (destructed == cb.second) cb.first(m_name);
186  }
187  }
188 
192  const std::string m_name;
194  const TClass* m_objClass;
196  const bool m_isArray;
198  const bool m_isRequired;
202  mutable bool m_changed{false};
204  std::vector<std::pair<std::function<void(const std::string&)>, bool>> m_callbacks;
206  friend class DBStoreEntry;
207  };
209 }
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::DBAccessorBase::operator==
virtual bool operator==(const DBAccessorBase &other) const
Check if two store accessors point to the same object/array.
Definition: DBAccessorBase.h:80
Belle2::DBAccessorBase::m_type
const DBStoreEntry::EPayloadType m_type
Type of the payload.
Definition: DBAccessorBase.h:190
Belle2::DBAccessorBase::m_changed
bool m_changed
Internal flag whether the object has changed since we last checked.
Definition: DBAccessorBase.h:202
Belle2::DBAccessorBase::hasChanged
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
Definition: DBAccessorBase.h:92
Belle2::DBAccessorBase::~DBAccessorBase
virtual ~DBAccessorBase()
Destructor.
Definition: DBAccessorBase.h:63
Belle2::DBAccessorBase::isRequired
bool isRequired() const
Check whether this conditions object is required (at least one user declared it as required)
Definition: DBAccessorBase.h:151
Belle2::DBAccessorBase::DBAccessorBase
DBAccessorBase(DBStoreEntry::EPayloadType type, const std::string &name, bool isRequired)
Constructor to access an object in the DBStore which is not a ROOT Object.
Definition: DBAccessorBase.h:52
Belle2::DBAccessorBase::m_entry
DBStoreEntry * m_entry
Pointer to the entry in the DBStore.
Definition: DBAccessorBase.h:200
Belle2::DBStoreEntry::getFilename
const std::string & getFilename() const
get the filename for this payload
Definition: DBStoreEntry.h:98
Belle2::DBStoreEntry::getChecksum
const std::string & getChecksum() const
get the checksum of the payload.
Definition: DBStoreEntry.h:101
Belle2::DBAccessorBase::getObject
const T * getObject() const
Return a pointer to the Object already cast to the correct type.
Definition: DBAccessorBase.h:155
Belle2::DBStoreEntry::isRequired
bool isRequired() const
check whether this payload is required for operation
Definition: DBStoreEntry.h:112
Belle2::DBAccessorBase::m_callbacks
std::vector< std::pair< std::function< void(const std::string &)>, bool > > m_callbacks
List of all registered callback functions.
Definition: DBAccessorBase.h:204
Belle2::DBStoreEntry::getObject
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:104
Belle2::DBStoreEntry
Class to hold one entry from the ConditionsDB in the DBStore.
Definition: DBStoreEntry.h:49
Belle2::DBAccessorBase::getFilename
const std::string & getFilename() const
Get the filename this object is loaded from.
Definition: DBAccessorBase.h:148
Belle2::DBStoreEntry::removeAccessor
void removeAccessor(DBAccessorBase *object)
Deregister an Accessor object and remove it from the list of registered objects.
Definition: DBStoreEntry.h:120
Belle2::DBAccessorBase::getRevision
unsigned int getRevision() const
Return current revision of the object.
Definition: DBAccessorBase.h:139
Belle2::DBAccessorBase
Base class for DBObjPtr and DBArray for easier common treatment.
Definition: DBAccessorBase.h:28
Belle2::DBStoreEntry::EPayloadType
EPayloadType
Possible Store entry types.
Definition: DBStoreEntry.h:52
Belle2::DBAccessorBase::getIoV
IntervalOfValidity getIoV() const
Return current IoV of the object.
Definition: DBAccessorBase.h:142
Belle2::DBStore::getEntry
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:42
Belle2::DBAccessorBase::getChecksum
const std::string & getChecksum() const
Get current checksum.
Definition: DBAccessorBase.h:145
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBStoreEntry::getIoV
IntervalOfValidity getIoV() const
get the validity of the payload
Definition: DBStoreEntry.h:96
Belle2::DBAccessorBase::storeEntryChanged
void storeEntryChanged(bool destructed)
Callback function which gets called by the DBStoreEntry object if it changes.
Definition: DBAccessorBase.h:175
Belle2::DBStore::Instance
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:36
Belle2::DBAccessorBase::m_name
const std::string m_name
Name of the payload in the database.
Definition: DBAccessorBase.h:192
Belle2::DBAccessorBase::m_isArray
const bool m_isArray
True if the payload is an array of objects.
Definition: DBAccessorBase.h:196
Belle2::DBAccessorBase::m_isRequired
const bool m_isRequired
True if the payload is required, otherwise no errors will be raised if it cannot be found.
Definition: DBAccessorBase.h:198
Belle2::DBAccessorBase::addCallback
void addCallback(std::function< void()> callback, bool onDestruction=false)
Add a callback method.
Definition: DBAccessorBase.h:121
Belle2::DBAccessorBase::ensureAttached
bool ensureAttached() const
Make sure we are attached to the the DBStore.
Definition: DBAccessorBase.h:162
Belle2::DBAccessorBase::isValid
bool isValid() const
Check whether a valid object was obtained from the database.
Definition: DBAccessorBase.h:75
Belle2::DBAccessorBase::m_objClass
const TClass * m_objClass
Class of the payload if type is c_Object.
Definition: DBAccessorBase.h:194
Belle2::DBStoreEntry::registerAccessor
void registerAccessor(DBAccessorBase *object)
Register an Accessor object to be notified on changes by calling DBAccessorBase::storeEntryChanged()
Definition: DBStoreEntry.h:118
Belle2::DBAccessorBase::addCallback
void addCallback(T *object, void(T::*callback)())
Add a callback method of an object.
Definition: DBAccessorBase.h:133
Belle2::DBAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DBStore.
Definition: DBAccessorBase.h:69
Belle2::DBAccessorBase::DBAccessorBase
DBAccessorBase(const std::string &name, const TClass *objClass, bool isArray, bool isRequired)
Constructor to access an object in the DBStore.
Definition: DBAccessorBase.h:38
Belle2::DBStoreEntry::getRevision
unsigned int getRevision() const
get the revision of the payload, this is an abitrary number which indicates the conditions version
Definition: DBStoreEntry.h:94
Belle2::DBAccessorBase::addCallback
void addCallback(std::function< void(const std::string &)> callback, bool onDestruction=false)
Add a callback method.
Definition: DBAccessorBase.h:108
Belle2::DBAccessorBase::operator!=
virtual bool operator!=(const DBAccessorBase &other) const
Check if two store accessors point to a different object/array.
Definition: DBAccessorBase.h:86