vari
Loading...
Searching...
No Matches
vptr.h
1
23
24#pragma once
25
26#include "vari/bits/assert.h"
27#include "vari/bits/ptr_core.h"
28#include "vari/bits/typelist.h"
29#include "vari/bits/util.h"
30#include "vari/forward.h"
31
32#include <cstddef>
33
34namespace vari
35{
36
37
40template < typename... Ts >
41class _vptr
42{
43public:
44 using types = typelist< Ts... >;
45
46 using reference = _vref< Ts... >;
47
48 constexpr _vptr() = default;
49 constexpr _vptr( _vptr const& ) = default;
50 constexpr _vptr( _vptr&& ) noexcept = default;
51 constexpr _vptr& operator=( _vptr const& ) = default;
52 constexpr _vptr& operator=( _vptr&& ) noexcept = default;
53
56 constexpr _vptr( std::nullptr_t ) noexcept
57 {
58 }
59
62 template < typename... Us >
63 requires( vconvertible_to< typelist< Us... >, types > )
64 constexpr explicit _vptr( _vref< Us... > const& r ) noexcept
65 : _core( r._core )
66 {
67 }
68
71 template < typename... Us >
72 requires( vconvertible_to< typelist< Us... >, types > )
73 constexpr _vptr( _vptr< Us... > const& p ) noexcept
74 : _core( p._core )
75 {
76 }
77
81 template < typename U >
82 requires( vconvertible_to< typelist< U >, types > )
83 constexpr _vptr( U* u ) noexcept
84 {
85 if ( u )
86 _core.set( *u );
87 }
88
91 constexpr auto& operator*() const noexcept
92 {
93 return *_core.ptr;
94 }
95
98 constexpr auto* operator->() const noexcept
99 {
100 return _core.ptr;
101 }
102
105 constexpr auto* get() const noexcept
106 {
107 return _core.ptr;
108 }
109
113 [[nodiscard]] constexpr index_type index() const noexcept
114 {
115 return _core.get_index();
116 }
117
120 constexpr explicit operator bool() const noexcept
121 {
122 return _core.ptr != nullptr;
123 }
124
127 constexpr reference vref() const noexcept
128 {
129 VARI_ASSERT( _core.ptr );
130
131 reference r;
132 r._core = _core;
133 return r;
134 }
135
138 template < typename... Fs >
139 constexpr decltype( auto ) visit( Fs&&... fs ) const
140 {
141 typename _check_unique_invocability< types >::template with_nullable_pure_ref<
142 Fs... >
143 _{};
144 if ( _core.ptr == nullptr )
145 return _dispatch_fun( empty, (Fs&&) fs... );
146 return _core.visit_impl( (Fs&&) fs... );
147 }
148
149
152 friend constexpr void swap( _vptr& lh, _vptr& rh ) noexcept
153 {
154 swap( lh._core, rh._core );
155 }
156
157private:
158 _ptr_core< types > _core;
159
160 template < typename... Us >
161 friend class _vref;
162 template < typename... Us >
163 friend class _vptr;
164 template < typename Deleter, typename... Us >
165 friend class _uvref;
166 template < typename Deleter, typename... Us >
167 friend class _uvptr;
168};
169
172template < typename... Lhs, typename... Rhs >
173constexpr auto operator<=>( _vptr< Lhs... > const& lh, _vptr< Rhs... > const& rh ) noexcept
174{
175 return lh.get() <=> rh.get();
176}
177
180template < typename... Lhs, typename... Rhs >
181constexpr bool operator==( _vptr< Lhs... > const& lh, _vptr< Rhs... > const& rh ) noexcept
182{
183 return lh.get() == rh.get();
184}
185
188template < typename... Ts >
189using vptr = _define_variadic< _vptr, typelist< Ts... > >;
190
191} // namespace vari
192
193VARI_GET_PTR_HASH_SPECIALIZATION( vari::_vptr );
A nullable owning pointer to one of the types in Ts...
Definition: uvptr.h:43
A nullable pointer to one of the types in Ts...
Definition: vptr.h:42
constexpr decltype(auto) visit(Fs &&... fs) const
Calls the appropriate function from the list fs..., based on the type of the current target,...
Definition: vptr.h:139
constexpr auto * get() const noexcept
Returns a pointer to the pointed-to type.
Definition: vptr.h:105
constexpr reference vref() const noexcept
Constructs a variadic reference that points to the same target as this pointer.
Definition: vptr.h:127
constexpr auto * operator->() const noexcept
Provides member access to the pointed-to type.
Definition: vptr.h:98
constexpr _vptr(U *u) noexcept
Constructs a vptr from a pointer to one of the types that vptr can reference.
Definition: vptr.h:83
constexpr _vptr(_vptr< Us... > const &p) noexcept
Copy constructor for any compatible vptr.
Definition: vptr.h:73
constexpr _vptr(_vref< Us... > const &r) noexcept
Copy constructor for any compatible vref.
Definition: vptr.h:64
constexpr index_type index() const noexcept
Returns the index representing the type currently being pointed-to.
Definition: vptr.h:113
constexpr auto & operator*() const noexcept
Dereferences to the pointed-to type.
Definition: vptr.h:91
constexpr _vptr(std::nullptr_t) noexcept
Construct a pointer in a null state.
Definition: vptr.h:56
friend constexpr void swap(_vptr &lh, _vptr &rh) noexcept
Swaps vptr with each other.
Definition: vptr.h:152
A non-nullable pointer to one of the types in Ts...
Definition: vref.h:40
MIT License.
Definition: dispatch.h:32
_define_variadic< _vptr, typelist< Ts... > > vptr
A nullable pointer to types derived out of Ts... list by flattening it and filtering for unique types...
Definition: vptr.h:189
_vptr_apply_t< T, unique_typelist_t< flatten_t< TL > >, Extra... > _define_variadic
Given a templated type T and typelist of types TL, aliases T<Us...> where Us... is flattend version o...
Definition: util.h:61
A non-nullable owning pointer to one of the types in Ts...
Definition: uvref.h:43