|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
template <typename T> |
|
|
class read_opt { |
|
|
class read_opt { |
|
|
std::string_view _key; |
|
|
|
|
|
std::optional<T>& _ref; |
|
|
|
|
|
|
|
|
std::string_view _key; |
|
|
|
|
|
T& _ref; |
|
|
|
|
|
bool _did_read = false; |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
read_opt(std::string_view key, std::optional<T>& ref) |
|
|
|
|
|
|
|
|
read_opt(std::string_view key, T& ref) |
|
|
: _key(key) |
|
|
: _key(key) |
|
|
, _ref(ref) {} |
|
|
, _ref(ref) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (key != _key) { |
|
|
if (key != _key) { |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
if (_ref.has_value()) { |
|
|
|
|
|
|
|
|
if (_did_read) { |
|
|
throw std::runtime_error(std::string(context) + ": Duplicated key '" + std::string(key) |
|
|
throw std::runtime_error(std::string(context) + ": Duplicated key '" + std::string(key) |
|
|
+ "' is not allowed."); |
|
|
+ "' is not allowed."); |
|
|
} |
|
|
} |
|
|
_ref.emplace(value); |
|
|
|
|
|
|
|
|
_ref = T(value); |
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|