mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Added more functionality to rfl::Ref
This commit is contained in:
+37
-4
@@ -24,17 +24,29 @@ class Ref {
|
||||
|
||||
Ref(Ref<T>&& _other) = default;
|
||||
|
||||
template <class Y>
|
||||
Ref(const Ref<Y>& _other) : ptr_(_other.ptr()) {}
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Ref(const Ref<U>& _other) : ptr_(_other.ptr()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Ref(Ref<U>&& _other) noexcept
|
||||
: ptr_(std::make_shared<T>(std::forward<U>(*_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<T>& ptr() const { return ptr_; }
|
||||
|
||||
/// Copy assignment operator.
|
||||
template <class Y>
|
||||
Ref<T>& operator=(const Ref<Y>& _other) {
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Ref<T>& operator=(const Ref<U>& _other) {
|
||||
ptr_ = _other.ptr();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment operator
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Ref<T>& operator=(Ref<U>&& _other) noexcept {
|
||||
ptr_ = std::make_shared<T>(std::forward<U>(*_other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment operator
|
||||
Ref<T>& operator=(Ref<T>&& _other) noexcept = default;
|
||||
|
||||
@@ -69,6 +90,18 @@ Ref<T> make_ref(Args&&... _args) {
|
||||
return Ref<T>::make(std::forward<Args>(_args)...);
|
||||
}
|
||||
|
||||
template <class T1, class T2>
|
||||
inline auto operator<=>(const Ref<T1>& _t1, const Ref<T2>& _t2) {
|
||||
return _t1.ptr() <=> _t2.ptr();
|
||||
}
|
||||
|
||||
template <class CharT, class Traits, class T>
|
||||
inline std::basic_ostream<CharT, Traits>& operator<<(
|
||||
std::basic_ostream<CharT, Traits>& _os, const Ref<T>& _b) {
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif // RFL_REF_HPP_
|
||||
|
||||
Reference in New Issue
Block a user