Audacity  3.0.3
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Setting< T > Class Template Reference

Class template adds default value, read, and write methods to CachingSetingBase. More...

#include <Prefs.h>

Inheritance diagram for Setting< T >:
[legend]
Collaboration diagram for Setting< T >:
[legend]

Public Types

using DefaultValueFunction = std::function< T() >
 

Public Member Functions

 Setting (const SettingBase &path, const T &defaultValue)
 Usual overload supplies a default value. More...
 
 Setting (const SettingBase &path, DefaultValueFunction function)
 This overload causes recomputation of the default each time it is needed. More...
 
const T & GetDefault () const
 
bool Read (T *pVar) const
 overload of Read returning a boolean that is true if the value was previously defined *‍/ More...
 
bool ReadWithDefault (T *pVar, const T &defaultValue) const
 overload of ReadWithDefault returning a boolean that is true if the value was previously defined *‍/ More...
 
Read () const
 overload of Read, always returning a value More...
 
ReadWithDefault (const T &defaultValue) const
 new direct use is discouraged but it may be needed in legacy code More...
 
bool Write (const T &value)
 Write value to config and return true if successful. More...
 
bool Reset ()
 Reset to the default value. More...
 
- Public Member Functions inherited from CachingSettingBase< T >
 CachingSettingBase (const SettingBase &path)
 
- Public Member Functions inherited from SettingBase
 SettingBase (const char *path)
 
 SettingBase (const wxChar *path)
 
 SettingBase (const wxString &path)
 
wxConfigBase * GetConfig () const
 
const wxString & GetPath () const
 
bool Delete ()
 Delete the key if present, and return true iff it was. More...
 

Protected Member Functions

bool DoWrite ()
 Write cached value to config and return true if successful. More...
 
- Protected Member Functions inherited from CachingSettingBase< T >
 CachingSettingBase (const CachingSettingBase &)=default
 
- Protected Member Functions inherited from SettingBase
 SettingBase (const SettingBase &)=default
 

Protected Attributes

mDefaultValue {}
 
const DefaultValueFunction mFunction
 
- Protected Attributes inherited from CachingSettingBase< T >
mCurrentValue {}
 
bool mValid {false}
 
- Protected Attributes inherited from SettingBase
const RegistryPath mPath
 

Detailed Description

template<typename T>
class Setting< T >

Class template adds default value, read, and write methods to CachingSetingBase.

Definition at line 96 of file Prefs.h.

Member Typedef Documentation

◆ DefaultValueFunction

template<typename T >
using Setting< T >::DefaultValueFunction = std::function< T() >

Definition at line 101 of file Prefs.h.

Constructor & Destructor Documentation

◆ Setting() [1/2]

template<typename T >
Setting< T >::Setting ( const SettingBase path,
const T &  defaultValue 
)
inline

Usual overload supplies a default value.

Definition at line 104 of file Prefs.h.

105  : CachingSettingBase< T >{ path }
106  , mDefaultValue{ defaultValue }
107  {}

◆ Setting() [2/2]

template<typename T >
Setting< T >::Setting ( const SettingBase path,
DefaultValueFunction  function 
)
inline

This overload causes recomputation of the default each time it is needed.

Definition at line 110 of file Prefs.h.

111  : CachingSettingBase< T >{ path }
112  , mFunction{ function }
113  {}

Member Function Documentation

◆ DoWrite()

template<typename T >
bool Setting< T >::DoWrite ( )
inlineprotected

Write cached value to config and return true if successful.

(But the config object is not flushed)

Definition at line 187 of file Prefs.h.

188  {
189  const auto config = this->GetConfig();
190  return this->mValid =
191  config ? config->Write( this->mPath, this->mCurrentValue ) : false;
192  }

Referenced by Setting< bool >::Write().

Here is the caller graph for this function:

◆ GetDefault()

template<typename T >
const T& Setting< T >::GetDefault ( ) const
inline

◆ Read() [1/2]

template<typename T >
T Setting< T >::Read ( ) const
inline

overload of Read, always returning a value

The value is the default stored in this in case the key is known to be absent from the config; but it returns type T's default value if there was failure to read the config

Definition at line 150 of file Prefs.h.

151  {
152  return ReadWithDefault( GetDefault() );
153  }

