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

#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...
 
void Invalidate () override
 
- Public Member Functions inherited from CachingSettingBase< T >
 CachingSettingBase (const SettingBase &path)
 
- Public Member Functions inherited from TransactionalSettingBase
virtual void Invalidate ()=0
 
 SettingBase (const char *path)
 
 SettingBase (const wxChar *path)
 
 SettingBase (const wxString &path)
 
 SettingBase (const SettingBase &)=default
 
- Public Member Functions inherited from SettingBase
 SettingBase (const char *path)
 
 SettingBase (const wxChar *path)
 
 SettingBase (const wxString &path)
 
wxConfigBase * GetConfig () const
 
const SettingPathGetPath () 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
 
virtual void EnterTransaction (size_t depth)=0
 
virtual bool Commit ()=0
 
virtual void Rollback () noexcept=0
 
- Protected Member Functions inherited from SettingBase
 SettingBase (const SettingBase &)=default
 

Protected Attributes

const DefaultValueFunction mFunction
 
mDefaultValue {}
 
std::vector< T > mPreviousValues
 
- Protected Attributes inherited from CachingSettingBase< T >
mCurrentValue {}
 
bool mValid {false}
 
- Protected Attributes inherited from SettingBase
const SettingPath mPath
 

Private Member Functions

void EnterTransaction (size_t depth) override
 
bool Commit () override
 
void Rollback () noexcept override
 

Detailed Description

template<typename T>
class Setting< T >

Class template adds default value, read, and write methods to CachingSetingBase and generates TransactionalSettingBase virtual functions

Definition at line 172 of file Prefs.h.

Member Typedef Documentation

◆ DefaultValueFunction

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

Definition at line 177 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 180 of file Prefs.h.

182 , mDefaultValue{ defaultValue }
183 {}
Class template adds an in-memory cache of a value to TransactionalSettingBase and support for Setting...
Definition: Prefs.h:157
T mDefaultValue
Definition: Prefs.h:333

◆ 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 186 of file Prefs.h.

188 , mFunction{ function }
189 {}
const DefaultValueFunction mFunction
Definition: Prefs.h:332

Member Function Documentation

◆ Commit()

template<typename T >
bool Setting< T >::Commit ( )
inlineoverrideprivatevirtual
Returns
true if successful

Implements TransactionalSettingBase.

Definition at line 296 of file Prefs.h.

297 {
298 // This can be only called from within the transaction
299 assert(!this->mPreviousValues.empty());
300
301 if (this->mPreviousValues.empty())
302 return false;
303
304 const auto result = this->mPreviousValues.size() > 1 || DoWrite();
305 mPreviousValues.pop_back();
306
307 return result;
308 }
bool DoWrite()
Write cached value to config and return true if successful.
Definition: Prefs.h:325
std::vector< T > mPreviousValues
Definition: Prefs.h:334

◆ 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 325 of file Prefs.h.

326 {
327 const auto config = this->GetConfig();
328 return this->mValid =
329 config ? config->Write( this->mPath, this->mCurrentValue ) : false;
330 }
wxConfigBase * GetConfig() const
Definition: Prefs.cpp:503
const SettingPath mPath
Definition: Prefs.h:90

References SettingBase::GetConfig(), and SettingBase::mPath.

Here is the call graph for this function:

◆ EnterTransaction()

template<typename T >
void Setting< T >::EnterTransaction ( size_t  depth)
inlineoverrideprivatevirtual

Implements TransactionalSettingBase.

Definition at line 288 of file Prefs.h.

289 {
290 const T value = Read();
291
292 for (size_t i = mPreviousValues.size(); i < depth; ++i)
293 this->mPreviousValues.emplace_back( value );
294 }
T Read() const
overload of Read, always returning a value
Definition: Prefs.h:226

References Read().

Here is the call graph for this function:

◆ GetDefault()

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

◆ Invalidate()

template<typename T >
void Setting< T >::Invalidate ( )
inlineoverridevirtual

Implements TransactionalSettingBase.

Definition at line 282 of file Prefs.h.

283 {
284 this->mValid = false;
285 }

◆ 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 226 of file Prefs.h.

227 {
228 return ReadWithDefault( GetDefault() );
229 }
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:206
const T & GetDefault() const
Definition: Prefs.h:192

◆ Read() [2/2]

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

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

Definition at line 200 of file Prefs.h.

201 {
202 return ReadWithDefault( pVar, GetDefault() );
203 }

