Sen API
Sen Libraries
Loading...
Searching...
No Matches
component.h
Go to the documentation of this file.
1// === component.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_KERNEL_COMPONENT_H
9#define SEN_KERNEL_COMPONENT_H
10
11// sen
16
17// kernel
19#include "sen/kernel/kernel.h"
20
21namespace sen::kernel
22{
23
25[[nodiscard]] inline PassResult done() { return Ok(OpState {OpFinished {}}); }
26
34{
35 SEN_NOCOPY_NOMOVE(Component)
36
37public: // special members
38 Component() noexcept = default;
39 virtual ~Component() = default;
40
41public:
44 virtual FuncResult preload(PreloadApi&& /* api */) { return Ok(); }
45
48 virtual FuncResult load(LoadApi&& /* api */) { return Ok(); }
49
53 virtual PassResult init(InitApi&& /* api */) { return done(); }
54
60 virtual FuncResult run(RunApi& /* api */) { return Ok(); }
61
64 virtual FuncResult unload(UnloadApi&& /* api */) { return Ok(); }
65
68 virtual void postUnloadCleanup() {}
69
71 [[nodiscard]] virtual bool isRealTimeOnly() const noexcept { return false; }
72
73protected: // helpers
75 [[nodiscard]] static PassResult delay(Duration time) noexcept { return Ok(OpState {OpNotFinished {time}}); }
76
78 [[nodiscard]] static PassResult done() noexcept { return kernel::done(); }
79};
80
81} // namespace sen::kernel
82
83//----------------------------------------------------------------------------------------------------------------------
84// Inline implementation
85//----------------------------------------------------------------------------------------------------------------------
86
87#define SEN_COMPONENT_MAKER senMakeKernelComponent // NOLINT(cppcoreguidelines-macro-usage)
88#define SEN_COMPONENT_INFO_MAKER senMakeKernelComponentInfo // NOLINT(cppcoreguidelines-macro-usage)
89
90namespace sen::kernel
91{
92
93constexpr auto* componentMakerFuncName = SEN_STRINGIFY(SEN_COMPONENT_MAKER);
95
96//--------------------------------------------------------------------------------------------------------------
97// Component
98//--------------------------------------------------------------------------------------------------------------
99
100[[nodiscard]] constexpr bool getDebugEnabled() noexcept
101{
102#if defined(DEBUG) || defined(_DEBUG)
103 return true;
104#else
105 return false;
106#endif
107}
108
109[[nodiscard]] constexpr WordSize getWordSize() noexcept
110{
111 if constexpr (sizeof(size_t) == 4U)
112 {
113 return WordSize::bits32;
114 }
115 else
116 {
117 return WordSize::bits64;
118 }
119}
120
121[[nodiscard]] constexpr const char* getGitRef() noexcept
122{
123#ifdef GIT_REF_SPEC
124 return GIT_REF_SPEC;
125#else
126 return "";
127#endif
128}
129
130[[nodiscard]] constexpr const char* getGitHash() noexcept
131{
132#ifdef GIT_HASH
133 return GIT_HASH;
134#else
135 return "";
136#endif
137}
138
139[[nodiscard]] inline GitStatus getGitStatus() noexcept
140{
141#ifdef GIT_STATUS
142 const auto* statusStr = GIT_STATUS;
143#else
144 const auto* statusStr = "";
145#endif
146
147 if (std::string(statusStr) == "clean")
148 {
149 return GitStatus::clean;
150 }
151
152 if (std::string(statusStr) == "dirty")
153 {
154 return GitStatus::modified;
155 }
156
157 return GitStatus::unknown;
158}
159
160} // namespace sen::kernel
161
162// declare the functions that will be defined in the shared library
163extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER();
164extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER();
165
168#define SEN_COMPONENT(component_name) \
169 \
170 extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER() \
171 { \
172 return new component_name(); /* NOLINT(cppcoreguidelines-owning-memory) */ \
173 } \
174 \
175 extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER() \
176 { \
177 static sen::kernel::ComponentInfo info {}; \
178 info.name = SEN_TARGET_NAME; \
179 info.description = SEN_TARGET_DESCRIPTION; \
180 info.buildInfo.maintainer = SEN_TARGET_MAINTAINER; \
181 info.buildInfo.version = SEN_TARGET_VERSION; \
182 info.buildInfo.compiler = SEN_COMPILER_STRING; \
183 info.buildInfo.debugMode = sen::kernel::getDebugEnabled(); \
184 info.buildInfo.wordSize = sen::kernel::getWordSize(); \
185 info.buildInfo.buildTime = std::string(__DATE__) + " " + __TIME__; \
186 info.buildInfo.gitRef = sen::kernel::getGitRef(); \
187 info.buildInfo.gitHash = sen::kernel::getGitHash(); \
188 info.buildInfo.gitStatus = sen::kernel::getGitStatus(); \
189 return &info; \
190 }
191
192#endif // SEN_KERNEL_COMPONENT_H
A time duration.
Definition duration.h:25
Base class for implementing sen kernel components.
Definition component.h:34
virtual void postUnloadCleanup()
Do an additional (and final) clean up step after unloading the component. This function is just calle...
Definition component.h:68
virtual FuncResult run(RunApi &)
Runs the component in a dedicated thread. This function is called once and only when the kernel has r...
Definition component.h:60
virtual FuncResult preload(PreloadApi &&)
Preload the component and initialize all self-contained resources. This function is just called once.
Definition component.h:44
virtual FuncResult load(LoadApi &&)
Load the component and initialize all self-contained resources. This function is just called once.
Definition component.h:48
virtual bool isRealTimeOnly() const noexcept
Return true here if your component is not designed to work with virtualized time.
Definition component.h:71
virtual PassResult init(InitApi &&)
Initialize the component and perform kernel-related operations. This may include dependency resolutio...
Definition component.h:53
static PassResult delay(Duration time) noexcept
Convenience function to return an operation delay request.
Definition component.h:75
static PassResult done() noexcept
Convenience function to return a finished operation result.
Definition component.h:78
Component() noexcept=default
virtual FuncResult unload(UnloadApi &&)
Unload any self-contained resources. This function is just called once.
Definition component.h:64
What can be done when initializing a component.
Definition component_api.h:213
What can be done when loading a component.
Definition component_api.h:202
What can be done when preloading a component.
Definition component_api.h:181
What can be done while a component is running.
Definition component_api.h:224
What can be done when unloading a component.
Definition component_api.h:291
#define SEN_COMPONENT_INFO_MAKER
Definition component.h:88
#define SEN_COMPONENT_MAKER
Definition component.h:87
impl::Ok< void > Ok() noexcept
If T is void, use the void specialization of Ok.
Definition result.h:434
@ time
Definition unit.h:34
Strong type for the unique identifier for a transport timer.
Definition native_object_impl.h:39
PassResult done()
Convenience helper for doing sen::Ok(sen::kernel::OpState {sen::kernel::OpFinished {}...
Definition component.h:25
constexpr const char * getGitRef() noexcept
Definition component.h:121
constexpr auto * componentMakerFuncName
Definition component.h:93
Result< OpState, ExecError > PassResult
The result of operations that may be called multiple times.
Definition component_api.h:53
Result< void, ExecError > FuncResult
The result of operations that are called once.
Definition component_api.h:50
constexpr auto * componentInfoMakerFuncName
Definition component.h:94
constexpr bool getDebugEnabled() noexcept
Definition component.h:100
constexpr const char * getGitHash() noexcept
Definition component.h:130
constexpr WordSize getWordSize() noexcept
Definition component.h:109
GitStatus getGitStatus() noexcept
Definition component.h:139