aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/string.ccm
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-22 17:39:27 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-22 17:39:27 +0100
commit09b7a494bd5de24f380095003fb7da4939de43e5 (patch)
tree125ba6cc66370399c578d81f98d1eeef14f85686 /nihil.ucl/string.ccm
parentd6c3858418c4c00adb18d927135f73ed5a54564a (diff)
downloadnihil-09b7a494bd5de24f380095003fb7da4939de43e5.tar.gz
nihil-09b7a494bd5de24f380095003fb7da4939de43e5.tar.bz2
nihil.ucl: improve reference management
Diffstat (limited to 'nihil.ucl/string.ccm')
-rw-r--r--nihil.ucl/string.ccm46
1 files changed, 39 insertions, 7 deletions
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<std::contiguous_iterator Iterator>
+ string(Iterator first, Iterator last)
+ : string(std::string_view(first, last))
+ {}
+
+ template<std::input_iterator Iterator>
+ requires(!std::contiguous_iterator<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{};