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// Defined in the target's generated build_info.cpp, never here: inline over macros they put the
124// git values on every compile line in the target, and those change every commit.
125// SEN_PRIVATE so each binary resolves its own copy. Exported, a target with no build_info of
126// its own links against the kernel's and reports the framework's hash as its own.
127[[nodiscard]] SEN_PRIVATE const char* getGitRef() noexcept;
128[[nodiscard]] SEN_PRIVATE const char* getGitHash() noexcept;
129[[nodiscard]] SEN_PRIVATE GitStatus getGitStatus() noexcept;
130
131// The commit's timestamp. A wall-clock build time is whenever the object was last recompiled,
132// which is not the same thing.
133[[nodiscard]] SEN_PRIVATE const char* getBuildTime() noexcept;
134
135} // namespace sen::kernel
136
137// declare the functions that will be defined in the shared library
138extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER();
139extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER();
140
143#define SEN_COMPONENT(component_name) \
144 \
145 extern "C" SEN_EXPORT sen::kernel::Component* SEN_COMPONENT_MAKER() \
146 { \
147 return new component_name(); /* NOLINT(cppcoreguidelines-owning-memory) */ \
148 } \
149 \
150 extern "C" SEN_EXPORT const sen::kernel::ComponentInfo* SEN_COMPONENT_INFO_MAKER() \
151 { \
152 static sen::kernel::ComponentInfo info {}; \
153 info.name = SEN_TARGET_NAME; \
154 info.description = SEN_TARGET_DESCRIPTION; \
155 info.buildInfo.maintainer = SEN_TARGET_MAINTAINER; \
156 info.buildInfo.version = SEN_TARGET_VERSION; \
157 info.buildInfo.compiler = SEN_COMPILER_STRING; \
158 info.buildInfo.debugMode = sen::kernel::getDebugEnabled(); \
159 info.buildInfo.wordSize = sen::kernel::getWordSize(); \
160 info.buildInfo.buildTime = sen::kernel::getBuildTime(); \
161 info.buildInfo.gitRef = sen::kernel::getGitRef(); \
162 info.buildInfo.gitHash = sen::kernel::getGitHash(); \
163 info.buildInfo.gitStatus = sen::kernel::getGitStatus(); \
164 return &info; \
165 }
166
167#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:458
What can be done when loading a component.
Definition component_api.h:447
What can be done when preloading a component.
Definition component_api.h:418
What can be done while a component is running.
Definition component_api.h:469
What can be done when unloading a component.
Definition component_api.h:557
#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 auto * componentMakerFuncName
Definition component.h:95
Result< OpState, ExecError > PassResult
The result of operations that may be called multiple times.
Definition component_api.h:64
SEN_PRIVATE GitStatus getGitStatus() noexcept
SEN_PRIVATE const char * getBuildTime() noexcept
SEN_PRIVATE const char * getGitHash() noexcept
Result< void, ExecError > FuncResult
The result of operations that are called once.
Definition component_api.h:61
constexpr auto * componentInfoMakerFuncName
Definition component.h:96
constexpr bool getDebugEnabled() noexcept
Definition component.h:102
SEN_PRIVATE const char * getGitRef() noexcept
constexpr WordSize getWordSize() noexcept
Definition component.h:111
Definition assert.h:17