1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
BasicSettings.h
Vitaly Sverchinsky
**********************************************************************/
#pragma once
#include <memory>
#include <wx/string.h>
#include <wx/arrstr.h>
#include "GlobalVariable.h"
namespace audacity
{
/**
* @brief Base class for objects that provide facility to store
* data persistently, and access it with string keys that are formed
* similarly to how paths in tree are formed.
*
* @details Each group (node) in path separated using '/'(slash) symbol. Path could be empty,
* in that case value belongs to the root group. If path starts with slash then it's
* considered to be absolute, otherwise it's relative to the current group, which
* could be changed using `BeginGroup` method.
*/
class PREFERENCES_API BasicSettings
{
public:
class PREFERENCES_API GroupScope final
{
friend class BasicSettings;
std::optional<std::reference_wrapper<BasicSettings>> mSettings;
GroupScope(BasicSettings& settings);
public:
GroupScope(const GroupScope&) = delete;
GroupScope(GroupScope&&) = delete;
GroupScope& operator=(const GroupScope&) = delete;
GroupScope& operator=(GroupScope&&) = delete;
void Reset() noexcept;
~GroupScope();
};
BasicSettings();
virtual ~BasicSettings();
BasicSettings(const BasicSettings&) = delete;
BasicSettings(BasicSettings&&) = default;
BasicSettings& operator=(const BasicSettings&) = delete;
BasicSettings& operator=(BasicSettings&&) = default;
///@brief Returns current group prefix
virtual wxString GetGroup() const = 0;
///@brief Returns all child groups within the current group
virtual wxArrayString GetChildGroups() const = 0;
///@brief Returns all child keys within the current group
virtual wxArrayString GetChildKeys() const = 0;
///@brief Checks whether specified key exists within the current group
virtual bool HasEntry(const wxString& key) const = 0;
///@brief Checks whether specified group exists relative to the current group
virtual bool HasGroup(const wxString& key) const = 0;
///@brief Returns true if group or entry exists
virtual bool Exists(const wxString& key) const;
/// @brief Removes group or entry within the current group, if exists.
/// Pass empty string to remove each entry in the current group
virtual bool Remove(const wxString& key) = 0;
///@brief Remove all groups and keys
virtual void Clear() = 0;
/// @brief Deletes specified group if exists
bool DeleteGroup(const wxString& key);
/// @brief Deletes specified entry if exists
bool DeleteEntry(const wxString& key);
/// @brief Appends a prefix to the current group or sets a new
/// absolute path. Group that was set as current before `BeginGroup`
/// is called, will be restored once `GroupScope` is destroyed.
GroupScope BeginGroup(const wxString& prefix);
virtual bool Read(const wxString& key, bool* value) const = 0;
virtual bool Read(const wxString& key, int* value) const = 0;
virtual bool Read(const wxString& key, long* value) const = 0;
virtual bool Read(const wxString& key, long long* value) const = 0;
virtual bool Read(const wxString& key, double* value) const = 0;
virtual bool Read(const wxString& key, wxString* value) const = 0;
virtual bool Read(const wxString& key, float* value) const;
/// @brief Uses wxFromString to read object
template<typename T>
bool Read(const wxString& key, T* value) const
{
wxString str;
if (!Read(key, &str))
return false;
return wxFromString(str, value);
}
template<typename T>
std::enable_if_t<std::is_scalar_v<T>, bool>
Read(const wxString& key, T* value, T defaultValue) const
{
if(!Read(key, value))
{
*value = defaultValue;
return false;
}
return true;
}
template<typename T>
std::enable_if_t<!std::is_scalar_v<T>, bool>
Read(const wxString& key, T* value, const T& defaultValue)
{
if(!Read(key, value))
{
*value = defaultValue;
return false;
}
return true;
}
wxString Read(const wxString& key, const wxString& defaultValue = wxEmptyString) const;
wxString Read(const wxString& key, const char* defaultValue) const;
wxString Read(const wxString& key, const wchar_t* defaultValue) const;
template<typename T>
std::enable_if_t<std::is_scalar_v<T>, T>
Read(const wxString& key, T defaultValue) const
{
T value;
if(!Read(key, &value))
return defaultValue;
return value;
}
template<typename T>
std::enable_if_t<!std::is_scalar_v<T>, T>
Read(const wxString& key, const T& defaultValue) const
{
T value;
if(!Read(key, &value))
return defaultValue;
return value;
}
virtual bool Write(const wxString& key, bool value) = 0;
virtual bool Write(const wxString& key, int value) = 0;
virtual bool Write(const wxString& key, long value) = 0;
virtual bool Write(const wxString& key, long long value) = 0;
virtual bool Write(const wxString& key, double value) = 0;
virtual bool Write(const wxString& key, const wxString& value) = 0;
virtual bool Write(const wxString& key, float value);
virtual bool Write(const wxString& key, const char* value);
virtual bool Write(const wxString& key, const wchar_t* value);
/// @brief Uses wxToString to convert object into string
template<typename T>
bool Write(const wxString& key, const T& value)
{
return Write(key, wxToString(value));
}
virtual bool Flush() noexcept = 0;
bool ReadBool(const wxString& key, bool defaultValue) const;
long ReadLong(const wxString& key, long defaultValue) const;
double ReadDouble(const wxString& key, double defaultValue) const;
template <typename T>
T ReadObject(const wxString& key, const T& defaultValue) const
{
return Read(key, defaultValue);
}
protected:
virtual void DoBeginGroup(const wxString& prefix) = 0;
virtual void DoEndGroup() noexcept = 0;
};
/// @brief Provides an access to application-wise settings
struct PREFERENCES_API ApplicationSettings final : GlobalHook<ApplicationSettings, std::unique_ptr<BasicSettings>()> { };
}
|