vari
Loading...
Searching...
No Matches
vref.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#include "vari/vptr.h"
32
33namespace vari
34{
35
38template < typename... Ts >
39class _vref
40{
41public:
42 using types = typelist< Ts... >;
43 using pointer = _vptr< Ts... >;
44 using reference = _vref< Ts... >;
45
48 template < typename... Us >
49 requires( vconvertible_to< typelist< Us... >, types > )
50 constexpr _vref( _vref< Us... > const& p ) noexcept
51 : _core( p._core )
52 {
53 }
54
57 template < typename U >
58 requires( vconvertible_to< typelist< U >, types > )
59 constexpr _vref( U& u ) noexcept
60 {
61 _core.set( u );
62 }
63
66 constexpr auto& operator*() const noexcept
67 {
68 return *_core.ptr;
69 }
70
73 constexpr auto* operator->() const noexcept
74 {
75 return _core.ptr;
76 }
77
80 constexpr auto* get() const noexcept
81 {
82 return _core.ptr;
83 }
84
87 [[nodiscard]] constexpr index_type index() const noexcept
88 {
89 return _core.get_index();
90 }
91
94 template < typename U >
95 requires( vconvertible_to< types, typelist< U > > )
96 constexpr operator U&() const noexcept
97 {
98 return *_core.ptr;
99 }
100
103 constexpr pointer vptr() const& noexcept
104 {
105 pointer res;
106 res._core = _core;
107 return res;
108 }
109
112 template < typename... Fs >
113 constexpr decltype( auto ) visit( Fs&&... fs ) const
114 {
115 typename _check_unique_invocability< types >::template with_pure_ref< Fs... > _{};
116 VARI_ASSERT( _core.ptr );
117 return _core.visit_impl( (Fs&&) fs... );
118 }
119
122 friend constexpr void swap( _vref& lh, _vref& rh ) noexcept
123 {
124 swap( lh._core, rh._core );
125 }
126
127private:
128 constexpr _vref() noexcept = default;
129
130 _ptr_core< types > _core;
131
132 template < typename... Us >
133 friend class _vref;
134 template < typename... Us >
135 friend class _vptr;
136 template < typename Deleter, typename... Us >
137 friend class _uvref;
138 template < typename Deleter, typename... Us >
139 friend class _uvptr;
140};
141
144template < typename... Lhs, typename... Rhs >
145constexpr auto operator<=>( _vref< Lhs... > const& lh, _vref< Rhs... > const& rh ) noexcept
146{
147 return lh.get() <=> rh.get();
148}
149
152template < typename... Lhs, typename... Rhs >
153constexpr bool operator==( _vref< Lhs... > const& lh, _vref< Rhs... > const& rh ) noexcept
154{
155 return lh.get() == rh.get();
156}
157
158
161template < typename... Ts >
162using vref = _define_variadic< _vref, typelist< Ts... > >;
163
164} // namespace vari
165
166VARI_GET_PTR_HASH_SPECIALIZATION( vari::_vref );
A nullable owning pointer to one of the types in Ts...
Definition: uvptr.h:43
constexpr pointer get() const noexcept
Returns a pointer to the pointed-to type.
Definition: uvptr.h:167
A nullable pointer to one of the types in Ts...
Definition: vptr.h:42
constexpr auto * get() const noexcept
Returns a pointer to the pointed-to type.
Definition: vptr.h:105
A non-nullable pointer to one of the types in Ts...
Definition: vref.h:40
constexpr _vref(U &u) noexcept
Constructs a vref from a reference to one of the types that vref can reference.
Definition: vref.h:59
constexpr pointer vptr() const &noexcept
Constructs a variadic pointer that points to the same target as the current reference.
Definition: vref.h:103
constexpr auto * operator->() const noexcept
Provides member access to the pointed-to type.
Definition: vref.h:73
constexpr index_type index() const noexcept
Returns the index representing the type currently being referenced.
Definition: vref.h:87
constexpr _vref(_vref< Us... > const &p) noexcept
Copy constructor for any compatible vref.
Definition: vref.h:50
constexpr auto * get() const noexcept
Returns a pointer to the pointed-to type.
Definition: vref.h:80
constexpr decltype(auto) visit(Fs &&... fs) const
Calls the appropriate function from the list fs..., based on the type of the current target.
Definition: vref.h:113
constexpr auto & operator*() const noexcept
Dereferences to the pointed-to type.
Definition: vref.h:66
friend constexpr void swap(_vref &lh, _vref &rh) noexcept
Swaps the current reference with another one.
Definition: vref.h:122
MIT License.
Definition: dispatch.h:32
_define_variadic< _vref, typelist< Ts... > > vref
A non-nullable pointer to types derived out of Ts... list by flattening it and filtering for unique t...
Definition: vref.h:162
_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