21 #ifndef _JSON_SETTINGS_H 22 #define _JSON_SETTINGS_H 27 #include <wx/string.h> 30 #include <nlohmann/json_fwd.hpp> 64 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true )
68 bool aCreateIfMissing,
bool aCreateIfDefault,
bool aWriteFile );
92 bool Contains(
const std::string& aPath )
const;
93 size_t Count(
const std::string& aPath )
const;
107 virtual bool Store();
114 virtual bool LoadFromFile(
const wxString& aDirectory =
"" );
121 virtual bool SaveToFile(
const wxString& aDirectory =
"",
bool aForce =
false );
143 template<
typename ValueType>
153 template<
typename ValueType>
154 void Set(
const std::string& aPath, ValueType aVal );
228 unsigned int& aTarget );
240 std::function<
bool(
void)> aMigrator );
249 template<
typename ValueType>
250 bool fromLegacy( wxConfigBase* aConfig,
const std::string& aKey,
const std::string& aDest );
259 const std::string& aDest );
268 const std::string& aDest );
272 return wxT(
"json" );
277 return wxEmptyString;
329 template<>
void JSON_SETTINGS::Set<wxString>(
const std::string& aPath, wxString aVal );
void ResetToDefaults()
Resets all parameters to default values.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
JSON_SETTINGS_INTERNALS * Internals()
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void from_json(const nlohmann::json &aJson, wxString &aString)
bool m_createIfMissing
Whether or not the backing store file should be created it if doesn't exist.
void SetReadOnly(bool aReadOnly)
wxString GetFilename() const
virtual wxString getLegacyFileExt() const
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
OPT< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
wxString GetFullFilename() const
nlohmann::json & At(const std::string &aPath)
Wrappers for the underlying JSON API so that most consumers don't need json.hpp All of these function...
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
void to_json(nlohmann::json &aJson, const wxString &aString)
The color scheme directory (e.g. ~/.config/kicad/colors/)
std::map< int, std::pair< int, std::function< bool()> > > m_migrators
A map of starting schema version to a pair of <ending version, migrator function>
wxString m_filename
The filename (not including path) of this settings file (inicode)
bool Migrate()
Migrates the schema of this settings from the version in the file to the latest version.
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
wxString m_legacy_filename
The filename of the wxConfig legacy file (if different from m_filename)
The settings directory inside a project folder.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
SETTINGS_LOC GetLocation() const
static bool SetIfPresent(const nlohmann::json &aObj, const std::string &aPath, wxString &aTarget)
Sets the given string if the given key/path is present.
void SetManager(SETTINGS_MANAGER *aManager)
std::vector< NESTED_SETTINGS * > m_nested_settings
Nested settings files that live inside this one, if any.
No directory prepended, full path in filename (used for PROJECT_FILE)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
SETTINGS_LOC m_location
The location of this settings file (.
Not stored in a file, but inside another JSON_SETTINGS.
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
bool m_resetParamsIfMissing
Whether or not to set parameters to their default value if missing from JSON on Load()
bool fromLegacyColor(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy COLOR4D stored in a wxConfig string to a given JSON pointer value.
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
The main config directory (e.g. ~/.config/kicad/)
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
void SetLocation(SETTINGS_LOC aLocation)
bool Contains(const std::string &aPath) const
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
void SetFilename(const wxString &aFilename)
bool m_createIfDefault
Whether or not the backing store file should be created if all parameters are still at their default ...
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
SETTINGS_MANAGER * m_manager
A pointer to the settings manager managing this file (may be null)
virtual wxString getFileExt() const
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig)
Migrates from wxConfig to JSON-based configuration.
void SetLegacyFilename(const wxString &aFilename)
int m_schemaVersion
Version of this settings schema.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
size_t Count(const std::string &aPath) const
bool m_writeFile
Whether or not the backing store file should be written.