/* * This source code is released into the public domain. */ module; #include #include export module nihil.config:string; import nihil; import nihil.ucl; import :option; import :store; namespace nihil::config { export struct string final : option { string(std::string &storage, std::string_view name, std::string_view description) noexcept : option(name, description) , _storage(storage) { store::get().register_option(this); } ~string() { store::get().unregister_option(this); } auto get_string() const -> std::string override { return _storage; }; auto set_string(std::string_view new_value) -> void override { _storage = new_value; } auto to_ucl() const -> ucl::object override { return ucl::string(_storage); } auto from_ucl(ucl::object const &uclobj) -> void override { try { _storage = object_cast(uclobj).value(); is_default(false); } catch (ucl::type_mismatch const &exc) { throw error(std::format("'{}': expected string, not {}", name(), str(exc.actual_type()))); } } private: std::string &_storage; }; } // namespace nihil::config