◆ Read() [2/2]

template<typename T >
bool Setting< T >::Read ( T *  pVar) const
inline

◆ ReadWithDefault() [1/2]

template<typename T >
T Setting< T >::ReadWithDefault ( const T &  defaultValue) const
inline

new direct use is discouraged but it may be needed in legacy code

Use the given default in case the preference is not defined, which may not be the default-default stored in this object.

Definition at line 158 of file Prefs.h.

159  {
160  const auto config = this->GetConfig();
161  return config
162  ? ( this->mValid = true, this->mCurrentValue =
163  config->ReadObject( this->mPath, defaultValue ) )
164  : T{};
165  }

◆ ReadWithDefault() [2/2]

template<typename T >
bool Setting< T >::ReadWithDefault ( T *  pVar,
const T &  defaultValue 
) const
inline

overload of ReadWithDefault returning a boolean that is true if the value was previously defined *‍/

Definition at line 130 of file Prefs.h.

131  {
132  if ( pVar )
133  *pVar = defaultValue;
134  if ( pVar && this->mValid ) {
135  *pVar = this->mCurrentValue;
136  return true;
137  }
138  const auto config = this->GetConfig();
139  if ( pVar && config ) {
140  if ((this->mValid = config->Read( this->mPath, &this->mCurrentValue )))
141  *pVar = this->mCurrentValue;
142  return this->mValid;
143  }
144  return (this->mValid = false);
145  }

Referenced by Effect::Effect(), AudioIOBase::GetSupportedCaptureRates(), Setting< bool >::Read(), and DeviceToolBar::UpdatePrefs().

Here is the caller graph for this function:

◆ Reset()

template<typename T >
bool Setting< T >::Reset ( )
inline

Reset to the default value.

Definition at line 179 of file Prefs.h.

180  {
181  return Write( GetDefault() );
182  }

Referenced by DevicePrefs::Commit(), RecordingPrefs::Commit(), and DeviceToolBar::SetDevices().

Here is the caller graph for this function:

◆ Write()

template<typename T >
bool Setting< T >::Write ( const T &  value)
inline

Write value to config and return true if successful.

Definition at line 168 of file Prefs.h.

169  {
170  const auto config = this->GetConfig();
171  if ( config ) {
172  this->mCurrentValue = value;
173  return DoWrite();
174  }
175  return false;
176  }

Referenced by DeviceToolBar::ChangeHost(), DevicePrefs::Commit(), QualityPrefs::Commit(), DeviceToolBar::FillHostDevices(), DeviceToolBar::FillInputChannels(), AudioIO::Init(), DeviceToolBar::OnChoice(), UpdatePopupDialog::OnDontShow(), Setting< bool >::Reset(), and DeviceToolBar::SetDevices().

Here is the caller graph for this function:

Member Data Documentation

◆ mDefaultValue

template<typename T >
T Setting< T >::mDefaultValue {}
mutableprotected

Definition at line 194 of file Prefs.h.

Referenced by Setting< bool >::GetDefault().

◆ mFunction

template<typename T >
const DefaultValueFunction Setting< T >::mFunction
protected

Definition at line 195 of file Prefs.h.

Referenced by Setting< bool >::GetDefault().


The documentation for this class was generated from the following file:
Setting::DoWrite
bool DoWrite()
Write cached value to config and return true if successful.
Definition: Prefs.h:187
Setting::Write
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:168
SettingBase::mPath
const RegistryPath mPath
Definition: Prefs.h:78
Setting::GetDefault
const T & GetDefault() const
Definition: Prefs.h:116
Setting::ReadWithDefault
bool ReadWithDefault(T *pVar, const T &defaultValue) const
overload of ReadWithDefault returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:130
CachingSettingBase::mCurrentValue
T mCurrentValue
Definition: Prefs.h:90
Setting::mDefaultValue
T mDefaultValue
Definition: Prefs.h:194
Setting::mFunction
const DefaultValueFunction mFunction
Definition: Prefs.h:195
CachingSettingBase::mValid
bool mValid
Definition: Prefs.h:91
SettingBase::GetConfig
wxConfigBase * GetConfig() const
Definition: Prefs.cpp:403
CachingSettingBase
Class template adds an in-memory cache of a value to SettingBase.
Definition: Prefs.h:84