Belle II Software  release-05-02-19
DBPointer.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/database/DBArray.h>
13 
14 namespace Belle2 {
21  std::string DBPointer_defaultName = "";
22 
26  template<class T, typename KEY, KEY(T::*METHOD)() const, const std::string& NAME = DBPointer_defaultName> class DBPointer {
27  public:
28 
33  explicit DBPointer(KEY key):
34  m_key(key), m_object(0) {};
35 
37  inline KEY key() const {return m_key;}
38 
40  inline operator KEY() const {return m_key;}
41 
43  inline KEY operator = (KEY key) {m_key = key; m_object = 0; return m_key;}
44 
49  inline bool isValid() const {return getPointer();}
50 
52  inline operator bool() const {return isValid();}
53 
54  inline const T& operator *() const {return *getPointer();}
55  inline const T* operator ->() const {return getPointer();}
57  private:
59  inline const T* getPointer() const
60  {
61  if (!m_object) {
62  DBArray<T> array(NAME);
63  m_object = array.getByKey(METHOD, m_key);
64  }
65  return m_object;
66  }
67 
69  KEY m_key;
70 
72  mutable const T* m_object;
73  };
75 }
Belle2::DBPointer_defaultName
std::string DBPointer_defaultName
Use default name for array in DBPointer.
Definition: DBPointer.h:29
Belle2::DBPointer::m_key
KEY m_key
Key value of the referred array element.
Definition: DBPointer.h:77
Belle2::DBPointer::operator=
KEY operator=(KEY key)
Setter for key value.
Definition: DBPointer.h:51
Belle2::DBArray
Class for accessing arrays of objects in the database.
Definition: DBArray.h:36
Belle2::DBPointer::isValid
bool isValid() const
Check whether we point to a valid object.
Definition: DBPointer.h:57
Belle2::operator*
B2Vector3< DataType > operator*(DataType a, const B2Vector3< DataType > &p)
non-memberfunction Scaling of 3-vectors with a real number
Definition: B2Vector3.h:528
Belle2::DBPointer::m_object
const T * m_object
Pointer to actual object.
Definition: DBPointer.h:80
Belle2::DBPointer::key
KEY key() const
Accessor for key value.
Definition: DBPointer.h:45
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBPointer::DBPointer
DBPointer(KEY key)
Constructor of pointer to an array element in the database.
Definition: DBPointer.h:41
Belle2::DBPointer::getPointer
const T * getPointer() const
Find the referred object and set m_object to it.
Definition: DBPointer.h:67
Belle2::DBArray::getByKey
const T * getByKey(KEY(T::*method)(void) const, KEY key) const
Access object by key instead of by index.
Definition: DBArray.h:79