ATLAS Offline Software
EncapsulatingSingleton.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // EncapsulatingSingleton.h, (c) ATLAS Detector software
8 // Author: Thomas Gillam (thomas.gillam@cern.ch)
9 // ExpressionParsing library
11 
12 #ifndef ENCAPSULATING_SINGLETON_H
13 #define ENCAPSULATING_SINGLETON_H
14 
15 #include <stdexcept>
16 
17 namespace ExpressionParsing {
18  template <typename T> struct EncapsulatedDefault {
19  typedef T type;
20  };
21 
22  template <typename T> class EncapsulatingSingleton {
23  public:
24  static T *getInstance() {
26  if (!instance.m_pointer) {
27  throw std::runtime_error("EncapsulatingSingleton: Error - no instance loaded.");
28  }
29  return instance.m_pointer;
30  }
31 
32  static void setInstance(T *proxyLoader) {
34  if (instance.m_pointer) {
35  delete instance.m_pointer;
36  }
37  instance.m_pointer = proxyLoader;
38  }
39 
41  static const EncapsulatingSingleton instance;
42  return instance;
43  }
44 
45  private:
47  typedef typename EncapsulatedDefault<T>::type Default;
48  m_pointer = new Default();
49  }
50 
52  if (m_pointer) {
53  delete m_pointer;
54  }
55  }
56 
59 
61  };
62 }
63 
64 #endif // ENCAPSULATING_SINGLETON_H
ExpressionParsing::EncapsulatingSingleton::m_pointer
T * m_pointer
Definition: EncapsulatingSingleton.h:60
ExpressionParsing::EncapsulatingSingleton::EncapsulatingSingleton
EncapsulatingSingleton()
Definition: EncapsulatingSingleton.h:46
ExpressionParsing::EncapsulatingSingleton::EncapsulatingSingleton
EncapsulatingSingleton(const EncapsulatingSingleton &)=delete
ExpressionParsing::EncapsulatingSingleton::getInstance
static T * getInstance()
Definition: EncapsulatingSingleton.h:24
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
ExpressionParsing
Namespace holding all the expression evaluation code.
Definition: ExpressionParser.h:26
ExpressionParsing::EncapsulatingSingleton::operator=
EncapsulatingSingleton & operator=(const EncapsulatingSingleton &)=delete
ExpressionParsing::EncapsulatingSingleton::~EncapsulatingSingleton
~EncapsulatingSingleton()
Definition: EncapsulatingSingleton.h:51
ExpressionParsing::EncapsulatingSingleton::getSingleton
static const EncapsulatingSingleton & getSingleton()
Definition: EncapsulatingSingleton.h:40
ExpressionParsing::EncapsulatedDefault
Definition: EncapsulatingSingleton.h:18
ExpressionParsing::EncapsulatingSingleton
Definition: EncapsulatingSingleton.h:22
ExpressionParsing::EncapsulatingSingleton::setInstance
static void setInstance(T *proxyLoader)
Definition: EncapsulatingSingleton.h:32
Default
ExpressionParsing::EncapsulatedDefault::type
T type
Definition: EncapsulatingSingleton.h:19