Settings variable abstract base, additional validation, and reading/writing JSON

This commit is contained in:
Dan Paulat 2022-12-17 00:38:20 -06:00
parent 1ad67de71b
commit 46c4dd3780
7 changed files with 408 additions and 17 deletions

View file

@ -0,0 +1,76 @@
#pragma once
#include <memory>
#include <string>
#include <boost/json/object.hpp>
namespace scwx
{
namespace qt
{
namespace settings
{
/**
* @brief Settings Variable base class
*/
class SettingsVariableBase
{
protected:
explicit SettingsVariableBase(const std::string& name);
~SettingsVariableBase();
public:
SettingsVariableBase(const SettingsVariableBase&) = delete;
SettingsVariableBase& operator=(const SettingsVariableBase&) = delete;
SettingsVariableBase(SettingsVariableBase&&) noexcept;
SettingsVariableBase& operator=(SettingsVariableBase&&) noexcept;
std::string name() const;
/**
* Sets the current value of the settings variable to default.
*/
virtual void SetValueToDefault() = 0;
/**
* Sets the current value of the settings variable to the staged value.
*/
virtual void Commit() = 0;
/**
* Reads the value from the JSON object. If the read value is out of range,
* the value is set to the minimum or maximum. If the read value fails
* validation, the value is set to default.
*
* @param json JSON object to read
*
* @return true if the read value is valid, false if the value was modified.
*/
virtual bool ReadValue(const boost::json::object& json) = 0;
/**
* Writes the current value to the JSON object.
*
* @param json JSON object to write
*/
virtual void WriteValue(boost::json::object& json) const = 0;
protected:
friend bool operator==(const SettingsVariableBase& lhs,
const SettingsVariableBase& rhs);
virtual bool Equals(const SettingsVariableBase& o) const;
private:
class Impl;
std::unique_ptr<Impl> p;
};
bool operator==(const SettingsVariableBase& lhs,
const SettingsVariableBase& rhs);
} // namespace settings
} // namespace qt
} // namespace scwx