iceoryx_doc  1.0.1
method_callback.hpp
1 // Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 
17 #ifndef IOX_UTILS_CXX_METHOD_CALLBACK_HPP
18 #define IOX_UTILS_CXX_METHOD_CALLBACK_HPP
19 
20 #include "iceoryx_utils/cxx/expected.hpp"
21 #include "iceoryx_utils/cxx/function_ref.hpp"
22 #include "iceoryx_utils/cxx/helplets.hpp"
23 
24 namespace iox
25 {
26 namespace cxx
27 {
28 namespace internal
29 {
31 {
32 };
33 } // namespace internal
34 
35 enum class MethodCallbackError
36 {
37  INVALID_STATE,
38  UNINITIALIZED_CALLBACK
39 };
40 
41 template <typename ReturnValue, typename... Args>
43 {
44  public:
45  template <typename T>
46  using ConstMethodPointer = ReturnValue (T::*)(Args...) const;
47 
48  ConstMethodCallback() = default;
49  ConstMethodCallback(const ConstMethodCallback& rhs) = default;
50  ConstMethodCallback& operator=(const ConstMethodCallback& rhs) = default;
51  ~ConstMethodCallback() = default;
52 
57  template <typename ClassType>
58  ConstMethodCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> const methodPtr) noexcept;
59 
63 
67  ConstMethodCallback& operator=(ConstMethodCallback&& rhs) noexcept;
68 
74  template <typename... MethodArguments>
75  expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) const noexcept;
76 
79  bool operator==(const ConstMethodCallback& rhs) const noexcept;
80 
83  bool operator!=(const ConstMethodCallback& rhs) const noexcept;
84 
87  explicit operator bool() const noexcept;
88 
91  bool isValid() const noexcept;
92 
96  template <typename ClassType>
97  void setCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> methodPtr) noexcept;
98 
100  template <typename ClassType>
101  const ClassType* getObjectPointer() const noexcept;
102 
104  template <typename ClassType>
105  auto getMethodPointer() const noexcept -> ConstMethodPointer<ClassType>;
106 
107  private:
108  const void* m_objectPtr{nullptr};
109  ConstMethodPointer<internal::GenericClass> m_methodPtr{nullptr};
110  cxx::function_ref<ReturnValue(const void*, ConstMethodPointer<internal::GenericClass>, Args...)> m_callback;
111 };
112 
113 template <typename ReturnValue, typename... Args>
115 {
116  public:
117  template <typename T>
118  using MethodPointer = ReturnValue (T::*)(Args...);
119 
120  MethodCallback() = default;
121  MethodCallback(const MethodCallback& rhs) = default;
122  MethodCallback& operator=(const MethodCallback& rhs) = default;
123  ~MethodCallback() = default;
124 
129  template <typename ClassType>
130  MethodCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;
131 
134  MethodCallback(MethodCallback&& rhs) noexcept;
135 
139  MethodCallback& operator=(MethodCallback&& rhs) noexcept;
140 
146  template <typename... MethodArguments>
147  expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) noexcept;
148 
151  bool operator==(const MethodCallback& rhs) const noexcept;
152 
155  bool operator!=(const MethodCallback& rhs) const noexcept;
156 
159  explicit operator bool() const noexcept;
160 
163  bool isValid() const noexcept;
164 
168  template <typename ClassType>
169  void setCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;
170 
172  template <typename ClassType>
173  ClassType* getObjectPointer() const noexcept;
174 
176  template <typename ClassType>
177  auto getMethodPointer() const noexcept -> MethodPointer<ClassType>;
178 
179  private:
180  void* m_objectPtr{nullptr};
181  MethodPointer<internal::GenericClass> m_methodPtr{nullptr};
182  cxx::function_ref<ReturnValue(void*, MethodPointer<internal::GenericClass>, Args...)> m_callback;
183 };
184 
185 } // namespace cxx
186 } // namespace iox
187 
188 #include "iceoryx_utils/internal/cxx/method_callback.inl"
189 
190 #endif
Definition: method_callback.hpp:43
const ClassType * getObjectPointer() const noexcept
Returns object pointer.
Definition: method_callback.inl:151
bool operator!=(const ConstMethodCallback &rhs) const noexcept
Inequality operator. Two ConstMethodCallback are not equal if they have different object or method po...
Definition: method_callback.inl:121
auto getMethodPointer() const noexcept -> ConstMethodPointer< ClassType >
Returns cond method pointer.
Definition: method_callback.inl:158
bool operator==(const ConstMethodCallback &rhs) const noexcept
Comparison operator. Two ConstMethodCallbacks are equal if they have the same object pointer and meth...
Definition: method_callback.inl:115
bool isValid() const noexcept
Verifies if the ConstMethodCallback is valid.
Definition: method_callback.inl:127
ConstMethodCallback(const ClassType &objectRef, ConstMethodPointer< ClassType > const methodPtr) noexcept
Constructs a ConstMethodCallback from a pointer to a specific object and a pointer to a method of tha...
expected< ReturnValue, MethodCallbackError > operator()(MethodArguments &&... args) const noexcept
Calls the method if the ConstMethodCallback is valid, otherwise it will return MethodCallbackError::U...
Definition: method_callback.inl:103
void setCallback(const ClassType &objectRef, ConstMethodPointer< ClassType > methodPtr) noexcept
Sets a new callback.
Definition: method_callback.inl:140
Definition: method_callback.hpp:115
expected< ReturnValue, MethodCallbackError > operator()(MethodArguments &&... args) noexcept
Calls the method if the MethodCallback is valid, otherwise it will return MethodCallbackError::UNINIT...
Definition: method_callback.inl:200
bool operator==(const MethodCallback &rhs) const noexcept
Comparison operator. Two MethodCallbacks are equal if they have the same object pointer and method po...
Definition: method_callback.inl:212
bool operator!=(const MethodCallback &rhs) const noexcept
Inequality operator. Two MethodCallbacks are not equal if they have different object or method pointe...
Definition: method_callback.inl:218
auto getMethodPointer() const noexcept -> MethodPointer< ClassType >
Returns cond method pointer.
Definition: method_callback.inl:255
void setCallback(ClassType &objectRef, MethodPointer< ClassType > methodPtr) noexcept
Sets a new callback.
Definition: method_callback.inl:237
ClassType * getObjectPointer() const noexcept
Returns objectRef.
Definition: method_callback.inl:248
bool isValid() const noexcept
Verifies if the MethodCallback is valid.
Definition: method_callback.inl:224
Definition: function_ref.hpp:32
Definition: method_callback.hpp:31
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28