1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004, 2005, 2007 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file singleton.hpp
21 \brief basic support for the singleton pattern
22*/
23
24#ifndef quantlib_singleton_hpp
25#define quantlib_singleton_hpp
26
27#include <ql/types.hpp>
28#include <type_traits>
29
30namespace QuantLib {
31
32 //! Basic support for the singleton pattern.
33 /*! The typical use of this class is:
34
35 \code
36 class Foo : public Singleton<Foo> {
37 friend class Singleton<Foo>;
38 private:
39 Foo() {}
40 public:
41 ...
42 };
43 \endcode
44
45 which, albeit sub-optimal, frees one from the concerns of creating and managing the unique instance
46 and can serve later as a single implemementation point should synchronization features be added.
47
48 Global can be used to distinguish Singletons that are local to a session (Global = false) or that are global
49 across all sessions (B = true). This is only relevant if QL_ENABLE_SESSIONS is enabled.
50
51 Notice that the creation and retrieval of (local or global) singleton instances through instance() is thread
52 safe, but obviously subsequent operations on the singleton have to be synchronized within the singleton
53 implementation itself.
54
55 \ingroup patterns
56 */
57 template <class T, class Global = std::integral_constant<bool, false> >
58 class Singleton {
59 public:
60 // disable copy/move
61 Singleton(const Singleton&) = delete;
62 Singleton(Singleton&&) = delete;
63 Singleton& operator=(const Singleton&) = delete;
64 Singleton& operator=(Singleton&&) = delete;
65 ~Singleton() = default;
66
67 //! access to the unique instance
68 static T& instance();
69
70 protected:
71 Singleton() = default;
72 };
73
74 // template definitions
75
76#ifdef QL_ENABLE_SESSIONS
77
78#if (defined(__GNUC__) && !defined(__clang__)) && (((__GNUC__ == 8) && (__GNUC_MINOR__ < 4)) || (__GNUC__ < 8))
79#pragma message("Singleton::instance() is always compiled with `-O0` for versions of GCC below 8.4 when sessions are enabled.")
80#pragma message("This is to work around the following compiler bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91757")
81#pragma message("If possible, please update your compiler to a more recent version.")
82#pragma GCC push_options
83#pragma GCC optimize("-O0")
84#endif
85
86 template <class T, class Global>
87 T& Singleton<T, Global>::instance() {
88 if(Global()) {
89 static T global_instance;
90 return global_instance;
91 } else {
92 thread_local static T local_instance;
93 return local_instance;
94 }
95 }
96
97#if (defined(__GNUC__) && !defined(__clang__)) && (((__GNUC__ == 8) && (__GNUC_MINOR__ < 4)) || (__GNUC__ < 8))
98#pragma GCC pop_options
99#endif
100
101#else
102
103 template <class T, class Global>
104 T& Singleton<T, Global>::instance() {
105 static T instance;
106 return instance;
107 }
108
109#endif
110
111 // backwards compatibility
112
113#if defined(QL_THREAD_KEY)
114 /*! \deprecated This typedef is obsolete. Do not use it.
115 Deprecated in version 1.29.
116 */
117 QL_DEPRECATED
118 typedef QL_THREAD_KEY ThreadKey;
119#else
120 /*! \deprecated This typedef is obsolete. Do not use it.
121 Deprecated in version 1.29.
122 */
123 QL_DEPRECATED
124 typedef Integer ThreadKey;
125#endif
126
127}
128
129#endif
130

source code of quantlib/ql/patterns/singleton.hpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.