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 _uvptr :
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 >;
56 constexpr _uvptr()
noexcept =
default;
63 constexpr _uvptr( std::nullptr_t )
noexcept
69 template <
typename Deleter2,
typename... Us >
71 vconvertible_to< typelist< Us... >, types > &&
72 convertible_deleter< Deleter2, Deleter > )
75 _core = std::move( p._core );
81 template <
typename Deleter2,
typename... Us >
83 vconvertible_to< typelist< Us... >, types > &&
84 convertible_deleter< Deleter2, Deleter > )
86 : dbox( std::move( (dbox&) p ) )
88 _core = std::move( p._core );
95 template <
typename U >
96 requires( vconvertible_type< U, types > )
107 template <
typename U >
108 requires( vconvertible_type< U, types > && copy_constructible_deleter< Deleter > )
109 constexpr _uvptr( U* u, Deleter
const& d ) noexcept
120 template <
typename U >
121 requires( vconvertible_type< U, types > && move_constructible_deleter< Deleter > )
122 constexpr _uvptr( U* u, Deleter&& d ) noexcept
123 : dbox( std::move( d ) )
140 template <
typename Deleter2,
typename... Us >
142 vconvertible_to< typelist< Us... >, types > &&
143 convertible_deleter< Deleter2, Deleter > )
146 _uvptr tmp{ std::move( p ) };
147 swap( _core, tmp._core );
148 swap( (dbox&) ( *
this ), (dbox&) tmp );
177 [[nodiscard]]
constexpr index_type
index() const noexcept
179 return _core.get_index();
184 template <
typename... Us >
185 requires( vconvertible_to< types, typelist< Us... > > )
195 template <
typename... Us >
196 requires( vconvertible_to< types, typelist< Us... > > )
205 template <
typename... Us >
206 constexpr operator _vptr< Us... >() && =
delete;
211 _core = std::move( ptr._core );
212 tmp.delete_ptr( _deleter_box< Deleter >::get() );
220 swap( res._core, _core );
226 constexpr explicit operator bool() const noexcept
228 return _core.get_index() != null_index;
235 VARI_ASSERT( _core.get_index() != null_index );
245 VARI_ASSERT( _core.get_index() != null_index );
247 swap( res._core, _core );
253 template <
typename... Fs >
254 constexpr decltype( auto )
visit( Fs&&... f )
const
257 typename _check_unique_invocability< types >::template with_nullable_pure_ref<
260 if ( _core.ptr ==
nullptr )
261 return _dispatch_fun( empty, (Fs&&) f... );
262 return _core.visit_impl( (Fs&&) f... );
268 template <
typename... Fs >
269 constexpr decltype( auto )
take( Fs&&... fs ) &&
271 typename _check_unique_invocability< types >::template with_deleter<
272 Deleter >::template with_nullable_uvref< Fs... >
275 if ( p._core.ptr ==
nullptr )
276 return _dispatch_fun( empty, (Fs&&) fs... );
277 return p._core.template take_impl< same_uvref >( (Fs&&) fs... );
298 _core.delete_ptr( _deleter_box< Deleter >::get() );
305 swap( lh._core, rh._core );
306 swap( (dbox&) lh, (dbox&) rh );
312 template <
typename Deleter2,
typename... Us >
315 template <
typename Deleter2,
typename... Us >
321template <
typename... Lhs,
typename... Rhs >
324 return lh.get() <=> rh.get();
329template <
typename... Lhs,
typename... Rhs >
332 return lh.get() == rh.get();
337template <
typename... Ts >
A nullable owning pointer to one of the types in Ts...
Definition: uvptr.h:43
constexpr _uvptr(U *u, Deleter &&d) noexcept
Constructs an uvptr which owns a pointer to one of the types that uvptr can reference.
Definition: uvptr.h:122
constexpr ~_uvptr()
Destroys the owned object, if any.
Definition: uvptr.h:296
friend constexpr void swap(_uvptr &lh, _uvptr &rh) noexcept
Swaps uvptr with each other.
Definition: uvptr.h:303
constexpr _uvptr(U *u) noexcept
Constructs an uvptr which owns a pointer to one of the types that uvptr can reference.
Definition: uvptr.h:97
constexpr _uvptr & operator=(std::nullptr_t) noexcept
nullptr assignment operator resets this pointer.
Definition: uvptr.h:132
Deleter & get_deleter() noexcept
Getter to the internal deleter.
Definition: uvptr.h:282
constexpr decltype(auto) take(Fs &&... fs) &&
Constructs an owning reference to currently pointed-to type and transfers ownership to it.
Definition: uvptr.h:269
constexpr decltype(auto) visit(Fs &&... f) const
Calls the appropriate function from the list fs..., based on the type of the current target,...
Definition: uvptr.h:254
constexpr auto * operator->() const noexcept
Provides member access to the pointed-to type.
Definition: uvptr.h:161
constexpr pointer get() const noexcept
Returns a pointer to the pointed-to type.
Definition: uvptr.h:167
Deleter const & get_deleter() const noexcept
Getter to the internal deleter.
Definition: uvptr.h:289
constexpr index_type index() const noexcept
Returns the index representing the type currently being pointed-to.
Definition: uvptr.h:177
constexpr _uvptr(U *u, Deleter const &d) noexcept
Constructs an uvptr which owns a pointer to one of the types that uvptr can reference.
Definition: uvptr.h:109
constexpr _uvptr(_uvref< Deleter2, Us... > &&p) noexcept
Constructs an uvptr by transfering ownership from uvref with compatible types.
Definition: uvptr.h:73
constexpr pointer release() noexcept
Release the ownership of managed object, if any.
Definition: uvptr.h:217
constexpr owning_reference vref() &&noexcept
Constructs an owning variadic reference and trasnfers ownership of current target to the reference.
Definition: uvptr.h:243
constexpr reference vref() const &noexcept
Constructs a variadic reference that points to the same target as this pointer.
Definition: uvptr.h:233
constexpr _uvptr(_uvptr< Deleter2, Us... > &&p) noexcept
Constructs an uvptr by transfering ownership from uvptr with compatible types.
Definition: uvptr.h:85
constexpr _uvptr(std::nullptr_t) noexcept
Construct a pointer in a null state.
Definition: uvptr.h:63
constexpr auto & operator*() const noexcept
Dereferences to the pointed-to type.
Definition: uvptr.h:154
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
MIT License.
Definition: dispatch.h:32
_define_variadic< _uvptr, typelist< Ts... >, def_del > uvptr
A nullable owning pointer to types derived out of Ts... list by flattening it and filtering for uniqu...
Definition: uvptr.h:338
_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
Default library deleter.
Definition: deleter.h:33