Belle II Software development
DBPointer.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/DBArray.h>
11
12namespace Belle2 {
19 std::string DBPointer_defaultName = "";
20
24 template<class T, typename KEY, KEY(T::*METHOD)() const, const std::string& NAME = DBPointer_defaultName> class DBPointer {
25 public:
26
31 explicit DBPointer(KEY key):
32 m_key(key), m_object(0) {};
33
35 inline KEY key() const {return m_key;}
36
38 inline operator KEY() const {return m_key;}
39
41 inline KEY operator = (KEY key) {m_key = key; m_object = 0; return m_key;}
42
47 inline bool isValid() const {return getPointer();}
48
50 inline operator bool() const {return isValid();}
51
52 inline const T& operator *() const {return *getPointer();}
53 inline const T* operator ->() const {return getPointer();}
55 private:
57 inline const T* getPointer() const
58 {
59 if (!m_object) {
60 DBArray<T> array(NAME);
61 m_object = array.getByKey(METHOD, m_key);
62 }
63 return m_object;
64 }
65
67 KEY m_key;
68
70 mutable const T* m_object;
71 };
73}
Class for accessing arrays of objects in the database.
Definition: DBArray.h:26
const T * getByKey(KEY(T::*method)(void) const, KEY key) const
Access object by key instead of by index.
Definition: DBArray.h:69
Class for pointing to an element in an array stored in the database.
Definition: DBPointer.h:24
const T & operator*() const
Imitate pointer functionality.
Definition: DBPointer.h:52
KEY m_key
Key value of the referred array element.
Definition: DBPointer.h:67
DBPointer(KEY key)
Constructor of pointer to an array element in the database.
Definition: DBPointer.h:31
bool isValid() const
Check whether we point to a valid object.
Definition: DBPointer.h:47
const T * getPointer() const
Find the referred object and set m_object to it.
Definition: DBPointer.h:57
KEY key() const
Accessor for key value.
Definition: DBPointer.h:35
KEY operator=(KEY key)
Setter for key value.
Definition: DBPointer.h:41
const T * operator->() const
Imitate pointer functionality.
Definition: DBPointer.h:53
const T * m_object
Pointer to actual object.
Definition: DBPointer.h:70
std::string DBPointer_defaultName
Use default name for array in DBPointer.
Definition: DBPointer.h:19
Abstract base class for different kinds of events.