iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
type_traits.hpp
Go to the documentation of this file.
1// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
3// Copyright (c) 2025 Contributors to the Eclipse Foundation
4//
5// See the NOTICE file(s) distributed with this work for additional
6// information regarding copyright ownership.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Apache Software License 2.0 which is available at
10// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
11// which is available at https://opensource.org/licenses/MIT.
12//
13// SPDX-License-Identifier: Apache-2.0 OR MIT
14
15#ifndef IOX2_BB_PRIMITIVES_TYPE_TRAITS_HPP
16#define IOX2_BB_PRIMITIVES_TYPE_TRAITS_HPP
17
18#include <cstdint>
19#include <type_traits>
20
21namespace iox2 {
22namespace legacy {
27template <typename T, typename C>
29 using type = T;
30};
31template <typename T, typename C>
33 using type = const T;
34};
38template <typename T, typename C>
40
47template <typename>
48constexpr bool always_false_v { false };
49
50#if __cplusplus >= 201703L
51template <typename C, typename... Cargs>
52using invoke_result = std::invoke_result<C, Cargs...>;
53#else
54template <typename C, typename... Cargs>
55using invoke_result = std::result_of<C(Cargs...)>;
56#endif
57
61template <typename Callable, typename... ArgTypes>
63 // This variant is chosen when Callable(ArgTypes) successfully resolves to a valid type, i.e. is invocable.
64 template <typename C, typename... As>
65 static constexpr std::true_type test(typename iox2::legacy::invoke_result<C, As...>::type*) noexcept {
66 return {};
67 }
68
69 // AXIVION Next Construct AutosarC++19_03-A8.4.1 : we require a SFINEA failure case where all
70 // parameter types (non invokable ones) are allowed, this can be achieved with variadic arguments
71 // This is chosen if Callable(ArgTypes) does not resolve to a valid type.
72 template <typename C, typename... As>
73 // NOLINTNEXTLINE(cert-dcl50-cpp)
74 static constexpr std::false_type test(...) noexcept {
75 return {};
76 }
77
78 // Test with nullptr as this can stand in for a pointer to any type.
79 static constexpr bool value { decltype(test<Callable, ArgTypes...>(nullptr))::value };
80};
81
88template <typename ReturnType, typename Callable, typename... ArgTypes>
90 template <typename C, typename... As>
91 static constexpr std::true_type
92 test(std::enable_if_t<
94 return {};
95 }
96 // AXIVION Next Construct AutosarC++19_03-A8.4.1 : we require a SFINEA failure case where all
97 // parameter types (non invokable ones) are allowed, this can be achieved with variadic arguments
98 template <typename C, typename... As>
99 // NOLINTNEXTLINE(cert-dcl50-cpp)
100 static constexpr std::false_type test(...) noexcept {
101 return {};
102 }
103
104 // Test with nullptr as this can stand in for a pointer to any type.
105 static constexpr bool value { decltype(test<Callable, ArgTypes...>(nullptr))::value };
106};
107
111template <typename T>
112struct is_function_pointer : std::false_type { };
113template <typename ReturnType, typename... ArgTypes>
114struct is_function_pointer<ReturnType (*)(ArgTypes...)> : std::true_type { };
115
117template <typename T>
118struct is_char_array : std::false_type { };
119
120template <uint64_t N>
121// AXIVION DISABLE STYLE AutosarC++19_03-A18.1.1 : struct used to deduce char array types, it does not use them
122// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
123struct is_char_array<char[N]> : std::true_type { };
124// AXIVION ENABLE STYLE AutosarC++19_03-A18.1.1
125
127template <typename...>
128using void_t = void;
129
131template <bool B>
132using bool_constant = std::integral_constant<bool, B>;
133
135template <class B>
136struct negation : bool_constant<!bool(B::value)> { };
137
138template <bool...>
139struct bool_pack { };
140
142template <class...>
143struct conjunction : std::true_type { };
144
145template <class Arg>
146struct conjunction<Arg> : Arg { };
147
148template <class Arg, class... Args>
149struct conjunction<Arg, Args...> : std::conditional_t<!bool(Arg::value), Arg, conjunction<Args...>> { };
150
152//
153// References:
154// - https://en.cppreference.com/w/cpp/types/remove_cvref
155// - https://wg21.link/meta.trans.other#lib:remove_cvref
156template <typename T>
158 using type_t = std::remove_cv_t<std::remove_reference_t<T>>;
159};
160
162//
163// References:
164// - https://en.cppreference.com/w/cpp/types/remove_cvref
165// - https://wg21.link/meta.type.synop#lib:remove_cvref_t
166template <typename T>
168
169template <typename T>
170using is_c_array_t = std::is_array<std::remove_reference_t<T>>;
171
172template <typename T>
174
175template <typename From, typename To>
176// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays)
177using is_convertible_t = std::is_convertible<From (*)[], To (*)[]>;
178
179template <typename Iter>
180using iter_reference_t = decltype(*std::declval<Iter&>());
181
182template <typename Iter, typename T>
185
189template <class From, class To>
190constexpr bool is_convertible_v = std::is_convertible<From, To>::value;
191
192} // namespace legacy
193} // namespace iox2
194
195#endif // IOX2_BB_PRIMITIVES_TYPE_TRAITS_HPP
void void_t
Maps a sequence of any types to the type void.
std::result_of< C(Cargs...)> invoke_result
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
decltype(*std::declval< Iter & >()) iter_reference_t
std::integral_constant< bool, B > bool_constant
Implementation C++17 bool_constant helper.
typename remove_cvref< T >::type_t remove_cvref_t
Implementation of C++20's std::remove_cvref_t.
constexpr bool is_convertible_v
Helper template from C++17.
std::is_convertible< From(*)[], To(*)[]> is_convertible_t
iox2::legacy::is_convertible_t< std::remove_reference_t< iter_reference_t< Iter > >, T > iter_has_convertible_ref_type_t
std::is_array< std::remove_reference_t< T > > is_c_array_t
typename add_const_conditionally< T, C >::type add_const_conditionally_t
Helper type for add_const_conditionally which adds const to type T if C has the const qualifier.
Conditionally add const to type T if C has the const qualifier.
Implementation of C++17 std::conjunction.
struct to check whether an argument is a char array
Check whether T is a function pointer with arbitrary signature.
Verifies whether the passed Callable type is in fact invocable with the given arguments and the resul...
static constexpr bool value
static constexpr std::true_type test(std::enable_if_t< std::is_convertible< typename iox2::legacy::invoke_result< C, As... >::type, ReturnType >::value > *) noexcept
static constexpr std::false_type test(...) noexcept
Verifies whether the passed Callable type is in fact invocable with the given arguments.
static constexpr std::true_type test(typename iox2::legacy::invoke_result< C, As... >::type *) noexcept
static constexpr bool value
static constexpr std::false_type test(...) noexcept
Implementation of C++17 negation.
Implementation of C++20's std::remove_cvref.
std::remove_cv_t< std::remove_reference_t< T > > type_t