Sen API
Sen Libraries
Loading...
Searching...
No Matches
gen.h
Go to the documentation of this file.
1// === gen.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_GEN_H
9#define SEN_CORE_OBJ_DETAIL_GEN_H
10
11// sen
16#include "sen/core/base/span.h"
18#include "sen/core/io/util.h"
19#include "sen/core/lang/vm.h"
40
41// std
42#include <initializer_list> // NOLINT(misc-include-cleaner): used by not visible due to macros
43#include <iterator> // NOLINT(misc-include-cleaner): used by not visible due to macros
44#include <optional>
45#include <type_traits>
46
47#define SEN_IMPL_GEN_NATIVE_MEMBERS \
48protected: \
49 [[nodiscard]] inline ::sen::Var senImplGetPropertyImpl(::sen::MemberHash propertyId) const override; \
50 inline void senImplSetNextPropertyUntyped(::sen::MemberHash propertyId, const ::sen::Var& value) override; \
51 [[nodiscard]] inline ::sen::Var senImplGetNextPropertyUntyped(::sen::MemberHash propertyId) const override; \
52 [[nodiscard]] inline uint32_t senImplComputeMaxReliableSerializedPropertySizeImpl() const override; \
53 inline void senImplStreamCall(::sen::MemberHash methodId, ::sen::InputStream& in, ::sen::StreamCallForwarder&& func) \
54 override; \
55 inline void senImplVariantCall( \
56 ::sen::MemberHash methodId, const ::sen::VarList& args, ::sen::VariantCallForwarder&& func) override; \
57 inline void senImplCommitImpl(::sen::TimeStamp time) override; \
58 inline void senImplWriteChangedPropertiesToStream( \
59 ::sen::OutputStream& confirmed, ::sen::impl::BufferProvider uni, ::sen::impl::BufferProvider multi) override; \
60 inline void senImplWriteAllPropertiesToStream(::sen::OutputStream& out) const override; \
61 inline void senImplWriteStaticPropertiesToStream(::sen::OutputStream& out) const override; \
62 inline void senImplWriteDynamicPropertiesToStream(::sen::OutputStream& out) const override; \
63 inline void senImplRemoveTypedConnection(::sen::ConnId id) override; \
64 [[nodiscard]] inline ::sen::impl::FieldValueGetter senImplGetFieldValueGetter( \
65 ::sen::MemberHash propertyId, ::sen::Span<uint16_t> fields) const override; \
66 inline void invokeAllPropertyCallbacks() override;
67
69#define SEN_IMPL_GEN_BASE_CLASS(classname) \
70 SEN_IMPL_GEN_NATIVE_MEMBERS \
71 \
72private: \
73 bool somePropertyIsDirty_ = false; \
74 \
75public: \
76 inline classname(std::string name, const ::sen::VarMap& args); \
77 ~classname() override = default; \
78 [[nodiscard]] inline ::sen::ConstTypeHandle<::sen::ClassType> getClass() const noexcept override; \
79 [[nodiscard]] static inline ::sen::ConstTypeHandle<::sen::ClassType> meta() noexcept; \
80 \
81private:
82
84#define SEN_IMPL_GEN_REMOTE_CLASS(classname) \
85public: \
86 explicit classname(::sen::impl::RemoteObjectInfo&& info); \
87 ~classname() override = default; \
88 \
89protected: \
90 void senImplRemoveTypedConnection(::sen::ConnId id) override; \
91 void senImplWriteAllPropertiesToStream(::sen::OutputStream& out) const override; \
92 void senImplWriteStaticPropertiesToStream(::sen::OutputStream& out) const override; \
93 void senImplWriteDynamicPropertiesToStream(::sen::OutputStream& out) const override; \
94 [[nodiscard]] ::sen::Var senImplGetPropertyImpl(::sen::MemberHash propertyId) const override; \
95 [[nodiscard]] bool readPropertyFromStream(::sen::MemberHash id, ::sen::InputStream& in, bool initialState) override; \
96 [[nodiscard]] ::sen::Var deserializeMethodReturnValueAsVariant(::sen::MemberHash methodId, ::sen::InputStream& in) \
97 const override; \
98 void serializeMethodArgumentsFromVariants( \
99 ::sen::MemberHash methodId, const ::sen::VarList& args, ::sen::OutputStream& out) const override; \
100 void drainInputs() override; \
101 void eventReceived(::sen::MemberHash eventId, ::sen::TimeStamp creationTime, const ::sen::Span<const uint8_t>& args) \
102 override; \
103 void invokeAllPropertyCallbacks() override; \
104 \
105private:
106
108#define SEN_IMPL_GEN_LOCAL_PROXY_CLASS(classname) \
109public: \
110 explicit classname(::sen::NativeObject* owner, const std::string& localPrefix); \
111 ~classname() override = default; \
112 \
113protected: \
114 void senImplWriteAllPropertiesToStream(::sen::OutputStream& out) const override; \
115 void senImplWriteStaticPropertiesToStream(::sen::OutputStream& out) const override; \
116 void senImplWriteDynamicPropertiesToStream(::sen::OutputStream& out) const override; \
117 void senImplRemoveTypedConnection(::sen::ConnId id) override; \
118 [[nodiscard]] ::sen::Var senImplGetPropertyImpl(::sen::MemberHash propertyId) const override; \
119 void drainInputsImpl(::sen::TimeStamp lastCommitTime) override; \
120 \
121private:
122
124#define SEN_IMPL_GEN_UNBOUNDED_SEQUENCE(classname, elementtype) \
125 class classname final: private std::vector<elementtype> \
126 { \
127 using Parent = std::vector<elementtype>; \
128 \
129 public: \
130 using Parent::value_type; \
131 \
132 using Parent::Parent; \
133 using Parent::operator=; \
134 using Parent::assign; \
135 \
136 using Parent::at; \
137 using Parent::operator[]; \
138 using Parent::front; \
139 using Parent::back; \
140 using Parent::data; \
141 \
142 using Parent::begin; \
143 using Parent::cbegin; \
144 using Parent::rbegin; \
145 using Parent::crbegin; \
146 using Parent::end; \
147 using Parent::cend; \
148 using Parent::rend; \
149 using Parent::crend; \
150 \
151 using Parent::empty; \
152 using Parent::size; \
153 using Parent::max_size; \
154 using Parent::reserve; \
155 using Parent::capacity; \
156 using Parent::shrink_to_fit; \
157 \
158 using Parent::clear; \
159 using Parent::insert; \
160 using Parent::emplace; \
161 using Parent::erase; \
162 using Parent::push_back; \
163 using Parent::emplace_back; \
164 using Parent::pop_back; \
165 using Parent::resize; \
166 using Parent::swap; \
167 \
168 constexpr const Parent& asVector() const& noexcept { return *this; } \
169 Parent asVector() && noexcept(std::is_nothrow_move_constructible_v<Parent>) { return std::move(*this); } \
170 \
171 friend bool operator==(const classname& lhs, const classname& rhs) { return lhs.asVector() == rhs.asVector(); } \
172 friend bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
173 };
174
176#define SEN_IMPL_GEN_BOUNDED_SEQUENCE(classname, element, maxSize) \
177 struct classname final: public ::sen::StaticVector<element, maxSize> \
178 { \
179 using Parent = ::sen::StaticVector<element, maxSize>; \
180 using Parent::Parent; \
181 using Parent::operator=; \
182 };
183
185#define SEN_IMPL_GEN_FIXED_SEQUENCE(classname, element, num_elements) \
186 class classname final: private std::array<element, num_elements> \
187 { \
188 using Parent = std::array<element, num_elements>; \
189 \
190 public: \
191 using Parent::value_type; \
192 \
193 using Parent::Parent; \
194 using Parent::operator=; \
195 \
196 using Parent::at; \
197 using Parent::operator[]; \
198 using Parent::front; \
199 using Parent::back; \
200 using Parent::data; \
201 \
202 using Parent::begin; \
203 using Parent::cbegin; \
204 using Parent::rbegin; \
205 using Parent::crbegin; \
206 using Parent::end; \
207 using Parent::cend; \
208 using Parent::rend; \
209 using Parent::crend; \
210 \
211 using Parent::empty; \
212 using Parent::size; \
213 using Parent::max_size; \
214 \
215 using Parent::fill; \
216 using Parent::swap; \
217 \
218 template <typename Y, typename = std::enable_if_t<std::is_convertible_v<Y, element>, int>> \
219 constexpr classname(std::initializer_list<Y> elems): Parent() \
220 { \
221 using DifferenceType = \
222 typename std::iterator_traits<typename std::initializer_list<Y>::iterator>::difference_type; \
223 SEN_ASSERT(std::distance(std::begin(elems), std::end(elems)) <= static_cast<DifferenceType>(num_elements)); \
224 std::move(std::begin(elems), \
225 std::next(std::begin(elems), \
226 std::min(std::distance(std::begin(elems), std::end(elems)), \
227 static_cast<DifferenceType>(num_elements))), \
228 std::begin(*this)); \
229 } \
230 \
231 constexpr const Parent& asArray() const& noexcept { return *this; } \
232 Parent asArray() && noexcept(std::is_nothrow_move_constructible_v<element>) { return std::move(*this); } \
233 \
234 friend bool operator==(const classname& lhs, const classname& rhs) { return lhs.asArray() == rhs.asArray(); } \
235 friend bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
236 };
237
239#define SEN_IMPL_GEN_STRUCT_OPERATORS(classname, doExport) \
240 SEN_MAYBE_EXPORT(doExport) std::ostream& operator<<(std::ostream& out, const classname& val); \
241 SEN_MAYBE_EXPORT(doExport) bool operator==(const classname& lhs, const classname& rhs); \
242 SEN_MAYBE_EXPORT(doExport) bool operator!=(const classname& lhs, const classname& rhs);
243
245#define SEN_IMPL_GEN_OPTIONAL(classname, elementtype) \
246 class classname final: private std::optional<elementtype> \
247 { \
248 using Parent = std::optional<elementtype>; \
249 \
250 public: \
251 using Parent::value_type; \
252 \
253 using Parent::Parent; \
254 using Parent::operator=; \
255 \
256 using Parent::operator->; \
257 using Parent::operator*; \
258 using Parent::operator bool; \
259 using Parent::has_value; \
260 using Parent::value; \
261 using Parent::value_or; \
262 \
263 using Parent::reset; \
264 using Parent::swap; \
265 using Parent::emplace; \
266 \
267 constexpr const Parent& asOptional() const& noexcept { return *this; } \
268 Parent asOptional() && noexcept(std::is_nothrow_move_constructible_v<Parent>) { return std::move(*this); } \
269 \
270 friend constexpr bool operator==(const classname& lhs, const classname& rhs) \
271 { \
272 return lhs.asOptional() == rhs.asOptional(); \
273 } \
274 friend constexpr bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
275 };
276
278#define SEN_IMPL_GEN_OPTIONAL_TRAITS(classname, doExport) \
279 template <> \
280 struct MetaTypeTrait<classname> \
281 { \
282 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<OptionalType> meta(); \
283 }; \
284 template <> \
285 struct VariantTraits<classname>: private OptionalTraitsBase<classname> \
286 { \
287 using OptionalTraitsBase<classname>::valueToVariant; \
288 using OptionalTraitsBase<classname>::variantToValue; \
289 }; \
290 template <> \
291 struct SerializationTraits<classname>: private OptionalTraitsBase<classname> \
292 { \
293 using OptionalTraitsBase<classname>::write; \
294 using OptionalTraitsBase<classname>::read; \
295 using OptionalTraitsBase<classname>::serializedSize; \
296 using OptionalTraitsBase<classname>::toJsonString; \
297 using OptionalTraitsBase<classname>::fromJsonString; \
298 };
299
301#define SEN_IMPL_GEN_ENUM_TRAITS(classname, doExport) \
302 template <> \
303 struct MetaTypeTrait<classname> \
304 { \
305 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<EnumType> meta(); \
306 }; \
307 template <> \
308 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private EnumTraitsBase<classname> \
309 { \
310 using EnumTraitsBase<classname>::valueToVariant; \
311 using EnumTraitsBase<classname>::variantToValue; \
312 }; \
313 template <> \
314 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private EnumTraitsBase<classname> \
315 { \
316 using EnumTraitsBase<classname>::write; \
317 using EnumTraitsBase<classname>::read; \
318 using EnumTraitsBase<classname>::serializedSize; \
319 using EnumTraitsBase<classname>::toJsonString; \
320 using EnumTraitsBase<classname>::fromJsonString; \
321 }; \
322 template <> \
323 struct SEN_MAYBE_EXPORT(doExport) StringConversionTraits<classname> \
324 { \
325 [[nodiscard]] static std::string_view toString(classname val); \
326 [[nodiscard]] static classname fromString(std::string_view val); \
327 }; \
328 \
329 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) std::string_view toString(classname value);
330
332#define SEN_IMPL_GEN_SEQUENCE_TRAITS(classname, doExport) \
333 template <> \
334 struct MetaTypeTrait<classname> \
335 { \
336 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static sen::ConstTypeHandle<SequenceType> meta(); \
337 }; \
338 template <> \
339 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private SequenceTraitsBase<classname> \
340 { \
341 using SequenceTraitsBase<classname>::valueToVariant; \
342 using SequenceTraitsBase<classname>::variantToValue; \
343 }; \
344 template <> \
345 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private SequenceTraitsBase<classname> \
346 { \
347 using SequenceTraitsBase<classname>::write; \
348 using SequenceTraitsBase<classname>::read; \
349 using SequenceTraitsBase<classname>::serializedSize; \
350 using SequenceTraitsBase<classname>::toJsonString; \
351 using SequenceTraitsBase<classname>::fromJsonString; \
352 };
353
355#define SEN_IMPL_GEN_ARRAY_TRAITS(classname, doExport) \
356 template <> \
357 struct MetaTypeTrait<classname> \
358 { \
359 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<SequenceType> meta(); \
360 }; \
361 template <> \
362 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private ArrayTraitsBase<classname> \
363 { \
364 using ArrayTraitsBase<classname>::valueToVariant; \
365 using ArrayTraitsBase<classname>::variantToValue; \
366 }; \
367 template <> \
368 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private ArrayTraitsBase<classname> \
369 { \
370 using ArrayTraitsBase<classname>::write; \
371 using ArrayTraitsBase<classname>::read; \
372 using ArrayTraitsBase<classname>::serializedSize; \
373 using ArrayTraitsBase<classname>::toJsonString; \
374 using ArrayTraitsBase<classname>::fromJsonString; \
375 };
376
378#define SEN_IMPL_GEN_STRUCT_TRAITS(classname, doExport) \
379 template <> \
380 struct MetaTypeTrait<classname> \
381 { \
382 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<StructType> meta(); \
383 }; \
384 template <> \
385 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected StructTraitsBase \
386 { \
387 static void valueToVariant(const classname& val, Var& var); \
388 static void variantToValue(const Var& var, classname& val); \
389 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
390 }; \
391 template <> \
392 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected StructTraitsBase \
393 { \
394 static void write(OutputStream& out, const classname& val); \
395 static void read(InputStream& in, classname& val); \
396 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
397 static std::string toJsonString(const classname& val); \
398 static void fromJsonString(const std::string& str, classname& val); \
399 };
400
402#define SEN_IMPL_GEN_VARIANT_TRAITS(classname, doExport) \
403 template <> \
404 struct SEN_MAYBE_EXPORT(doExport) MetaTypeTrait<classname> \
405 { \
406 [[nodiscard]] static ConstTypeHandle<VariantType> meta(); \
407 }; \
408 template <> \
409 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected VariantTraitsBase<classname> \
410 { \
411 static void valueToVariant(const classname& val, Var& var); \
412 static void variantToValue(const Var& var, classname& val); \
413 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
414 using VariantTraitsBase<classname>::tryPrintField; \
415 }; \
416 template <> \
417 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected VariantTraitsBase<classname> \
418 { \
419 static void write(OutputStream& out, const classname& val); \
420 static void read(InputStream& in, classname& val); \
421 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
422 static std::string toJsonString(const classname& val); \
423 static void fromJsonString(const std::string& str, classname& val); \
424 };
425
427#define SEN_IMPL_GEN_CLASS_META_TRAITS(classname, doExport) \
428 template <> \
429 struct MetaTypeTrait<classname> \
430 { \
431 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<ClassType> meta(); \
432 };
433
435#define SEN_IMPL_GEN_CLASS_TRAITS_TEMPLATE(interfaceName, remoteProxy, localProxy, baseName) \
436 template <> \
437 struct SenClassRelation<interfaceName> \
438 { \
439 using InterfaceType = interfaceName; \
440 template <typename T> \
441 using BaseType = baseName<T>; \
442 static constexpr bool isBaseTypeTemplate = true; \
443 using RemoteProxyType = remoteProxy; \
444 using LocalProxyType = localProxy; \
445 }; \
446 template <> \
447 struct SenClassRelation<remoteProxy> \
448 { \
449 using InterfaceType = interfaceName; \
450 template <typename T> \
451 using BaseType = baseName<T>; \
452 static constexpr bool isBaseTypeTemplate = true; \
453 using RemoteProxyType = remoteProxy; \
454 using LocalProxyType = localProxy; \
455 }; \
456 template <> \
457 struct SenClassRelation<localProxy> \
458 { \
459 using InterfaceType = interfaceName; \
460 template <typename T> \
461 using BaseType = baseName<T>; \
462 static constexpr bool isBaseTypeTemplate = true; \
463 using RemoteProxyType = remoteProxy; \
464 using LocalProxyType = localProxy; \
465 }; \
466 template <typename T> \
467 struct SenClassRelation<baseName<T>> \
468 { \
469 using InterfaceType = interfaceName; \
470 template <typename U> \
471 using BaseType = baseName<U>; \
472 static constexpr bool isBaseTypeTemplate = true; \
473 using RemoteProxyType = remoteProxy; \
474 using LocalProxyType = localProxy; \
475 };
476
478#define SEN_IMPL_GEN_CLASS_TRAITS_NORMAL(interfaceName, remoteProxy, localProxy, baseName) \
479 template <> \
480 struct SenClassRelation<interfaceName> \
481 { \
482 using InterfaceType = interfaceName; \
483 using BaseType = baseName; \
484 static constexpr bool isBaseTypeTemplate = false; \
485 using RemoteProxyType = remoteProxy; \
486 using LocalProxyType = localProxy; \
487 }; \
488 template <> \
489 struct SenClassRelation<remoteProxy> \
490 { \
491 using InterfaceType = interfaceName; \
492 using BaseType = baseName; \
493 static constexpr bool isBaseTypeTemplate = false; \
494 using RemoteProxyType = remoteProxy; \
495 using LocalProxyType = localProxy; \
496 }; \
497 template <> \
498 struct SenClassRelation<localProxy> \
499 { \
500 using InterfaceType = interfaceName; \
501 using BaseType = baseName; \
502 static constexpr bool isBaseTypeTemplate = false; \
503 using RemoteProxyType = remoteProxy; \
504 using LocalProxyType = localProxy; \
505 }; \
506 template <> \
507 struct SenClassRelation<baseName> \
508 { \
509 using InterfaceType = interfaceName; \
510 using BaseType = baseName; \
511 static constexpr bool isBaseTypeTemplate = false; \
512 using RemoteProxyType = remoteProxy; \
513 using LocalProxyType = localProxy; \
514 };
515
517#define SEN_IMPL_GEN_QUANTITY_TRAITS(classname, maxVal, minVal, doExport) \
518 template <> \
519 struct MetaTypeTrait<classname> \
520 { \
521 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<QuantityType> meta(); \
522 }; \
523 template <> \
524 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private QuantityTraitsBase<classname> \
525 { \
526 using QuantityTraitsBase<classname>::valueToVariant; \
527 using QuantityTraitsBase<classname>::variantToValue; \
528 }; \
529 template <> \
530 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private QuantityTraitsBase<classname> \
531 { \
532 using QuantityTraitsBase<classname>::write; \
533 using QuantityTraitsBase<classname>::read; \
534 using QuantityTraitsBase<classname>::serializedSize; \
535 using QuantityTraitsBase<classname>::toJsonString; \
536 using QuantityTraitsBase<classname>::fromJsonString; \
537 }; \
538 template <> \
539 struct SEN_MAYBE_EXPORT(doExport) QuantityTraits<classname> \
540 { \
541 using ValueType = typename classname::ValueType; \
542 \
543 static constexpr ValueType max = maxVal; \
544 static constexpr ValueType min = minVal; \
545 \
546 [[nodiscard]] static ::std::optional<const sen::Unit*> unit() noexcept; \
547 [[nodiscard]] static const sen::Unit& unit(sen::Unit::EnsureTag); \
548 };
549
550#endif // SEN_CORE_OBJ_DETAIL_GEN_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...