ADTF
Loading...
Searching...
No Matches
runnable.h
Go to the documentation of this file.
1
7#pragma once
8#include "runnable_intf.h"
10
11namespace adtf
12{
13namespace base
14{
34 typename ANT_INTERFACE = ant::IRunnable,
35 typename FLASH_OR_ANT_INTERFACE = flash::IRunnable,
36 typename CALLABLE = flash::IRunnable::tRunFunction>
37class runnable : public ucom::object<ant::IRunnable, flash::IRunnable, ANT_INTERFACE, FLASH_OR_ANT_INTERFACE>
38{
39 static_assert(std::disjunction_v<
40 // MSVC hard-errors inside <utility> ("tuple index out of bounds") when a bind
41 // expression is checked against a signature taking fewer arguments than it has
42 // placeholders - which the very next alternative would do.
43 std::is_bind_expression<std::decay_t<CALLABLE>>,
44 std::is_invocable_r<tResult, std::decay_t<CALLABLE>>,
45 std::is_invocable_r<tResult, std::decay_t<CALLABLE>, tNanoSeconds>,
46 std::is_invocable_r<tResult, std::decay_t<CALLABLE>, tTimeStamp>,
47 std::is_invocable_r<tResult,
48 std::decay_t<CALLABLE>,
51 const void*,
52 size_t>>,
53 "Instantiation of runnable requires a callable with a compatible signature: "
54 "tResult(void) or tResult(tNanoSeconds) or tResult(tTimeStamp) or "
55 "tResult(tNanoSeconds, ant::IRunnable::tActivationType, const void*, size_t)");
56
57 static_assert(std::is_base_of_v<ant::IRunnable, ANT_INTERFACE>,
58 "ANT_INTERFACE template parameter at runnable<DEFAULT_ACTIVATION_TYPE, ANT_INTERFACE, "
59 "FLASH_OR_ANT_INTERFACE, CALLABLE> must be derived from ant::IRunnable");
60
61 static_assert(std::is_base_of_v<flash::IRunnable, FLASH_OR_ANT_INTERFACE> ||
62 std::is_base_of_v<ant::IRunnable, FLASH_OR_ANT_INTERFACE>,
63 "FLASH_OR_ANT_INTERFACE template parameter at runnable<DEFAULT_ACTIVATION_TYPE, ANT_INTERFACE, "
64 "FLASH_OR_ANT_INTERFACE, CALLABLE> must be derived from flash::IRunnable or ant::IRunnable");
65
66protected:
68 std::decay_t<CALLABLE> m_fcRunFunc;
69
70public:
72 runnable() = delete;
73
78 runnable(CALLABLE&& fcRunFunc) noexcept:
79 m_fcRunFunc(std::forward<CALLABLE>(fcRunFunc)), m_ui32ActivationType(DEFAULT_ACTIVATION_TYPE)
80 {
81 }
82
90 template<typename CALLABLE_WITH_TIMSTAMP,
91 typename CALLABLE_TYPE = CALLABLE,
92 std::enable_if_t<std::is_same_v<std::decay_t<CALLABLE_TYPE>, flash::IRunnable::tRunFunction> &&
93 std::is_invocable_r_v<tResult, std::decay_t<CALLABLE_WITH_TIMSTAMP>, tTimeStamp>,
94 bool> = true>
95 runnable(CALLABLE_WITH_TIMSTAMP&& fcRunFunc) noexcept:
97 [fcRunFunc = std::forward<CALLABLE_WITH_TIMSTAMP>(fcRunFunc)](tNanoSeconds tmTrigger) noexcept -> tResult
98 {
99 RETURN_IF_FAILED(fcRunFunc(duration_cast<tTimeStamp>(tmTrigger)));
101 }),
102 m_ui32ActivationType(DEFAULT_ACTIVATION_TYPE)
103 {
104 }
105
111 runnable(CALLABLE&& fcRunFunc, ant::IRunnable::tActivationType ui32ActivationType) noexcept:
112 m_fcRunFunc(std::forward<CALLABLE>(fcRunFunc)), m_ui32ActivationType(ui32ActivationType)
113 {
114 }
115
117 ~runnable() override = default;
118
132 tResult Run(tTimeStamp tmTimeofActivation,
133 ant::IRunnable::tActivationType ui32ActivationType,
134 const void* pvUserData,
135 size_t nUserDataSize) override
136 {
137 return Run(duration_cast<tNanoSeconds>(tmTimeofActivation), ui32ActivationType, pvUserData, nUserDataSize);
138 }
139
153 tResult Run(tNanoSeconds tmTimeofActivation,
154 ant::IRunnable::tActivationType ui32ActivationType,
155 const void* pvUserData,
156 size_t nUserDataSize) override
157 {
158 if (m_ui32ActivationType == ui32ActivationType || m_ui32ActivationType == ant::IRunnable::RUN_UNSPECIFIED)
159 {
160 if constexpr (std::is_invocable_r_v<tResult, CALLABLE, tNanoSeconds, ant::IRunnable::tActivationType,
161 const void*, size_t>)
162 {
163 RETURN_IF_FAILED(m_fcRunFunc(tmTimeofActivation, ui32ActivationType, pvUserData, nUserDataSize));
164 }
165 else if constexpr (std::is_invocable_r_v<tResult, CALLABLE, tTimeStamp>)
166 {
167 (void) pvUserData; // touch to avoid unused parameter warning
168 (void) nUserDataSize;
169 RETURN_IF_FAILED(m_fcRunFunc(duration_cast<tTimeStamp>(tmTimeofActivation)));
170 }
171 else if constexpr (std::is_invocable_r_v<tResult, CALLABLE, tNanoSeconds>)
172 {
173 (void) pvUserData; // touch to avoid unused parameter warning
174 (void) nUserDataSize;
175 RETURN_IF_FAILED(m_fcRunFunc(tmTimeofActivation));
176 }
177 else if constexpr (std::is_invocable_r_v<tResult, CALLABLE>)
178 {
179 (void) tmTimeofActivation; // touch to avoid unused parameter warning
180 (void) ui32ActivationType;
181 (void) pvUserData;
182 (void) nUserDataSize;
183 RETURN_IF_FAILED(m_fcRunFunc());
184 }
185 else
186 {
187 // Unreachable while the static_assert above holds.
188 static_assert(base::penguin::detail::always_false<CALLABLE>, "Unexpected CALLABLE type.");
189 }
191 }
192 RETURN_ERROR(ERR_INVALID_ARG);
193 }
194
200 {
201 return m_ui32ActivationType;
202 }
203
204private:
206};
207
215template<typename CALLABLE, typename... ARGS>
216runnable(CALLABLE&&, ARGS&&...)
218
219namespace ant
220{
233 typename ANT_INTERFACE = ant::IRunnable,
234 typename CALLABLE = ant::IRunnable::tRunFunction>
236} // namespace ant
237
243#define ADTF_RUN_FUNCTION(_fcName_) \
244 [this](tTimeStamp tmTime) -> tResult \
245 { \
246 return _fcName_(tmTime); \
247 }
248
254#define ADTF_RUN_FUNCTION_NS(_fcName_) \
255 [this](adtf::base::flash::tNanoSeconds tmTime) -> tResult \
256 { \
257 return _fcName_(tmTime); \
258 }
259
260} // namespace base
261} // namespace adtf
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:775
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
Definition runnable_intf.h:27
std::function< tResult(tTimeStamp)> tRunFunction
Type definition of the function used to implement Run.
Definition runnable_intf.h:38
tActivationType
Definition runnable_intf.h:45
@ RUN_UNSPECIFIED
unspecified Run activation type.
Definition runnable_intf.h:47
Definition runnable_intf.h:112
std::function< tResult(tNanoSeconds)> tRunFunction
Type definition of the function used to implement Run.
Definition runnable_intf.h:123
Definition runnable.h:38
ant::IRunnable::tActivationType GetActivationType() const override
Definition runnable.h:199
runnable(CALLABLE &&fcRunFunc, ant::IRunnable::tActivationType ui32ActivationType) noexcept
Definition runnable.h:111
tResult Run(tTimeStamp tmTimeofActivation, ant::IRunnable::tActivationType ui32ActivationType, const void *pvUserData, size_t nUserDataSize) override
Definition runnable.h:132
tResult Run(tNanoSeconds tmTimeofActivation, ant::IRunnable::tActivationType ui32ActivationType, const void *pvUserData, size_t nUserDataSize) override
Definition runnable.h:153
runnable()=delete
CTOR.
runnable(CALLABLE_WITH_TIMSTAMP &&fcRunFunc) noexcept
Definition runnable.h:95
Definition object.h:397
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
Definition adtf_class_version.h:16
adtf::base::runnable< DEFAULT_ACTIVATION_TYPE, ANT_INTERFACE, flash::IRunnable, CALLABLE > runnable
Definition runnable.h:235
constexpr bool always_false
helper template to get a always false expression.
Definition adtf_base_type_traits.h:23
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
runnable(CALLABLE &&, ARGS &&...) -> runnable< ant::IRunnable::RUN_UNSPECIFIED, ant::IRunnable, flash::IRunnable, CALLABLE && >
DestinationTimeStamp duration_cast(const SourceTimeStamp &) noexcept
Duration cast base template to converted between different time resolution.
Definition chrono.h:155
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
A timestamp with nanosecond precision.
Definition chrono.h:23