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)
 
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 182 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 185 of file Prefs.h.

187 , mDefaultValue{ defaultValue }
188 {}
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:338

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

193 , mFunction{ function }
194 {}
const DefaultValueFunction mFunction
Definition: Prefs.h:337

Member Function Documentation

◆ Commit()

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

Implements TransactionalSettingBase.

Definition at line 301 of file Prefs.h.

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

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

331 {
332 const auto config = this->GetConfig();
333 return this->mValid =
334 config ? config->Write( this->mPath, this->mCurrentValue ) : false;
335 }
audacity::BasicSettings * GetConfig() const
Definition: Prefs.cpp:518
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 293 of file Prefs.h.

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

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

288 {
289 this->mValid = false;
290 }

Referenced by ApplicationPrefs::Commit(), DevicePrefs::Commit(), GUIPrefs::Commit(), RecordingPrefs::Commit(), TracksBehaviorsPrefs::Commit(), and TracksPrefs::Commit().

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

232 {
233 return ReadWithDefault( GetDefault() );
234 }
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:211
const T & GetDefault() const
Definition: Prefs.h:197

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

206 {
207 return ReadWithDefault( pVar, GetDefault() );
208 }

Referenced by AudioIO::AudioIO(), cloud::audiocom::OAuthService::AuthoriseRefreshToken(), DeviceToolBar::ChangeDevice(), AudioSetupToolBar::ChangeDeviceLabel(), DeviceToolBar::ChangeHost(), AudioSetupToolBar::ChangeHost(), ProjectAudioManager::ChooseExistingRecordingTracks(), RecordingPrefs::Commit(), ConfirmSave(), ProjectAudioManager::DoRecord(), cloud::audiocom::UserService::DownloadAvatar(), SpectrumVZoomHandle::DoZoom(), EditToolBar::EditToolBar(), ExportAudioDialog::ExportAudioDialog(), FFmpegStartup(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), AudioIOBase::GetDeviceInfo(), cloud::audiocom::UserService::GetDisplayName(), GetEditClipsCanMove(), ProjectManager::GetEstimatedRecordingMinsLeftOnDisk(), GetMIDIDeviceInfo(), GetNextWindowPlacement(), AudioIOBase::getPlayDevIndex(), AudioIOBase::getRecordDevIndex(), FFmpegFunctions::GetSearchPaths(), AudioIOBase::GetSupportedCaptureRates(), anonymous_namespace{EnvelopeHandle.cpp}::GetTimeTrackData(), cloud::audiocom::UserService::GetUserSlug(), AudioIOBase::HandleDeviceChange(), cloud::audiocom::OAuthService::HasRefreshToken(), SpectrogramSettings::Globals::LoadPrefs(), SpectrogramSettings::LoadPrefs(), WaveformSettings::LoadPrefs(), SpectrogramSettings::ColorSchemeEnumSetting::Migrate(), ToolManager::ModifyToolbarMenus(), FrequencyPlotDialog::OnReplot(), anonymous_namespace{PluginMenus.cpp}::OnResetConfig(), UpdateManager::OnTimer(), FFmpegImportPlugin::Open(), FrequencyPlotDialog::Populate(), DevicePrefs::Populate(), FFmpegNotFoundDialog::PopulateOrExchange(), SoundActivatedRecordDialog::PopulateOrExchange(), RecordingPrefs::PopulateOrExchange(), ProjectRate::ProjectRate(), ThemeBase::ReadImageCache(), anonymous_namespace{SelectionBar.cpp}::ReadSelectionMode(), ReadSnapTo(), 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(), 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 239 of file Prefs.h.

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

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

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

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

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

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

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

◆ Write()

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

Write value to config and return true if successful.

Definition at line 257 of file Prefs.h.

258 {
259 const auto config = this->GetConfig();
260
261 if (config == nullptr)
262 return false;
263
264 switch ( SettingScope::Add( *this ) ) {
265 // Eager writes, but not flushed, when there is no transaction
266 default:
268 this->mCurrentValue = value;
269 return DoWrite();
270 }
271
272 // Deferred writes, with flush, if there is a commit later
275 this->mCurrentValue = value;
276 this->mValid = true;
277 return true;
278 }
279 }
static AddResult Add(TransactionalSettingBase &setting)
Definition: Prefs.cpp:278
@ 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(), cloud::audiocom::UserService::ClearUserData(), DevicePrefs::Commit(), cloud::audiocom::OAuthService::DoAuthorise(), anonymous_namespace{FileMenus.cpp}::DoImport(), cloud::audiocom::UserService::DownloadAvatar(), TimerRecordDialog::ExecutePostRecordActions(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), FindFFmpegLibs(), AudioIO::Init(), LoadFFmpeg(), AudioSetupToolBar::OnChannels(), DeviceToolBar::OnChoice(), UpdatePopupDialog::OnDontShow(), ExportAudioDialog::OnExport(), App::OnInit(), FFmpegNotFoundDialog::OnOk(), BenchmarkDialog::OnRun(), FFmpegImportPlugin::Open(), 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(), cloud::audiocom::OAuthService::UnlinkAccount(), anonymous_namespace{SelectionBar.cpp}::UpdateSelectionMode(), and 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 338 of file Prefs.h.

◆ mFunction

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

Definition at line 337 of file Prefs.h.

◆ mPreviousValues

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

Definition at line 339 of file Prefs.h.


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