LuaState tovalue, pushvalue for dynamic::Value

This commit is contained in:
MihailRis
2024-04-01 00:04:10 +03:00
parent 8950f4ecd5
commit 75a7b89fc8
9 changed files with 126 additions and 31 deletions
+8
View File
@@ -353,6 +353,10 @@ Value::~Value() {
}
}
Value Value::of(bool value) {
return Value(valtype::boolean, value);
}
Value Value::of(number_u value) {
if (std::holds_alternative<integer_t>(value)) {
return Value(valtype::integer, std::get<integer_t>(value));
@@ -360,3 +364,7 @@ Value Value::of(number_u value) {
return Value(valtype::number, std::get<number_t>(value));
}
}
Value Value::of(const std::string& value) {
return Value(valtype::string, value);
}
+2
View File
@@ -33,7 +33,9 @@ namespace dynamic {
Value(valtype type, valvalue value);
~Value();
static Value of(bool value);
static Value of(number_u value);
static Value of(const std::string& value);
};
class List {
+2 -8
View File
@@ -2,19 +2,13 @@
#include "../util/stringutil.h"
template<class T>
std::string NumberSetting<T>::toString() const {
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<int64_t>(value * 100)) + "%";
return std::to_string(static_cast<integer_t>(value * 100)) + "%";
default:
return "invalid format";
}
}
template class NumberSetting<float>;
template class NumberSetting<double>;
template class NumberSetting<int>;
template class NumberSetting<uint>;
+17 -16
View File
@@ -4,6 +4,8 @@
#include <limits>
#include <string>
#include "../typedefs.h"
enum class setting_format {
simple, percent
};
@@ -26,47 +28,46 @@ public:
virtual std::string toString() const = 0;
};
template<class T>
class NumberSetting : public Setting {
protected:
T initial;
T value;
T min;
T max;
number_t initial;
number_t value;
number_t min;
number_t max;
public:
NumberSetting(
T value,
T min=std::numeric_limits<T>::min(),
T max=std::numeric_limits<T>::max(),
number_t value,
number_t min=std::numeric_limits<number_t>::min(),
number_t max=std::numeric_limits<number_t>::max(),
setting_format format=setting_format::simple
) : Setting(format),
initial(value),
value(value),
min(min),
max(max)
max(max)
{}
T& operator*() {
number_t& operator*() {
return value;
}
T get() const {
number_t get() const {
return value;
}
void set(T value) {
void set(number_t value) {
this->value = value;
}
T getMin() const {
number_t getMin() const {
return min;
}
T getMax() const {
number_t getMax() const {
return max;
}
T getT() const {
number_t getT() const {
return (value - min) / (max - min);
}
@@ -76,7 +77,7 @@ public:
virtual std::string toString() const override;
static inline NumberSetting createPercent(T def) {
static inline NumberSetting createPercent(number_t def) {
return NumberSetting(def, 0.0, 1.0, setting_format::percent);
}
};