diff options
Diffstat (limited to 'nihil.ucl/integer.cc')
| -rw-r--r-- | nihil.ucl/integer.cc | 68 |
1 files changed, 47 insertions, 21 deletions
diff --git a/nihil.ucl/integer.cc b/nihil.ucl/integer.cc index 16328d4..f4f08ef 100644 --- a/nihil.ucl/integer.cc +++ b/nihil.ucl/integer.cc @@ -6,36 +6,65 @@ module; #include <compare> #include <cstdlib> +#include <expected> +#include <system_error> #include <ucl.h> module nihil.ucl; +import nihil; + namespace nihil::ucl { +integer::integer() + : integer(0) +{ +} + +integer::integer(contained_type value) + : integer(noref, [&] { + auto *uobj = ::ucl_object_fromint(value); + if (uobj == nullptr) + throw std::system_error( + std::make_error_code(std::errc(errno))); + return uobj; + }()) +{ +} + integer::integer(ref_t, ::ucl_object_t const *uobj) - : object(nihil::ucl::ref, uobj) + : object(nihil::ucl::ref, [&] { + auto actual_type = static_cast<object_type>( + ::ucl_object_type(uobj)); + if (actual_type != integer::ucl_type) + throw type_mismatch(integer::ucl_type, actual_type); + return uobj; + }()) { - if (type() != ucl_type) - throw type_mismatch(ucl_type, type()); } integer::integer(noref_t, ::ucl_object_t *uobj) - : object(noref, uobj) + : object(nihil::ucl::noref, [&] { + auto actual_type = static_cast<object_type>( + ::ucl_object_type(uobj)); + if (actual_type != integer::ucl_type) + throw type_mismatch(integer::ucl_type, actual_type); + return uobj; + }()) { - if (type() != ucl_type) - throw type_mismatch(ucl_type, type()); } -integer::integer() - : integer(0) -{} - -integer::integer(contained_type value) - : object(noref, ::ucl_object_fromint(value)) +auto make_integer(integer::contained_type value) + -> std::expected<integer, error> { - if (_object == nullptr) - throw error("failed to create UCL object"); + auto *uobj = ::ucl_object_fromint(value); + if (uobj == nullptr) + return std::unexpected(error( + errc::failed_to_create_object, + error(std::errc(errno)))); + + return integer(noref, uobj); } auto integer::value(this integer const &self) -> contained_type @@ -49,26 +78,23 @@ auto integer::value(this integer const &self) -> contained_type std::abort(); } -auto operator== (integer const &a, integer const &b) --> bool +auto operator== (integer const &a, integer const &b) -> bool { return a.value() == b.value(); } -auto operator<=> (integer const &a, integer const &b) --> std::strong_ordering +auto operator<=> (integer const &a, integer const &b) -> std::strong_ordering { return a.value() <=> b.value(); } -auto operator== (integer const &a, integer::contained_type b) --> bool +auto operator== (integer const &a, integer::contained_type b) -> bool { return a.value() == b; } auto operator<=> (integer const &a, integer::contained_type b) --> std::strong_ordering + -> std::strong_ordering { return a.value() <=> b; } |
