Belle II Software  release-05-02-19
DBImportObjPtr.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/database/DBImportBase.h>
13 #include <stdexcept>
14 
15 namespace Belle2 {
25  template<class T> class DBImportObjPtr: public DBImportBase {
26  public:
27 
33  explicit DBImportObjPtr(const std::string& name = ""):
34  DBImportBase(DBStore::objectName<T>(name))
35  {}
36 
41  {
42  if (m_object) delete m_object;
43  }
44 
49  template<class ...Args> void construct(Args&& ... params)
50  {
51  if (m_object) delete m_object;
52  m_object = new T(std::forward<Args>(params)...);
53  }
54 
58  inline T* operator ->() const
59  {
60  if (!m_object)
61  throw std::out_of_range("DBImportObjPtr::operator ->, for "
62  + getName() + ", "
63  "object does not exist or is invisible");
64  return static_cast<T*>(m_object);
65  }
66 
67  };
69 }
Belle2::DBImportBase
Base class for importing objects to the database.
Definition: DBImportBase.h:33
Belle2::DBImportObjPtr::operator->
T * operator->() const
Imitate pointer functionality.
Definition: DBImportObjPtr.h:66
Belle2::DBImportObjPtr::construct
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Definition: DBImportObjPtr.h:57
Belle2::DBImportObjPtr::DBImportObjPtr
DBImportObjPtr(const std::string &name="")
Constructor: the object itself is not allocated here, but in construct(...) function.
Definition: DBImportObjPtr.h:41
Belle2::DBImportObjPtr::~DBImportObjPtr
~DBImportObjPtr()
Destructor.
Definition: DBImportObjPtr.h:48
Belle2::DBStore
Singleton class to cache database objects.
Definition: DBStore.h:42
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBImportBase::m_object
TObject * m_object
pointer to allocated object or array
Definition: DBImportBase.h:110
Belle2::DBImportBase::getName
const std::string & getName() const
Returns the name under which the object will be stored in the database.
Definition: DBImportBase.h:51