Quantum++  v1.0-rc2
A modern C++11 quantum computing library
singleton.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef INTERNAL_CLASSES_SINGLETON_H_
28 #define INTERNAL_CLASSES_SINGLETON_H_
29 
30 namespace qpp
31 {
32 namespace internal // internal class, do not modify
33 {
75 template<typename T>
76 class Singleton
77 {
78 protected:
79  Singleton() noexcept = default;
80 
81  Singleton(const Singleton&) = delete;
82 
83  Singleton& operator=(const Singleton&) = delete;
84 
85  virtual ~Singleton() = default; // to silence base class Singleton<T> has a
86  // non-virtual destructor [-Weffc++]
87 
88 public:
89  static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value)
90  {
91  // Guaranteed to be destroyed.
92  // Instantiated on first use.
93  // Thread safe in C++11
94  static T instance;
95 
96  return instance;
97  }
98 
99 #ifndef NO_THREAD_LOCAL_
100 
102  noexcept(std::is_nothrow_constructible<T>::value)
103  {
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_ */
Singleton policy class, used internally to implement the singleton pattern via CRTP (Curiously recurr...
Definition: singleton.h:76
Quantum++ main namespace.
Definition: codes.h:30
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:101
Singleton() noexcept=default
static T & get_instance() noexcept(std::is_nothrow_constructible< T >::value)
Definition: singleton.h:89