Belle II Software light-2406-ragdoll
DBAccessorBase Class Reference

Base class for DBObjPtr and DBArray for easier common treatment. More...

#include <DBAccessorBase.h>

Inheritance diagram for DBAccessorBase:
Collaboration diagram for DBAccessorBase:

Public Member Functions

 DBAccessorBase (const std::string &name, const TClass *objClass, bool isArray, bool isRequired)
 Constructor to access an object in the DBStore.
 
 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.
 
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 wether 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)
 

Protected Member Functions

template<class T = TObject>
const T * getObject () const
 Return a pointer to the Object already cast to the correct type.
 
bool ensureAttached () const
 Make sure we are attached to the the DBStore.
 
void storeEntryChanged (bool destructed)
 Callback function which gets called by the DBStoreEntry object if it changes.
 

Protected Attributes

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.
 
DBStoreEntrym_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.
 

Friends

class DBStoreEntry
 Allow the DBStoreEntry to call the callback notifier.
 

Detailed Description

Base class for DBObjPtr and DBArray for easier common treatment.

Definition at line 26 of file DBAccessorBase.h.

Constructor & Destructor Documentation

◆ DBAccessorBase() [1/2]

DBAccessorBase ( const std::string &  name,
const TClass *  objClass,
bool  isArray,
bool  isRequired 
)
inline

Constructor to access an object in the DBStore.

Parameters
nameName under which the object is stored in the database (and DBStore).
objClassThe type of the object.
isArrayFlag that indicates whether this is a single object or a TClonesArray.
isRequiredIf true emit errors if the object cannot be found in the Database

Definition at line 36 of file DBAccessorBase.h.

36 :
38 m_entry{DBStore::Instance().getEntry(name, objClass, isArray, isRequired)}, m_changed{isValid()}
39 {
41 }
bool m_changed
Internal flag whether the object has changed since we last checked.
const TClass * m_objClass
Class of the payload if type is c_Object.
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.
DBStoreEntry * m_entry
Pointer to the entry in the DBStore.
bool isRequired() const
Check whether this conditions object is required (at least one user declared it as required)
const bool m_isArray
True if the payload is an array of objects.
const DBStoreEntry::EPayloadType m_type
Type of the payload.
const bool m_isRequired
True if the payload is required, otherwise no errors will be raised if it cannot be found.
@ c_Object
A ROOT file containing a object with the name of the DBStoreEntry.
Definition: DBStoreEntry.h:56
void registerAccessor(DBAccessorBase *object)
Register an Accessor object to be notified on changes by calling DBAccessorBase::storeEntryChanged()
Definition: DBStoreEntry.h:123
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:28
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:34

◆ DBAccessorBase() [2/2]

DBAccessorBase ( DBStoreEntry::EPayloadType  type,
const std::string &  name,
bool  isRequired 
)
inline

Constructor to access an object in the DBStore which is not a ROOT Object.

Parameters
typeshould be one of c_RootFile or c_RawFile.
nameName under which the object is stored in the database (and DBStore).
isRequiredIf true emit errors if the object cannot be found in the Database

Definition at line 49 of file DBAccessorBase.h.

49 :
50 m_type(type), m_name(name), m_objClass(nullptr), m_isArray(false), m_isRequired(isRequired),
51 m_entry{DBStore::Instance().getEntry(type, name, nullptr, false, isRequired)}, m_changed{isValid()}
52 {
54 }

◆ ~DBAccessorBase()

virtual ~DBAccessorBase ( )
inlinevirtual

Destructor.

Virtual because this is a base class.

Definition at line 60 of file DBAccessorBase.h.

61 {
62 if (m_entry) m_entry->removeAccessor(this);
63 }
void removeAccessor(DBAccessorBase *object)
Deregister an Accessor object and remove it from the list of registered objects.
Definition: DBStoreEntry.h:125

Member Function Documentation

◆ addCallback() [1/3]

void addCallback ( std::function< void()>  callback,
bool  onDestruction = false 
)
inline

Add a callback method.

The given method will be called whenever there is a new database entry for this DBAccessor.

Parameters
callbackfunction pointer to a callback function
onDestructionif 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 }
void addCallback(std::function< void(const std::string &)> callback, bool onDestruction=false)
Add a callback method.

◆ addCallback() [2/3]

void addCallback ( std::function< void(const std::string &)>  callback,
bool  onDestruction = false 
)
inline

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
callbackfunction pointer to a callback function
onDestructionif 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 }
std::vector< std::pair< std::function< void(const std::string &)>, bool > > m_callbacks
List of all registered callback functions.

◆ addCallback() [3/3]

void addCallback ( T *  object,
void(T::*)()  callback 
)
inline

Add a callback method of an object.

The given method will be called whenever there is a new database entry for this DBAccessor.

Parameters
objectThe object with the callback method.
callbackThe callback method.

Definition at line 130 of file DBAccessorBase.h.

131 {
132 addCallback([ = ](const std::string&) {(*object.*callback)();});
133 }

◆ ensureAttached()

bool ensureAttached ( ) const
inlineprotected

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");
176 m_changed = true;
177 if (!m_entry) return false;
178 m_entry->registerAccessor(const_cast<DBAccessorBase*>(this));
179 }
180 return true;
181 }
DBAccessorBase(const std::string &name, const TClass *objClass, bool isArray, bool isRequired)
Constructor to access an object in the DBStore.

