iceoryx_hoofs 2.0.3
type_traits.hpp
1// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// SPDX-License-Identifier: Apache-2.0
17
18#ifndef IOX_HOOFS_CXX_TYPE_TRAITS_HPP
19#define IOX_HOOFS_CXX_TYPE_TRAITS_HPP
20
21#include <type_traits>
22
23namespace iox
24{
25namespace cxx
26{
32template <typename T, typename C>
34{
35 using type = T;
36};
37template <typename T, typename C>
38struct add_const_conditionally<T, const C>
39{
40 using type = const T;
41};
45template <typename T, typename C>
46using add_const_conditionally_t = typename add_const_conditionally<T, C>::type;
47
54template <typename>
55constexpr bool always_false_v = false;
56
57// windows defines __cplusplus as 199711L
58#if __cplusplus < 201703L && !defined(_WIN32)
59template <typename C, typename... Cargs>
60using invoke_result = std::result_of<C(Cargs...)>;
61#elif __cplusplus >= 201703L || defined(_WIN32)
62template <typename C, typename... Cargs>
63using invoke_result = std::invoke_result<C, Cargs...>;
64#endif
65
69template <typename Callable, typename... ArgTypes>
71{
72 // This variant is chosen when Callable(ArgTypes) successfully resolves to a valid type, i.e. is invocable.
74 template <typename C, typename... As>
75 static constexpr std::true_type test(typename cxx::invoke_result<C, As...>::type*) noexcept
76 {
77 return {};
78 }
79
80 // This is chosen if Callable(ArgTypes) does not resolve to a valid type.
81 template <typename C, typename... As>
82 static constexpr std::false_type test(...) noexcept
83 {
84 return {};
85 }
86
87 // Test with nullptr as this can stand in for a pointer to any type.
88 static constexpr bool value = decltype(test<Callable, ArgTypes...>(nullptr))::value;
89};
90
97template <typename ReturnType, typename Callable, typename... ArgTypes>
99{
100 template <typename C, typename... As>
101 static constexpr std::true_type test(
102 std::enable_if_t<std::is_convertible<typename cxx::invoke_result<C, As...>::type, ReturnType>::value>*) noexcept
103 {
104 return {};
105 }
106
107 template <typename C, typename... As>
108 static constexpr std::false_type test(...) noexcept
109 {
110 return {};
111 }
112
113 // Test with nullptr as this can stand in for a pointer to any type.
114 static constexpr bool value = decltype(test<Callable, ArgTypes...>(nullptr))::value;
115};
116
120template <typename T>
121struct is_function_pointer : std::false_type
122{
123};
124template <typename ReturnType, typename... ArgTypes>
125struct is_function_pointer<ReturnType (*)(ArgTypes...)> : std::true_type
126{
127};
128
130template <typename...>
131using void_t = void;
132} // namespace cxx
133} // namespace iox
134
135#endif // IOX_HOOFS_CXX_TYPE_TRAITS_HPP
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
Conditionally add const to type T if C has the const qualifier.
Definition: type_traits.hpp:34
Check whether T is a function pointer with arbitrary signature.
Definition: type_traits.hpp:122
Verifies whether the passed Callable type is in fact invocable with the given arguments and the resul...
Definition: type_traits.hpp:99
Verifies whether the passed Callable type is in fact invocable with the given arguments.
Definition: type_traits.hpp:71
static constexpr std::true_type test(typename cxx::invoke_result< C, As... >::type *) noexcept
Definition: type_traits.hpp:75