iceoryx_doc  1.0.1
type_traits.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 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_UTILS_CXX_TYPE_TRAITS_HPP
19 #define IOX_UTILS_CXX_TYPE_TRAITS_HPP
20 
21 #include <type_traits>
22 
23 namespace iox
24 {
25 namespace cxx
26 {
32 template <typename T, typename C>
34 {
35  using type = T;
36 };
37 template <typename T, typename C>
38 struct add_const_conditionally<T, const C>
39 {
40  using type = const T;
41 };
45 template <typename T, typename C>
46 using add_const_conditionally_t = typename add_const_conditionally<T, C>::type;
47 
51 template <typename Callable, typename... ArgTypes>
53 {
54  // This variant is chosen when Callable(ArgTypes) successfully resolves to a valid type, i.e. is invocable.
56  template <typename C, typename... As>
57  static constexpr std::true_type test(typename std::result_of<C(As...)>::type*)
58  {
59  return {};
60  }
61 
62  // This is chosen if Callable(ArgTypes) does not resolve to a valid type.
63  template <typename C, typename... As>
64  static constexpr std::false_type test(...)
65  {
66  return {};
67  }
68 
69  // Test with nullptr as this can stand in for a pointer to any type.
70  static constexpr bool value = decltype(test<Callable, ArgTypes...>(nullptr))::value;
71 };
72 
76 template <typename Callable = void, typename ReturnType = void, typename ArgTypes = void>
77 struct has_signature : std::false_type
78 {
79 };
80 
81 template <typename Callable, typename ReturnType, typename... ArgTypes>
82 struct has_signature<Callable,
83  ReturnType(ArgTypes...),
84  typename std::enable_if<
85  std::is_convertible<typename std::result_of<Callable(ArgTypes...)>::type, ReturnType>::value,
86  void>::type> : std::true_type
87 {
88 };
89 
90 
92 template <typename...>
93 using void_t = void;
94 } // namespace cxx
95 } // namespace iox
96 
97 #endif // IOX_UTILS_CXX_TYPE_TRAITS_HPP
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Conditionally add const to type T if C has the const qualifier.
Definition: type_traits.hpp:34
Verfies the signature ReturnType(ArgTypes...) of the provided Callable type.
Definition: type_traits.hpp:78
Verifies whether the passed Callable type is in fact invocable with the given arguments.
Definition: type_traits.hpp:53
static constexpr std::true_type test(typename std::result_of< C(As...)>::type *)
Definition: type_traits.hpp:57