LeviLamina
Loading...
Searching...
No Matches
NonOwnerPointer.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4#include "mc/deps/core/utility/EnableNonOwnerReferences.h"
5
6namespace Bedrock {
7
8template <typename T>
10public:
11 std::shared_ptr<::Bedrock::EnableNonOwnerReferences::ControlBlock> mControlBlock;
12 T* mPointer{};
13
14 NonOwnerPointer(std::shared_ptr<::Bedrock::EnableNonOwnerReferences::ControlBlock> cb, T* p)
15 : mControlBlock(std::move(cb)),
16 mPointer(p) {}
17
18 NonOwnerPointer(T const& t)
20 static_cast<::Bedrock::EnableNonOwnerReferences const&>(t).mControlBlock,
21 const_cast<T*>(std::addressof(t))
22 ) {}
23
24 NonOwnerPointer() noexcept {}
25 NonOwnerPointer(std::nullptr_t) noexcept {}
26 T* get() const {
27 if (mControlBlock && mControlBlock->mIsValid) {
28 return mPointer;
29 }
30 return nullptr;
31 }
32 explicit operator bool() const noexcept { return get() != nullptr; }
33
34 [[nodiscard]] constexpr operator T*() const { return get(); }
35 [[nodiscard]] constexpr T* operator->() const { return get(); }
36 [[nodiscard]] constexpr T& operator*() const { return *get(); }
37
38 [[nodiscard]] bool operator==(nullptr_t) noexcept { return get() == nullptr; }
39 template <class U>
40 [[nodiscard]] bool operator==(NonOwnerPointer<U> const& r) noexcept {
41 return get() == r.get();
42 }
43 template <class U>
44 [[nodiscard]] bool operator<=>(NonOwnerPointer<U> const& r) noexcept {
45 return get() <=> r.get();
46 }
47};
48
49}; // namespace Bedrock
Definition EnableNonOwnerReferences.h:7
Definition NonOwnerPointer.h:9