Quantum++  v1.2
A modern C++11 quantum computing library
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 - 2019 Vlad Gheorghiu (vgheorgh@gmail.com)
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 {
79 template <typename T>
80 class Singleton {
81  protected:
82  Singleton() noexcept = default;
83 
84  Singleton(const Singleton&) = delete;
85 
86  Singleton& operator=(const Singleton&) = delete;
87 
88  virtual ~Singleton() = default; // to silence base class Singleton<T> has a
89  // non-virtual destructor [-Weffc++]
90 
91  public:
92  static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value) {
93  // Guaranteed to be destroyed.
94  // Instantiated on first use.
95  // Thread safe in C++11
96  static T instance;
97 
98  return instance;
99  }
100 
101 #ifndef NO_THREAD_LOCAL_
102 
103  static T& get_thread_local_instance() noexcept(
104  std::is_nothrow_constructible<T>::value) {
105  // Guaranteed to be destroyed.
106  // Instantiated on first use.
107  // Thread safe in C++11
108  thread_local static T instance;
109 
110  return instance;
111  }
112 
113 #endif // NO_THREAD_LOCAL_
114 }; /* class Singleton */
115 
116 } /* namespace internal */
117 } /* namespace qpp */
118 
119 #endif /* INTERNAL_CLASSES_SINGLETON_H_ */
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:80
Quantum++ main namespace.
Definition: circuits.h:35
STL namespace.
Singleton & operator=(const Singleton &)=delete
virtual ~Singleton()=default
static T & get_thread_local_instance() noexcept(std::is_nothrow_constructible< T >::value)
Definition: singleton.h:103
Singleton() noexcept=default
static T & get_instance() noexcept(std::is_nothrow_constructible< T >::value)
Definition: singleton.h:92