10#ifndef __AUDACITY_GLOBAL_VARIABLE__
11#define __AUDACITY_GLOBAL_VARIABLE__
34 bool ScopedOnly =
true>
54 static auto Set( std::conditional_t<ScopedOnly,
dummy,
55 mutable_type> replacement) -> std::conditional_t<ScopedOnly, void,
58 if constexpr (!ScopedOnly)
59 return Assign(std::move(replacement));
80 if constexpr (ScopedOnly)
88 static_assert(!ScopedOnly);
93 if constexpr (ScopedOnly)
100 std::conditional_t<ScopedOnly, mutable_type, std::optional<mutable_type>>
126 static_assert(!std::is_reference_v<stored_type>);
127 if constexpr (std::is_convertible_v<
146 auto result = std::move(instance);
147 instance = std::move(replacement);
153template<
typename Tag,
typename Signature,
auto Default =
nullptr,
156 const std::function<Signature>, Default, Options...
160 using result_type =
typename std::function<Signature>::result_type;
162 const std::function<Signature>,
Default, Options...
168 template<
typename... Arguments>
173 return fn(std::forward<Arguments>(arguments)...);
174 else if constexpr (std::is_void_v<result_type>)
181 template<
typename Derived>
185 std::make_unique<Derived>(std::forward<
decltype(args)>(args)...);
190 template<
typename Derived>
194 std::make_shared<Derived>(std::forward<
decltype(args)>(args)...);
204template<
typename Tag,
auto DefaultFunction,
auto... Options>
206 std::remove_pointer_t<decltype(DefaultFunction)>, DefaultFunction,
Global function-valued variable, adding a convenient Call()
typename std::function< Signature >::result_type result_type
static result_type Call(Arguments &&...arguments)
Null check of the installed function is done for you.
typename GlobalVariable< Tag, const std::function< Signature >, Default, Options... >::Scope Scope
RAII guard for temporary installation of a value; movable.
std::conditional_t< ScopedOnly, mutable_type, std::optional< mutable_type > > m_previous
Scope(mutable_type value)
Scope & operator=(Scope &&other)=default
Scope(Scope &&other)=default
Class template to generate global variables.
GlobalVariable()=delete
Use static functions only. Don't directly construct this.
static mutable_type & Instance()
Generate the static variable.
std::remove_const_t< Type > mutable_type
static mutable_type Assign(mutable_type &&replacement)
static auto Set(std::conditional_t< ScopedOnly, dummy, mutable_type > replacement) -> std::conditional_t< ScopedOnly, void, mutable_type >
Move in a new value, move out and return the previous.
static stored_type & Get()
Get the installed value.
RegisteredInitializer initializer
Can generate overriding hooks of shared_ptr factories.
Can generate overriding hooks of unique_ptr factories.
Can guarantee that the global variable's lifetime encloses those of other objects of static duration.