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 ValueType = T
 
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)
 
audacity::BasicSettingsGetConfig () 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 177 of file Prefs.h.

Member Typedef Documentation

◆ DefaultValueFunction

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

Definition at line 184 of file Prefs.h.

◆ ValueType

template<typename T >
using Setting< T >::ValueType = T

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

189 , mDefaultValue{ defaultValue }
190 {}
Class template adds an in-memory cache of a value to TransactionalSettingBase and support for Setting...
Definition: Prefs.h:162
T mDefaultValue
Definition: Prefs.h:340

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

195 , mFunction{ function }
196 {}
const DefaultValueFunction mFunction
Definition: Prefs.h:339

Member Function Documentation

◆ Commit()

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

Implements TransactionalSettingBase.

Definition at line 303 of file Prefs.h.

304 {
305 // This can be only called from within the transaction
306 assert(!this->mPreviousValues.empty());
307
308 if (this->mPreviousValues.empty())
309 return false;
310
311 const auto result = this->mPreviousValues.size() > 1 || DoWrite();
312 mPreviousValues.pop_back();
313
314 return result;
315 }
bool DoWrite()
Write cached value to config and return true if successful.
Definition: Prefs.h:332
std::vector< T > mPreviousValues
Definition: Prefs.h:341

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

333 {
334 const auto config = this->GetConfig();
335 return this->mValid =
336 config ? config->Write( this->mPath, this->mCurrentValue ) : false;
337 }
audacity::BasicSettings * GetConfig() const
Definition: Prefs.cpp:544
const SettingPath mPath
Definition: Prefs.h:95

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

296 {
297 const T value = Read();
298
299 for (size_t i = mPreviousValues.size(); i < depth; ++i)
300 this->mPreviousValues.emplace_back( value );
301 }
T Read() const
overload of Read, always returning a value
Definition: Prefs.h:233

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

290 {
291 this->mValid = false;
292 }

Referenced by anonymous_namespace{AudioComPrefsPanel.cpp}::AudioComPrefsPanel::Commit(), DevicePrefs::Commit(), GUIPrefs::Commit(), MidiIOPrefs::Commit(), RecordingPrefs::Commit(), TracksBehaviorsPrefs::Commit(), TracksPrefs::Commit(), and audacity::cloud::audiocom::anonymous_namespace{OAuthService.cpp}::OAuthServiceSettingsResetHandler::OnSettingResetEnd().

Here is the caller graph for this function:

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

234 {
235 return ReadWithDefault( GetDefault() );
236 }
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:213
const T & GetDefault() const
Definition: Prefs.h:199

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

208 {
209 return ReadWithDefault( pVar, GetDefault() );
210 }

Referenced by AudioIO::AudioIO(), audacity::cloud::audiocom::OAuthService::AuthoriseRefreshToken(), anonymous_namespace{AudioComPrefsPanel.cpp}::AudioComPrefsPanel::Browse(), DeviceToolBar::ChangeDevice(), AudioSetupToolBar::ChangeDeviceLabel(), DeviceToolBar::ChangeHost(), AudioSetupToolBar::ChangeHost(), ProjectAudioManager::ChooseExistingRecordingTracks(), RecordingPrefs::Commit(), ProjectAudioManager::DoRecord(), audacity::cloud::audiocom::UserService::DownloadAvatar(), SpectrumVZoomHandle::DoZoom(), anonymous_namespace{WaveformView.cpp}::DrawIndividualSamples(), EditToolBar::EditToolBar(), ExportAudioDialog::ExportAudioDialog(), FFmpegStartup(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), AudioIOBase::GetDeviceInfo(), audacity::cloud::audiocom::UserService::GetDisplayName(), GetEditClipsCanMove(), ProjectManager::GetEstimatedRecordingMinsLeftOnDisk(), GetMIDIDeviceInfo(), GetNextWindowPlacement(), AudioIOBase::getPlayDevIndex(), AudioIOBase::getRecordDevIndex(), FFmpegFunctions::GetSearchPaths(), anonymous_namespace{EnvelopeHandle.cpp}::GetTimeTrackData(), audacity::cloud::audiocom::UserService::GetUserId(), audacity::cloud::audiocom::UserService::GetUserSlug(), AudioIOBase::HandleDeviceChange(), anonymous_namespace{TracksPrefs.cpp}::Handler::Handler(), audacity::cloud::audiocom::OAuthService::HasRefreshToken(), AudacityApp::InitPart2(), AudioIOBase::IsCaptureRateSupported(), SpectrogramSettings::Globals::LoadPrefs(), SpectrogramSettings::LoadPrefs(), WaveformSettings::LoadPrefs(), SpectrogramSettings::ColorSchemeEnumSetting::Migrate(), ToolManager::ModifyToolbarMenus(), anonymous_namespace{TransportMenus.cpp}::OnPunchAndRoll(), FrequencyPlotDialog::OnReplot(), anonymous_namespace{PluginMenus.cpp}::OnResetConfig(), anonymous_namespace{CloudProjectFileIOExtensions.cpp}::IOExtension::OnSave(), FFmpegImportPlugin::Open(), audacity::cloud::audiocom::sync::anonymous_namespace{CloudSyncHousekeeper.cpp}::Housekeeper::PerformHousekeeping(), FrequencyPlotDialog::Populate(), DevicePrefs::Populate(), MidiIOPrefs::Populate(), FFmpegNotFoundDialog::PopulateOrExchange(), SoundActivatedRecordDialog::PopulateOrExchange(), RecordingPrefs::PopulateOrExchange(), ProjectRate::ProjectRate(), anonymous_namespace{SelectionBar.cpp}::ReadSelectionMode(), ReadSnapTo(), Journal::RecordEnabled(), audacity::cloud::audiocom::ServiceConfig::ServiceConfig(), FrequencyPlotDialog::Show(), UpdateManager::Start(), AudioIO::StartMonitoring(), AudioIO::StartPortAudioStream(), anonymous_namespace{MIDIPlay.h}::MIDIPlay::StartPortMidiStream(), AudioIO::StartStream(), AudioIO::StopStream(), audacity::cloud::audiocom::CloudSyncService::SyncCloudSnapshot(), WaveTrack::SyncLockAdjust(), audacity::network_manager::anonymous_namespace{CurlHandleManager.cpp}::CurlConfig::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 241 of file Prefs.h.

