mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 00:30:05 +00:00 
			
		
		
		
	Settings variable abstract base, additional validation, and reading/writing JSON
This commit is contained in:
		
							parent
							
								
									1ad67de71b
								
							
						
					
					
						commit
						46c4dd3780
					
				
					 7 changed files with 408 additions and 17 deletions
				
			
		|  | @ -1,8 +1,8 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/qt/settings/settings_variable_base.hpp> | ||||
| 
 | ||||
| #include <functional> | ||||
| #include <memory> | ||||
| #include <string> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
|  | @ -12,7 +12,7 @@ namespace settings | |||
| { | ||||
| 
 | ||||
| template<class T> | ||||
| class SettingsVariable | ||||
| class SettingsVariable : public SettingsVariableBase | ||||
| { | ||||
| public: | ||||
|    explicit SettingsVariable(const std::string& name); | ||||
|  | @ -24,25 +24,120 @@ public: | |||
|    SettingsVariable(SettingsVariable&&) noexcept; | ||||
|    SettingsVariable& operator=(SettingsVariable&&) noexcept; | ||||
| 
 | ||||
|    std::string name() const; | ||||
|    /**
 | ||||
|     * Gets the current value of the settings variable. | ||||
|     * | ||||
|     * @return Current value | ||||
|     */ | ||||
|    T GetValue() const; | ||||
| 
 | ||||
|    T            GetValue() const; | ||||
|    bool         SetValue(const T& value); | ||||
|    /**
 | ||||
|     * Sets the current value of the settings variable. | ||||
|     * | ||||
|     * @param value Value to set | ||||
|     * | ||||
|     * @return true if the current value was set, false if the value failed | ||||
|     * validation. | ||||
|     */ | ||||
|    bool SetValue(const T& value); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the current value of the settings variable. If the value is out of | ||||
|     * range, the value is set to the minimum or maximum. If the value fails | ||||
|     * validation, the value is set to default. | ||||
|     * | ||||
|     * @param value Value to set | ||||
|     * | ||||
|     * @return true if the value is valid, false if the value was modified. | ||||
|     */ | ||||
|    virtual bool SetValueOrDefault(const T& value); | ||||
|    void         SetValueToDefault(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the current value of the settings variable to default. | ||||
|     */ | ||||
|    void SetValueToDefault() override; | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the staged value of the settings variable. | ||||
|     * | ||||
|     * @param value Value to stage | ||||
|     * | ||||
|     * @return true if the staged value was set, false if the value failed | ||||
|     * validation. | ||||
|     */ | ||||
|    bool StageValue(const T& value); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the current value of the settings variable to the staged value. | ||||
|     */ | ||||
|    void Commit(); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Validate the value against the defined parameters of the settings | ||||
|     * variable. | ||||
|     * | ||||
|     * @param value Value to validate | ||||
|     * | ||||
|     * @return true if the value is valid, false if the value failed validation. | ||||
|     */ | ||||
|    virtual bool Validate(const T& value) const; | ||||
| 
 | ||||
|    /**
 | ||||
|     * Gets the default value of the settings variable. | ||||
|     * | ||||
|     * @return Default value | ||||
|     */ | ||||
|    T GetDefault() const; | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the default value of the settings variable. | ||||
|     * | ||||
|     * @param value Default value | ||||
|     */ | ||||
|    void SetDefault(const T& value); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the minimum value of the settings variable. | ||||
|     * | ||||
|     * @param value Minimum value | ||||
|     */ | ||||
|    void SetMinimum(const T& value); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets the maximum value of the settings variable. | ||||
|     * | ||||
|     * @param value Maximum value | ||||
|     */ | ||||
|    void SetMaximum(const T& value); | ||||
| 
 | ||||
|    /**
 | ||||
|     * Sets a custom validator function for the settings variable. | ||||
|     * | ||||
|     * @param validator Validator function | ||||
|     */ | ||||
|    void SetValidator(std::function<bool(const T&)> validator); | ||||
| 
 | ||||
|    /**
 | ||||
|     * 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) override; | ||||
| 
 | ||||
|    /**
 | ||||
|     * Writes the current value to the JSON object. | ||||
|     * | ||||
|     * @param json JSON object to write | ||||
|     */ | ||||
|    virtual void WriteValue(boost::json::object& json) const override; | ||||
| 
 | ||||
| protected: | ||||
|    virtual bool Equals(const SettingsVariableBase& o) const override; | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|    std::unique_ptr<Impl> p; | ||||
|  | @ -50,11 +145,11 @@ private: | |||
| 
 | ||||
| #ifdef SETTINGS_VARIABLE_IMPLEMENTATION | ||||
| template class SettingsVariable<bool>; | ||||
| template class SettingsVariable<int64_t>; | ||||
| template class SettingsVariable<std::int64_t>; | ||||
| template class SettingsVariable<std::string>; | ||||
| 
 | ||||
| // Containers are not to be used directly
 | ||||
| template class SettingsVariable<std::vector<int64_t>>; | ||||
| template class SettingsVariable<std::vector<std::int64_t>>; | ||||
| #endif | ||||
| 
 | ||||
| } // namespace settings
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat