/src/quantlib/ql/patterns/singleton.hpp
Line | Count | Source |
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 | | <https://www.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 | | |
30 | | namespace 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 | 63.0M | T& Singleton<T, Global>::instance() { |
105 | 63.0M | static T instance; |
106 | 63.0M | return instance; |
107 | 63.0M | } QuantLib::Singleton<QuantLib::ObservableSettings, std::__1::integral_constant<bool, false> >::instance() Line | Count | Source | 104 | 5.75M | T& Singleton<T, Global>::instance() { | 105 | 5.75M | static T instance; | 106 | 5.75M | return instance; | 107 | 5.75M | } |
QuantLib::Singleton<QuantLib::Settings, std::__1::integral_constant<bool, false> >::instance() Line | Count | Source | 104 | 1.55M | T& Singleton<T, Global>::instance() { | 105 | 1.55M | static T instance; | 106 | 1.55M | return instance; | 107 | 1.55M | } |
QuantLib::Singleton<QuantLib::LazyObject::Defaults, std::__1::integral_constant<bool, false> >::instance() Line | Count | Source | 104 | 55.7M | T& Singleton<T, Global>::instance() { | 105 | 55.7M | static T instance; | 106 | 55.7M | return instance; | 107 | 55.7M | } |
Unexecuted instantiation: QuantLib::Singleton<QuantLib::IndexManager, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::IborCoupon::Settings, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::UnitOfMeasureConversionManager, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::ExchangeRateManager, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::CommoditySettings, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::SeedGenerator, std::__1::integral_constant<bool, false> >::instance() Unexecuted instantiation: QuantLib::Singleton<QuantLib::Money::Settings, std::__1::integral_constant<bool, false> >::instance() |
108 | | |
109 | | #endif |
110 | | |
111 | | } |
112 | | |
113 | | #endif |