42
42
#include < cuda/std/__concepts/__concept_macros.h>
43
43
#include < cuda/std/__concepts/_One_of.h>
44
44
#include < cuda/std/__concepts/all_of.h>
45
+ #include < cuda/std/__new_>
46
+ #include < cuda/std/__type_traits/is_constructible.h>
45
47
#include < cuda/std/__type_traits/is_nothrow_constructible.h>
46
48
#include < cuda/std/__type_traits/is_same.h>
47
49
#include < cuda/std/__type_traits/remove_cvref.h>
50
+ #include < cuda/std/__type_traits/type_set.h>
48
51
#include < cuda/std/__utility/exchange.h>
49
52
#include < cuda/std/__utility/forward.h>
53
+ #include < cuda/std/__utility/in_place.h>
50
54
51
55
namespace cuda ::experimental::mr
52
56
{
@@ -77,19 +81,25 @@ private:
77
81
78
82
using __vtable = _CUDA_VMR::_Filtered_vtable<_Properties...>;
79
83
84
+ // ! @brief Validates that a set of \c _OtherProperties... is a superset of \c _Properties... .
80
85
template <class ... _OtherProperties>
81
86
static constexpr bool __properties_match =
82
87
_CUDA_VSTD::__type_set_contains<_CUDA_VSTD::__make_type_set<_OtherProperties...>, _Properties...>;
83
88
89
+ // ! @brief Validates that a passed in \c _Resource satisfies the \c resource or \c async_resource concept respectively
90
+ // ! as well as all properties in \c _Properties... .
91
+ template <class _Resource >
92
+ static constexpr bool __valid_resource =
93
+ _Alloc_type == _CUDA_VMR::_AllocType::_Async
94
+ ? _CUDA_VMR::async_resource_with<_Resource, _Properties...>
95
+ : _CUDA_VMR::resource_with<_Resource, _Properties...>;
96
+
84
97
public:
85
98
// ! @brief Constructs a \c basic_any_resource from a type that satisfies the \c resource or \c async_resource
86
99
// ! concept as well as all properties.
87
100
// ! @param __res The resource to be wrapped within the \c basic_any_resource.
88
101
_LIBCUDACXX_TEMPLATE (class _Resource , class __resource_t = _CUDA_VSTD::remove_cvref_t <_Resource>)
89
- _LIBCUDACXX_REQUIRES (
90
- (!__is_basic_any_resource<_Resource>) _LIBCUDACXX_AND(_CUDA_VMR::resource_with<__resource_t , _Properties...>)
91
- _LIBCUDACXX_AND (_Alloc_type != _CUDA_VMR::_AllocType::_Async
92
- || (_CUDA_VMR::async_resource_with<__resource_t , _Properties...>) ))
102
+ _LIBCUDACXX_REQUIRES ((!__is_basic_any_resource<_Resource>) _LIBCUDACXX_AND __valid_resource<__resource_t >)
93
103
basic_any_resource (_Resource&& __res) noexcept
94
104
: _CUDA_VMR::_Resource_base<_Alloc_type, _CUDA_VMR::_WrapperType::_Owning>(
95
105
nullptr , &_CUDA_VMR::__alloc_vtable<_Alloc_type, _CUDA_VMR::_WrapperType::_Owning, __resource_t >)
@@ -105,8 +115,31 @@ public:
105
115
}
106
116
}
107
117
118
+ // ! @brief Constructs a \c basic_any_resource wrapping an object of type \c _Resource that
119
+ // ! is constructed from \c __args... . \c _Resource must satisfy the \c resource or \c async_resource
120
+ // ! concept, and it must provide all properties in \c _Properties... .
121
+ // ! @param __args The arguments used to construct the instance of \c _Resource to be wrapped within the
122
+ // ! \c basic_any_resource.
123
+ _LIBCUDACXX_TEMPLATE (class _Resource , class ... _Args)
124
+ _LIBCUDACXX_REQUIRES (_CCCL_TRAIT(_CUDA_VSTD::is_constructible, _Resource, _Args...)
125
+ _LIBCUDACXX_AND __valid_resource<_Resource>)
126
+ basic_any_resource (_CUDA_VSTD::in_place_type_t <_Resource>, _Args&&... __args) noexcept
127
+ : _CUDA_VMR::_Resource_base<_Alloc_type, _CUDA_VMR::_WrapperType::_Owning>(
128
+ nullptr , &_CUDA_VMR::__alloc_vtable<_Alloc_type, _CUDA_VMR::_WrapperType::_Owning, _Resource>)
129
+ , __vtable(__vtable::template _Create<_Resource>())
130
+ {
131
+ if constexpr (_CUDA_VMR::_IsSmall<_Resource>())
132
+ {
133
+ ::new (static_cast <void *>(this ->__object .__buf_ )) _Resource (_CUDA_VSTD::forward<_Args>(__args)...);
134
+ }
135
+ else
136
+ {
137
+ this ->__object .__ptr_ = new _Resource (_CUDA_VSTD::forward<_Args>(__args)...);
138
+ }
139
+ }
140
+
108
141
// ! @brief Conversion from a \c basic_any_resource with the same set of properties but in a different order.
109
- // ! This constructor also handles conversion from \c async_any_resource to \c any_resource
142
+ // ! This constructor also handles conversion from \c any_async_resource to \c any_resource
110
143
// ! @param __other The other \c basic_any_resource.
111
144
_LIBCUDACXX_TEMPLATE (_CUDA_VMR::_AllocType _OtherAllocType, class ... _OtherProperties)
112
145
_LIBCUDACXX_REQUIRES (
@@ -260,19 +293,64 @@ template <class... _Properties>
260
293
using any_resource = basic_any_resource<_CUDA_VMR::_AllocType::_Default, _Properties...>;
261
294
262
295
// ! @rst
263
- // ! .. _cudax-memory-resource-async- any-resource:
296
+ // ! .. _cudax-memory-resource-any-async -resource:
264
297
// !
265
298
// ! Type erased wrapper around an `async_resource`
266
299
// ! -----------------------------------------------
267
300
// !
268
- // ! ``async_any_resource `` wraps any given :ref:`async resource <libcudacxx-extended-api-memory-resources-resource>`
301
+ // ! ``any_async_resource `` wraps any given :ref:`async resource <libcudacxx-extended-api-memory-resources-resource>`
269
302
// ! that satisfies the required properties. It owns the contained resource, taking care of construction / destruction.
270
303
// ! This makes it especially suited for use in e.g. container types that need to ensure that the lifetime of the
271
304
// ! container exceeds the lifetime of the memory resource used to allocate the storage
272
305
// !
273
306
// ! @endrst
274
307
template <class ... _Properties>
275
- using async_any_resource = basic_any_resource<_CUDA_VMR::_AllocType::_Async, _Properties...>;
308
+ using any_async_resource = basic_any_resource<_CUDA_VMR::_AllocType::_Async, _Properties...>;
309
+
310
+ // ! @rst
311
+ // ! .. _cudax-memory-resource-make-any-resource:
312
+ // !
313
+ // ! Factory function for `any_resource` objects
314
+ // ! -------------------------------------------
315
+ // !
316
+ // ! ``make_any_resource`` constructs an :ref:`any_resource <cudax-memory-resource-any-resource>` object that wraps a
317
+ // ! newly constructed instance of the given resource type. The resource type must satisfy the ``cuda::mr::resource``
318
+ // ! concept and provide all of the properties specified in the template parameter pack.
319
+ // !
320
+ // ! @param __args The arguments used to construct the instance of the resource type.
321
+ // !
322
+ // ! @endrst
323
+ template <class _Resource , class ... _Properties, class ... _Args>
324
+ auto make_any_resource (_Args&&... __args) -> any_resource<_Properties...>
325
+ {
326
+ static_assert (_CUDA_VMR::resource<_Resource>, " _Resource does not satisfy the cuda::mr::resource concept" );
327
+ static_assert (_CUDA_VMR::resource_with<_Resource, _Properties...>,
328
+ " Resource does not satisfy the required properties" );
329
+ return any_resource<_Properties...>{_CUDA_VSTD::in_place_type<_Resource>, _CUDA_VSTD::forward<_Args>(__args)...};
330
+ }
331
+
332
+ // ! @rst
333
+ // ! .. _cudax-memory-resource-make-any-async-resource:
334
+ // !
335
+ // ! Factory function for `any_async_resource` objects
336
+ // ! -------------------------------------------------
337
+ // !
338
+ // ! ``make_any_async_resource`` constructs an :ref:`any_async_resource <cudax-memory-resource-any-async-resource>`
339
+ // ! object that wraps a newly constructed instance of the given resource type. The resource type must satisfy the
340
+ // ! ``cuda::mr::async_resource`` concept and provide all of the properties specified in the template parameter pack.
341
+ // !
342
+ // ! @param __args The arguments used to construct the instance of the resource type.
343
+ // !
344
+ // ! @endrst
345
+ template <class _Resource , class ... _Properties, class ... _Args>
346
+ auto make_any_async_resource (_Args&&... __args) -> any_async_resource<_Properties...>
347
+ {
348
+ static_assert (_CUDA_VMR::async_resource<_Resource>,
349
+ " _Resource does not satisfy the cuda::mr::async_resource concept" );
350
+ static_assert (_CUDA_VMR::async_resource_with<_Resource, _Properties...>,
351
+ " Resource does not satisfy the required properties" );
352
+ return any_async_resource<_Properties...>{_CUDA_VSTD::in_place_type<_Resource>, _CUDA_VSTD::forward<_Args>(__args)...};
353
+ }
276
354
277
355
} // namespace cuda::experimental::mr
278
356
0 commit comments