◆ getChecksum()

const std::string & getChecksum ( ) const
inline

Get current checksum.

Definition at line 145 of file DBAccessorBase.h.

145{ ensureAttached(); return m_entry->getChecksum(); }
bool ensureAttached() const
Make sure we are attached to the the DBStore.
const std::string & getChecksum() const
get the checksum of the payload.
Definition: DBStoreEntry.h:104

◆ getFilename()

const std::string & getFilename ( ) const
inline

Get the filename this object is loaded from.

Definition at line 148 of file DBAccessorBase.h.

148{ ensureAttached(); return m_entry->getFilename(); }
const std::string & getFilename() const
get the filename for this payload
Definition: DBStoreEntry.h:101

◆ getGlobaltag()

const std::string & getGlobaltag ( ) const
inline

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(); }
const std::string & getGlobaltag() const
get the globaltag name (or testing payloads path) from which the payload is picked.
Definition: DBStoreEntry.h:94

◆ getIntraRunBoundaries()

const std::vector< unsigned int > getIntraRunBoundaries ( ) const
inline

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.

const std::vector< unsigned int > getIntraRunBoundaries() const
return the boundaries of the intra-run changes of the payload, if any
Definition: DBStoreEntry.h:121

◆ getIoV()

IntervalOfValidity getIoV ( ) const
inline

Return current IoV of the object.

Definition at line 142 of file DBAccessorBase.h.

142{ ensureAttached(); return m_entry->getIoV(); }
IntervalOfValidity getIoV() const
get the validity of the payload
Definition: DBStoreEntry.h:99

◆ getName()

const std::string & getName ( ) const
inline

Return name under which the object is saved in the DBStore.

Definition at line 66 of file DBAccessorBase.h.

66{ return m_name; }

◆ getObject()

const T * getObject ( ) const
inlineprotected

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 }
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

◆ getRevision()

unsigned int getRevision ( ) const
inline

Return current revision of the object.

Definition at line 139 of file DBAccessorBase.h.

139{ ensureAttached(); return m_entry->getRevision(); }
unsigned int getRevision() const
get the revision of the payload, this is an abitrary number which indicates the conditions version
Definition: DBStoreEntry.h:97

◆ hasChanged()

bool hasChanged ( )
inline

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
inline

Check wether this conditions object has some intra-run dependencies.

Definition at line 151 of file DBAccessorBase.h.

bool isIntraRunDependent() const
return whether or not the payload might change even during the run
Definition: DBStoreEntry.h:119

◆ isRequired()

bool isRequired ( ) const
inline

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(); }
bool isRequired() const
check whether this payload is required for operation
Definition: DBStoreEntry.h:115

◆ isValid()

bool isValid ( ) const
inline

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 bool ( ) const
inline

Imitate pointer functionality.

Definition at line 74 of file DBAccessorBase.h.

◆ operator!=()

virtual bool operator!= ( const DBAccessorBase other) const
inlinevirtual

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==()

virtual bool operator== ( const DBAccessorBase other) const
inlinevirtual

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 }
const std::string & getName() const
Return name under which the object is saved in the DBStore.

◆ storeEntryChanged()

void storeEntryChanged ( bool  destructed)
inlineprotected

Callback function which gets called by the DBStoreEntry object if it changes.

Definition at line 184 of file DBAccessorBase.h.

185 {
186 // we obviously changed
187 m_changed = true;
188 // StoreEntry is destructed, remove reference
189 if (destructed) m_entry = nullptr;
190 // Now run all registered callbacks
191 // TODO: We could guard m_callbacks against insertions during callback
192 // execution to prevent exponential growth of callbacks
193 for (const auto& cb : m_callbacks) {
194 if (destructed == cb.second) cb.first(m_name);
195 }
196 }

Friends And Related Function Documentation

◆ DBStoreEntry

friend class DBStoreEntry
friend

Allow the DBStoreEntry to call the callback notifier.

Definition at line 215 of file DBAccessorBase.h.

Member Data Documentation

◆ m_callbacks

std::vector<std::pair<std::function<void(const std::string&)>, bool> > m_callbacks
protected

List of all registered callback functions.

Definition at line 213 of file DBAccessorBase.h.

◆ m_changed

bool m_changed {false}
mutableprotected

Internal flag whether the object has changed since we last checked.

Definition at line 211 of file DBAccessorBase.h.

◆ m_entry

DBStoreEntry* m_entry
mutableprotected

Pointer to the entry in the DBStore.

Definition at line 209 of file DBAccessorBase.h.

◆ m_isArray

const bool m_isArray
protected

True if the payload is an array of objects.

Definition at line 205 of file DBAccessorBase.h.

◆ m_isRequired

const bool m_isRequired
protected

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

const std::string m_name
protected

Name of the payload in the database.

Definition at line 201 of file DBAccessorBase.h.

◆ m_objClass

const TClass* m_objClass
protected

Class of the payload if type is c_Object.

Definition at line 203 of file DBAccessorBase.h.

◆ m_type

const DBStoreEntry::EPayloadType m_type
protected

Type of the payload.

Definition at line 199 of file DBAccessorBase.h.


The documentation for this class was generated from the following file: