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, Type(*)() 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
initializeroptional 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 , Type(*)() 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 , Type(*)() 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 , Type(*)() 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 , Type(*)() 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 , Type(*)() initializer = nullptr, bool ScopedOnly = true>
static mutable_type GlobalVariable< Tag, Type, initializer, ScopedOnly >::Assign ( mutable_type &&  replacement)
inlinestaticprivate

Definition at line 137 of file GlobalVariable.h.

138 {
139 auto &instance = Instance();
140 auto result = std::move(instance);
141 instance = std::move(replacement);
142 return result;
143 }
static mutable_type & Instance()
Generate the static variable.

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

Here is the caller graph for this function:

◆ Get()

template<typename Tag , typename Type , Type(*)() 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().

Here is the call graph for this function:

◆ Instance()

template<typename Tag , typename Type , Type(*)() 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 (initializer != nullptr) {
128 static mutable_type instance{ initializer() };
129 return instance;
130 }
131 else {
132 static mutable_type instance;
133 return instance;
134 }
135 }
std::remove_const_t< Type > mutable_type

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

◆ Set()

template<typename Tag , typename Type , Type(*)() 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 }

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