mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 14:26:19 +08:00
Added std::hash and std::swap for Box and Ref
This commit is contained in:
+65
-49
@@ -10,86 +10,102 @@ namespace rfl {
|
||||
/// unique_ptr, it is 100% guaranteed to be filled at all times.
|
||||
template <class T>
|
||||
class Box {
|
||||
public:
|
||||
/// The only way of creating new boxes is
|
||||
/// Box<T>::make(...).
|
||||
template <class... Args>
|
||||
static Box<T> make(Args&&... _args) {
|
||||
return Box<T>(std::make_unique<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
public:
|
||||
/// The only way of creating new boxes is
|
||||
/// Box<T>::make(...).
|
||||
template <class... Args>
|
||||
static Box<T> make(Args&&... _args) {
|
||||
return Box<T>(std::make_unique<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
Box() : ptr_(std::make_unique<T>()) {}
|
||||
Box() : ptr_(std::make_unique<T>()) {}
|
||||
|
||||
Box(const Box<T>& _other) = delete;
|
||||
Box(const Box<T>& _other) = delete;
|
||||
|
||||
Box(Box<T>&& _other) = default;
|
||||
Box(Box<T>&& _other) = default;
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Box(Box<U>&& _other) noexcept
|
||||
: ptr_(std::make_unique<T>(std::forward<U>(*_other))) {}
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Box(Box<U>&& _other) noexcept
|
||||
: ptr_(std::make_unique<T>(std::forward<U>(*_other))) {}
|
||||
|
||||
~Box() = default;
|
||||
~Box() = default;
|
||||
|
||||
/// Returns a pointer to the underlying object
|
||||
T* get() const { return ptr_.get(); }
|
||||
/// Returns a pointer to the underlying object
|
||||
T* get() const { return ptr_.get(); }
|
||||
|
||||
/// Copy assignment operator
|
||||
Box<T>& operator=(const Box<T>& _other) = delete;
|
||||
/// Copy assignment operator
|
||||
Box<T>& operator=(const Box<T>& _other) = delete;
|
||||
|
||||
/// Move assignment operator
|
||||
Box<T>& operator=(Box<T>&& _other) noexcept = default;
|
||||
/// Move assignment operator
|
||||
Box<T>& operator=(Box<T>&& _other) noexcept = default;
|
||||
|
||||
/// Move assignment operator
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Box<T>& operator=(Box<U>&& _other) noexcept {
|
||||
ptr_ = std::make_unique<T>(std::forward<U>(*_other));
|
||||
return *this;
|
||||
}
|
||||
/// Move assignment operator
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Box<T>& operator=(Box<U>&& _other) noexcept {
|
||||
ptr_ = std::make_unique<T>(std::forward<U>(*_other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& operator*() { return *ptr_; }
|
||||
/// Returns the underlying object.
|
||||
T& operator*() { return *ptr_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& operator*() const { 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->() { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T* operator->() const { return ptr_.get(); }
|
||||
/// Returns the underlying object.
|
||||
T* operator->() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying unique_ptr
|
||||
const std::unique_ptr<T>& ptr() const { return ptr_; }
|
||||
/// Returns the underlying unique_ptr
|
||||
const std::unique_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Box(std::unique_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Box(std::unique_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying unique_ptr_
|
||||
std::unique_ptr<T> ptr_;
|
||||
private:
|
||||
/// The underlying unique_ptr_
|
||||
std::unique_ptr<T> ptr_;
|
||||
};
|
||||
|
||||
/// Generates a new Ref<T>.
|
||||
template <class T, class... Args>
|
||||
Box<T> make_box(Args&&... _args) {
|
||||
return Box<T>::make(std::forward<Args>(_args)...);
|
||||
return Box<T>::make(std::forward<Args>(_args)...);
|
||||
}
|
||||
|
||||
template <class T1, class T2>
|
||||
inline auto operator<=>(const Box<T1>& _t1, const Box<T2>& _t2) {
|
||||
return _t1.ptr() <=> _t2.ptr();
|
||||
inline auto operator<=>(const Box<T1>& _b1, const Box<T2>& _b2) {
|
||||
return _b1.ptr() <=> _b2.ptr();
|
||||
}
|
||||
|
||||
template <class CharT, class Traits, class T>
|
||||
inline std::basic_ostream<CharT, Traits>& operator<<(
|
||||
std::basic_ostream<CharT, Traits>& _os, const Box<T>& _b) {
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
namespace std {
|
||||
|
||||
template <class T>
|
||||
struct hash<rfl::Box<T>> {
|
||||
size_t operator()(const rfl::Box<T>& _b) const {
|
||||
return hash<unique_ptr<T>>()(_b.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void swap(rfl::Box<T>& _b1, rfl::Box<T>& _b2) {
|
||||
return swap(_b1.ptr(), _b2.ptr());
|
||||
}
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif
|
||||
|
||||
+74
-58
@@ -10,99 +10,115 @@ namespace rfl {
|
||||
/// shared_ptr, it is 100% guaranteed to be filled at all times.
|
||||
template <class T>
|
||||
class Ref {
|
||||
public:
|
||||
/// The default way of creating new references is
|
||||
/// Ref<T>::make(...) or make_ref<T>(...).
|
||||
template <class... Args>
|
||||
static Ref<T> make(Args&&... _args) {
|
||||
return Ref<T>(std::make_shared<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
public:
|
||||
/// The default way of creating new references is
|
||||
/// Ref<T>::make(...) or make_ref<T>(...).
|
||||
template <class... Args>
|
||||
static Ref<T> make(Args&&... _args) {
|
||||
return Ref<T>(std::make_shared<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
Ref() : ptr_(std::make_shared<T>()) {}
|
||||
Ref() : ptr_(std::make_shared<T>()) {}
|
||||
|
||||
Ref(const Ref<T>& _other) = default;
|
||||
Ref(const Ref<T>& _other) = default;
|
||||
|
||||
Ref(Ref<T>&& _other) = default;
|
||||
Ref(Ref<T>&& _other) = default;
|
||||
|
||||
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(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))) {}
|
||||
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;
|
||||
~Ref() = default;
|
||||
|
||||
/// Returns a pointer to the underlying object
|
||||
T* get() const { return ptr_.get(); }
|
||||
/// 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*() { return *ptr_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T& operator*() const { 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->() { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
T* operator->() const { return ptr_.get(); }
|
||||
/// Returns the underlying object.
|
||||
T* operator->() const { return ptr_.get(); }
|
||||
|
||||
/// Returns the underlying shared_ptr
|
||||
const std::shared_ptr<T>& ptr() const { return ptr_; }
|
||||
/// Returns the underlying shared_ptr
|
||||
const std::shared_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
/// Copy assignment operator.
|
||||
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;
|
||||
}
|
||||
/// Copy assignment operator.
|
||||
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
|
||||
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;
|
||||
/// Move assignment operator
|
||||
Ref<T>& operator=(Ref<T>&& _other) noexcept = default;
|
||||
|
||||
/// Copy assignment operator
|
||||
Ref<T>& operator=(const Ref<T>& _other) = default;
|
||||
/// Copy assignment operator
|
||||
Ref<T>& operator=(const Ref<T>& _other) = default;
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
|
||||
private:
|
||||
/// The underlying shared_ptr_
|
||||
std::shared_ptr<T> ptr_;
|
||||
private:
|
||||
/// The underlying shared_ptr_
|
||||
std::shared_ptr<T> ptr_;
|
||||
};
|
||||
|
||||
/// Generates a new Ref<T>.
|
||||
template <class T, class... Args>
|
||||
Ref<T> make_ref(Args&&... _args) {
|
||||
return Ref<T>::make(std::forward<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();
|
||||
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;
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
namespace std {
|
||||
|
||||
template <class T>
|
||||
struct hash<rfl::Ref<T>> {
|
||||
size_t operator()(const rfl::Ref<T>& _r) const {
|
||||
return hash<shared_ptr<T>>()(_r.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void swap(rfl::Ref<T>& _r1, rfl::Ref<T>& _r2) {
|
||||
return swap(_r1.ptr(), _r2.ptr());
|
||||
}
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif // RFL_REF_HPP_
|
||||
|
||||
|
||||
Reference in New Issue
Block a user