2020-05-26 11:22:18 +00:00
|
|
|
#ifndef ARBUTILS_BORROWED_PTR_HPP
|
|
|
|
#define ARBUTILS_BORROWED_PTR_HPP
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
/// A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed from another
|
|
|
|
/// owning object that is assumed to always be kept alive during the entire lifetime of the borrowing object.
|
|
|
|
template <class T> class borrowed_ptr {
|
|
|
|
private:
|
|
|
|
T* _raw;
|
|
|
|
|
|
|
|
public:
|
2020-05-26 11:44:06 +00:00
|
|
|
inline borrowed_ptr<T>() : _raw(nullptr){};
|
2020-05-26 11:22:18 +00:00
|
|
|
inline borrowed_ptr<T>(T* ptr) : _raw(ptr){};
|
|
|
|
inline borrowed_ptr(const borrowed_ptr<T>& other) : _raw(other._raw){};
|
|
|
|
inline borrowed_ptr(const std::unique_ptr<T>& other) : _raw(other.get()){};
|
|
|
|
|
|
|
|
~borrowed_ptr() = default;
|
|
|
|
|
2020-05-26 11:44:06 +00:00
|
|
|
inline borrowed_ptr<T>& operator=(const borrowed_ptr<T>& rhs) {
|
|
|
|
_raw = rhs._raw;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-05-26 11:22:18 +00:00
|
|
|
|
2020-05-26 12:01:52 +00:00
|
|
|
inline T* operator->() const noexcept { return _raw; }
|
|
|
|
inline T* GetRaw() const noexcept { return _raw; }
|
2020-05-26 11:22:18 +00:00
|
|
|
|
|
|
|
inline bool operator==(const borrowed_ptr& rhs) const { return _raw == rhs._raw; }
|
|
|
|
inline bool operator!=(const borrowed_ptr& rhs) const { return _raw != rhs._raw; }
|
2020-05-26 11:44:06 +00:00
|
|
|
|
|
|
|
[[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; }
|
2020-05-26 11:22:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ARBUTILS_BORROWED_PTR_HPP
|