diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-23 18:34:18 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-23 18:34:18 +0100 |
| commit | 32b4443ba2ec5c3f7c09221ab9b21911a3126ef9 (patch) | |
| tree | cb6346997078626dc512e5e46e95796e375690ee /nihil.ucl/object.cc | |
| parent | d5963532328ce5f1c9f266bf7e760b7d18a60c15 (diff) | |
| download | nihil-32b4443ba2ec5c3f7c09221ab9b21911a3126ef9.tar.gz nihil-32b4443ba2ec5c3f7c09221ab9b21911a3126ef9.tar.bz2 | |
add separate module implementation files
Diffstat (limited to 'nihil.ucl/object.cc')
| -rw-r--r-- | nihil.ucl/object.cc | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/nihil.ucl/object.cc b/nihil.ucl/object.cc new file mode 100644 index 0000000..f435b90 --- /dev/null +++ b/nihil.ucl/object.cc @@ -0,0 +1,123 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <cstdlib> +#include <string> +#include <utility> + +#include <ucl.h> + +module nihil.ucl; + +namespace nihil::ucl { + +object::object(ref_t, ::ucl_object_t const *object) + : _object(::ucl_object_ref(object)) +{ +} + +object::object(noref_t, ::ucl_object_t *object) + : _object(object) +{ +} + +object::~object() { + if (_object != nullptr) + ::ucl_object_unref(_object); +} + +object::object(object &&other) noexcept + : _object(std::exchange(other._object, nullptr)) +{} + +object::object(object const &other) noexcept + : _object(nullptr) +{ + *this = other; +} + +auto object::operator=(this object &self, object &&other) noexcept + -> object & +{ + if (&self != &other) + self._object = std::exchange(other._object, nullptr); + return self; +} + +auto object::operator=(this object &self, object const &other) + -> object & +{ + if (&self != &other) { + auto *new_uobj = ::ucl_object_copy(other.get_ucl_object()); + if (new_uobj == nullptr) + throw error("failed to copy UCL object"); + + if (self._object != nullptr) + ::ucl_object_unref(self._object); + self._object = new_uobj; + } + + return self; +} + +auto object::ref(this object const &self) -> object +{ + return object(nihil::ucl::ref, self.get_ucl_object()); +} + +auto object::type(this object const &self) -> object_type +{ + auto utype = ::ucl_object_type(self.get_ucl_object()); + return static_cast<object_type>(utype); +} + +auto object::get_ucl_object(this object &self) -> ::ucl_object_t * +{ + if (self._object == nullptr) + throw error("attempt to access empty UCL object"); + return self._object; +} + +auto object::get_ucl_object(this object const &self) -> ::ucl_object_t const * +{ + if (self._object == nullptr) + throw error("attempt to access empty UCL object"); + return self._object; +} + +// Return the key of this object. +auto object::key(this object const &self) -> std::string_view +{ + auto dlen = std::size_t{}; + auto const *dptr = ::ucl_object_keyl(self.get_ucl_object(), + &dlen); + return {dptr, dlen}; +} + +auto swap(object &a, object &b) -> void +{ + std::swap(a._object, b._object); +} + +auto operator<=>(object const &lhs, object const &rhs) -> std::strong_ordering +{ + auto cmp = ::ucl_object_compare(lhs.get_ucl_object(), + rhs.get_ucl_object()); + + if (cmp < 0) + return std::strong_ordering::less; + else if (cmp > 0) + return std::strong_ordering::greater; + else + return std::strong_ordering::equal; +} + +auto operator==(object const &lhs, object const &rhs) -> bool +{ + return (lhs <=> rhs) == std::strong_ordering::equal; +} + +} // namespace nihil::ucl |
