diff --git a/include/rfl/Box.hpp b/include/rfl/Box.hpp index 6efd45c3..deb39a48 100644 --- a/include/rfl/Box.hpp +++ b/include/rfl/Box.hpp @@ -10,86 +10,102 @@ namespace rfl { /// unique_ptr, it is 100% guaranteed to be filled at all times. template class Box { - public: - /// The only way of creating new boxes is - /// Box::make(...). - template - static Box make(Args&&... _args) { - return Box(std::make_unique(std::forward(_args)...)); - } + public: + /// The only way of creating new boxes is + /// Box::make(...). + template + static Box make(Args&&... _args) { + return Box(std::make_unique(std::forward(_args)...)); + } - Box() : ptr_(std::make_unique()) {} + Box() : ptr_(std::make_unique()) {} - Box(const Box& _other) = delete; + Box(const Box& _other) = delete; - Box(Box&& _other) = default; + Box(Box&& _other) = default; - template , - bool>::type = true> - Box(Box&& _other) noexcept - : ptr_(std::make_unique(std::forward(*_other))) {} + template , + bool>::type = true> + Box(Box&& _other) noexcept + : ptr_(std::make_unique(std::forward(*_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& operator=(const Box& _other) = delete; + /// Copy assignment operator + Box& operator=(const Box& _other) = delete; - /// Move assignment operator - Box& operator=(Box&& _other) noexcept = default; + /// Move assignment operator + Box& operator=(Box&& _other) noexcept = default; - /// Move assignment operator - template , - bool>::type = true> - Box& operator=(Box&& _other) noexcept { - ptr_ = std::make_unique(std::forward(*_other)); - return *this; - } + /// Move assignment operator + template , + bool>::type = true> + Box& operator=(Box&& _other) noexcept { + ptr_ = std::make_unique(std::forward(*_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& ptr() const { return ptr_; } + /// Returns the underlying unique_ptr + const std::unique_ptr& ptr() const { return ptr_; } - private: - /// Only make is allowed to use this constructor. - explicit Box(std::unique_ptr&& _ptr) : ptr_(std::move(_ptr)) {} + private: + /// Only make is allowed to use this constructor. + explicit Box(std::unique_ptr&& _ptr) : ptr_(std::move(_ptr)) {} - private: - /// The underlying unique_ptr_ - std::unique_ptr ptr_; + private: + /// The underlying unique_ptr_ + std::unique_ptr ptr_; }; /// Generates a new Ref. template Box make_box(Args&&... _args) { - return Box::make(std::forward(_args)...); + return Box::make(std::forward(_args)...); } template -inline auto operator<=>(const Box& _t1, const Box& _t2) { - return _t1.ptr() <=> _t2.ptr(); +inline auto operator<=>(const Box& _b1, const Box& _b2) { + return _b1.ptr() <=> _b2.ptr(); } template inline std::basic_ostream& operator<<( std::basic_ostream& _os, const Box& _b) { - _os << _b.get(); - return _os; + _os << _b.get(); + return _os; } } // namespace rfl +namespace std { + +template +struct hash> { + size_t operator()(const rfl::Box& _b) const { + return hash>()(_b.ptr()); + } +}; + +template +inline void swap(rfl::Box& _b1, rfl::Box& _b2) { + return swap(_b1.ptr(), _b2.ptr()); +} + +} // namespace std + #endif diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index 7ae80417..df3b5667 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -10,99 +10,115 @@ namespace rfl { /// shared_ptr, it is 100% guaranteed to be filled at all times. template class Ref { - public: - /// The default way of creating new references is - /// Ref::make(...) or make_ref(...). - template - static Ref make(Args&&... _args) { - return Ref(std::make_shared(std::forward(_args)...)); - } + public: + /// The default way of creating new references is + /// Ref::make(...) or make_ref(...). + template + static Ref make(Args&&... _args) { + return Ref(std::make_shared(std::forward(_args)...)); + } - Ref() : ptr_(std::make_shared()) {} + Ref() : ptr_(std::make_shared()) {} - Ref(const Ref& _other) = default; + Ref(const Ref& _other) = default; - Ref(Ref&& _other) = default; + Ref(Ref&& _other) = default; - template , - bool>::type = true> - 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))) {} + template , + bool>::type = true> + Ref(Ref&& _other) noexcept + : ptr_(std::make_shared(std::forward(*_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& ptr() const { return ptr_; } + /// Returns the underlying shared_ptr + const std::shared_ptr& ptr() const { return ptr_; } - /// Copy assignment operator. - template , - bool>::type = true> - Ref& operator=(const Ref& _other) { - ptr_ = _other.ptr(); - return *this; - } + /// Copy assignment operator. + 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 + 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; + /// Move assignment operator + Ref& operator=(Ref&& _other) noexcept = default; - /// Copy assignment operator - Ref& operator=(const Ref& _other) = default; + /// Copy assignment operator + Ref& operator=(const Ref& _other) = default; - private: - /// Only make is allowed to use this constructor. - explicit Ref(std::shared_ptr&& _ptr) : ptr_(std::move(_ptr)) {} + private: + /// Only make is allowed to use this constructor. + explicit Ref(std::shared_ptr&& _ptr) : ptr_(std::move(_ptr)) {} - private: - /// The underlying shared_ptr_ - std::shared_ptr ptr_; + private: + /// The underlying shared_ptr_ + std::shared_ptr ptr_; }; /// Generates a new Ref. template Ref make_ref(Args&&... _args) { - return Ref::make(std::forward(_args)...); + return Ref::make(std::forward(_args)...); } template inline auto operator<=>(const Ref& _t1, const Ref& _t2) { - return _t1.ptr() <=> _t2.ptr(); + return _t1.ptr() <=> _t2.ptr(); } template inline std::basic_ostream& operator<<( std::basic_ostream& _os, const Ref& _b) { - _os << _b.get(); - return _os; + _os << _b.get(); + return _os; } } // namespace rfl +namespace std { + +template +struct hash> { + size_t operator()(const rfl::Ref& _r) const { + return hash>()(_r.ptr()); + } +}; + +template +inline void swap(rfl::Ref& _r1, rfl::Ref& _r2) { + return swap(_r1.ptr(), _r2.ptr()); +} + +} // namespace std + #endif // RFL_REF_HPP_