Audacity 3.2.0
Classes | Public Types | Static Public Member Functions | Private Member Functions | Static Private Member Functions | List of all members
GlobalVariable< Tag, Type, initializer, ScopedOnly > Class Template Reference

Class template to generate global variables. More...

#include <GlobalVariable.h>

Inheritance diagram for GlobalVariable< Tag, Type, initializer, ScopedOnly >:
[legend]

Classes

struct  dummy
 
struct  Initializer
 Can guarantee that the global variable's lifetime encloses those of other objects of static duration. More...
 
class  Scope
 RAII guard for temporary installation of a value; movable. More...
 

Public Types

using variable_type = GlobalVariable
 
using stored_type = Type
 
using mutable_type = std::remove_const_t< Type >
 

Static Public Member Functions

static stored_typeGet ()
 Get the installed value. More...
 
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. More...
 

Private Member Functions

 GlobalVariable ()=delete
 Use static functions only. Don't directly construct this. More...
 

Static Private Member Functions

static mutable_typeInstance ()
 Generate the static variable. More...
 
static mutable_type Assign (mutable_type &&replacement)
 

Detailed Description

template<typename Tag, typename Type, auto initializer = nullptr, bool ScopedOnly = true>
class GlobalVariable< Tag, Type, initializer, ScopedOnly >

Class template to generate global variables.

The variable is constructed when first used, regardless of the static initialization sequence of translation units.

Template Parameters
Tagdistinguishes GlobalVariables with the same Type; often a "CRTP" argument
Typemust be non-reference, and default-constructible if there is no initializer function; if const-qualified, that means Get() gives non-mutating access, but Set() and Scope{...} are still possible if Type is movable
initializerinitial value, or a function that computes initial value
ScopedOnlyif true, then enforce RAII for changes of the variable (do not generate Set() or Scope::Commit())

Definition at line 35 of file GlobalVariable.h.

Member Typedef Documentation

◆ mutable_type

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
using GlobalVariable< Tag, Type, initializer, ScopedOnly >::mutable_type = std::remove_const_t<Type>

Definition at line 40 of file GlobalVariable.h.

◆ stored_type

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
using GlobalVariable< Tag, Type, initializer, ScopedOnly >::stored_type = Type

Definition at line 39 of file GlobalVariable.h.

◆ variable_type

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
using GlobalVariable< Tag, Type, initializer, ScopedOnly >::variable_type = GlobalVariable

Definition at line 38 of file GlobalVariable.h.

Constructor & Destructor Documentation

◆ GlobalVariable()

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
GlobalVariable< Tag, Type, initializer, ScopedOnly >::GlobalVariable ( )
privatedelete

Use static functions only. Don't directly construct this.

Member Function Documentation

◆ Assign()

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
static mutable_type GlobalVariable< Tag, Type, initializer, ScopedOnly >::Assign ( mutable_type &&  replacement)
inlinestaticprivate

Definition at line 143 of file GlobalVariable.h.

144 {
145 auto &instance = Instance();
146 auto result = std::move(instance);
147 instance = std::move(replacement);
148 return result;
149 }
static mutable_type & Instance()
Generate the static variable.

References GlobalVariable< Tag, Type, initializer, ScopedOnly >::Instance().

Referenced by GlobalVariable< Tag, Type, initializer, ScopedOnly >::Get(), GlobalVariable< Tag, Type, initializer, ScopedOnly >::Set(), and GlobalVariable< Tag, Type, initializer, ScopedOnly >::Scope::~Scope().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Get()

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
static stored_type & GlobalVariable< Tag, Type, initializer, ScopedOnly >::Get ( )
inlinestatic

Get the installed value.

Definition at line 43 of file GlobalVariable.h.

44 {
45 // Force generation of non-inline Assign, in case it needs dllexport linkage
47 return Instance();
48 }
static mutable_type Assign(mutable_type &&replacement)

References GlobalVariable< Tag, Type, initializer, ScopedOnly >::Assign(), and GlobalVariable< Tag, Type, initializer, ScopedOnly >::Instance().

Here is the call graph for this function:

◆ Instance()

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
static mutable_type & GlobalVariable< Tag, Type, initializer, ScopedOnly >::Instance ( )
inlinestaticprivate

Generate the static variable.

Definition at line 124 of file GlobalVariable.h.

125 {
126 static_assert(!std::is_reference_v<stored_type>);
127 if constexpr (std::is_convertible_v<
128 decltype(initializer), mutable_type
129 >) {
130 static mutable_type instance{ initializer };
131 return instance;
132 }
133 else if constexpr (initializer != nullptr) {
134 static mutable_type instance{ initializer() };
135 return instance;
136 }
137 else {
138 static mutable_type instance;
139 return instance;
140 }
141 }
std::remove_const_t< Type > mutable_type

References Journal::Events::anonymous_namespace{JournalEvents.cpp}::initializer.

Referenced by GlobalVariable< Tag, Type, initializer, ScopedOnly >::Assign(), GlobalVariable< Tag, Type, initializer, ScopedOnly >::Get(), and GlobalVariable< Tag, Type, initializer, ScopedOnly >::Initializer::Initializer().

Here is the caller graph for this function:

◆ Set()

template<typename Tag , typename Type , auto initializer = nullptr, bool ScopedOnly = true>
static auto GlobalVariable< Tag, Type, initializer, ScopedOnly >::Set ( std::conditional_t< ScopedOnly, dummy, mutable_type replacement) -> std::conditional_t<ScopedOnly, void, mutable_type>
inlinestatic

Move in a new value, move out and return the previous.

Calls won't compile if ScopedOnly is true, though the function can still be generated at compile time. (std::enable_if_t doesn't work here)

Definition at line 54 of file GlobalVariable.h.

57 {
58 if constexpr (!ScopedOnly)
59 return Assign(std::move(replacement));
60 }

References GlobalVariable< Tag, Type, initializer, ScopedOnly >::Assign().

Here is the call graph for this function:

The documentation for this class was generated from the following file: