diff options
Diffstat (limited to 'compat/borrowed-ptr.hpp')
-rw-r--r-- | compat/borrowed-ptr.hpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp new file mode 100644 index 00000000..f649cae3 --- /dev/null +++ b/compat/borrowed-ptr.hpp @@ -0,0 +1,49 @@ +#pragma once + +namespace floormat::detail_borrowed_ptr { +struct control_block_; +} // namespace floormat::detail_borrowed_ptr + +namespace floormat { +template<typename T> class bptr; +template<typename T> bptr<T> static_pointer_cast(const bptr<T>& p); + +template<typename T> +class bptr final // NOLINT(*-special-member-functions) +{ + T* ptr; // todo add simple_bptr that doesn't allow casting. should only have the control block element. + detail_borrowed_ptr::control_block_* blk; + + friend bptr<T> static_pointer_cast<T>(const bptr<T>& p); + +public: + template<typename... Ts> + requires std::is_constructible_v<T, Ts&&...> + explicit bptr(InPlaceInitT, Ts&&... args) noexcept; + + constexpr bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions) + constexpr bptr() noexcept; + explicit bptr(T* ptr) noexcept; + ~bptr() noexcept; + + template<typename Y> requires std::is_convertible_v<Y*, T*> bptr(const bptr<Y>&) noexcept; + template<typename Y> requires std::is_convertible_v<Y*, T*> bptr(bptr<Y>&&) noexcept; + template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(const bptr<Y>&) noexcept; + template<typename Y> requires std::is_convertible_v<Y*, T*> bptr& operator=(bptr<Y>&&) noexcept; + + void swap() noexcept; + T* get() noexcept; + const T* get() const noexcept; + T& operator*() const noexcept; + T* operator->() const noexcept; + uint32_t use_count() const noexcept; + explicit operator bool() const noexcept; +}; + +template<typename T> constexpr bptr<T>::bptr(std::nullptr_t) noexcept: ptr{nullptr}, blk{nullptr} {} +template<typename T> constexpr bptr<T>::bptr() noexcept: bptr{nullptr} {} + + +template<typename T> bptr(T* ptr) -> bptr<T>; + +} // namespace floormat |