242 {
243 if (this->mValid)
244 return this->mCurrentValue;
245 const auto config = this->GetConfig();
246 if (config) {
247 this->mCurrentValue =
248 config->ReadObject(this->mPath, defaultValue);
249 // If config file contains a value that agrees with the default, we
250 // can't detect that, so assume invalidity still
251 this->mValid = (this->mCurrentValue != defaultValue);
252 return this->mCurrentValue;
253 }
254 else
255 return T{};
256 }

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

214 {
215 if ( pVar )
216 *pVar = defaultValue;
217 if ( pVar && this->mValid ) {
218 *pVar = this->mCurrentValue;
219 return true;
220 }
221 const auto config = this->GetConfig();
222 if ( pVar && config ) {
223 if ((this->mValid = config->Read( this->mPath, &this->mCurrentValue )))
224 *pVar = this->mCurrentValue;
225 return this->mValid;
226 }
227 return (this->mValid = false);
228 }

References SettingBase::GetConfig().

Referenced by EffectBase::EffectBase(), WaveTrack::GetDefaultAudioTrackNamePreference(), GetNextWindowPlacement(), AudioIOBase::IsCaptureRateSupported(), 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 284 of file Prefs.h.

285 {
286 return Write( GetDefault() );
287 }
bool Write(const T &value)
Write value to config and return true if successful.
Definition: Prefs.h:259

Referenced by DevicePrefs::Commit(), RecordingPrefs::Commit(), anonymous_namespace{PluginMenus.cpp}::OnResetConfig(), 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 317 of file Prefs.h.

318 {
319 // This can be only called from within the transaction
320 assert(!this->mPreviousValues.empty());
321
322 if (!this->mPreviousValues.empty())
323 {
324 this->mCurrentValue = std::move(this->mPreviousValues.back());
325 this->mPreviousValues.pop_back();
326 }
327 }

◆ Write()

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

Write value to config and return true if successful.

Definition at line 259 of file Prefs.h.

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

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

Referenced by DeviceToolBar::ChangeHost(), AudioSetupToolBar::ChangeHost(), audacity::cloud::audiocom::UserService::ClearUserData(), DevicePrefs::Commit(), MidiIOPrefs::Commit(), anonymous_namespace{FileMenus.cpp}::DoImport(), audacity::cloud::audiocom::UserService::DownloadAvatar(), TimerRecordDialog::ExecutePostRecordActions(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), FindFFmpegLibs(), AudioIO::Init(), LoadFFmpeg(), AudioSetupToolBar::OnChannels(), DeviceToolBar::OnChoice(), ExportAudioDialog::OnExport(), FFmpegNotFoundDialog::OnOk(), BenchmarkDialog::OnRun(), FFmpegImportPlugin::Open(), audacity::cloud::audiocom::OAuthService::ParseTokenResponse(), ProjectRate::ProjectRate(), ReadSnapTo(), SpectrogramSettings::Globals::SavePrefs(), SpectrogramSettings::SavePrefs(), anonymous_namespace{ProjectManager.cpp}::SaveWindowPreferences(), AudioSetupToolBar::SetDevices(), DeviceToolBar::SetDevices(), ProjectTimeSignature::SetLowerTimeSignature(), AudioIO::SetMixer(), Journal::SetRecordEnabled(), ProjectSnap::SetSnapTo(), ProjectTimeSignature::SetTempo(), ProjectTimeSignature::SetUpperTimeSignature(), UpdateManager::Start(), audacity::cloud::audiocom::OAuthService::UnlinkAccount(), anonymous_namespace{SelectionBar.cpp}::UpdateSelectionMode(), and audacity::cloud::audiocom::UserService::UpdateUserData().

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

◆ mFunction

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

Definition at line 339 of file Prefs.h.

◆ mPreviousValues

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

Definition at line 341 of file Prefs.h.


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