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:
42 // --8<-- [start:hooks]
45 virtual FuncResult preload(PreloadApi&& /* api */) { return Ok(); }
46
49 virtual FuncResult load(LoadApi&& /* api */) { return Ok(); }
50
54 virtual PassResult init(InitApi&& /* api */) { return done(); }
55
61 virtual FuncResult run(RunApi& /* api */) { return Ok(); }
62
65 virtual FuncResult unload(UnloadApi&& /* api */) { return Ok(); }
66
69 virtual void postUnloadCleanup() {}
70
72 [[nodiscard]] virtual bool isRealTimeOnly() const noexcept { return false; }
73 // --8<-- [end:hooks]
74
75protected: // helpers
77 [[nodiscard]] static PassResult delay(Duration time) noexcept { return Ok(OpState {OpNotFinished {time}}); }
78
80 [[nodiscard]] static PassResult done() noexcept { return kernel::done(); }
81};
82
83} // namespace sen::kernel
84
85//----------------------------------------------------------------------------------------------------------------------
86// Inline implementation
87//----------------------------------------------------------------------------------------------------------------------
88
89#define SEN_COMPONENT_MAKER senMakeKernelComponent // NOLINT(cppcoreguidelines-macro-usage)
90#define SEN_COMPONENT_INFO_MAKER senMakeKernelComponentInfo // NOLINT(cppcoreguidelines-macro-usage)
91
92namespace sen::kernel
93{
94
95constexpr auto* componentMakerFuncName = SEN_STRINGIFY(SEN_COMPONENT_MAKER);
97
98//--------------------------------------------------------------------------------------------------------------
99// Component
100//--------------------------------------------------------------------------------------------------------------
101
102[[nodiscard]] constexpr bool getDebugEnabled() noexcept
103{
104#if defined(DEBUG) || defined(_DEBUG)
105 return true;
106#else
107 return false;
108#endif
109}
110
111[[nodiscard]] constexpr WordSize getWordSize() noexcept
112{
113 if constexpr (sizeof(size_t) == 4U)
114 {
115 return WordSize::bits32;
116 }
117 else
118 {
119 return WordSize::bits64;
120 }
121}
122
123[[nodiscard]] constexpr const char* getGitRef() noexcept
124{
125#ifdef GIT_REF_SPEC
126 return GIT_REF_SPEC;
127#else
128 return "";
129#endif
130}
131
132[[nodiscard]] constexpr const char* getGitHash() noexcept
133{
134#ifdef GIT_HASH
135 return GIT_HASH;
136#else
137 return "";
138#endif
139}
140
141[[nodiscard]] inline GitStatus getGitStatus() noexcept
142{
143#ifdef GIT_STATUS
144 const auto* statusStr = GIT_STATUS;
145#else
146 const auto* statusStr = "";
147#endif
148
149 if (std::string(statusStr) == "clean")
150 {
151 return GitStatus::clean;
152 }
153
154 if (std::string(statusStr) == "dirty")
155 {
156 return GitStatus::modified;
157 }
158
159 return GitStatus::unknown;
160}
161
162} // namespace sen::kernel
163
164// declare the functions that will be defined in the shared library
165extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER();
166extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER();
167
170#define SEN_COMPONENT(component_name) \
171 \
172 extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER() \
173 { \
174 return new component_name(); /* NOLINT(cppcoreguidelines-owning-memory) */ \
175 } \
176 \
177 extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER() \
178 { \
179 static sen::kernel::ComponentInfo info {}; \
180 info.name = SEN_TARGET_NAME; \
181 info.description = SEN_TARGET_DESCRIPTION; \
182 info.buildInfo.maintainer = SEN_TARGET_MAINTAINER; \
183 info.buildInfo.version = SEN_TARGET_VERSION; \
184 info.buildInfo.compiler = SEN_COMPILER_STRING; \
185 info.buildInfo.debugMode = sen::kernel::getDebugEnabled(); \
186 info.buildInfo.wordSize = sen::kernel::getWordSize(); \
187 info.buildInfo.buildTime = std::string(__DATE__) + " " + __TIME__; \
188 info.buildInfo.gitRef = sen::kernel::getGitRef(); \
189 info.buildInfo.gitHash = sen::kernel::getGitHash(); \
190 info.buildInfo.gitStatus = sen::kernel::getGitStatus(); \
191 return &info; \
192 }
193
194#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:69
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:61
virtual FuncResult preload(PreloadApi &&)
Preload the component and initialize all self-contained resources. This function is just called once.
Definition component.h:45
virtual FuncResult load(LoadApi &&)
Load the component and initialize all self-contained resources. This function is just called once.
Definition component.h:49
virtual bool isRealTimeOnly() const noexcept
Return true here if your component is not designed to work with virtualized time.
Definition component.h:72
virtual PassResult init(InitApi &&)
Initialize the component and perform kernel-related operations. This may include dependency resolutio...
Definition component.h:54
static PassResult delay(Duration time) noexcept
Convenience function to return an operation delay request.
Definition component.h:77
static PassResult done() noexcept
Convenience function to return a finished operation result.
Definition component.h:80
Component() noexcept=default
virtual FuncResult unload(UnloadApi &&)
Unload any self-contained resources. This function is just called once.
Definition component.h:65
What can be done when initializing a component.
Definition component_api.h:255
What can be done when loading a component.
Definition component_api.h:244
What can be done when preloading a component.
Definition component_api.h:223
What can be done while a component is running.
Definition component_api.h:266
What can be done when unloading a component.
Definition component_api.h:351
#define SEN_COMPONENT_INFO_MAKER
Definition component.h:90
#define SEN_COMPONENT_MAKER
Definition component.h:89
impl::Ok< void > Ok() noexcept
If T is void, use the void specialization of Ok.
Definition result.h:437
@ 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:123
constexpr auto * componentMakerFuncName
Definition component.h:95
Result< OpState, ExecError > PassResult
The result of operations that may be called multiple times.
Definition component_api.h:60
Result< void, ExecError > FuncResult
The result of operations that are called once.
Definition component_api.h:57
constexpr auto * componentInfoMakerFuncName
Definition component.h:96
constexpr bool getDebugEnabled() noexcept
Definition component.h:102
constexpr const char * getGitHash() noexcept
Definition component.h:132
constexpr WordSize getWordSize() noexcept
Definition component.h:111
GitStatus getGitStatus() noexcept
Definition component.h:141