#pragma once #include "Properties.h" #include "ServerProperties.h" #include #include #include #include #include using std::string; using std::string_view; using std::unordered_map; namespace Feather::Config { template 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 op, T defaultValue) : key(key), value(defaultValue), op(op) {} void Init(Properties *properties) { try { SetValue(properties->Get(key)); } catch (const std::out_of_range& err) { // ignore unreferenced local var (void)err; // process default value from constructor SetValue(value); } } void SetValue(T value) { if (op) { this->value = op(value); } else { this->value = value; } } T GetValue() { return value; } // Property can be automatically cast to T inline operator T() const { return value; } inline string ToString() const { return std::to_string(value); } string_view key; private: T value = T(); std::function op; }; template <> inline string Property::ToString() const { return value; } template <> inline string Property::ToString() const { return value ? "true" : "false"; } template std::ostream& operator<<(std::ostream& os, const Property& prop) { os << prop.key << "=" << prop.ToString(); return os; } }