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