From 03210cdc73a510a5075fec8f74e23c3de3158b4a Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 31 Oct 2023 18:18:23 +0100 Subject: [PATCH] Added more functionality to rfl::Ref --- include/rfl/Ref.hpp | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index 90f2ba50..7ae80417 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -24,17 +24,29 @@ class Ref { Ref(Ref&& _other) = default; - template - Ref(const Ref& _other) : ptr_(_other.ptr()) {} + template , + bool>::type = true> + Ref(const Ref& _other) : ptr_(_other.ptr()) {} + + template , + bool>::type = true> + Ref(Ref&& _other) noexcept + : ptr_(std::make_shared(std::forward(*_other))) {} ~Ref() = default; /// Returns a pointer to the underlying object T* get() const { return ptr_.get(); } + /// Returns the underlying object. + T& operator*() { return *ptr_; } + /// Returns the underlying object. T& operator*() const { return *ptr_; } + /// Returns the underlying object. + T* operator->() { return ptr_.get(); } + /// Returns the underlying object. T* operator->() const { return ptr_.get(); } @@ -42,12 +54,21 @@ class Ref { const std::shared_ptr& ptr() const { return ptr_; } /// Copy assignment operator. - template - Ref& operator=(const Ref& _other) { + template , + bool>::type = true> + Ref& operator=(const Ref& _other) { ptr_ = _other.ptr(); return *this; } + /// Move assignment operator + template , + bool>::type = true> + Ref& operator=(Ref&& _other) noexcept { + ptr_ = std::make_shared(std::forward(*_other)); + return *this; + } + /// Move assignment operator Ref& operator=(Ref&& _other) noexcept = default; @@ -69,6 +90,18 @@ Ref make_ref(Args&&... _args) { return Ref::make(std::forward(_args)...); } +template +inline auto operator<=>(const Ref& _t1, const Ref& _t2) { + return _t1.ptr() <=> _t2.ptr(); +} + +template +inline std::basic_ostream& operator<<( + std::basic_ostream& _os, const Ref& _b) { + _os << _b.get(); + return _os; +} + } // namespace rfl #endif // RFL_REF_HPP_