iceoryx_doc  1.0.1
function_ref.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_FUNCTION_REF_HPP
19 #define IOX_UTILS_CXX_FUNCTION_REF_HPP
20 
21 #include "iceoryx_utils/cxx/type_traits.hpp"
22 
23 #include <cstddef>
24 #include <iostream>
25 #include <memory>
26 
27 namespace iox
28 {
29 namespace cxx
30 {
31 template <typename SignatureType>
33 
34 template <typename...>
35 struct is_function_ref : std::false_type
36 {
37 };
38 
39 template <typename... Targs>
40 struct is_function_ref<function_ref<Targs...>> : std::true_type
41 {
42 };
43 
67 template <class ReturnType, class... ArgTypes>
68 class function_ref<ReturnType(ArgTypes...)>
69 {
70  using SignatureType = ReturnType(ArgTypes...);
71 
72  public:
75  function_ref() noexcept;
76 
77  ~function_ref() noexcept = default;
78 
79  function_ref(const function_ref&) noexcept = default;
80 
81  function_ref& operator=(const function_ref&) noexcept = default;
82 
85  template <typename CallableType,
86  typename = std::enable_if_t<!is_function_ref<std::remove_reference_t<CallableType>>::value>,
87  typename = std::enable_if_t<is_invocable<CallableType, ArgTypes...>::value>>
88  function_ref(CallableType&& callable) noexcept;
89 
90  function_ref(function_ref&& rhs) noexcept;
91 
92  function_ref& operator=(function_ref&& rhs) noexcept;
93 
97  ReturnType operator()(ArgTypes... args) const noexcept;
98 
101  explicit operator bool() const noexcept;
102 
105  void swap(function_ref& rhs) noexcept;
106 
107  private:
108  void* m_pointerToCallable{nullptr};
109  ReturnType (*m_functionPointer)(void*, ArgTypes...){nullptr};
110 };
111 
112 } // namespace cxx
113 } // namespace iox
114 
115 #include "iceoryx_utils/internal/cxx/function_ref.inl"
116 
117 #endif
Definition: function_ref.hpp:32
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
Definition: function_ref.hpp:36
Verifies whether the passed Callable type is in fact invocable with the given arguments.
Definition: type_traits.hpp:53