Audacity 3.2.0
BasicSettings.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 BasicSettings.h
7
8 Vitaly Sverchinsky
9
10**********************************************************************/
11#pragma once
12
13#include <memory>
14#include <wx/string.h>
15#include <wx/arrstr.h>
16#include "GlobalVariable.h"
17
18namespace audacity
19{
30 class PREFERENCES_API BasicSettings
31 {
32 public:
33 class PREFERENCES_API GroupScope final
34 {
35 friend class BasicSettings;
36
37 std::optional<std::reference_wrapper<BasicSettings>> mSettings;
38
40
41 public:
42 GroupScope(const GroupScope&) = delete;
44 GroupScope& operator=(const GroupScope&) = delete;
46
47 void Reset() noexcept;
48
49 ~GroupScope();
50 };
51
53 virtual ~BasicSettings();
54
55 BasicSettings(const BasicSettings&) = delete;
57 BasicSettings& operator=(const BasicSettings&) = delete;
58 BasicSettings& operator=(BasicSettings&&) = default;
59
61 virtual wxString GetGroup() const = 0;
63 virtual wxArrayString GetChildGroups() const = 0;
65 virtual wxArrayString GetChildKeys() const = 0;
66
68 virtual bool HasEntry(const wxString& key) const = 0;
70 virtual bool HasGroup(const wxString& key) const = 0;
72 virtual bool Exists(const wxString& key) const;
73
76 virtual bool Remove(const wxString& key) = 0;
78 virtual void Clear() = 0;
79
81 bool DeleteGroup(const wxString& key);
83 bool DeleteEntry(const wxString& key);
84
88 GroupScope BeginGroup(const wxString& prefix);
89
90 virtual bool Read(const wxString& key, bool* value) const = 0;
91 virtual bool Read(const wxString& key, int* value) const = 0;
92 virtual bool Read(const wxString& key, long* value) const = 0;
93 virtual bool Read(const wxString& key, long long* value) const = 0;
94 virtual bool Read(const wxString& key, double* value) const = 0;
95 virtual bool Read(const wxString& key, wxString* value) const = 0;
96 virtual bool Read(const wxString& key, float* value) const;
97
99 template<typename T>
100 bool Read(const wxString& key, T* value) const
101 {
102 wxString str;
103 if (!Read(key, &str))
104 return false;
105 return wxFromString(str, value);
106 }
107
108 template<typename T>
109 std::enable_if_t<std::is_scalar_v<T>, bool>
110 Read(const wxString& key, T* value, T defaultValue) const
111 {
112 if(!Read(key, value))
113 {
114 *value = defaultValue;
115 return false;
116 }
117 return true;
118 }
119
120 template<typename T>
121 std::enable_if_t<!std::is_scalar_v<T>, bool>
122 Read(const wxString& key, T* value, const T& defaultValue)
123 {
124 if(!Read(key, value))
125 {
126 *value = defaultValue;
127 return false;
128 }
129 return true;
130 }
131
132 wxString Read(const wxString& key, const wxString& defaultValue = wxEmptyString) const;
133 wxString Read(const wxString& key, const char* defaultValue) const;
134 wxString Read(const wxString& key, const wchar_t* defaultValue) const;
135
136 template<typename T>
137 std::enable_if_t<std::is_scalar_v<T>, T>
138 Read(const wxString& key, T defaultValue) const
139 {
140 T value;
141 if(!Read(key, &value))
142 return defaultValue;
143 return value;
144 }
145
146 template<typename T>
147 std::enable_if_t<!std::is_scalar_v<T>, T>
148 Read(const wxString& key, const T& defaultValue) const
149 {
150 T value;
151 if(!Read(key, &value))
152 return defaultValue;
153 return value;
154 }
155
156 virtual bool Write(const wxString& key, bool value) = 0;
157 virtual bool Write(const wxString& key, int value) = 0;
158 virtual bool Write(const wxString& key, long value) = 0;
159 virtual bool Write(const wxString& key, long long value) = 0;
160 virtual bool Write(const wxString& key, double value) = 0;
161 virtual bool Write(const wxString& key, const wxString& value) = 0;
162
163 virtual bool Write(const wxString& key, float value);
164 virtual bool Write(const wxString& key, const char* value);
165 virtual bool Write(const wxString& key, const wchar_t* value);
166
168 template<typename T>
169 bool Write(const wxString& key, const T& value)
170 {
171 return Write(key, wxToString(value));
172 }
173
174 virtual bool Flush() noexcept = 0;
175
176 bool ReadBool(const wxString& key, bool defaultValue) const;
177 long ReadLong(const wxString& key, long defaultValue) const;
178 double ReadDouble(const wxString& key, double defaultValue) const;
179
180 template <typename T>
181 T ReadObject(const wxString& key, const T& defaultValue) const
182 {
183 return Read(key, defaultValue);
184 }
185
186 protected:
187
188 virtual void DoBeginGroup(const wxString& prefix) = 0;
189 virtual void DoEndGroup() noexcept = 0;
190 };
191
193 struct PREFERENCES_API ApplicationSettings final : GlobalHook<ApplicationSettings, std::unique_ptr<BasicSettings>()> { };
194}
#define str(a)
static const AudacityProject::AttachedObjects::RegisteredFactory key
wxString wxToString(const Stringifyable &obj)
bool wxFromString(const wxString &str, Stringifyable *obj)
static Settings & settings()
Definition: TrackInfo.cpp:47
Global function-valued variable, adding a convenient Call()
GroupScope(GroupScope &&)=delete
GroupScope & operator=(GroupScope &&)=delete
GroupScope & operator=(const GroupScope &)=delete
std::optional< std::reference_wrapper< BasicSettings > > mSettings
Definition: BasicSettings.h:37
GroupScope(const GroupScope &)=delete
Base class for objects that provide facility to store data persistently, and access it with string ke...
Definition: BasicSettings.h:31
virtual bool Write(const wxString &key, const wxString &value)=0
std::enable_if_t< std::is_scalar_v< T >, bool > Read(const wxString &key, T *value, T defaultValue) const
virtual bool Flush() noexcept=0
std::enable_if_t<!std::is_scalar_v< T >, T > Read(const wxString &key, const T &defaultValue) const
virtual bool Write(const wxString &key, double value)=0
virtual bool Write(const wxString &key, bool value)=0
virtual bool Write(const wxString &key, long value)=0
virtual bool Write(const wxString &key, long long value)=0
std::enable_if_t< std::is_scalar_v< T >, T > Read(const wxString &key, T defaultValue) const
bool Write(const wxString &key, const T &value)
Uses wxToString to convert object into string.
virtual void DoBeginGroup(const wxString &prefix)=0
virtual bool Write(const wxString &key, int value)=0
virtual void DoEndGroup() noexcept=0
std::enable_if_t<!std::is_scalar_v< T >, bool > Read(const wxString &key, T *value, const T &defaultValue)
PROJECT_FILE_IO_API void Remove(const FilePath &path)
STL namespace.
Provides an access to application-wise settings.