From 09b7a494bd5de24f380095003fb7da4939de43e5 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 22 Jun 2025 17:39:27 +0100 Subject: nihil.ucl: improve reference management --- nihil.ucl/string.ccm | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'nihil.ucl/string.ccm') diff --git a/nihil.ucl/string.ccm b/nihil.ucl/string.ccm index e41c70f..ffad847 100644 --- a/nihil.ucl/string.ccm +++ b/nihil.ucl/string.ccm @@ -21,19 +21,51 @@ export struct string final : object { inline static constexpr object_type ucl_type = object_type::string; - string(value_type value) - : object(::ucl_object_fromstring_common( - value.data(), value.size(), UCL_STRING_RAW)) + // Create a new string from a UCL object. + string(ref_t, ::ucl_object_t const *uobj) + : object(nihil::ucl::ref, uobj) { - if (_object == nullptr) - throw error("failed to create UCL object"); + if (type() != ucl_type) + throw type_mismatch(ucl_type, type()); } - explicit string(::ucl_object_t *uobj) : object(uobj) + string(noref_t, ::ucl_object_t *uobj) + : object(noref, uobj) { - assert(type() == object_type::string); + if (type() != ucl_type) + throw type_mismatch(ucl_type, type()); + } + + // Create a new UCL string from a string. + string(std::string_view value) + : object(nihil::ucl::ref, + ::ucl_object_fromstring_common( + value.data(), value.size(), + UCL_STRING_RAW)) + { + if (_object == nullptr) + throw error("failed to create UCL object"); } + // Create a new UCL string from an iterator pair. + template + string(Iterator first, Iterator last) + : string(std::string_view(first, last)) + {} + + template + requires(!std::contiguous_iterator) + string(Iterator first, Iterator last) + : string(std::string(first, last)) + {} + + // Create a new UCL string from a range. + string(std::from_range_t, std::ranges::range auto &&range) + : string(std::ranges::begin(range), + std::ranges::end(range)) + {} + + // Return the value of this string. auto value(this string const &self) -> value_type { char const *dptr{}; -- cgit v1.2.3