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 using Parent::reference; \
193 using Parent::const_reference; \
194 using Parent::pointer; \
195 using Parent::const_pointer; \
196 using Parent::iterator; \
197 using Parent::const_iterator; \
198 using Parent::reverse_iterator; \
199 using Parent::const_reverse_iterator; \
200 \
201 using Parent::Parent; \
202 using Parent::operator=; \
203 \
204 using Parent::at; \
205 using Parent::operator[]; \
206 using Parent::front; \
207 using Parent::back; \
208 using Parent::data; \
209 \
210 using Parent::begin; \
211 using Parent::cbegin; \
212 using Parent::rbegin; \
213 using Parent::crbegin; \
214 using Parent::end; \
215 using Parent::cend; \
216 using Parent::rend; \
217 using Parent::crend; \
218 \
219 using Parent::empty; \
220 using Parent::size; \
221 using Parent::max_size; \
222 \
223 using Parent::fill; \
224 using Parent::swap; \
225 \
226 template <typename Y, typename = std::enable_if_t<std::is_convertible_v<Y, element>, int>> \
227 constexpr classname(std::initializer_list<Y> elems): Parent() \
228 { \
229 using DifferenceType = \
230 typename std::iterator_traits<typename std::initializer_list<Y>::iterator>::difference_type; \
231 SEN_ASSERT(std::distance(std::begin(elems), std::end(elems)) <= static_cast<DifferenceType>(num_elements)); \
232 std::move(std::begin(elems), \
233 std::next(std::begin(elems), \
234 std::min(std::distance(std::begin(elems), std::end(elems)), \
235 static_cast<DifferenceType>(num_elements))), \
236 std::begin(*this)); \
237 } \
238 \
239 constexpr const Parent& asArray() const& noexcept { return *this; } \
240 Parent asArray() && noexcept(std::is_nothrow_move_constructible_v<element>) { return std::move(*this); } \
241 \
242 friend bool operator==(const classname& lhs, const classname& rhs) { return lhs.asArray() == rhs.asArray(); } \
243 friend bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
244 };
245
247#define SEN_IMPL_GEN_STRUCT_OPERATORS(classname, doExport) \
248 SEN_MAYBE_EXPORT(doExport) std::ostream& operator<<(std::ostream& out, const classname& val); \
249 SEN_MAYBE_EXPORT(doExport) bool operator==(const classname& lhs, const classname& rhs); \
250 SEN_MAYBE_EXPORT(doExport) bool operator!=(const classname& lhs, const classname& rhs);
251
253#define SEN_IMPL_GEN_OPTIONAL(classname, elementtype) \
254 class classname final: private std::optional<elementtype> \
255 { \
256 using Parent = std::optional<elementtype>; \
257 \
258 public: \
259 using Parent::value_type; \
260 \
261 using Parent::Parent; \
262 using Parent::operator=; \
263 \
264 using Parent::operator->; \
265 using Parent::operator*; \
266 using Parent::operator bool; \
267 using Parent::has_value; \
268 using Parent::value; \
269 using Parent::value_or; \
270 \
271 using Parent::reset; \
272 using Parent::swap; \
273 using Parent::emplace; \
274 \
275 constexpr const Parent& asOptional() const& noexcept { return *this; } \
276 Parent asOptional() && noexcept(std::is_nothrow_move_constructible_v<Parent>) { return std::move(*this); } \
277 \
278 friend constexpr bool operator==(const classname& lhs, const classname& rhs) \
279 { \
280 return lhs.asOptional() == rhs.asOptional(); \
281 } \
282 friend constexpr bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
283 };
284
286#define SEN_IMPL_GEN_OPTIONAL_TRAITS(classname, doExport) \
287 template <> \
288 struct MetaTypeTrait<classname> \
289 { \
290 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<OptionalType> meta(); \
291 }; \
292 template <> \
293 struct VariantTraits<classname>: private OptionalTraitsBase<classname> \
294 { \
295 using OptionalTraitsBase<classname>::valueToVariant; \
296 using OptionalTraitsBase<classname>::variantToValue; \
297 }; \
298 template <> \
299 struct SerializationTraits<classname>: private OptionalTraitsBase<classname> \
300 { \
301 using OptionalTraitsBase<classname>::write; \
302 using OptionalTraitsBase<classname>::read; \
303 using OptionalTraitsBase<classname>::serializedSize; \
304 using OptionalTraitsBase<classname>::toJsonString; \
305 using OptionalTraitsBase<classname>::fromJsonString; \
306 };
307
309#define SEN_IMPL_GEN_ENUM_TRAITS(classname, doExport) \
310 template <> \
311 struct MetaTypeTrait<classname> \
312 { \
313 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<EnumType> meta(); \
314 }; \
315 template <> \
316 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private EnumTraitsBase<classname> \
317 { \
318 using EnumTraitsBase<classname>::valueToVariant; \
319 using EnumTraitsBase<classname>::variantToValue; \
320 }; \
321 template <> \
322 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private EnumTraitsBase<classname> \
323 { \
324 using EnumTraitsBase<classname>::write; \
325 using EnumTraitsBase<classname>::read; \
326 using EnumTraitsBase<classname>::serializedSize; \
327 using EnumTraitsBase<classname>::toJsonString; \
328 using EnumTraitsBase<classname>::fromJsonString; \
329 }; \
330 template <> \
331 struct SEN_MAYBE_EXPORT(doExport) StringConversionTraits<classname> \
332 { \
333 [[nodiscard]] static std::string_view toString(classname val); \
334 [[nodiscard]] static classname fromString(std::string_view val); \
335 }; \
336 \
337 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) std::string_view toString(classname value);
338
340#define SEN_IMPL_GEN_SEQUENCE_TRAITS(classname, doExport) \
341 template <> \
342 struct MetaTypeTrait<classname> \
343 { \
344 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static sen::ConstTypeHandle<SequenceType> meta(); \
345 }; \
346 template <> \
347 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private SequenceTraitsBase<classname> \
348 { \
349 using SequenceTraitsBase<classname>::valueToVariant; \
350 using SequenceTraitsBase<classname>::variantToValue; \
351 }; \
352 template <> \
353 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private SequenceTraitsBase<classname> \
354 { \
355 using SequenceTraitsBase<classname>::write; \
356 using SequenceTraitsBase<classname>::read; \
357 using SequenceTraitsBase<classname>::serializedSize; \
358 using SequenceTraitsBase<classname>::toJsonString; \
359 using SequenceTraitsBase<classname>::fromJsonString; \
360 };
361
363#define SEN_IMPL_GEN_ARRAY_TRAITS(classname, doExport) \
364 template <> \
365 struct MetaTypeTrait<classname> \
366 { \
367 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<SequenceType> meta(); \
368 }; \
369 template <> \
370 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private ArrayTraitsBase<classname> \
371 { \
372 using ArrayTraitsBase<classname>::valueToVariant; \
373 using ArrayTraitsBase<classname>::variantToValue; \
374 }; \
375 template <> \
376 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private ArrayTraitsBase<classname> \
377 { \
378 using ArrayTraitsBase<classname>::write; \
379 using ArrayTraitsBase<classname>::read; \
380 using ArrayTraitsBase<classname>::serializedSize; \
381 using ArrayTraitsBase<classname>::toJsonString; \
382 using ArrayTraitsBase<classname>::fromJsonString; \
383 };
384
386#define SEN_IMPL_GEN_STRUCT_TRAITS(classname, doExport) \
387 template <> \
388 struct MetaTypeTrait<classname> \
389 { \
390 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<StructType> meta(); \
391 }; \
392 template <> \
393 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected StructTraitsBase \
394 { \
395 static void valueToVariant(const classname& val, Var& var); \
396 static void variantToValue(const Var& var, classname& val); \
397 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
398 }; \
399 template <> \
400 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected StructTraitsBase \
401 { \
402 static void write(OutputStream& out, const classname& val); \
403 static void read(InputStream& in, classname& val); \
404 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
405 static std::string toJsonString(const classname& val); \
406 static void fromJsonString(const std::string& str, classname& val); \
407 };
408
410#define SEN_IMPL_GEN_VARIANT_TRAITS(classname, doExport) \
411 template <> \
412 struct SEN_MAYBE_EXPORT(doExport) MetaTypeTrait<classname> \
413 { \
414 [[nodiscard]] static ConstTypeHandle<VariantType> meta(); \
415 }; \
416 template <> \
417 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected VariantTraitsBase<classname> \
418 { \
419 static void valueToVariant(const classname& val, Var& var); \
420 static void variantToValue(const Var& var, classname& val); \
421 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
422 using VariantTraitsBase<classname>::tryPrintField; \
423 }; \
424 template <> \
425 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected VariantTraitsBase<classname> \
426 { \
427 static void write(OutputStream& out, const classname& val); \
428 static void read(InputStream& in, classname& val); \
429 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
430 static std::string toJsonString(const classname& val); \
431 static void fromJsonString(const std::string& str, classname& val); \
432 };
433
435#define SEN_IMPL_GEN_CLASS_META_TRAITS(classname, doExport) \
436 template <> \
437 struct MetaTypeTrait<classname> \
438 { \
439 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<ClassType> meta(); \
440 };
441
443#define SEN_IMPL_GEN_CLASS_TRAITS_TEMPLATE(interfaceName, remoteProxy, localProxy, baseName) \
444 template <> \
445 struct SenClassRelation<interfaceName> \
446 { \
447 using InterfaceType = interfaceName; \
448 template <typename T> \
449 using BaseType = baseName<T>; \
450 static constexpr bool isBaseTypeTemplate = true; \
451 using RemoteProxyType = remoteProxy; \
452 using LocalProxyType = localProxy; \
453 }; \
454 template <> \
455 struct SenClassRelation<remoteProxy> \
456 { \
457 using InterfaceType = interfaceName; \
458 template <typename T> \
459 using BaseType = baseName<T>; \
460 static constexpr bool isBaseTypeTemplate = true; \
461 using RemoteProxyType = remoteProxy; \
462 using LocalProxyType = localProxy; \
463 }; \
464 template <> \
465 struct SenClassRelation<localProxy> \
466 { \
467 using InterfaceType = interfaceName; \
468 template <typename T> \
469 using BaseType = baseName<T>; \
470 static constexpr bool isBaseTypeTemplate = true; \
471 using RemoteProxyType = remoteProxy; \
472 using LocalProxyType = localProxy; \
473 }; \
474 template <typename T> \
475 struct SenClassRelation<baseName<T>> \
476 { \
477 using InterfaceType = interfaceName; \
478 template <typename U> \
479 using BaseType = baseName<U>; \
480 static constexpr bool isBaseTypeTemplate = true; \
481 using RemoteProxyType = remoteProxy; \
482 using LocalProxyType = localProxy; \
483 };
484
486#define SEN_IMPL_GEN_CLASS_TRAITS_NORMAL(interfaceName, remoteProxy, localProxy, baseName) \
487 template <> \
488 struct SenClassRelation<interfaceName> \
489 { \
490 using InterfaceType = interfaceName; \
491 using BaseType = baseName; \
492 static constexpr bool isBaseTypeTemplate = false; \
493 using RemoteProxyType = remoteProxy; \
494 using LocalProxyType = localProxy; \
495 }; \
496 template <> \
497 struct SenClassRelation<remoteProxy> \
498 { \
499 using InterfaceType = interfaceName; \
500 using BaseType = baseName; \
501 static constexpr bool isBaseTypeTemplate = false; \
502 using RemoteProxyType = remoteProxy; \
503 using LocalProxyType = localProxy; \
504 }; \
505 template <> \
506 struct SenClassRelation<localProxy> \
507 { \
508 using InterfaceType = interfaceName; \
509 using BaseType = baseName; \
510 static constexpr bool isBaseTypeTemplate = false; \
511 using RemoteProxyType = remoteProxy; \
512 using LocalProxyType = localProxy; \
513 }; \
514 template <> \
515 struct SenClassRelation<baseName> \
516 { \
517 using InterfaceType = interfaceName; \
518 using BaseType = baseName; \
519 static constexpr bool isBaseTypeTemplate = false; \
520 using RemoteProxyType = remoteProxy; \
521 using LocalProxyType = localProxy; \
522 };
523
525#define SEN_IMPL_GEN_QUANTITY_TRAITS(classname, maxVal, minVal, doExport) \
526 template <> \
527 struct MetaTypeTrait<classname> \
528 { \
529 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<QuantityType> meta(); \
530 }; \
531 template <> \
532 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private QuantityTraitsBase<classname> \
533 { \
534 using QuantityTraitsBase<classname>::valueToVariant; \
535 using QuantityTraitsBase<classname>::variantToValue; \
536 }; \
537 template <> \
538 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private QuantityTraitsBase<classname> \
539 { \
540 using QuantityTraitsBase<classname>::write; \
541 using QuantityTraitsBase<classname>::read; \
542 using QuantityTraitsBase<classname>::serializedSize; \
543 using QuantityTraitsBase<classname>::toJsonString; \
544 using QuantityTraitsBase<classname>::fromJsonString; \
545 }; \
546 template <> \
547 struct SEN_MAYBE_EXPORT(doExport) QuantityTraits<classname> \
548 { \
549 using ValueType = typename classname::ValueType; \
550 \
551 static constexpr ValueType max = maxVal; \
552 static constexpr ValueType min = minVal; \
553 \
554 [[nodiscard]] static ::std::optional<const sen::Unit*> unit() noexcept; \
555 [[nodiscard]] static const sen::Unit& unit(sen::Unit::EnsureTag); \
556 };
557
558#endif // SEN_CORE_OBJ_DETAIL_GEN_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...