/* * This source code is released into the public domain. */ module; #include #include #include #include #include export module nihil.ucl:boolean; import :object; namespace nihil::ucl { export struct boolean final : object { using value_type = bool; inline static constexpr object_type ucl_type = object_type::boolean; // Create a new boolean from a UCL object. boolean(ref_t, ::ucl_object_t const *uobj) : object(nihil::ucl::ref, uobj) { if (type() != ucl_type) throw type_mismatch(ucl_type, type()); } boolean(noref_t, ::ucl_object_t *uobj) : object(noref, uobj) { if (type() != ucl_type) throw type_mismatch(ucl_type, type()); } // Create a new boolean from a value. explicit boolean(value_type value) : object(noref, ::ucl_object_frombool(value)) { if (_object == nullptr) throw error("failed to create UCL object"); } // Return this object's value. auto value(this boolean const &self) -> value_type { auto v = value_type{}; auto const *uobj = self.get_ucl_object(); if (::ucl_object_toboolean_safe(uobj, &v)) return v; std::abort(); } }; /* * Comparison operators. */ export auto operator== (boolean const &a, boolean const &b) -> bool { return a.value() == b.value(); } export auto operator<=> (boolean const &a, boolean const &b) -> std::strong_ordering { return a.value() <=> b.value(); } export auto operator== (boolean const &a, boolean::value_type b) -> bool { return a.value() == b; } export auto operator<=> (boolean const &a, boolean::value_type b) -> std::strong_ordering { return a.value() <=> b; } } // namespace nihil::ucl