#ifndef RFL_REF_HPP_ #define RFL_REF_HPP_ #include #include namespace rfl { /// The Ref class behaves very similarly to the shared_ptr, but unlike the /// 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)...)); } Ref(const Ref& _other) : ptr_(_other.ptr_){}; Ref(Ref&& _other) : ptr_(std::move(_other.ptr_)) {} template Ref(const Ref& _other) : ptr_(_other.ptr()) {} ~Ref() = default; /// Returns a pointer to the underlying object inline T* get() const { return ptr_.get(); } /// Returns the underlying object. inline T& operator*() const { return *ptr_; } /// Returns the underlying object. inline T* operator->() const { return ptr_.get(); } /// Returns the underlying shared_ptr inline const std::shared_ptr& ptr() const { return ptr_; } /// Copy assignment operator. template Ref& operator=(const Ref& _other) { ptr_ = _other.ptr(); return *this; } /// Move assignment operator inline Ref& operator=(Ref&& _other) noexcept { if (&_other != this) { ptr_ = std::move(_other.ptr_); } return *this; } /// Copy assignment operator inline Ref& operator=(const Ref& _other) { if (&_other != this) { ptr_ = _other.ptr_; } return *this; } 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_; }; /// Generates a new Ref. template Ref make_ref(Args&&... _args) { return Ref::make(std::forward(_args)...); } } // namespace rfl #endif // RFL_REF_HPP_