iceoryx_hoofs  2.0.2
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_HOOFS_CXX_FUNCTION_REF_HPP
19 #define IOX_HOOFS_CXX_FUNCTION_REF_HPP
20 
21 #include "iceoryx_hoofs/cxx/requires.hpp"
22 #include "iceoryx_hoofs/cxx/type_traits.hpp"
23 
24 #include <cstddef>
25 #include <iostream>
26 #include <memory>
27 #include <type_traits>
28 
29 namespace iox
30 {
31 namespace cxx
32 {
33 template <typename SignatureType>
35 
36 
63 template <class ReturnType, class... ArgTypes>
64 class function_ref<ReturnType(ArgTypes...)>
65 {
66  using SignatureType = ReturnType(ArgTypes...);
67 
68  template <typename T1, typename T2>
69  using has_same_decayed_type = typename std::integral_constant<
70  bool,
71  bool(std::is_same<typename std::decay<T1>::type, typename std::decay<T2>::type>::value)>;
72 
73  public:
76  function_ref() noexcept;
77 
78  ~function_ref() noexcept = default;
79 
80  function_ref(const function_ref&) noexcept = default;
81 
82  function_ref& operator=(const function_ref&) noexcept = default;
83 
86  template <typename CallableType,
87  typename = std::enable_if_t<!is_function_pointer<CallableType>::value
88  && !has_same_decayed_type<CallableType, function_ref>::value
89  && is_invocable<CallableType, ArgTypes...>::value>>
90  function_ref(CallableType&& callable) noexcept;
91 
98  function_ref(ReturnType (*function)(ArgTypes...)) noexcept;
99 
100  function_ref(function_ref&& rhs) noexcept;
101 
102  function_ref& operator=(function_ref&& rhs) noexcept;
103 
108  ReturnType operator()(ArgTypes... args) const noexcept;
109 
112  explicit operator bool() const noexcept;
113 
116  void swap(function_ref& rhs) noexcept;
117 
118  private:
119  void* m_pointerToCallable{nullptr};
120  ReturnType (*m_functionPointer)(void*, ArgTypes...){nullptr};
121 };
122 
123 } // namespace cxx
124 } // namespace iox
125 
126 #include "iceoryx_hoofs/internal/cxx/function_ref.inl"
127 
128 #endif
function_ref() noexcept
Creates an empty function_ref in an invalid state.
Definition: function_ref.hpp:34
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
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.
Definition: type_traits.hpp:71