aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/boolean.ccm
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/boolean.ccm
parent90aa957ca9b7c217af7569009d1675e0f3ff8e9b (diff)
downloadnihil-001c9917ace09f7b1c80d96eb067e1d37e86c546.tar.gz
nihil-001c9917ace09f7b1c80d96eb067e1d37e86c546.tar.bz2
improve error handling
Diffstat (limited to 'nihil.ucl/boolean.ccm')
-rw-r--r--nihil.ucl/boolean.ccm55
1 files changed, 48 insertions, 7 deletions
diff --git a/nihil.ucl/boolean.ccm b/nihil.ucl/boolean.ccm
index 78ede17..068dfdd 100644
--- a/nihil.ucl/boolean.ccm
+++ b/nihil.ucl/boolean.ccm
@@ -7,6 +7,8 @@ module;
#include <cassert>
#include <cstdint>
#include <cstdlib>
+#include <expected>
+#include <format>
#include <string>
#include <ucl.h>
@@ -22,21 +24,37 @@ export struct boolean final : object {
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);
- boolean(noref_t, ::ucl_object_t *uobj);
-
- // Create a new default-initialised boolean.
+ /*
+ * Create a boolean holding the value false. Throws std::system_error
+ * on failure.
+ */
boolean();
- // Create a new boolean from a value.
- explicit boolean(contained_type value);
+ /*
+ * Create a boolean holding a specific value. Throws std::system_error
+ * on failure.
+ */
+ explicit boolean(bool);
+
+ /*
+ * Create a new boolean from a UCL object. Throws type_mismatch
+ * on failure.
+ */
+ boolean(ref_t, ::ucl_object_t const *uobj);
+ boolean(noref_t, ::ucl_object_t *uobj);
// Return this object's value.
auto value(this boolean const &self) -> contained_type;
};
/*
+ * Boolean constructors. These return an error instead of throwing.
+ */
+
+export [[nodiscard]] auto
+make_boolean(boolean::contained_type = false) -> std::expected<boolean, error>;
+
+/*
* Comparison operators.
*/
@@ -48,3 +66,26 @@ export auto operator<=> (boolean const &a, boolean::contained_type b)
-> std::strong_ordering;
} // namespace nihil::ucl
+
+/*
+ * std::formatter for a boolean. This provides the same format operations
+ * as std::formatter<bool>.
+ */
+export template<>
+struct std::formatter<nihil::ucl::boolean, char>
+{
+ std::formatter<bool> base_formatter;
+
+ template<class ParseContext>
+ constexpr ParseContext::iterator parse(ParseContext& ctx)
+ {
+ return base_formatter.parse(ctx);
+ }
+
+ template<class FmtContext>
+ FmtContext::iterator format(nihil::ucl::boolean const &o,
+ FmtContext& ctx) const
+ {
+ return base_formatter.format(o.value(), ctx);
+ }
+};