-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpointer_base.h
More file actions
54 lines (40 loc) · 1.83 KB
/
pointer_base.h
File metadata and controls
54 lines (40 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
namespace moar
{
template<class C, typename T = void>
class pointer_base
{
protected:
using element_type = std::remove_extent_t<T>;
using pointer_type = std::add_pointer_t<element_type>;
pointer_type immutable_ptr_;
private:
pointer_base() = default;
public:
[[nodiscard]] constexpr auto get() const noexcept { return immutable_ptr_; }
[[nodiscard]] constexpr explicit operator bool() const noexcept { return get() != nullptr; }
[[nodiscard]] constexpr std::add_lvalue_reference_t<element_type> operator*() const { return *get(); }
[[nodiscard]] constexpr auto operator->() const noexcept { return get(); }
[[nodiscard]] constexpr explicit operator auto () const noexcept { return get(); }
#if __cplusplus >= 202302L
template<class Self>
[[maybe_unused]] constexpr pointer_type release(this Self&& self) noexcept { return self.internal_release(); }
template<class Self>
constexpr void reset(this Self&& self, pointer_type new_ptr = nullptr) noexcept
{
self.internal_reset(new_ptr);
}
#else
[[maybe_unused]] constexpr pointer_type release() noexcept { return static_cast<C*>(this)->internal_release(); }
constexpr void reset(pointer_type new_ptr) noexcept
{
static_cast<C*>(this)->internal_reset(new_ptr);
}
#endif
constexpr void reset() noexcept { this->reset(nullptr); }
constexpr void reset(nullptr_t new_ptr) noexcept { this->reset(reinterpret_cast<pointer_type>(new_ptr)); }
constexpr void reset(void* new_ptr) noexcept { this->reset(reinterpret_cast<pointer_type>(new_ptr)); }
constexpr void reset(std::size_t new_ptr) noexcept { this->reset(reinterpret_cast<pointer_type>(new_ptr)); }
friend C;
};
}