Quantum++  v0.8.8.2
C++11 quantum computing library
singleton.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 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 {
34 
76 template<typename T>
77 class Singleton
78 {
79 protected:
80  Singleton() noexcept = default;
81 
82  Singleton(const Singleton&) = delete;
83 
84  Singleton& operator=(const Singleton&) = delete;
85 
86  virtual ~Singleton() = default; // to silence base class Singleton<T> has a
87  // non-virtual destructor [-Weffc++]
88 
89 public:
90  static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value)
91  {
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 
103  noexcept(std::is_nothrow_constructible<T>::value)
104  {
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:77
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:102
Singleton() noexcept=default
static T & get_instance() noexcept(std::is_nothrow_constructible< T >::value)
Definition: singleton.h:90