2020-07-24 03:46:37 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Properties.h"
|
|
|
|
#include "ServerProperties.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <functional>
|
2020-08-03 06:23:09 +01:00
|
|
|
#include <ostream>
|
2020-07-24 03:46:37 +01:00
|
|
|
|
|
|
|
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) {}
|
2020-07-31 01:48:48 +01:00
|
|
|
Property(string_view key, std::function<T(T)> op, T defaultValue) : key(key), value(defaultValue), op(op) {}
|
2020-07-24 03:46:37 +01:00
|
|
|
|
|
|
|
void Init(Properties *properties)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SetValue(properties->Get<T>(key));
|
|
|
|
}
|
2020-07-31 01:48:48 +01:00
|
|
|
catch (const std::out_of_range& err)
|
2020-07-24 03:46:37 +01:00
|
|
|
{
|
2020-08-13 01:58:24 +01:00
|
|
|
// ignore unreferenced local var
|
|
|
|
(void)err;
|
|
|
|
|
2020-07-24 03:46:37 +01:00
|
|
|
// process default value from constructor
|
|
|
|
SetValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 01:48:48 +01:00
|
|
|
void SetValue(T value)
|
2020-07-24 03:46:37 +01:00
|
|
|
{
|
|
|
|
if (op) {
|
|
|
|
this->value = op(value);
|
|
|
|
} else {
|
|
|
|
this->value = value;
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 01:48:48 +01:00
|
|
|
|
|
|
|
T GetValue() { return value; }
|
|
|
|
|
|
|
|
// Property<T> can be automatically cast to T
|
2020-08-07 05:49:29 +01:00
|
|
|
inline operator T() const { return value; }
|
2020-07-24 03:46:37 +01:00
|
|
|
|
2020-08-03 06:23:09 +01:00
|
|
|
inline string ToString() const { return std::to_string(value); }
|
2020-07-24 03:46:37 +01:00
|
|
|
|
|
|
|
string_view key;
|
|
|
|
private:
|
|
|
|
T value = T();
|
|
|
|
std::function<T(T)> op;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2020-08-03 06:23:09 +01:00
|
|
|
inline string Property<string>::ToString() const
|
2020-07-24 03:46:37 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-08-03 06:23:09 +01:00
|
|
|
inline string Property<bool>::ToString() const
|
2020-07-24 03:46:37 +01:00
|
|
|
{
|
|
|
|
return value ? "true" : "false";
|
|
|
|
}
|
2020-08-03 06:23:09 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Property<T>& prop)
|
|
|
|
{
|
|
|
|
os << prop.key << "=" << prop.ToString();
|
|
|
|
return os;
|
|
|
|
}
|
2020-08-01 00:57:33 +01:00
|
|
|
}
|