settings test

This commit is contained in:
MihailRis
2024-03-29 13:58:19 +03:00
parent a4960097f0
commit 32120d8af4
10 changed files with 125 additions and 27 deletions
+14
View File
@@ -0,0 +1,14 @@
#include "setting.h"
#include "../util/stringutil.h"
std::string NumberSetting::toString() const {
switch (getFormat()) {
case setting_format::simple:
return util::to_string(value);
case setting_format::percent:
return std::to_string(static_cast<int>(value * 100)) + "%";
default:
return "invalid format";
}
}
+71
View File
@@ -0,0 +1,71 @@
#ifndef DATA_SETTING_H_
#define DATA_SETTING_H_
#include <string>
enum class setting_format {
simple, percent
};
template<class T>
class Setting {
protected:
T value;
setting_format format;
public:
Setting(T value, setting_format format) : value(value), format(format) {
}
virtual ~Setting() {}
T& operator*() {
return value;
}
virtual const T& get() const {
return value;
}
virtual void set(const T& value) {
this->value = value;
}
virtual setting_format getFormat() const {
return format;
}
virtual std::string toString() const = 0;
};
class NumberSetting : public Setting<double> {
protected:
double min;
double max;
public:
NumberSetting(double value, double min, double max, setting_format format)
: Setting(value, format), min(min), max(max) {}
double& operator*() {
return value;
}
double getMin() const {
return min;
}
double getMax() const {
return max;
}
double getT() const {
return (value - min) / (max - min);
}
virtual std::string toString() const override;
static inline NumberSetting createPercent(double def) {
return NumberSetting(def, 0.0, 1.0, setting_format::percent);
}
};
#endif // DATA_SETTING_H_