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 /* std::vector<bool> declares no data(), and libc++ and the MSVC STL do not \
141 declare it at all, so a using-declaration is ill-formed there. A member \
142 template is instantiated only when called: a bool sequence has no data(). */ \
143 template <typename E = elementtype> \
144 auto data() \
145 { \
146 return static_cast<std::vector<E>&>(*this).data(); \
147 } \
148 template <typename E = elementtype> \
149 auto data() const \
150 { \
151 return static_cast<const std::vector<E>&>(*this).data(); \
152 } \
153 \
154 using Parent::begin; \
155 using Parent::cbegin; \
156 using Parent::rbegin; \
157 using Parent::crbegin; \
158 using Parent::end; \
159 using Parent::cend; \
160 using Parent::rend; \
161 using Parent::crend; \
162 \
163 using Parent::empty; \
164 using Parent::size; \
165 using Parent::max_size; \
166 using Parent::reserve; \
167 using Parent::capacity; \
168 using Parent::shrink_to_fit; \
169 \
170 using Parent::clear; \
171 using Parent::insert; \
172 using Parent::emplace; \
173 using Parent::erase; \
174 using Parent::push_back; \
175 using Parent::emplace_back; \
176 using Parent::pop_back; \
177 using Parent::resize; \
178 using Parent::swap; \
179 \
180 constexpr const Parent& asVector() const& noexcept { return *this; } \
181 Parent asVector() && noexcept(std::is_nothrow_move_constructible_v<Parent>) { return std::move(*this); } \
182 \
183 friend bool operator==(const classname& lhs, const classname& rhs) { return lhs.asVector() == rhs.asVector(); } \
184 friend bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
185 };
188#define SEN_IMPL_GEN_BOUNDED_SEQUENCE(classname, element, maxSize) \
189 struct classname final: public ::sen::StaticVector<element, maxSize> \
190 { \
191 using Parent = ::sen::StaticVector<element, maxSize>; \
192 using Parent::Parent; \
193 using Parent::operator=; \
194 };
197#define SEN_IMPL_GEN_FIXED_SEQUENCE(classname, element, num_elements) \
198 class classname final: private std::array<element, num_elements> \
199 { \
200 using Parent = std::array<element, num_elements>; \
201 \
202 public: \
203 using Parent::value_type; \
204 using Parent::reference; \
205 using Parent::const_reference; \
206 using Parent::pointer; \
207 using Parent::const_pointer; \
208 using Parent::iterator; \
209 using Parent::const_iterator; \
210 using Parent::reverse_iterator; \
211 using Parent::const_reverse_iterator; \
212 \
213 using Parent::Parent; \
214 using Parent::operator=; \
215 \
216 using Parent::at; \
217 using Parent::operator[]; \
218 using Parent::front; \
219 using Parent::back; \
220 using Parent::data; \
221 \
222 using Parent::begin; \
223 using Parent::cbegin; \
224 using Parent::rbegin; \
225 using Parent::crbegin; \
226 using Parent::end; \
227 using Parent::cend; \
228 using Parent::rend; \
229 using Parent::crend; \
230 \
231 using Parent::empty; \
232 using Parent::size; \
233 using Parent::max_size; \
234 \
235 using Parent::fill; \
236 using Parent::swap; \
237 \
238 template <typename Y, typename = std::enable_if_t<std::is_convertible_v<Y, element>, int>> \
239 constexpr classname(std::initializer_list<Y> elems): Parent() \
240 { \
241 using DifferenceType = \
242 typename std::iterator_traits<typename std::initializer_list<Y>::iterator>::difference_type; \
243 SEN_ASSERT(std::distance(std::begin(elems), std::end(elems)) <= static_cast<DifferenceType>(num_elements)); \
244 std::move(std::begin(elems), \
245 std::next(std::begin(elems), \
246 std::min(std::distance(std::begin(elems), std::end(elems)), \
247 static_cast<DifferenceType>(num_elements))), \
248 std::begin(*this)); \
249 } \
250 \
251 constexpr const Parent& asArray() const& noexcept { return *this; } \
252 Parent asArray() && noexcept(std::is_nothrow_move_constructible_v<element>) { return std::move(*this); } \
253 \
254 friend bool operator==(const classname& lhs, const classname& rhs) { return lhs.asArray() == rhs.asArray(); } \
255 friend bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
256 };
259#define SEN_IMPL_GEN_STRUCT_OPERATORS(classname, doExport) \
260 SEN_MAYBE_EXPORT(doExport) std::ostream& operator<<(std::ostream& out, const classname& val); \
261 SEN_MAYBE_EXPORT(doExport) bool operator==(const classname& lhs, const classname& rhs); \
262 SEN_MAYBE_EXPORT(doExport) bool operator!=(const classname& lhs, const classname& rhs);
265#define SEN_IMPL_GEN_OPTIONAL(classname, elementtype) \
266 class classname final: private std::optional<elementtype> \
267 { \
268 using Parent = std::optional<elementtype>; \
269 \
270 public: \
271 using Parent::value_type; \
272 \
273 using Parent::Parent; \
274 using Parent::operator=; \
275 \
276 using Parent::operator->; \
277 using Parent::operator*; \
278 using Parent::operator bool; \
279 using Parent::has_value; \
280 using Parent::value; \
281 using Parent::value_or; \
282 \
283 using Parent::reset; \
284 using Parent::swap; \
285 using Parent::emplace; \
286 \
287 constexpr const Parent& asOptional() const& noexcept { return *this; } \
288 Parent asOptional() && noexcept(std::is_nothrow_move_constructible_v<Parent>) { return std::move(*this); } \
289 \
290 friend constexpr bool operator==(const classname& lhs, const classname& rhs) \
291 { \
292 return lhs.asOptional() == rhs.asOptional(); \
293 } \
294 friend constexpr bool operator!=(const classname& lhs, const classname& rhs) { return !(lhs == rhs); } \
295 };
298#define SEN_IMPL_GEN_OPTIONAL_TRAITS(classname, doExport) \
299 template <> \
300 struct MetaTypeTrait<classname> \
301 { \
302 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<OptionalType> meta(); \
303 }; \
304 template <> \
305 struct VariantTraits<classname>: private OptionalTraitsBase<classname> \
306 { \
307 using OptionalTraitsBase<classname>::valueToVariant; \
308 using OptionalTraitsBase<classname>::variantToValue; \
309 }; \
310 template <> \
311 struct SerializationTraits<classname>: private OptionalTraitsBase<classname> \
312 { \
313 using OptionalTraitsBase<classname>::write; \
314 using OptionalTraitsBase<classname>::read; \
315 using OptionalTraitsBase<classname>::serializedSize; \
316 using OptionalTraitsBase<classname>::toJsonString; \
317 using OptionalTraitsBase<classname>::fromJsonString; \
318 };
321#define SEN_IMPL_GEN_ENUM_TRAITS(classname, doExport) \
322 template <> \
323 struct MetaTypeTrait<classname> \
324 { \
325 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<EnumType> meta(); \
326 }; \
327 template <> \
328 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private EnumTraitsBase<classname> \
329 { \
330 using EnumTraitsBase<classname>::valueToVariant; \
331 using EnumTraitsBase<classname>::variantToValue; \
332 }; \
333 template <> \
334 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private EnumTraitsBase<classname> \
335 { \
336 using EnumTraitsBase<classname>::write; \
337 using EnumTraitsBase<classname>::read; \
338 using EnumTraitsBase<classname>::serializedSize; \
339 using EnumTraitsBase<classname>::toJsonString; \
340 using EnumTraitsBase<classname>::fromJsonString; \
341 }; \
342 template <> \
343 struct SEN_MAYBE_EXPORT(doExport) StringConversionTraits<classname> \
344 { \
345 [[nodiscard]] static std::string_view toString(classname val); \
346 [[nodiscard]] static classname fromString(std::string_view val); \
347 }; \
348 \
349 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) std::string_view toString(classname value);
352#define SEN_IMPL_GEN_SEQUENCE_TRAITS(classname, doExport) \
353 template <> \
354 struct MetaTypeTrait<classname> \
355 { \
356 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static sen::ConstTypeHandle<SequenceType> meta(); \
357 }; \
358 template <> \
359 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private SequenceTraitsBase<classname> \
360 { \
361 using SequenceTraitsBase<classname>::valueToVariant; \
362 using SequenceTraitsBase<classname>::variantToValue; \
363 }; \
364 template <> \
365 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private SequenceTraitsBase<classname> \
366 { \
367 using SequenceTraitsBase<classname>::write; \
368 using SequenceTraitsBase<classname>::read; \
369 using SequenceTraitsBase<classname>::serializedSize; \
370 using SequenceTraitsBase<classname>::toJsonString; \
371 using SequenceTraitsBase<classname>::fromJsonString; \
372 };
375#define SEN_IMPL_GEN_ARRAY_TRAITS(classname, doExport) \
376 template <> \
377 struct MetaTypeTrait<classname> \
378 { \
379 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<SequenceType> meta(); \
380 }; \
381 template <> \
382 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private ArrayTraitsBase<classname> \
383 { \
384 using ArrayTraitsBase<classname>::valueToVariant; \
385 using ArrayTraitsBase<classname>::variantToValue; \
386 }; \
387 template <> \
388 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private ArrayTraitsBase<classname> \
389 { \
390 using ArrayTraitsBase<classname>::write; \
391 using ArrayTraitsBase<classname>::read; \
392 using ArrayTraitsBase<classname>::serializedSize; \
393 using ArrayTraitsBase<classname>::toJsonString; \
394 using ArrayTraitsBase<classname>::fromJsonString; \
395 };
398#define SEN_IMPL_GEN_STRUCT_TRAITS(classname, doExport) \
399 template <> \
400 struct MetaTypeTrait<classname> \
401 { \
402 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<StructType> meta(); \
403 }; \
404 template <> \
405 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected StructTraitsBase \
406 { \
407 static void valueToVariant(const classname& val, Var& var); \
408 static void variantToValue(const Var& var, classname& val); \
409 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
410 }; \
411 template <> \
412 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected StructTraitsBase \
413 { \
414 static void write(OutputStream& out, const classname& val); \
415 static void read(InputStream& in, classname& val); \
416 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
417 static std::string toJsonString(const classname& val); \
418 static void fromJsonString(const std::string& str, classname& val); \
419 };
422#define SEN_IMPL_GEN_VARIANT_TRAITS(classname, doExport) \
423 template <> \
424 struct SEN_MAYBE_EXPORT(doExport) MetaTypeTrait<classname> \
425 { \
426 [[nodiscard]] static ConstTypeHandle<VariantType> meta(); \
427 }; \
428 template <> \
429 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: protected VariantTraitsBase<classname> \
430 { \
431 static void valueToVariant(const classname& val, Var& var); \
432 static void variantToValue(const Var& var, classname& val); \
433 static std::function<lang::Value(const void*)> getFieldValueGetterFunction(Span<uint16_t> fields); \
434 using VariantTraitsBase<classname>::tryPrintField; \
435 }; \
436 template <> \
437 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: protected VariantTraitsBase<classname> \
438 { \
439 static void write(OutputStream& out, const classname& val); \
440 static void read(InputStream& in, classname& val); \
441 [[nodiscard]] static uint32_t serializedSize(const classname& val) noexcept; \
442 static std::string toJsonString(const classname& val); \
443 static void fromJsonString(const std::string& str, classname& val); \
444 };
447#define SEN_IMPL_GEN_CLASS_META_TRAITS(classname, doExport) \
448 template <> \
449 struct MetaTypeTrait<classname> \
450 { \
451 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<ClassType> meta(); \
452 };
455#define SEN_IMPL_GEN_CLASS_TRAITS_TEMPLATE(interfaceName, remoteProxy, localProxy, baseName) \
456 template <> \
457 struct SenClassRelation<interfaceName> \
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 <> \
467 struct SenClassRelation<remoteProxy> \
468 { \
469 using InterfaceType = interfaceName; \
470 template <typename T> \
471 using BaseType = baseName<T>; \
472 static constexpr bool isBaseTypeTemplate = true; \
473 using RemoteProxyType = remoteProxy; \
474 using LocalProxyType = localProxy; \
475 }; \
476 template <> \
477 struct SenClassRelation<localProxy> \
478 { \
479 using InterfaceType = interfaceName; \
480 template <typename T> \
481 using BaseType = baseName<T>; \
482 static constexpr bool isBaseTypeTemplate = true; \
483 using RemoteProxyType = remoteProxy; \
484 using LocalProxyType = localProxy; \
485 }; \
486 template <typename T> \
487 struct SenClassRelation<baseName<T>> \
488 { \
489 using InterfaceType = interfaceName; \
490 template <typename U> \
491 using BaseType = baseName<U>; \
492 static constexpr bool isBaseTypeTemplate = true; \
493 using RemoteProxyType = remoteProxy; \
494 using LocalProxyType = localProxy; \
495 };
498#define SEN_IMPL_GEN_CLASS_TRAITS_NORMAL(interfaceName, remoteProxy, localProxy, baseName) \
499 template <> \
500 struct SenClassRelation<interfaceName> \
501 { \
502 using InterfaceType = interfaceName; \
503 using BaseType = baseName; \
504 static constexpr bool isBaseTypeTemplate = false; \
505 using RemoteProxyType = remoteProxy; \
506 using LocalProxyType = localProxy; \
507 }; \
508 template <> \
509 struct SenClassRelation<remoteProxy> \
510 { \
511 using InterfaceType = interfaceName; \
512 using BaseType = baseName; \
513 static constexpr bool isBaseTypeTemplate = false; \
514 using RemoteProxyType = remoteProxy; \
515 using LocalProxyType = localProxy; \
516 }; \
517 template <> \
518 struct SenClassRelation<localProxy> \
519 { \
520 using InterfaceType = interfaceName; \
521 using BaseType = baseName; \
522 static constexpr bool isBaseTypeTemplate = false; \
523 using RemoteProxyType = remoteProxy; \
524 using LocalProxyType = localProxy; \
525 }; \
526 template <> \
527 struct SenClassRelation<baseName> \
528 { \
529 using InterfaceType = interfaceName; \
530 using BaseType = baseName; \
531 static constexpr bool isBaseTypeTemplate = false; \
532 using RemoteProxyType = remoteProxy; \
533 using LocalProxyType = localProxy; \
534 };
537#define SEN_IMPL_GEN_QUANTITY_TRAITS(classname, maxVal, minVal, doExport) \
538 template <> \
539 struct MetaTypeTrait<classname> \
540 { \
541 [[nodiscard]] SEN_MAYBE_EXPORT(doExport) static ConstTypeHandle<QuantityType> meta(); \
542 }; \
543 template <> \
544 struct SEN_MAYBE_EXPORT(doExport) VariantTraits<classname>: private QuantityTraitsBase<classname> \
545 { \
546 using QuantityTraitsBase<classname>::valueToVariant; \
547 using QuantityTraitsBase<classname>::variantToValue; \
548 }; \
549 template <> \
550 struct SEN_MAYBE_EXPORT(doExport) SerializationTraits<classname>: private QuantityTraitsBase<classname> \
551 { \
552 using QuantityTraitsBase<classname>::write; \
553 using QuantityTraitsBase<classname>::read; \
554 using QuantityTraitsBase<classname>::serializedSize; \
555 using QuantityTraitsBase<classname>::toJsonString; \
556 using QuantityTraitsBase<classname>::fromJsonString; \
557 }; \
558 template <> \
559 struct SEN_MAYBE_EXPORT(doExport) QuantityTraits<classname> \
560 { \
561 using ValueType = typename classname::ValueType; \
562 \
563 static constexpr ValueType max = maxVal; \
564 static constexpr ValueType min = minVal; \
565 \
566 [[nodiscard]] static ::std::optional<const sen::Unit*> unit() noexcept; \
567 [[nodiscard]] static const sen::Unit& unit(sen::Unit::EnsureTag); \
568 };
569
570#endif // SEN_CORE_OBJ_DETAIL_GEN_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...