Browse Source

read_opt can deal with non-optional values

default_compile_flags
vector-of-bool 5 years ago
parent
commit
8d1e707e83
1 changed files with 6 additions and 5 deletions
  1. +6
    -5
      src/libman/parse.hpp

+ 6
- 5
src/libman/parse.hpp View File



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;
} }



Loading…
Cancel
Save