FeatherMC/src/config/Property.h

68 lines
1.5 KiB
C
Raw Normal View History

#pragma once
#include "Properties.h"
#include "ServerProperties.h"
#include <string>
#include <string_view>
#include <unordered_map>
#include <functional>
using std::string;
using std::string_view;
using std::unordered_map;
namespace Feather
{
template <typename T>
class Property
{
public:
Property(string_view key) : key(key) {}
Property(string_view key, T defaultValue) : key(key), value(defaultValue) {}
Property(string_view key, std::function<T(T)> op, T defaultValue) : key(key), op(op), value(defaultValue) {}
void Init(Properties *properties)
{
try
{
SetValue(properties->Get<T>(key));
}
catch (std::out_of_range err)
{
// process default value from constructor
SetValue(value);
}
}
virtual void SetValue(T value)
{
if (op) {
this->value = op(value);
} else {
this->value = value;
}
}
virtual T GetValue() { return value; }
inline string ToString() { return std::to_string(value); }
string_view key;
private:
T value = T();
std::function<T(T)> op;
};
template <>
inline string Property<string>::ToString()
{
return value;
}
template <>
inline string Property<bool>::ToString()
{
return value ? "true" : "false";
}
}