ADTF
function_ptr.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <a_utils.h>
11 #include "object.h"
12 #include <functional>
13 
14 #define ADTF_UCOM_PROVIDES_IFUNCTION 1
15 
16 namespace adtf
17 {
18 namespace ucom
19 {
20 namespace nitro
21 {
22 
27 template <typename Signature>
28 class ifunction;
29 
33 template <typename ResultType, typename ...Arguments>
34 class ifunction<ResultType(Arguments...)>: public ant::IObject
35 {
36  public:
40  virtual ResultType operator()(Arguments... args) const = 0;
41 };
42 
46 template <typename Signature>
48 
52 template <typename Signature>
54 
58 template <typename ResultType, typename ...Arguments>
59 class function_wrapper<ResultType(Arguments...)>: public catwo::object<ifunction<ResultType(Arguments...)>>
60 {
61  public:
66  function_wrapper(std::function<ResultType(Arguments...)> fnFunction):
67  m_fnFunction(fnFunction)
68  {
69  }
70 
71  function_wrapper() = default;
72  function_wrapper(const function_wrapper&) = default;
74  function_wrapper& operator=(const function_wrapper&) = default;
75  function_wrapper& operator=(function_wrapper&&) = default;
76  ~function_wrapper() = default;
77 
78  ResultType operator()(Arguments... args) const override
79  {
80  return m_fnFunction(std::forward<Arguments>(args)...);
81  }
82 
83  private:
84  std::function<ResultType(Arguments...)> m_fnFunction;
85 };
86 
88 
89 namespace detail
90 {
91 
92 template <typename Function>
93 struct function_traits : public function_traits<decltype(&Function::operator())>
94 {
95 };
96 
97 template <typename ClassType, typename ReturnType, typename... Arguments>
98 struct function_traits<ReturnType(ClassType::*)(Arguments...) const>
99 {
100  using return_type = ReturnType;
101 
102  template <std::size_t Index>
103  using argument = typename std::tuple_element<Index, std::tuple<Arguments...>>::type;
104 
105  static constexpr std::size_t argument_count = sizeof...(Arguments);
106 };
107 
108 template <typename ClassType, typename ReturnType, typename... Arguments>
109 struct function_traits<ReturnType(ClassType::*)(Arguments...)>
110 {
111  using return_type = ReturnType;
112 
113  template <std::size_t Index>
114  using argument = typename std::tuple_element<Index, std::tuple<Arguments...>>::type;
115 
116  static constexpr std::size_t argument_count = sizeof...(Arguments);
117 };
118 
119 template<typename FunctionTraits, typename Unused>
120 struct signature;
121 
122 template<typename FunctionTraits, std::size_t... Indices>
123 struct signature<FunctionTraits, std::index_sequence<Indices...>>
124 {
125  using type = typename FunctionTraits::return_type(typename FunctionTraits::template argument<Indices>...);
126 };
127 
128 template<typename Callable, typename FunctionTraits = function_traits<std::remove_reference_t<Callable>>>
129 using signature_t = typename signature<FunctionTraits, std::make_index_sequence<FunctionTraits::argument_count>>::type;
130 
131 template <typename Signature>
132 class function_ptr;
133 
134 template <typename ResultType, typename ...Arguments>
135 class function_ptr<ResultType(Arguments...)>: public object_ptr<ifunction<ResultType(Arguments...)>>
136 {
137 public:
138  using base_type = object_ptr<ifunction<ResultType(Arguments...)>>;
139 
140  using base_type::base_type;
141 
142  ResultType operator()(Arguments... args) const
143  {
144  return (*this->Get())(std::forward<Arguments>(args)...);
145  }
146 };
147 
148 }
149 
151 
158 template <typename Callable, typename Signature = detail::signature_t<Callable>>
159 inline function_wrapper<Signature> make_function(Callable&& fnCallback)
160 {
161  return function_wrapper<Signature>(std::forward<Callable>(fnCallback));
162 }
163 
170 template <typename Callable, typename Signature = detail::signature_t<Callable>>
172 {
173  return ant::make_object_ptr<function_wrapper<Signature>>(std::forward<Callable>(fnCallback));
174 }
175 
181 template <typename Signature>
182 inline std::function<Signature> to_std_function(const ifunction<Signature>& fnFunction)
183 {
184  return std::function<Signature>(std::ref(fnFunction));
185 }
186 
192 template <typename Signature>
193 inline std::function<Signature> to_std_function(const ifunction_ptr<Signature>& pFunction)
194 {
195  return std::function<Signature>(detail::function_ptr<Signature>(pFunction));
196 }
197 
198 }
199 
200 using nitro::ifunction;
202 using nitro::function_wrapper;
206 
207 }
208 }
Copyright © Audi Electronics Venture GmbH.
Base class for every interface type within the uCOM.
Definition: object_intf.h:31
Base object pointer to realize binary compatible reference counting in interface methods.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:379
function_wrapper(std::function< ResultType(Arguments...)> fnFunction)
Constructor that initializes the object with the given function reference.
Definition: function_ptr.h:66
Implements iucom_function for the given signature.
Definition: function_ptr.h:53
virtual ResultType operator()(Arguments... args) const =0
The call operator which calls the referenced function.
Interface for functions that can be passed via IObject interfaces.
Definition: function_ptr.h:28
ant::object_ptr< ifunction< Signature > > make_function_ptr(Callable &&fnCallback)
creates an object_ptr shared pointer to an ucom_function pointing to the given callable.
Definition: function_ptr.h:171
function_wrapper< Signature > make_function(Callable &&fnCallback)
creates a ucom_function object for the given callable.
Definition: function_ptr.h:159
std::function< Signature > to_std_function(const ifunction< Signature > &fnFunction)
Helper conversion to a std::function object.
Definition: function_ptr.h:182
ant::iobject_ptr< ifunction< Signature > > ifunction_ptr
Interface for shared pointers to iucom_function.
Definition: function_ptr.h:47
ant::object_ptr< T > object_ptr
Alias always bringing the latest version of ant::object_ptr into scope.
Definition: object_ptr.h:413
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.