From 001c9917ace09f7b1c80d96eb067e1d37e86c546 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Fri, 27 Jun 2025 12:08:58 +0100 Subject: improve error handling --- nihil.ucl/real.cc | 65 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'nihil.ucl/real.cc') diff --git a/nihil.ucl/real.cc b/nihil.ucl/real.cc index b371072..b3d50c3 100644 --- a/nihil.ucl/real.cc +++ b/nihil.ucl/real.cc @@ -7,26 +7,28 @@ module; #include #include #include +#include #include +#include #include module nihil.ucl; +import nihil; + namespace nihil::ucl { -real::real(ref_t, ::ucl_object_t const *uobj) - : object(nihil::ucl::ref, uobj) +auto make_real(real::contained_type value) + -> std::expected { - if (type() != ucl_type) - throw type_mismatch(ucl_type, type()); -} + auto *uobj = ::ucl_object_fromdouble(value); + if (uobj == nullptr) + return std::unexpected(error( + errc::failed_to_create_object, + error(std::errc(errno)))); -real::real(noref_t, ::ucl_object_t *uobj) - : object(noref, uobj) -{ - if (type() != ucl_type) - throw type_mismatch(ucl_type, type()); + return real(noref, uobj); } real::real() @@ -35,10 +37,36 @@ real::real() } real::real(contained_type value) - : object(noref, ::ucl_object_fromdouble(value)) + : real(noref, [&] { + auto *uobj = ::ucl_object_fromdouble(value); + if (uobj == nullptr) + throw std::system_error( + std::make_error_code(std::errc(errno))); + return uobj; + }()) +{ +} + +real::real(ref_t, ::ucl_object_t const *uobj) + : object(nihil::ucl::ref, [&] { + auto actual_type = static_cast( + ::ucl_object_type(uobj)); + if (actual_type != real::ucl_type) + throw type_mismatch(real::ucl_type, actual_type); + return uobj; + }()) +{ +} + +real::real(noref_t, ::ucl_object_t *uobj) + : object(nihil::ucl::noref, [&] { + auto actual_type = static_cast( + ::ucl_object_type(uobj)); + if (actual_type != real::ucl_type) + throw type_mismatch(real::ucl_type, actual_type); + return uobj; + }()) { - if (_object == nullptr) - throw error("failed to create UCL object"); } auto real::value(this real const &self) -> contained_type @@ -52,26 +80,23 @@ auto real::value(this real const &self) -> contained_type std::abort(); } -auto operator== (real const &a, real const &b) --> bool +auto operator== (real const &a, real const &b) -> bool { return a.value() == b.value(); } -auto operator<=> (real const &a, real const &b) --> std::partial_ordering +auto operator<=> (real const &a, real const &b) -> std::partial_ordering { return a.value() <=> b.value(); } -auto operator== (real const &a, real::contained_type b) --> bool +auto operator== (real const &a, real::contained_type b) -> bool { return a.value() == b; } auto operator<=> (real const &a, real::contained_type b) --> std::partial_ordering + -> std::partial_ordering { return a.value() <=> b; } -- cgit v1.2.3