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/deleter.h"
31#include "vari/forward.h"
41template <
typename Deleter,
typename... Ts >
42class _uvref :
private _deleter_box< Deleter >
44 template <
typename... Us >
46 using dbox = _deleter_box< Deleter >;
49 using types = typelist< Ts... >;
51 using core_type = _ptr_core< types >;
61 template <
typename Deleter2,
typename... Us >
63 vconvertible_to< typelist< Us... >, types > &&
64 convertible_deleter< Deleter2, Deleter > )
66 : dbox( std::move( (dbox&) p ) )
68 _core = std::move( p._core );
74 template <
typename U >
75 requires( vconvertible_type< U, types > )
76 constexpr explicit _uvref( U& u )
noexcept
85 template <
typename U >
86 requires( vconvertible_type< U, types > && copy_constructible_deleter< Deleter > )
87 constexpr explicit _uvref( U& u, Deleter
const& d ) noexcept
97 template <
typename U >
98 requires( vconvertible_type< U, types > && move_constructible_deleter< Deleter > )
99 constexpr explicit _uvref( U& u, Deleter&& d ) noexcept
100 : dbox( std::move( d ) )
107 template <
typename Deleter2,
typename... Us >
109 vconvertible_to< typelist< Us... >, types > &&
110 convertible_deleter< Deleter2, Deleter > )
113 _uvref tmp{ std::move( p ) };
114 swap( _core, tmp._core );
115 swap( (dbox&) ( *
this ), (dbox&) tmp );
136 VARI_ASSERT( _core.ptr );
144 [[nodiscard]]
constexpr index_type
index() const noexcept
146 VARI_ASSERT( _core.ptr );
152 template <
typename... Us >
153 requires( vconvertible_to< types, typelist< Us... > > )
156 VARI_ASSERT( _core.ptr );
157 return vptr().vref();
161 template <
typename... Us >
162 requires( vconvertible_to< types, typelist< Us... > > )
165 VARI_ASSERT( _core.ptr );
166 return vptr().vref();
170 template <
typename... Us >
171 constexpr operator _vref< Us... >() && =
delete;
177 VARI_ASSERT( _core.ptr );
187 VARI_ASSERT( _core.ptr );
189 swap( res._core, _core );
195 template <
typename... Fs >
196 constexpr decltype( auto )
visit( Fs&&... f )
const
198 typename _check_unique_invocability< types >::template with_pure_ref< Fs... > _{};
199 VARI_ASSERT( _core.ptr );
200 return _core.visit_impl( (Fs&&) f... );
205 template <
typename... Fs >
206 constexpr decltype( auto )
take( Fs&&... fs ) &&
208 typename _check_unique_invocability< types >::template with_deleter<
209 Deleter >::template with_uvref< Fs... >
211 VARI_ASSERT( _core.ptr );
214 return tmp.template take_impl< same_uvref >( (Fs&&) fs... );
236 _core.delete_ptr( dbox::get() );
243 swap( lh._core, rh._core );
244 swap( (dbox&) lh, (dbox&) rh );
248 constexpr _uvref() noexcept = default;
252 template < typename... Us >
255 template < typename Deleter2, typename... Us >
258 template < typename Deleter2, typename... Us >
264template < typename... Lhs, typename... Rhs >
265constexpr auto operator<=>(
_uvref< Lhs... > const& lh,
_uvref< Rhs... > const& rh ) noexcept
267 return lh.
get() <=> rh.
get();
272template <
typename... Lhs,
typename... Rhs >
275 return lh.get() == rh.get();
280template <
typename... Ts >
284template <
typename T >
287 return uvref< T >( *
new T( std::move( item ) ) );
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
A non-nullable pointer to one of the types in Ts...
Definition: vref.h:40
constexpr auto * get() const noexcept
Returns a pointer to the pointed-to type.
Definition: vref.h:80
MIT License.
Definition: dispatch.h:32
_define_variadic< _uvref, typelist< Ts... >, def_del > uvref
A non-nullable owning pointer to types derived out of Ts... list by flattening it and filtering for u...
Definition: uvref.h:281
constexpr uvref< T > uwrap(T item)
Wraps object item into uvref of its type.
Definition: uvref.h:285
_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
constexpr _uvref(U &u, Deleter &&d) noexcept
Constructs an uvref which owns a reference to one of the types that uvref can reference.
Definition: uvref.h:99
friend constexpr void swap(_uvref &lh, _uvref &rh) noexcept
Swaps uvref with each other.
Definition: uvref.h:241
constexpr auto & operator*() const noexcept
Dereferences to the pointed-to type.
Definition: uvref.h:121
constexpr pointer vptr() const &noexcept
Constructs a variadic pointer that points to the same target as the current reference.
Definition: uvref.h:175
constexpr reference get() const noexcept
Returns a reference to the pointed-to type.
Definition: uvref.h:134
constexpr _uvref(U &u) noexcept
Constructs an uvref which owns a reference to one of the types that uvref can reference.
Definition: uvref.h:76
Deleter & get_deleter() noexcept
Getter to the internal deleter.
Definition: uvref.h:219
constexpr index_type index() const noexcept
Returns the index representing the type currently being referenced.
Definition: uvref.h:144
constexpr ~_uvref()
Destroys the owned object.
Definition: uvref.h:234
constexpr _uvref(U &u, Deleter const &d) noexcept
Constructs an uvref which owns a reference to one of the types that uvref can reference.
Definition: uvref.h:87
constexpr _uvref(_uvref< Deleter2, Us... > &&p) noexcept
Constructs an uvref by transfering ownership from uvref with compatible types.
Definition: uvref.h:65
constexpr owning_pointer vptr() &&noexcept
Constructs an owning variadic pointer and transfers ownership of current target to the pointer.
Definition: uvref.h:185
constexpr auto * operator->() const noexcept
Provides member access to the pointed-to type.
Definition: uvref.h:128
constexpr decltype(auto) visit(Fs &&... f) const
Calls the appropriate function from the list fs..., based on the type of the current target.
Definition: uvref.h:196
Deleter const & get_deleter() const noexcept
Getter to the internal deleter.
Definition: uvref.h:226
constexpr decltype(auto) take(Fs &&... fs) &&
Constructs an owning reference to currently pointed-to type and transfers ownership to it.
Definition: uvref.h:206
Default library deleter.
Definition: deleter.h:33