XACC
singleton.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2020 Vlad Gheorghiu.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef INTERNAL_CLASSES_SINGLETON_H_
33 #define INTERNAL_CLASSES_SINGLETON_H_
34 
35 namespace qpp {
36 namespace internal // internal class, do not modify
37 {
78 template <typename T>
79 class Singleton {
80  protected:
81  Singleton() noexcept = default;
82 
83  Singleton(const Singleton&) = delete;
84 
85  Singleton& operator=(const Singleton&) = delete;
86 
87  virtual ~Singleton() = default; // to silence base class Singleton<T> has a
88  // non-virtual destructor [-Weffc++]
89 
90  public:
91  static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value) {
92  // Guaranteed to be destroyed.
93  // Instantiated on first use.
94  // Thread safe in C++11
95  static T instance;
96 
97  return instance;
98  }
99 
100 #ifndef NO_THREAD_LOCAL_
101 
102  static T& get_thread_local_instance() noexcept(
103  std::is_nothrow_constructible<T>::value) {
104  // Guaranteed to be destroyed.
105  // Instantiated on first use.
106  // Thread safe in C++11
107  thread_local static T instance;
108 
109  return instance;
110  }
111 
112 #endif // NO_THREAD_LOCAL_
113 }; /* class Singleton */
114 
115 } /* namespace internal */
116 } /* namespace qpp */
117 
118 #endif /* INTERNAL_CLASSES_SINGLETON_H_ */
Quantum++ main namespace.
Definition: circuits.h:35