iceoryx_hoofs  2.0.2
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_HOOFS_CXX_METHOD_CALLBACK_HPP
18 #define IOX_HOOFS_CXX_METHOD_CALLBACK_HPP
19 
20 #include "iceoryx_hoofs/cxx/expected.hpp"
21 #include "iceoryx_hoofs/cxx/function_ref.hpp"
22 #include "iceoryx_hoofs/cxx/helplets.hpp"
23 
24 namespace iox
25 {
26 namespace cxx
27 {
28 namespace internal
29 {
30 class GenericClass
31 {
32 };
33 } // namespace internal
34 
35 enum class MethodCallbackError
36 {
37  UNINITIALIZED_CALLBACK
38 };
39 
40 template <typename ReturnValue, typename... Args>
42 {
43  public:
44  template <typename T>
45  using ConstMethodPointer = ReturnValue (T::*)(Args...) const;
46 
47  ConstMethodCallback() noexcept = default;
48  ConstMethodCallback(const ConstMethodCallback& rhs) noexcept = default;
49  ConstMethodCallback& operator=(const ConstMethodCallback& rhs) noexcept = default;
50  ~ConstMethodCallback() noexcept = default;
51 
56  template <typename ClassType>
57  ConstMethodCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> const methodPtr) noexcept;
58 
62 
67 
73  template <typename... MethodArguments>
74  expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) const noexcept;
75 
78  bool operator==(const ConstMethodCallback& rhs) const noexcept;
79 
82  bool operator!=(const ConstMethodCallback& rhs) const noexcept;
83 
86  explicit operator bool() const noexcept;
87 
90  bool isValid() const noexcept;
91 
95  template <typename ClassType>
96  void setCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> methodPtr) noexcept;
97 
99  template <typename ClassType>
100  const ClassType* getObjectPointer() const noexcept;
101 
103  template <typename ClassType>
104  auto getMethodPointer() const noexcept -> ConstMethodPointer<ClassType>;
105 
106  private:
107  const void* m_objectPtr{nullptr};
108  ConstMethodPointer<internal::GenericClass> m_methodPtr{nullptr};
109  cxx::function_ref<ReturnValue(const void*, ConstMethodPointer<internal::GenericClass>, Args...)> m_callback;
110 };
111 
112 template <typename ReturnValue, typename... Args>
114 {
115  public:
116  template <typename T>
117  using MethodPointer = ReturnValue (T::*)(Args...);
118 
119  MethodCallback() noexcept = default;
120  MethodCallback(const MethodCallback& rhs) noexcept = default;
121  MethodCallback& operator=(const MethodCallback& rhs) noexcept = default;
122  ~MethodCallback() noexcept = default;
123 
128  template <typename ClassType>
129  MethodCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;
130 
134 
139 
145  template <typename... MethodArguments>
146  expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) noexcept;
147 
150  bool operator==(const MethodCallback& rhs) const noexcept;
151 
154  bool operator!=(const MethodCallback& rhs) const noexcept;
155 
158  explicit operator bool() const noexcept;
159 
162  bool isValid() const noexcept;
163 
167  template <typename ClassType>
168  void setCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;
169 
171  template <typename ClassType>
172  ClassType* getObjectPointer() const noexcept;
173 
175  template <typename ClassType>
176  auto getMethodPointer() const noexcept -> MethodPointer<ClassType>;
177 
178  private:
179  void* m_objectPtr{nullptr};
180  MethodPointer<internal::GenericClass> m_methodPtr{nullptr};
181  cxx::function_ref<ReturnValue(void*, MethodPointer<internal::GenericClass>, Args...)> m_callback;
182 };
183 
184 } // namespace cxx
185 } // namespace iox
186 
187 #include "iceoryx_hoofs/internal/cxx/method_callback.inl"
188 
189 #endif
Definition: method_callback.hpp:42
ConstMethodCallback & operator=(ConstMethodCallback &&rhs) noexcept
Move assignment operator.
const ClassType * getObjectPointer() const noexcept
Returns object pointer.
expected< ReturnValue, MethodCallbackError > operator()(MethodArguments &&... args) const noexcept
Calls the method if the ConstMethodCallback is valid, otherwise it will return MethodCallbackError::U...
bool operator!=(const ConstMethodCallback &rhs) const noexcept
Inequality operator. Two ConstMethodCallback are not equal if they have different object or method po...
auto getMethodPointer() const noexcept -> ConstMethodPointer< ClassType >
Returns cond method pointer.
bool operator==(const ConstMethodCallback &rhs) const noexcept
Comparison operator. Two ConstMethodCallbacks are equal if they have the same object pointer and meth...
bool isValid() const noexcept
Verifies if the ConstMethodCallback is valid.
ConstMethodCallback(ConstMethodCallback &&rhs) noexcept
Move constructor.
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...
void setCallback(const ClassType &objectRef, ConstMethodPointer< ClassType > methodPtr) noexcept
Sets a new callback.
Definition: method_callback.hpp:114
expected< ReturnValue, MethodCallbackError > operator()(MethodArguments &&... args) noexcept
Calls the method if the MethodCallback is valid, otherwise it will return MethodCallbackError::UNINIT...
bool operator==(const MethodCallback &rhs) const noexcept
Comparison operator. Two MethodCallbacks are equal if they have the same object pointer and method po...
bool operator!=(const MethodCallback &rhs) const noexcept
Inequality operator. Two MethodCallbacks are not equal if they have different object or method pointe...
auto getMethodPointer() const noexcept -> MethodPointer< ClassType >
Returns cond method pointer.
ClassType * getObjectPointer() const noexcept
Returns objectRef.
void setCallback(ClassType &objectRef, MethodPointer< ClassType > methodPtr) noexcept
Sets a new callback.
MethodCallback(MethodCallback &&rhs) noexcept
Move constructor.
MethodCallback(ClassType &objectRef, MethodPointer< ClassType > methodPtr) noexcept
Constructs a MethodCallback from a pointer to a specific object and a pointer to a method of that obj...
bool isValid() const noexcept
Verifies if the MethodCallback is valid.
MethodCallback & operator=(MethodCallback &&rhs) noexcept
Move assignment operator.
Definition: function_ref.hpp:34
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29