Belle II Software  release-08-01-10
DBImportObjPtr.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/DBImportBase.h>
11 #include <stdexcept>
12 
13 namespace Belle2 {
23  template<class T> class DBImportObjPtr: public DBImportBase {
24  public:
25 
31  explicit DBImportObjPtr(const std::string& name = ""):
32  DBImportBase(DBStore::objectName<T>(name))
33  {}
34 
39  {
40  if (m_object) delete m_object;
41  }
42 
47  template<class ...Args> void construct(Args&& ... params)
48  {
49  if (m_object) delete m_object;
50  m_object = new T(std::forward<Args>(params)...);
51  }
52 
56  inline T* operator ->() const
57  {
58  if (!m_object)
59  throw std::out_of_range("DBImportObjPtr::operator ->, for "
60  + getName() + ", "
61  "object does not exist or is invisible");
62  return static_cast<T*>(m_object);
63  }
64 
65  };
67 }
Base class for importing objects to the database.
Definition: DBImportBase.h:23
const std::string & getName() const
Returns the name under which the object will be stored in the database.
Definition: DBImportBase.h:41
TObject * m_object
pointer to allocated object or array
Definition: DBImportBase.h:100
Class for importing a single object to the database.
T * operator->() const
Imitate pointer functionality.
DBImportObjPtr(const std::string &name="")
Constructor: the object itself is not allocated here, but in construct(...) function.
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
~DBImportObjPtr()
Destructor.
Singleton class to cache database objects.
Definition: DBStore.h:31
Abstract base class for different kinds of events.