QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
singleton.hpp
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
24#ifndef quantlib_singleton_hpp
25#define quantlib_singleton_hpp
26
27#include <ql/types.hpp>
28#include <type_traits>
29
30namespace QuantLib {
31
33
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;
65 ~Singleton() = default;
66
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>
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>
105 static T instance;
106 return instance;
107 }
108
109#endif
110
111 // backwards compatibility
112
113#if defined(QL_THREAD_KEY)
117 QL_DEPRECATED
118 typedef QL_THREAD_KEY ThreadKey;
119#else
123 QL_DEPRECATED
125#endif
126
127}
128
129#endif
Basic support for the singleton pattern.
Definition: singleton.hpp:58
Singleton(Singleton &&)=delete
Singleton(const Singleton &)=delete
Singleton & operator=(Singleton &&)=delete
static T & instance()
access to the unique instance
Definition: singleton.hpp:104
Singleton & operator=(const Singleton &)=delete
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35
QL_DEPRECATED typedef Integer ThreadKey
Definition: singleton.hpp:124