ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
16namespace adtf
17{
18namespace ucom
19{
20namespace nitro
21{
22
27template <typename Signature>
29
33template <typename ResultType, typename ...Arguments>
34class ifunction<ResultType(Arguments...)>: public ant::IObject
35{
36 public:
40 virtual ResultType operator()(Arguments... args) const = 0;
41};
42
46template <typename Signature>
48
52template <typename Signature>
54
58template <typename ResultType, typename ...Arguments>
59class 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
89namespace detail
90{
91
92template <typename Function>
93struct function_traits : public function_traits<decltype(&Function::operator())>
94{
95};
96
97template <typename ClassType, typename ReturnType, typename... Arguments>
98struct 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
108template <typename ClassType, typename ReturnType, typename... Arguments>
109struct 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
119template<typename FunctionTraits, typename Unused>
120struct signature;
121
122template<typename FunctionTraits, std::size_t... Indices>
123struct signature<FunctionTraits, std::index_sequence<Indices...>>
124{
125 using type = typename FunctionTraits::return_type(typename FunctionTraits::template argument<Indices>...);
126};
127
128template<typename Callable, typename FunctionTraits = function_traits<std::remove_reference_t<Callable>>>
129using signature_t = typename signature<FunctionTraits, std::make_index_sequence<FunctionTraits::argument_count>>::type;
130
131template <typename Signature>
132class function_ptr;
133
134template <typename ResultType, typename ...Arguments>
135class function_ptr<ResultType(Arguments...)>: public object_ptr<ifunction<ResultType(Arguments...)>>
136{
137public:
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
158template <typename Callable, typename Signature = detail::signature_t<Callable>>
159inline function_wrapper<Signature> make_function(Callable&& fnCallback)
160{
161 return function_wrapper<Signature>(std::forward<Callable>(fnCallback));
162}
163
170template <typename Callable, typename Signature = detail::signature_t<Callable>>
172{
173 return ant::make_object_ptr<function_wrapper<Signature>>(std::forward<Callable>(fnCallback));
174}
175
181template <typename Signature>
182inline std::function<Signature> to_std_function(const ifunction<Signature>& fnFunction)
183{
184 return std::function<Signature>(std::ref(fnFunction));
185}
186
192template <typename Signature>
193inline 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
200using nitro::ifunction;
202using nitro::function_wrapper;
206
207}
208}
Copyright © Audi Electronics Venture GmbH.
Base class for every interface type within the uCOM.
Definition object_intf.h:33
Base object pointer to realize binary compatible reference counting in interface methods.
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition object.h:397
function_wrapper(std::function< ResultType(Arguments...)> fnFunction)
Constructor that initializes the object with the given function reference.
Implements iucom_function for the given signature.
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.
object_ptr< Implementation > make_object_ptr(Args &&... args)
Create an instance of type object_ptr with Implementation as the shared resource.
Namespace for all functionality provided since v3.13.
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.
ant::iobject_ptr< ifunction< Signature > > ifunction_ptr
Interface for shared pointers to iucom_function.
function_wrapper< Signature > make_function(Callable &&fnCallback)
creates a ucom_function object for the given callable.
std::function< Signature > to_std_function(const ifunction< Signature > &fnFunction)
Helper conversion to a std::function object.
Namespace for the ADTF uCOM3 SDK.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.