aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/integer.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-27 12:08:58 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-27 12:08:58 +0100
commit001c9917ace09f7b1c80d96eb067e1d37e86c546 (patch)
tree89e360961b9659a8c6b897c5412b7d6834b8eed9 /nihil.ucl/integer.cc
parent90aa957ca9b7c217af7569009d1675e0f3ff8e9b (diff)
downloadnihil-001c9917ace09f7b1c80d96eb067e1d37e86c546.tar.gz
nihil-001c9917ace09f7b1c80d96eb067e1d37e86c546.tar.bz2
improve error handling
Diffstat (limited to 'nihil.ucl/integer.cc')
-rw-r--r--nihil.ucl/integer.cc68
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;
}