Class for accessing arrays of objects in the database.
More...
#include <DBArray.h>
|
| DBArray (const std::string &name="", bool required=true) |
| Constructor to access an array of objects in the DBStore.
|
|
int | getEntries () const |
| Get the number of objects in the array.
|
|
const T * | operator[] (int i) const |
| Access to the stored objects.
|
|
template<class KEY> |
const T * | getByKey (KEY(T::*method)(void) const, KEY key) const |
| Access object by key instead of by index.
|
|
const_iterator | begin () const |
| Return const_iterator to first entry.
|
|
const_iterator | end () const |
| Return const_iterator to last entry +1.
|
|
const std::string & | getName () const |
| Return name under which the object is saved in the DBStore.
|
|
bool | isValid () const |
| Check whether a valid object was obtained from the database.
|
|
| operator bool () const |
| Imitate pointer functionality.
|
|
virtual bool | operator== (const DBAccessorBase &other) const |
| Check if two store accessors point to the same object/array.
|
|
virtual bool | operator!= (const DBAccessorBase &other) const |
| Check if two store accessors point to a different object/array.
|
|
bool | hasChanged () |
| Check whether the object has changed since the last call to hasChanged of the accessor).
|
|
void | addCallback (std::function< void(const std::string &)> callback, bool onDestruction=false) |
| Add a callback method.
|
|
void | addCallback (std::function< void()> callback, bool onDestruction=false) |
| Add a callback method.
|
|
template<class T> |
void | addCallback (T *object, void(T::*callback)()) |
| Add a callback method of an object.
|
|
const std::string & | getGlobaltag () const |
| Return the globaltag name (or testing payloads path) this object is loaded from.
|
|
unsigned int | getRevision () const |
| Return current revision of the object.
|
|
IntervalOfValidity | getIoV () const |
| Return current IoV of the object.
|
|
const std::string & | getChecksum () const |
| Get current checksum.
|
|
const std::string & | getFilename () const |
| Get the filename this object is loaded from.
|
|
bool | isIntraRunDependent () const |
| Check whether this conditions object has some intra-run dependencies.
|
|
const std::vector< unsigned int > | getIntraRunBoundaries () const |
| Get the intra-run boundaries, if any.
|
|
bool | isRequired () const |
| Check whether this conditions object is required (at least one user declared it as required)
|
|
|
const DBStoreEntry::EPayloadType | m_type |
| Type of the payload.
|
|
const std::string | m_name |
| Name of the payload in the database.
|
|
const TClass * | m_objClass |
| Class of the payload if type is c_Object.
|
|
const bool | m_isArray |
| True if the payload is an array of objects.
|
|
const bool | m_isRequired |
| True if the payload is required, otherwise no errors will be raised if it cannot be found.
|
|
DBStoreEntry * | m_entry |
| Pointer to the entry in the DBStore.
|
|
bool | m_changed {false} |
| Internal flag whether the object has changed since we last checked.
|
|
std::vector< std::pair< std::function< void(const std::string &)>, bool > > | m_callbacks |
| List of all registered callback functions.
|
|
template<class T>
class Belle2::DBArray< T >
Class for accessing arrays of objects in the database.
Definition at line 26 of file DBArray.h.
◆ const_iterator
◆ DBArray()
template<class T>
DBArray |
( |
const std::string & | name = "", |
|
|
bool | required = true ) |
|
inlineexplicit |
Constructor to access an array of objects in the DBStore.
- Parameters
-
name | Name under which the array is stored in the database (and DBStore). |
required | if false there will be no errors if the object cannot be found in the database. However this will require the user to check if the object is valid before each use. |
Definition at line 38 of file DBArray.h.
38 :
39 DBAccessorBase(DBStore::arrayName<T>(name), T::Class(), true, required) {}
◆ addCallback() [1/3]
void addCallback |
( |
std::function< void()> | callback, |
|
|
bool | onDestruction = false ) |
|
inlineinherited |
Add a callback method.
The given method will be called whenever there is a new database entry for this DBAccessor.
- Parameters
-
callback | function pointer to a callback function |
onDestruction | if true the callback will not be called for each change but only when the Database entry is deleted and can be used to remove dangling pointers to the entry. |
Definition at line 118 of file DBAccessorBase.h.
119 {
120 addCallback([callback](const std::string&) -> void { callback(); }, onDestruction);
121 }
◆ addCallback() [2/3]
void addCallback |
( |
std::function< void(const std::string &)> | callback, |
|
|
bool | onDestruction = false ) |
|
inlineinherited |
Add a callback method.
The given method will be called whenever there is a new database entry for this DBAccessor. The one parameter is the name of the DB entry which changed (in case the same callback method is to be used for multiple payloads.
- Parameters
-
callback | function pointer to a callback function |
onDestruction | if true the callback will not be called for each change but only when the Database entry is deleted and can be used to remove dangling pointers to the entry. |
Definition at line 105 of file DBAccessorBase.h.
106 {
107 m_callbacks.emplace_back(callback, onDestruction);
108 }
◆ addCallback() [3/3]
template<class T>
void addCallback |
( |
T * | object, |
|
|
void(T::* | callback )() ) |
|
inlineinherited |
Add a callback method of an object.
The given method will be called whenever there is a new database entry for this DBAccessor.
- Parameters
-
object | The object with the callback method. |
callback | The callback method. |
Definition at line 130 of file DBAccessorBase.h.
131 {
132 addCallback([ = ](const std::string&) {(*object.*callback)();});
133 }
◆ begin()
Return const_iterator to first entry.
Definition at line 82 of file DBArray.h.
82{ return const_iterator(getObject<TClonesArray>(), false); }
◆ end()
Return const_iterator to last entry +1.
Definition at line 84 of file DBArray.h.
84{ return const_iterator(getObject<TClonesArray>(), true); }
◆ ensureAttached()
bool ensureAttached |
( |
| ) |
const |
|
inlineprotectedinherited |
Make sure we are attached to the the DBStore.
If not try to reconnect
Definition at line 171 of file DBAccessorBase.h.
172 {
173 if (!m_entry) {
174 B2DEBUG(32, "DBAccessor " << m_name << " lost connection, reattaching");
175 m_entry = DBStore::Instance().getEntry(m_type, m_name, m_objClass, m_isArray, m_isRequired);
176 m_changed = true;
177 if (!m_entry) return false;
178 m_entry->registerAccessor(const_cast<DBAccessorBase*>(this));
179 }
180 return true;
181 }
◆ getByKey()
template<class T>
template<class KEY>
const T * getByKey |
( |
KEY(T::* | method )(void) const, |
|
|
KEY | key ) const |
|
inline |
Access object by key instead of by index.
- Parameters
-
method | pointer to a member function of the objects in the array which will return the key for a given element |
key | key to look for. If none of the elements return this key then NULL is returned |
Definition at line 69 of file DBArray.h.
70 {
71 const TClonesArray& array = *getObject<TClonesArray>();
72 for (int i = 0; i < getEntries(); i++) {
73 T* obj = static_cast<T*>(array.At(i));
74 if ((*obj.*method)() == key) {
75 return obj;
76 }
77 }
78 return nullptr;
79 }
◆ getChecksum()
const std::string & getChecksum |
( |
| ) |
const |
|
inlineinherited |
Get current checksum.
Definition at line 145 of file DBAccessorBase.h.
145{ ensureAttached(); return m_entry->getChecksum(); }
◆ getEntries()
Get the number of objects in the array.
Definition at line 42 of file DBArray.h.
42{ return isValid() ? getObject<TClonesArray>()->GetEntriesFast() : 0;}
◆ getFilename()
const std::string & getFilename |
( |
| ) |
const |
|
inlineinherited |
Get the filename this object is loaded from.
Definition at line 148 of file DBAccessorBase.h.
148{ ensureAttached(); return m_entry->getFilename(); }
◆ getGlobaltag()
const std::string & getGlobaltag |
( |
| ) |
const |
|
inlineinherited |
Return the globaltag name (or testing payloads path) this object is loaded from.
Definition at line 136 of file DBAccessorBase.h.
136{ ensureAttached(); return m_entry->getGlobaltag(); }
◆ getIntraRunBoundaries()
const std::vector< unsigned int > getIntraRunBoundaries |
( |
| ) |
const |
|
inlineinherited |
Get the intra-run boundaries, if any.
If the intra-run dependency is by event number, the event numbers used as boundaries are returned.
Definition at line 157 of file DBAccessorBase.h.
157{ ensureAttached(); return m_entry->getIntraRunBoundaries(); }
◆ getIoV()
Return current IoV of the object.
Definition at line 142 of file DBAccessorBase.h.
142{ ensureAttached(); return m_entry->getIoV(); }
◆ getName()
const std::string & getName |
( |
| ) |
const |
|
inlineinherited |
◆ getObject()
template<class T = TObject>
const T * getObject |
( |
| ) |
const |
|
inlineprotectedinherited |
Return a pointer to the Object already cast to the correct type.
Definition at line 164 of file DBAccessorBase.h.
165 {
166 if (!ensureAttached()) return nullptr;
167 return reinterpret_cast<const T*>(m_entry->getObject());
168 }
◆ getRevision()
unsigned int getRevision |
( |
| ) |
const |
|
inlineinherited |
Return current revision of the object.
Definition at line 139 of file DBAccessorBase.h.
139{ ensureAttached(); return m_entry->getRevision(); }
◆ hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
Definition at line 89 of file DBAccessorBase.h.
90 {
91 const bool ret = m_changed;
92 m_changed = false;
93 return ret;
94 }
◆ isIntraRunDependent()
bool isIntraRunDependent |
( |
| ) |
const |
|
inlineinherited |
Check whether this conditions object has some intra-run dependencies.
Definition at line 151 of file DBAccessorBase.h.
151{ ensureAttached(); return m_entry->isIntraRunDependent(); }
◆ isRequired()
bool isRequired |
( |
| ) |
const |
|
inlineinherited |
Check whether this conditions object is required (at least one user declared it as required)
Definition at line 160 of file DBAccessorBase.h.
160{ ensureAttached(); return m_entry->isRequired(); }
◆ isValid()
Check whether a valid object was obtained from the database.
- Returns
- True if the object exists.
Definition at line 72 of file DBAccessorBase.h.
72{if (!ensureAttached()) return false; return (m_entry->getObject() != nullptr);}
◆ operator bool()
◆ operator!=()
Check if two store accessors point to a different object/array.
Definition at line 83 of file DBAccessorBase.h.
84 {
85 return !(*this == other);
86 }
◆ operator==()
Check if two store accessors point to the same object/array.
Definition at line 77 of file DBAccessorBase.h.
78 {
79 return getName() == other.getName();
80 }
◆ operator[]()
template<class T>
const T * operator[] |
( |
int | i | ) |
const |
|
inline |
Access to the stored objects.
Out-of-bounds accesses throw an std::out_of_range exception
- Parameters
-
- Returns
- pointer to the object
Definition at line 51 of file DBArray.h.
52 {
53 if (!isValid()) {
54 throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ": Object not valid");
55 }
56
57 TObject* obj = getObject<TClonesArray>()->At(i);
58 if (obj == nullptr)
59 throw std::out_of_range("Out-of-range access in DBArray::operator[], for " + getName() + ", index " + std::to_string(i));
60 return static_cast<T*>(obj);
61 }
◆ storeEntryChanged()
void storeEntryChanged |
( |
bool | destructed | ) |
|
|
inlineprotectedinherited |
Callback function which gets called by the DBStoreEntry object if it changes.
Definition at line 184 of file DBAccessorBase.h.
185 {
186
187 m_changed = true;
188
189 if (destructed) m_entry = nullptr;
190
191
192
193 for (const auto& cb : m_callbacks) {
194 if (destructed == cb.second) cb.first(m_name);
195 }
196 }
◆ m_callbacks
std::vector<std::pair<std::function<void(const std::string&)>, bool> > m_callbacks |
|
protectedinherited |
◆ m_changed
|
mutableprotectedinherited |
Internal flag whether the object has changed since we last checked.
Definition at line 211 of file DBAccessorBase.h.
◆ m_entry
|
mutableprotectedinherited |
◆ m_isArray
◆ m_isRequired
True if the payload is required, otherwise no errors will be raised if it cannot be found.
Definition at line 207 of file DBAccessorBase.h.
◆ m_name
◆ m_objClass
◆ m_type
The documentation for this class was generated from the following file: