21 #ifndef _JSON_SETTINGS_H 22 #define _JSON_SETTINGS_H 24 #include <nlohmann/json.hpp> 26 #include <wx/string.h> 57 JSON_SETTINGS( aFilename, aLocation, aSchemaVersion, true, true, true ) {}
60 bool aCreateIfMissing,
bool aCreateIfDefault,
bool aWriteFile );
94 virtual bool LoadFromFile(
const wxString& aDirectory =
"" );
101 virtual bool SaveToFile(
const wxString& aDirectory =
"",
bool aForce =
false );
123 template<
typename ValueType>
130 return ret->get<ValueType>();
147 template<
typename ValueType>
148 void Set(
const std::string& aPath, ValueType aVal )
232 unsigned int& aTarget );
244 std::function<
bool(
void)> aMigrator );
253 template<
typename ValueType>
254 bool fromLegacy( wxConfigBase* aConfig,
const std::string& aKey,
const std::string& aDest );
263 const std::string& aDest );
272 const std::string& aDest );
276 return wxT(
"json" );
281 return wxEmptyString;
334 template<>
void JSON_SETTINGS::Set<wxString>(
const std::string& aPath, wxString aVal );
void ResetToDefaults()
Resets all parameters to default values.
std::vector< nlohmann::json::json_pointer > m_preserved_paths
A list of JSON pointers that are preserved during a read-update-write to disk.
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
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
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.
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
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.
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.
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.
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
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
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.
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...
bool m_writeFile
Whether or not the backing store file should be written.