Referenced by AudioIO::AudioIO(), DeviceToolBar::ChangeDevice(), AudioSetupToolBar::ChangeDevice(), DeviceToolBar::ChangeHost(), AudioSetupToolBar::ChangeHost(), ProjectAudioManager::ChooseExistingRecordingTracks(), RecordingPrefs::Commit(), ConfirmSave(), ProjectAudioManager::DoRecord(), SpectrumVZoomHandle::DoZoom(), ExportWavPack::Export(), FFmpegStartup(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), AudioIOBase::GetDeviceInfo(), GetEditClipsCanMove(), ProjectManager::GetEstimatedRecordingMinsLeftOnDisk(), GetMIDIDeviceInfo(), GetNextWindowPlacement(), AudioIOBase::getPlayDevIndex(), AudioIOBase::getRecordDevIndex(), FFmpegFunctions::GetSearchPaths(), AudioIOBase::GetSupportedCaptureRates(), AudioIOBase::HandleDeviceChange(), SpectrogramSettings::Globals::LoadPrefs(), SpectrogramSettings::LoadPrefs(), WaveformSettings::LoadPrefs(), SpectrogramSettings::ColorSchemeEnumSetting::Migrate(), FrequencyPlotDialog::OnReplot(), UpdateManager::OnTimer(), FFmpegImportPlugin::Open(), FrequencyPlotDialog::Populate(), DevicePrefs::Populate(), QualityPrefs::Populate(), ExportWavPackOptions::PopulateOrExchange(), FFmpegNotFoundDialog::PopulateOrExchange(), SoundActivatedRecordDialog::PopulateOrExchange(), RecordingPrefs::PopulateOrExchange(), ProjectRate::ProjectRate(), ThemeBase::ReadImageCache(), Journal::RecordEnabled(), ResetPreferences(), FrequencyPlotDialog::Show(), UpdateManager::Start(), AudioIO::StartMonitoring(), AudioIO::StartPortAudioStream(), anonymous_namespace{MIDIPlay.h}::MIDIPlay::StartPortMidiStream(), AudioIO::StartStream(), AudioIO::StopStream(), WaveTrack::SyncLockAdjust(), UpdatePopupDialog::UpdatePopupDialog(), ViewInfo::UpdatePrefs(), ZoomInfo::UpdatePrefs(), AdornedRulerPanel::UpdatePrefs(), SpectrogramSettings::UpdatePrefs(), WaveformSettings::UpdatePrefs(), AudioSetupToolBar::UpdatePrefs(), DeviceToolBar::UpdatePrefs(), TrackArtist::UpdatePrefs(), and MeterPanel::UpdatePrefs().

◆ 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 234 of file Prefs.h.

235 {
236 if (this->mValid)
237 return this->mCurrentValue;
238 const auto config = this->GetConfig();
239 if (config) {
240 this->mCurrentValue =
241 config->ReadObject(this->mPath, defaultValue);
242 // If config file contains a value that agrees with the default, we
243 // can't detect that, so assume invalidity still
244 this->mValid = (this->mCurrentValue != defaultValue);
245 return this->mCurrentValue;
246 }
247 else
248 return T{};
249 }

References SettingBase::GetConfig(), and SettingBase::mPath.

Here is the call graph for this function:

◆ 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 206 of file Prefs.h.

207 {
208 if ( pVar )
209 *pVar = defaultValue;
210 if ( pVar && this->mValid ) {
211 *pVar = this->mCurrentValue;
212 return true;
213 }
214 const auto config = this->GetConfig();
215 if ( pVar && config ) {
216 if ((this->mValid = config->Read( this->mPath, &this->mCurrentValue )))
217 *pVar = this->mCurrentValue;
218 return this->mValid;
219 }
220 return (this->mValid = false);
221 }

References SettingBase::GetConfig().

Referenced by EffectBase::EffectBase(), WaveTrack::GetDefaultAudioTrackNamePreference(), GetNextWindowPlacement(), AudioIOBase::GetSupportedCaptureRates(), AudioSetupToolBar::UpdatePrefs(), and DeviceToolBar::UpdatePrefs().

Here is the call graph for this function:
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 277 of file Prefs.h.

278 {
279 return Write( GetDefault() );
280 }
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:252

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

Here is the caller graph for this function:

◆ Rollback()

template<typename T >
void Setting< T >::Rollback ( )
inlineoverrideprivatevirtualnoexcept

Implements TransactionalSettingBase.

Definition at line 310 of file Prefs.h.

311 {
312 // This can be only called from within the transaction
313 assert(!this->mPreviousValues.empty());
314
315 if (!this->mPreviousValues.empty())
316 {
317 this->mCurrentValue = std::move(this->mPreviousValues.back());
318 this->mPreviousValues.pop_back();
319 }
320 }

◆ Write()

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

Write value to config and return true if successful.

Definition at line 252 of file Prefs.h.

253 {
254 const auto config = this->GetConfig();
255
256 if (config == nullptr)
257 return false;
258
259 switch ( SettingScope::Add( *this ) ) {
260 // Eager writes, but not flushed, when there is no transaction
261 default:
263 this->mCurrentValue = value;
264 return DoWrite();
265 }
266
267 // Deferred writes, with flush, if there is a commit later
270 this->mCurrentValue = value;
271 this->mValid = true;
272 return true;
273 }
274 }
static AddResult Add(TransactionalSettingBase &setting)
Definition: Prefs.cpp:263
@ NotAdded
Definition: Prefs.h:128
@ PreviouslyAdded
Definition: Prefs.h:128

References SettingScope::Add(), SettingScope::Added, SettingBase::GetConfig(), SettingScope::NotAdded, and SettingScope::PreviouslyAdded.

Referenced by DeviceToolBar::ChangeHost(), AudioSetupToolBar::ChangeHost(), DevicePrefs::Commit(), QualityPrefs::Commit(), anonymous_namespace{FileMenus.cpp}::DoImport(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), FindFFmpegLibs(), AudioIO::Init(), LoadFFmpeg(), DeviceToolBar::OnChoice(), UpdatePopupDialog::OnDontShow(), App::OnInit(), AudioSetupToolBar::OnMenu(), FFmpegNotFoundDialog::OnOk(), BenchmarkDialog::OnRun(), FFmpegImportPlugin::Open(), ProjectRate::ProjectRate(), SpectrogramSettings::Globals::SavePrefs(), SpectrogramSettings::SavePrefs(), anonymous_namespace{ProjectManager.cpp}::SaveWindowPreferences(), AudioSetupToolBar::SetDevices(), DeviceToolBar::SetDevices(), AudioIO::SetMixer(), Journal::SetRecordEnabled(), and UpdateManager::Start().

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

Member Data Documentation

◆ mDefaultValue

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

Definition at line 333 of file Prefs.h.

◆ mFunction

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

Definition at line 332 of file Prefs.h.

◆ mPreviousValues

template<typename T >
std::vector<T> Setting< T >::mPreviousValues
protected

Definition at line 334 of file Prefs.h.


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