Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
statics.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5#ifndef INKSCAPE_UTIL_STATICS_BIN_H
6#define INKSCAPE_UTIL_STATICS_BIN_H
7
8#include <optional>
9
10namespace Inkscape::Util {
11
12class StaticHolderBase;
13
61{
62public:
63 static StaticsBin &get();
64
65 void destroy();
66
67private:
69
71
72 friend class StaticHolderBase;
73};
74
76{
77public:
80
81protected:
83
84 virtual void destroy() = 0;
85 virtual bool active() const = 0;
86
87private:
89
91
92 friend StaticsBin;
93};
94
95template <typename... Ts>
96struct Depends;
97
98template <typename Deps>
100
101template <typename T, typename... Ts>
102struct DependencyRegisterer<Depends<T, Ts...>> : DependencyRegisterer<Depends<Ts...>>
103{
105 {
106 T::getStaticHolder();
107 }
108};
109
110template <typename T, typename Deps = Depends<>>
112 : private DependencyRegisterer<Deps>
113 , private StaticHolderBase
114{
115public:
116 template <typename... Args>
117 T &get(Args&&... args)
118 {
119 [[unlikely]] if (!opt) {
120 opt.emplace(std::forward<Args>(args)...);
121 }
122 return *opt;
123 }
124
125protected:
126 void destroy() override
127 {
128 opt.reset();
129 }
130
131 bool active() const override
132 {
133 return opt.has_value();
134 }
135
136private:
137 struct ConstructibleT : T
138 {
139 using T::T;
140 };
141
142 std::optional<ConstructibleT> opt;
143};
144
145template <typename T, typename Deps = Depends<>>
147{
148public:
151
152 template <typename... Args>
153 static T &get(Args&&... args)
154 {
155 return getStaticHolder().get(std::forward<Args>(args)...);
156 }
157
159 {
160 static StaticHolder<T, Deps> instance;
161 return instance;
162 }
163
164protected:
165 EnableSingleton() = default;
166};
167
168} // namespace Inkscape::Util
169
170#endif // INKSCAPE_UTIL_STATICS_BIN_H
static StaticHolder< T, Deps > & getStaticHolder()
Definition statics.h:158
EnableSingleton(EnableSingleton const &)=delete
EnableSingleton & operator=(EnableSingleton const &)=delete
static T & get(Args &&... args)
Definition statics.h:153
StaticHolderBase & operator=(StaticHolderBase const &)=delete
StaticHolderBase *const next
Definition statics.h:88
StaticHolderBase(StaticHolderBase const &)=delete
virtual bool active() const =0
std::optional< ConstructibleT > opt
Definition statics.h:142
T & get(Args &&... args)
Definition statics.h:117
bool active() const override
Definition statics.h:131
void destroy() override
Definition statics.h:126
The following system provides a way of dealing with statics/singletons with unusual lifetime requirem...
Definition statics.h:61
static StaticsBin & get()
Definition statics.cpp:8
StaticHolderBase * head
Definition statics.h:70
Miscellaneous supporting code.
Definition document.h:93
UI::Widget::Bin bin