Sen API
Sen Libraries
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1// === helpers.h =======================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_LIBS_KERNEL_TEST_INTEGRATION_TRANSPORT_HELPERS_H
9#define SEN_LIBS_KERNEL_TEST_INTEGRATION_TRANSPORT_HELPERS_H
10
11// sen
14
15// generated code
16#include "test_helpers/test_helpers.stl.h"
17
18// sen
20#include "sen/core/meta/var.h"
25
26// spdlog
27#include <spdlog/logger.h>
28
29// std
30#include <algorithm>
31#include <array>
32#include <cmath>
33#include <future>
34#include <list>
35#include <memory>
36#include <string>
37#include <type_traits>
38#include <vector>
39
40namespace sen::test
41{
42
43//--------------------------------------------------------------------------------------------------------------
44// Helpers
45//--------------------------------------------------------------------------------------------------------------
46
47template <typename T>
48[[nodiscard]] inline bool eq(T a, T b)
49{
50 // handle possible quantities of floating point types
51 if constexpr (!std::is_same_v<T, sen::TimeStamp> && sen::HasValueType<T>::value)
52 {
53 return eq(a.get(), b.get());
54 }
55
56 if constexpr (std::is_floating_point_v<T>)
57 {
58 constexpr T eps = 1e-3;
59 return std::fabs(a - b) < eps;
60 }
61
62 return a == b;
63}
64
65template <typename PropType>
66[[nodiscard]] inline bool checkProp(std::promise<TestResult>& promise,
67 const std::string& propName,
69 sen::MaybeRef<PropType> expectation)
70{
71 if (!eq(value, expectation))
72 {
73 std::string err;
74 err.append("Incorrect value of ");
75 err.append(propName);
76 promise.set_value(TestResult(err));
77 return false;
78 }
79
80 return true;
81}
82
83[[nodiscard]] inline bool checkMember(std::promise<TestResult>& promise, const std::string& memberName, bool isOk)
84{
85 if (isOk)
86 {
87 return true;
88 }
89
90 std::string err = "Member ";
91 err.append(memberName);
92 err.append(" adaptation failed.");
93 promise.set_value(TestResult(err));
94 return false;
95}
96
97template <typename T>
98[[nodiscard]] inline bool allObjectsWithState(const std::list<T*>& objects, const ConnectionState state)
99{
100 return std::all_of(objects.begin(), objects.end(), [state](const T* obj) { return obj->getState() == state; });
101}
102
105class SEN_EXPORT ProcessTerminatorImpl final: public ProcessTerminatorBase
106{
107public:
108 ProcessTerminatorImpl(std::string name, const sen::VarMap& args);
109
110public:
112
113private:
114 std::shared_ptr<sen::Subscription<StatefulObjectInterface>> objectsSub_;
115 std::vector<sen::ConnectionGuard> guards_;
116 std::shared_ptr<spdlog::logger> logger_;
117};
118
120class SEN_EXPORT StateFulObjectImpl: public StatefulObjectBase
121{
122public:
123 StateFulObjectImpl(std::string name, const sen::VarMap& args);
124
125public:
127
128protected:
129 [[nodiscard]] sen::kernel::KernelApi* getApi() const;
130 [[nodiscard]] spdlog::logger* getLogger() const;
131
132private:
133 sen::kernel::KernelApi* api_ = nullptr;
134 std::shared_ptr<spdlog::logger> logger_;
135};
136
138class SEN_EXPORT PublisherImpl: public PublisherBase<StateFulObjectImpl>
139{
140public:
141 PublisherImpl(std::string name, const sen::VarMap& args);
142
143public:
145 void update(sen::kernel::RunApi& runApi) override;
146
147protected:
148 virtual void action1();
149 virtual void action2();
150 virtual void action3();
151 virtual void action4();
152
153private:
154 std::shared_ptr<sen::ObjectSource> bus_;
155 std::shared_ptr<sen::Subscription<ListenerInterface>> listenersSub_;
156 std::vector<sen::ConnectionGuard> cbGuards_;
157 std::array<bool, 5U> actionFlags_ = {false, false, false, false, false};
158};
159
162class SEN_EXPORT ListenerImpl: public ListenerBase<StateFulObjectImpl>
163{
164public:
165 ListenerImpl(std::string name, const sen::VarMap& args);
166
167public:
169 void update(sen::kernel::RunApi& runApi) override;
170
171protected:
172 virtual void check1();
173 virtual void check2();
174 virtual void check3();
175 virtual void check4();
176
177private:
178 std::shared_ptr<sen::ObjectSource> bus_;
179 std::shared_ptr<sen::Subscription<PublisherInterface>> publisherSub_;
180 std::vector<sen::ConnectionGuard> cbGuards_;
181 std::array<bool, 5U> checkFlags_ = {false, false, false, false, false};
182};
183
184} // namespace sen::test
185
186#endif // SEN_LIBS_KERNEL_TEST_INTEGRATION_TRANSPORT_HELPERS_H
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
User-facing kernel functions.
Definition component_api.h:113
API for objects when registered.
Definition component_api.h:206
What can be done while a component is running.
Definition component_api.h:260
virtual void check1()
virtual void check2()
void registered(sen::kernel::RegistrationApi &api) override
virtual void check3()
ListenerImpl(std::string name, const sen::VarMap &args)
void update(sen::kernel::RunApi &runApi) override
virtual void check4()
void registered(sen::kernel::RegistrationApi &api) override
ProcessTerminatorImpl(std::string name, const sen::VarMap &args)
virtual void action2()
virtual void action3()
void update(sen::kernel::RunApi &runApi) override
void registered(sen::kernel::RegistrationApi &api) override
virtual void action1()
virtual void action4()
PublisherImpl(std::string name, const sen::VarMap &args)
StateFulObjectImpl(std::string name, const sen::VarMap &args)
void registered(sen::kernel::RegistrationApi &api) override
spdlog::logger * getLogger() const
sen::kernel::KernelApi * getApi() const
std::conditional_t< std::is_arithmetic_v< T >||shouldBePassedByValueV< T >, T, AddConstRef< T > > MaybeRef
returns 'const T&' or 'T' depending on the type
Definition class_helpers.h:46
std::map< std::string, Var, std::less<> > VarMap
A map of vars to represent structures.
Definition var.h:107
Definition reader_writer.h:19
bool eq(T a, T b)
Definition helpers.h:48
bool checkProp(std::promise< TestResult > &promise, const std::string &propName, sen::MaybeRef< PropType > value, sen::MaybeRef< PropType > expectation)
Definition helpers.h:66
bool allObjectsWithState(const std::list< T * > &objects, const ConnectionState state)
Definition helpers.h:98
bool checkMember(std::promise< TestResult > &promise, const std::string &memberName, bool isOk)
Definition helpers.h:83
return false if T has not an 'using ValueType' or a 'typedef ValueType'
Definition class_helpers.h:51