Sen API
Sen Libraries
Loading...
Searching...
No Matches
native_object_impl.h
Go to the documentation of this file.
1// === native_object_impl.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_CORE_OBJ_DETAIL_NATIVE_OBJECT_IMPL_H
9#define SEN_CORE_OBJ_DETAIL_NATIVE_OBJECT_IMPL_H
10
11// sen
19#include "sen/core/meta/type.h"
20#include "sen/core/meta/var.h"
23
24// std
25#include <functional>
26#include <string>
27
28//--------------------------------------------------------------------------------------------------------------
29// Forward declarations
30//--------------------------------------------------------------------------------------------------------------
31
32namespace sen::impl
33{
34class NativeObjectProxy;
35class FilteredProvider;
36} // namespace sen::impl
37
38namespace sen::kernel
39{
40class RegistrationApi;
41class RunApi;
42class PipelineComponent;
43} // namespace sen::kernel
44
45namespace sen::kernel::impl
46{
47class RemoteParticipant;
48class LocalParticipant;
49class Runner;
50class ObjectUpdate;
51} // namespace sen::kernel::impl
52
53namespace sen::impl
54{
55
56//--------------------------------------------------------------------------------------------------------------
57// Helper types
58//--------------------------------------------------------------------------------------------------------------
59
61using BufferProvider = std::function<ResizableBufferWriter<FixedMemoryBlock>(std::size_t)>;
62
63//--------------------------------------------------------------------------------------------------------------
64// Used by the generated code
65//--------------------------------------------------------------------------------------------------------------
66
67template <typename T>
68void writePropertyToStream(uint32_t id, uint32_t valueSize, MaybeRef<T> val, OutputStream& out);
69
70template <typename T>
71void writePropertyToStream(uint32_t id, MaybeRef<T> val, OutputStream& out);
72
73template <typename T>
74void writePropertyToStream(uint32_t id, MaybeRef<T> val, BufferProvider provider);
75
76template <typename T>
77[[nodiscard]] T fromStream(InputStream& in);
78
79template <typename T>
80void variantToStream(OutputStream& out, const Var& var)
81{
82 SerializationTraits<T>::write(out, toValue<T>(var));
83}
84
85template <typename T>
86[[nodiscard]] Var variantFromStream(InputStream& in)
87{
88 return toVariant<T>(fromStream<T>(in));
89}
90
91inline void dispatchIfChanged(PropertyFlags& flags,
92 EventBuffer<>& eventBuffer,
93 uint32_t propertyId,
94 ObjectId objectId,
95 TimeStamp time,
96 TransportMode transport,
97 Object* producer)
98{
99 if (flags.changedInLastCycle())
100 {
101 eventBuffer.dispatch(propertyId, time, objectId, transport, false, nullptr, producer);
102 }
103}
104
105inline void unconditionalDispatch(EventBuffer<>& eventBuffer,
106 uint32_t propertyId,
107 ObjectId objectId,
108 TimeStamp time,
109 TransportMode transport,
110 Object* producer)
111{
112 eventBuffer.dispatch(propertyId, time, objectId, transport, false, nullptr, producer);
113}
114
115template <typename T>
116inline void writePropertyIfChanged(PropertyFlags& flags, uint32_t propertyId, OutputStream& out, MaybeRef<T> val)
117{
118 if (flags.changedInLastCycle())
119 {
120 writePropertyToStream<T>(propertyId, val, out);
121 }
122}
123
124template <typename T>
125inline void writePropertyIfChanged(PropertyFlags& flags, uint32_t propertyId, BufferProvider provider, MaybeRef<T> val)
126{
127 if (flags.changedInLastCycle())
128 {
129 writePropertyToStream<T>(propertyId, val, provider);
130 }
131}
132
133template <typename T>
134T getFromMap(const std::string& name, const VarMap& map, std::function<void(const T&)> validator = nullptr);
135
136template <typename T>
137void tryGetFromMap(const std::string& name,
138 const VarMap& map,
139 T& val,
140 std::function<void(const T&)> validator = nullptr);
141
142struct FieldValueGetter
143{
144 lang::ValueGetter getterFunc;
145 const impl::PropertyFlags* flags = nullptr;
146};
147
148//----------------------------------------------------------------------------------------------------------------------
149// Inline implementation
150//----------------------------------------------------------------------------------------------------------------------
151
152//--------------------------------------------------------------------------------------------------------------
153// Used by the generated code
154//--------------------------------------------------------------------------------------------------------------
155
156template <typename T>
157inline void writePropertyToStream(uint32_t id, uint32_t valueSize, MaybeRef<T> val, OutputStream& out)
158{
159 out.writeUInt32(id);
160 out.writeUInt32(valueSize);
161 SerializationTraits<T>::write(out, val);
162}
163
164template <typename T>
165inline void writePropertyToStream(uint32_t id, MaybeRef<T> val, OutputStream& out)
166{
167 writePropertyToStream<T>(id, SerializationTraits<T>::serializedSize(val), val, out);
168}
169
170template <typename T>
171inline void writePropertyToStream(uint32_t id, MaybeRef<T> val, BufferProvider provider)
172{
173 const auto valueSize = SerializationTraits<T>::serializedSize(val);
174
175 auto writer = provider(getSerializedSize(id) + getSerializedSize(valueSize));
176 OutputStream out(writer);
177
178 writePropertyToStream<T>(id, valueSize, val, out);
179}
180
181template <typename T>
182inline T fromStream(InputStream& in)
183{
184 T result {};
185 SerializationTraits<T>::read(in, result);
186 return result;
187}
188
189template <typename T>
190inline T getFromMap(const std::string& name, const VarMap& map, std::function<void(const T&)> validator)
191{
192 if (!validator)
193 { // Provide a default validator
194 validator = [](const T&) {};
195 }
196
197 auto itr = map.find(name);
198 if (itr == map.end())
199 {
200 std::string err;
201 err.append("could not find mandatory item '");
202 err.append(name);
203 err.append("' in map");
206 }
207
208 auto value = toValue<T>(itr->second);
209 validator(value);
210 return value;
211}
212
213template <typename T>
214inline void tryGetFromMap(const std::string& name, const VarMap& map, T& val, std::function<void(const T&)> validator)
215{
216 if (!validator)
217 { // Provide a default validator
218 validator = [](const T&) {};
219 }
220
221 auto itr = map.find(name);
222 if (itr != map.end())
223 {
224 auto valCopy = toValue<T>(itr->second);
225 validator(valCopy);
226#if SEN_GCC_VERSION_CHECK_SMALLER(12, 0, 0)
227# pragma GCC diagnostic push
228# pragma GCC diagnostic ignored "-Wstringop-overflow"
229 // Misdetection for wrong assignment
230 val = valCopy;
231# pragma GCC diagnostic pop
232#else
233 val = valCopy;
234#endif
235 }
236}
237
238} // namespace sen::impl
239
240#endif // SEN_CORE_OBJ_DETAIL_NATIVE_OBJECT_IMPL_H
API for objects when registered.
Definition component_api.h:170
What can be done while a component is running.
Definition component_api.h:224
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...
InputStreamTemplate< LittleEndian > InputStream
Definition input_stream.h:84
Var toVariant(const T &val)
Definition core/include/sen/core/io/util.h:160
T toValue(const Var &var)
Definition core/include/sen/core/io/util.h:152
std::function< Value()> ValueGetter
Gets a value from some source.
Definition vm.h:78
#define SEN_UNREACHABLE()
Definition compiler_macros.h:420
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
@ time
Definition unit.h:34
TransportMode
How to transport information.
Definition type.h:56
Strong type for the unique identifier for a transport timer.
Definition native_object_impl.h:39
OutputStreamTemplate< LittleEndian > OutputStream
Definition output_stream.h:64