alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
InnerTypeAllowedCast.hpp
Go to the documentation of this file.
1/* Copyright 2025 Simeon Ehrig
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7
9
10#include <concepts>
11
12namespace alpaka::internal
13{
14 /** Get the element type without cv qualifier or static dimension from a value or reference type T.
15 *
16 * @example
17 * int const -> int
18 * int const & -> int
19 * int -> int
20 * &int const[2][2] -> int
21 *
22 */
23 template<typename T>
24 struct GetElementType
25 {
26 /** The trait GetElementType removes an optional reference and NonRefType removes the cv-qualifiers.
27 Two nested traits are required because we need the specialization for C static array. */
28 template<typename U>
29 struct NonRefType
30 {
31 using type = std::decay_t<U>;
32 };
33
34 template<alpaka::concepts::CStaticArray U>
35 struct NonRefType<U>
36 {
37 using type = typename std::remove_all_extents_t<std::remove_cv_t<U>>;
38 };
39
40 using type = typename NonRefType<std::remove_reference_t<T>>::type;
41 static constexpr bool is_const = std::is_const_v<std::remove_reference_t<T>>;
42 };
43
44 template<typename T>
45 using GetElementType_t = typename GetElementType<T>::type;
46
47 namespace concepts
48 {
49 /** Concept to restrict copy or move constructor of a DataSource which creates a new object with a different
50 * inner type.
51 *
52 * @tparam T_Type element type of the new object
53 * @tparam T_Type_Other element type of the object which is copied or moved
54 *
55 * @details
56 * Needs to fulfill the following requirements
57 * - the datatype without cv-qualifier needs to be the same
58 * - following const/mutable conversion to const/mutable are allowed
59 * - mutable -> mutable
60 * - const -> const
61 * - mutable -> const
62 */
63 template<typename T_Type, typename T_Type_Other>
64 concept InnerTypeAllowedCast = requires {
65 /// the value type without cv-qualifier needs to be the same
66 requires std::same_as<GetElementType_t<T_Type>, GetElementType_t<T_Type_Other>>;
67 /// check the correct cast of a const/mutable inner type to another const/mutable inner type
68 requires !(GetElementType<T_Type_Other>::is_const && !GetElementType<T_Type>::is_const);
69 };
70 } // namespace concepts
71} // namespace alpaka::internal