#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)...)); } /// Wrapper around a shared_ptr, leads to a runtime error, /// if the shared_ptr is not allocated. explicit Ref(const std::shared_ptr& _ptr) { assert_true(_ptr); if (!_ptr) { throw std::runtime_error( "Could not create Ref from shared_ptr, because shared_ptr was " "not " "set."); } ptr_ = _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; } private: /// Only make is allowed to use this constructor. explicit Ref(std::shared_ptr&& _ptr) : ptr_(std::forward>(_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_