EmbeddedProto 4.0.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. EmbeddedProto/EmbeddedProto.py +122 -0
  2. EmbeddedProto/Features.py +255 -0
  3. EmbeddedProto/Field.py +1295 -0
  4. EmbeddedProto/Oneof.py +74 -0
  5. EmbeddedProto/ProtoFile.py +210 -0
  6. EmbeddedProto/TypeDefinitions.py +367 -0
  7. EmbeddedProto/__init__.py +0 -0
  8. EmbeddedProto/__main__.py +28 -0
  9. EmbeddedProto/config.py +173 -0
  10. EmbeddedProto/custom_header.py +513 -0
  11. EmbeddedProto/embedded_proto_options.proto +62 -0
  12. EmbeddedProto/embedded_proto_options_pb2.py +37 -0
  13. EmbeddedProto/field_options.py +344 -0
  14. EmbeddedProto/main.py +316 -0
  15. EmbeddedProto/src/EmbeddedProto/BytesStringCallback.h +476 -0
  16. EmbeddedProto/src/EmbeddedProto/Defines.h +118 -0
  17. EmbeddedProto/src/EmbeddedProto/EmptyArray.h +81 -0
  18. EmbeddedProto/src/EmbeddedProto/Errors.h +53 -0
  19. EmbeddedProto/src/EmbeddedProto/FieldStringBytes.h +793 -0
  20. EmbeddedProto/src/EmbeddedProto/Fields.h +832 -0
  21. EmbeddedProto/src/EmbeddedProto/Functional.h +412 -0
  22. EmbeddedProto/src/EmbeddedProto/MessageCallback.h +688 -0
  23. EmbeddedProto/src/EmbeddedProto/MessageInterface.h +792 -0
  24. EmbeddedProto/src/EmbeddedProto/MessageSizeCalculator.h +109 -0
  25. EmbeddedProto/src/EmbeddedProto/MessageState.h +207 -0
  26. EmbeddedProto/src/EmbeddedProto/ReadBufferFixedSize.h +204 -0
  27. EmbeddedProto/src/EmbeddedProto/ReadBufferInterface.h +181 -0
  28. EmbeddedProto/src/EmbeddedProto/ReadBufferSection.h +232 -0
  29. EmbeddedProto/src/EmbeddedProto/RepeatedField.h +1053 -0
  30. EmbeddedProto/src/EmbeddedProto/RepeatedFieldCallback.h +432 -0
  31. EmbeddedProto/src/EmbeddedProto/RepeatedFieldFixedSize.h +367 -0
  32. EmbeddedProto/src/EmbeddedProto/Version.h +39 -0
  33. EmbeddedProto/src/EmbeddedProto/WireFormatter.h +852 -0
  34. EmbeddedProto/src/EmbeddedProto/WriteBufferFixedSize.h +114 -0
  35. EmbeddedProto/src/EmbeddedProto/WriteBufferInterface.h +134 -0
  36. EmbeddedProto/src/EmbeddedProto.h +55 -0
  37. EmbeddedProto/templates/FieldBasic_Deserialize.h.jinja2 +26 -0
  38. EmbeddedProto/templates/FieldBasic_GetSet.h.jinja2 +89 -0
  39. EmbeddedProto/templates/FieldBytes_GetSet.h.jinja2 +86 -0
  40. EmbeddedProto/templates/FieldEnum_Deserialize.h.jinja2 +50 -0
  41. EmbeddedProto/templates/FieldEnum_GetSet.h.jinja2 +93 -0
  42. EmbeddedProto/templates/FieldErrorRecursive_GetSet.h.jinja2 +29 -0
  43. EmbeddedProto/templates/FieldMap_GetSet.h.jinja2 +203 -0
  44. EmbeddedProto/templates/FieldMsg_Deserialize.h.jinja2 +44 -0
  45. EmbeddedProto/templates/FieldMsg_GetSet.h.jinja2 +93 -0
  46. EmbeddedProto/templates/FieldRepeated_GetSet.h.jinja2 +94 -0
  47. EmbeddedProto/templates/FieldString_GetSet.h.jinja2 +86 -0
  48. EmbeddedProto/templates/Field_DeserializePartial.h.jinja2 +33 -0
  49. EmbeddedProto/templates/Field_Serialize.h.jinja2 +112 -0
  50. EmbeddedProto/templates/Field_SerializePartial.h.jinja2 +119 -0
  51. EmbeddedProto/templates/Header.h.jinja2 +108 -0
  52. EmbeddedProto/templates/TypeDefEnum.h.jinja2 +45 -0
  53. EmbeddedProto/templates/TypeDefMsg.h.jinja2 +791 -0
  54. EmbeddedProto/templates/TypeOneof.h.jinja2 +178 -0
  55. EmbeddedProto/version.json +3 -0
  56. embeddedproto-4.0.0b1.dist-info/METADATA +141 -0
  57. embeddedproto-4.0.0b1.dist-info/RECORD +63 -0
  58. embeddedproto-4.0.0b1.dist-info/WHEEL +5 -0
  59. embeddedproto-4.0.0b1.dist-info/entry_points.txt +3 -0
  60. embeddedproto-4.0.0b1.dist-info/licenses/LICENSE +30 -0
  61. embeddedproto-4.0.0b1.dist-info/licenses/LICENSES/GPL-3.0-only.txt +674 -0
  62. embeddedproto-4.0.0b1.dist-info/licenses/LICENSES/LicenseRef-EmbeddedProto-Commercial.txt +12 -0
  63. embeddedproto-4.0.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,832 @@
1
+ /*
2
+ * Copyright (C) 2020-2026 Embedded AMS B.V. - All Rights Reserved
3
+ *
4
+ * This file is part of Embedded Proto.
5
+ *
6
+ * Embedded Proto is dual licensed. You may use it under the terms of the
7
+ * GNU General Public License version 3 (GPLv3) as published by the Free
8
+ * Software Foundation, or under a commercial license from Embedded AMS B.V.
9
+ *
10
+ * Under the GPLv3 you must release the source code of
11
+ * any product you distribute that includes Embedded Proto or code
12
+ * generated by it. A commercial license removes that obligation.
13
+ * See <https://embeddedproto.com/pricing/>.
14
+ *
15
+ * Embedded Proto is distributed WITHOUT ANY WARRANTY; without even the
16
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
+ * See the LICENSE file in the root of the repository for details.
18
+ *
19
+ * SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-EmbeddedProto-Commercial
20
+ *
21
+ * Embedded AMS B.V., Hoorn, the Netherlands - info at EmbeddedProto dot com
22
+ */
23
+
24
+ #ifndef _FIELDS_H_
25
+ #define _FIELDS_H_
26
+
27
+ #include "Errors.h"
28
+ #include "Defines.h"
29
+ #include "WireFormatter.h"
30
+ #include "WriteBufferInterface.h"
31
+ #include "ReadBufferInterface.h"
32
+ #include "MessageSizeCalculator.h"
33
+ #include "MessageState.h"
34
+
35
+ #include <cstdint>
36
+
37
+ #ifdef MSG_TO_STRING
38
+ #include <cstdio>
39
+ #endif
40
+
41
+ namespace EmbeddedProto
42
+ {
43
+
44
+ class Field
45
+ {
46
+ public:
47
+ enum class FieldTypes
48
+ {
49
+ int32,
50
+ int64,
51
+ uint32,
52
+ uint64,
53
+ sint32,
54
+ sint64,
55
+ boolean,
56
+ enumeration,
57
+ fixed64,
58
+ sfixed64,
59
+ doublefixed,
60
+ string,
61
+ bytes,
62
+ message,
63
+ repeated,
64
+ fixed32,
65
+ sfixed32,
66
+ floatfixed
67
+ };
68
+
69
+
70
+ Field() = default;
71
+
72
+ #ifdef VIRTUAL_DESTRUCTORS_ENABLED
73
+ virtual ~Field() = default;
74
+ #else
75
+ protected:
76
+ //! Protected and non-virtual by default, see VIRTUAL_DESTRUCTORS_ENABLED in Defines.h.
77
+ ~Field() = default;
78
+
79
+ public:
80
+ #endif
81
+
82
+ virtual Error serialize(WriteBufferInterface& buffer) const = 0;
83
+
84
+ //! Deserialize this field from the bytes in the given buffer.
85
+ /*!
86
+ \param buffer data from which the field should be obtained.
87
+ \return NO_ERROR if everything went ok.
88
+ */
89
+ virtual Error deserialize(ReadBufferInterface& buffer) = 0;
90
+
91
+ //! Deserialize this field but also check if given wire type matches the field type.
92
+ /*!
93
+ \param buffer data from which the field should be obtained.
94
+ \param wire_type The wire type obtained from the tag used to match with this field type.
95
+ \return NO_ERROR if everything went ok.
96
+ */
97
+ virtual Error deserialize_check_type(ReadBufferInterface& buffer,
98
+ const ::EmbeddedProto::WireFormatter::WireType& wire_type) = 0;
99
+
100
+ #ifdef PARTIAL_SERIALIZATION_ENABLED
101
+ //! Serialize this field with partial state support for LENGTH_DELIMITED fields.
102
+ /*!
103
+ Handles TAG->SIZE->DATA state machine for length-delimited field types.
104
+ \param field_number The field number for the tag.
105
+ \param buffer The buffer to write to.
106
+ \param state External state object tracking serialization progress.
107
+ \param optional If true, serialize even if size is zero.
108
+ \return NO_ERRORS if successful, BUFFER_FULL if buffer full.
109
+ */
110
+ virtual Error serialize_partial_as_field(uint32_t field_number,
111
+ WriteBufferInterface& buffer,
112
+ MessageState& state,
113
+ bool optional) const = 0;
114
+
115
+ //! Deserialize this field with partial state support for LENGTH_DELIMITED fields.
116
+ /*!
117
+ Handles SIZE->DATA state machine for length-delimited field types.
118
+ \param buffer The buffer to read from.
119
+ \param state External state object tracking deserialization progress.
120
+ \return NO_ERRORS when field reaches COMPLETE.
121
+ */
122
+ virtual Error deserialize_partial_as_field(ReadBufferInterface& buffer,
123
+ MessageState& state) = 0;
124
+ #endif
125
+
126
+ //! Calculate the size of this message when serialized.
127
+ /*!
128
+ \return The number of bytes this message will require once serialized.
129
+ */
130
+ uint32_t serialized_size() const;
131
+
132
+ //! Serialize this field with LENGTH_DELIMITED wire type.
133
+ /*!
134
+ Writes [tag][size][data] to the buffer.
135
+ \param field_number The field number for the tag.
136
+ \param size The size of the data to be written.
137
+ \param buffer The buffer to write to.
138
+ \param optional If true, serialize even if size is zero.
139
+ \return NO_ERRORS if successful.
140
+ */
141
+ Error serialize_len(const uint32_t field_number,
142
+ const uint32_t size,
143
+ WriteBufferInterface& buffer,
144
+ const bool optional) const;
145
+
146
+ //! Serialize this field with VARINT, FIXED32, or FIXED64 wire type.
147
+ /*!
148
+ Writes [tag][data] to the buffer.
149
+ \param field_number The field number for the tag.
150
+ \param wire_type The wire type (VARINT, FIXED32, or FIXED64).
151
+ \param is_default True if the field has its default value.
152
+ \param buffer The buffer to write to.
153
+ \param optional If true, serialize even if value is default.
154
+ \return NO_ERRORS if successful.
155
+ */
156
+ Error serialize_scalar(const uint32_t field_number,
157
+ const WireFormatter::WireType wire_type,
158
+ const bool is_default,
159
+ WriteBufferInterface& buffer,
160
+ const bool optional) const;
161
+
162
+ //! Reset the field to it's initial value.
163
+ virtual void clear() = 0;
164
+
165
+ protected:
166
+
167
+ #ifdef PARTIAL_SERIALIZATION_ENABLED
168
+ //! Helper method for TAG and SIZE phases of partial serialization.
169
+ /*!
170
+ Handles the shared TAG and SIZE phases for LENGTH_DELIMITED fields.
171
+ \param field_number The field number for the tag.
172
+ \param size The size of the data to be written.
173
+ \param buffer The buffer to write to.
174
+ \param state External state object tracking serialization progress.
175
+ \param optional If true, serialize even if size is zero.
176
+ \return NO_ERRORS when phase reaches DATA, other errors on failure.
177
+ */
178
+ Error serialize_partial_tag_and_size(uint32_t field_number,
179
+ uint32_t size,
180
+ WriteBufferInterface& buffer,
181
+ MessageState& state,
182
+ bool optional) const;
183
+
184
+ //! Helper method for SIZE phase of partial deserialization.
185
+ /*!
186
+ Handles the shared SIZE phase for LENGTH_DELIMITED fields.
187
+ \param buffer The buffer to read from.
188
+ \param state External state object tracking deserialization progress.
189
+ \return NO_ERRORS when phase reaches DATA, other errors on failure.
190
+ */
191
+ Error deserialize_partial_size_phase(ReadBufferInterface& buffer,
192
+ MessageState& state) const;
193
+ #endif
194
+
195
+ #ifdef MSG_TO_STRING
196
+ //! Write all the data in this field to a human readable string.
197
+ /*!
198
+ \param str A string view object with a pointer to the current location in the character array and its maximum length.
199
+ \param indent_level How many spaces should be added before this field.
200
+ \param name A pointer to the name of this field.
201
+ \param first_field A boolean indicating if this is the first field we serialize in this message.
202
+ \return A string view struct detail how many bytes are left in the string and a pointer to the point where to continue.
203
+ */
204
+ virtual ::EmbeddedProto::string_view to_string(::EmbeddedProto::string_view& str, const uint32_t indent_level, char const* name, const bool first_field) const = 0;
205
+
206
+ #endif // End of MSG_TO_STRING
207
+ };
208
+
209
+ template<Field::FieldTypes FIELDTYPE, class VARIABLE_TYPE, WireFormatter::WireType WIRETYPE, uint32_t MAX_SER_SIZE>
210
+ class FieldTemplate
211
+ {
212
+ public:
213
+ using TYPE = VARIABLE_TYPE;
214
+ using CLASS_TYPE = FieldTemplate<FIELDTYPE, VARIABLE_TYPE, WIRETYPE, MAX_SER_SIZE>;
215
+
216
+ FieldTemplate() = default;
217
+ FieldTemplate(const VARIABLE_TYPE& v) : value_(v) { };
218
+ FieldTemplate(const VARIABLE_TYPE&& v) : value_(v) { };
219
+ FieldTemplate(const CLASS_TYPE& ft) : value_(ft.value_) { };
220
+
221
+ ~FieldTemplate() = default;
222
+
223
+ inline Error serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer, const bool optional) const
224
+ {
225
+ Error return_value = Error::NO_ERRORS;
226
+
227
+ // For non-optional fields, skip serialization if value equals default (zero/false)
228
+ if(optional || (static_cast<VARIABLE_TYPE>(0) != value_))
229
+ {
230
+ return_value = WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WIRETYPE), buffer);
231
+ if(Error::NO_ERRORS == return_value)
232
+ {
233
+ return_value = serialize(buffer);
234
+ }
235
+ }
236
+
237
+ return return_value;
238
+ }
239
+
240
+ #ifdef PARTIAL_SERIALIZATION_ENABLED
241
+ inline Error serialize_partial_with_id(uint32_t field_number,
242
+ WriteBufferInterface& buffer,
243
+ MessageState& state,
244
+ bool optional) const
245
+ {
246
+ Error return_value = Error::NO_ERRORS;
247
+
248
+ // For non-optional fields, skip serialization if value equals default (zero/false)
249
+ if(optional || (static_cast<VARIABLE_TYPE>(0) != value_))
250
+ {
251
+ if(::EmbeddedProto::FieldProcessingPhase::TAG == state.phase)
252
+ {
253
+ // For scalar fields, we need atomic write of tag+value
254
+ // Check if we have enough space for both tag and value
255
+ const uint32_t tag_size = WireFormatter::VarintSize(WireFormatter::MakeTag(field_number, WIRETYPE));
256
+ const uint32_t value_size = this->serialized_size();
257
+ const uint32_t required_space = tag_size + value_size;
258
+
259
+ // The tag and value are written as a pair on the strength of this check, see
260
+ // WriteBufferInterface::get_available_size(). When the pair does not fit the
261
+ // state is left untouched so the retry starts at the tag again.
262
+ if(buffer.get_available_size() < required_space)
263
+ {
264
+ return_value = Error::BUFFER_FULL;
265
+ }
266
+ else
267
+ {
268
+ return_value = WireFormatter::SerializeVarint(WireFormatter::MakeTag(field_number, WIRETYPE), buffer);
269
+ if(Error::NO_ERRORS == return_value)
270
+ {
271
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::DATA;
272
+ }
273
+ else
274
+ {
275
+ // Defensive: a conforming buffer accepts the tag after the space check
276
+ // above. The state is untouched so a retry starts at the tag again.
277
+ return_value = Error::BUFFER_FULL;
278
+ }
279
+ }
280
+ }
281
+
282
+ if((Error::NO_ERRORS == return_value) && (::EmbeddedProto::FieldProcessingPhase::DATA == state.phase))
283
+ {
284
+ // Write value - this should always fit if TAG phase succeeded
285
+ return_value = serialize(buffer);
286
+ if(Error::NO_ERRORS == return_value)
287
+ {
288
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::COMPLETE;
289
+ }
290
+ else
291
+ {
292
+ // Defensive: a conforming buffer accepts the value after the space check
293
+ // in the TAG phase, so this branch cannot be reached. Should a buffer
294
+ // over-report its free space the tag is already in the buffer and cannot
295
+ // be taken back, a retry from the TAG phase writes it a second time.
296
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::TAG;
297
+ return_value = Error::BUFFER_FULL;
298
+ }
299
+ }
300
+ }
301
+ else
302
+ {
303
+ // Field has default value - skip to complete
304
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::COMPLETE;
305
+ }
306
+
307
+ return return_value;
308
+ }
309
+ #endif
310
+
311
+ #ifdef PARTIAL_SERIALIZATION_ENABLED
312
+ //! Deserialize scalar/enum with expected wire type in partial mode.
313
+ inline Error deserialize_partial_check_type(ReadBufferInterface& buffer,
314
+ MessageState& state,
315
+ const ::EmbeddedProto::WireFormatter::WireType expected_wire_type)
316
+ {
317
+ Error return_value = Error::NO_ERRORS;
318
+
319
+ if(::EmbeddedProto::FieldProcessingPhase::DATA != state.phase)
320
+ {
321
+ return_value = Error::STATE_MISMATCH;
322
+ }
323
+ else if(expected_wire_type != state.wire_type)
324
+ {
325
+ return_value = Error::INVALID_WIRETYPE;
326
+ }
327
+ else
328
+ {
329
+ return_value = deserialize(buffer);
330
+ if(Error::NO_ERRORS == return_value)
331
+ {
332
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::COMPLETE;
333
+ }
334
+ }
335
+
336
+ return return_value;
337
+ }
338
+ #endif
339
+
340
+ inline Error serialize(WriteBufferInterface& buffer) const
341
+ {
342
+ return serialize_<FIELDTYPE>(buffer);
343
+ }
344
+
345
+ inline Error deserialize(ReadBufferInterface& buffer)
346
+ {
347
+ return deserialize_<FIELDTYPE>(buffer);
348
+ }
349
+
350
+ //! \see Field::deserialize()
351
+ inline Error deserialize_check_type(ReadBufferInterface& buffer,
352
+ const ::EmbeddedProto::WireFormatter::WireType& wire_type)
353
+ {
354
+ Error return_value = WIRETYPE == wire_type ? Error::NO_ERRORS : Error::INVALID_WIRETYPE;
355
+ if(Error::NO_ERRORS == return_value)
356
+ {
357
+ return_value = deserialize(buffer);
358
+ }
359
+ return return_value;
360
+ }
361
+
362
+ inline void set(const VARIABLE_TYPE& v) { value_ = v; }
363
+ inline void set(const VARIABLE_TYPE&& v) { value_ = v; }
364
+
365
+ inline void set(const CLASS_TYPE& ft) { value_ = ft.value_; }
366
+ inline void set(const CLASS_TYPE&& ft) { value_ = ft.value_; }
367
+
368
+ inline CLASS_TYPE& operator=(const VARIABLE_TYPE& v)
369
+ {
370
+ value_ = v;
371
+ return *this;
372
+ }
373
+ inline CLASS_TYPE& operator=(const VARIABLE_TYPE&& v)
374
+ {
375
+ value_ = v;
376
+ return *this;
377
+ }
378
+ inline CLASS_TYPE& operator=(const CLASS_TYPE& ft)
379
+ {
380
+ value_ = ft.value_;
381
+ return *this;
382
+ }
383
+ inline CLASS_TYPE& operator=(const CLASS_TYPE&& ft) noexcept
384
+ {
385
+ value_ = ft.value_;
386
+ return *this;
387
+ }
388
+
389
+ inline const VARIABLE_TYPE& get() const { return value_; }
390
+ inline VARIABLE_TYPE& get() { return value_; }
391
+
392
+ //! This is the conversion operator.
393
+ /*!
394
+ Sonar would like this to be explicit but this is not practial in normal usage with other
395
+ integer and floating point types.
396
+ */
397
+ inline operator VARIABLE_TYPE() const { return value_; } //NOSONAR
398
+
399
+ inline bool operator==(const VARIABLE_TYPE& rhs) { return value_ == rhs; }
400
+ inline bool operator!=(const VARIABLE_TYPE& rhs) { return value_ != rhs; }
401
+ inline bool operator>(const VARIABLE_TYPE& rhs) { return value_ > rhs; }
402
+ inline bool operator<(const VARIABLE_TYPE& rhs) { return value_ < rhs; }
403
+ inline bool operator>=(const VARIABLE_TYPE& rhs) { return value_ >= rhs; }
404
+ inline bool operator<=(const VARIABLE_TYPE& rhs) { return value_ <= rhs; }
405
+
406
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
407
+ inline bool operator==(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ == rhs.get(); }
408
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
409
+ inline bool operator!=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ != rhs.get(); }
410
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
411
+ inline bool operator>(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ > rhs.get(); }
412
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
413
+ inline bool operator<(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ < rhs.get(); }
414
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
415
+ inline bool operator>=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ >= rhs.get(); }
416
+ template<Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS, WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS>
417
+ inline bool operator<=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS>& rhs) { return value_ <= rhs.get(); }
418
+
419
+ inline void clear() { value_ = static_cast<VARIABLE_TYPE>(0); }
420
+
421
+ inline uint32_t serialized_size() const
422
+ {
423
+ ::EmbeddedProto::MessageSizeCalculator calcBuffer;
424
+ this->serialize(calcBuffer);
425
+ return calcBuffer.get_size();
426
+ }
427
+
428
+ //! When serialized with the most unfavrouble value how much bytes does this field need.
429
+ /*!
430
+ This function takes into account the field number and tag combination.
431
+ \param[in] field_number We need to include the field number. This because large field numbers require more bytes.
432
+ \return The number of bytes required at most.
433
+ */
434
+ static inline constexpr uint32_t max_serialized_size(const uint32_t field_number)
435
+ {
436
+ return MAX_SER_SIZE + WireFormatter::VarintSize(WireFormatter::MakeTag(field_number, WIRETYPE));
437
+ }
438
+
439
+ static inline constexpr uint32_t max_serialized_size()
440
+ {
441
+ return MAX_SER_SIZE;
442
+ }
443
+
444
+ #ifdef MSG_TO_STRING
445
+
446
+ //! Write all the data in this field to a human readable string.
447
+ /*!
448
+ \param str A string view object with a pointer to the current location in the character array and its maximum length.
449
+ \param indent_level How many spaces should be added before this field.
450
+ \param name A pointer to the name of this field.
451
+ \param first_field A boolean indicating if this is the first field we serialize in this message.
452
+ \return A string view struct detail how many bytes are left in the string and a pointer to the point where to continue.
453
+ */
454
+ ::EmbeddedProto::string_view to_string(::EmbeddedProto::string_view& str, const uint32_t indent_level, char const* name, const bool first_field) const
455
+ {
456
+ ::EmbeddedProto::string_view left_chars = str;
457
+ int32_t n_chars_used = 0;
458
+
459
+ if(!first_field)
460
+ {
461
+ // Add a comma behind the previous field.
462
+ n_chars_used = snprintf(left_chars.data, left_chars.size, ",\n");
463
+ if(0 < n_chars_used) {
464
+ // Update the character pointer and characters left in the array.
465
+ const int32_t actual_chars_used = EmbeddedProto::min(n_chars_used, left_chars.size);
466
+ left_chars.data += actual_chars_used;
467
+ left_chars.size -= actual_chars_used;
468
+ }
469
+ }
470
+
471
+ n_chars_used = 0;
472
+
473
+ if(nullptr != name)
474
+ {
475
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%*s\"%s\": ", indent_level, " ", name);
476
+ }
477
+ else
478
+ {
479
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%*s", indent_level, " ");
480
+ }
481
+
482
+ if(0 < n_chars_used)
483
+ {
484
+ // Update the character pointer and characters left in the array.
485
+ const int32_t actual_chars_used = EmbeddedProto::min(n_chars_used, left_chars.size);
486
+ left_chars.data += actual_chars_used;
487
+ left_chars.size -= actual_chars_used;
488
+ }
489
+
490
+ if constexpr((Field::FieldTypes::int32 == FIELDTYPE) ||
491
+ (Field::FieldTypes::sint32 == FIELDTYPE) ||
492
+ (Field::FieldTypes::sfixed32 == FIELDTYPE))
493
+ {
494
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%d", get());
495
+ }
496
+ else if constexpr((Field::FieldTypes::int64 == FIELDTYPE) ||
497
+ (Field::FieldTypes::sint64 == FIELDTYPE) ||
498
+ (Field::FieldTypes::sfixed64 == FIELDTYPE))
499
+ {
500
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%ld", get());
501
+ }
502
+ else if constexpr((Field::FieldTypes::uint32 == FIELDTYPE) ||
503
+ (Field::FieldTypes::fixed32 == FIELDTYPE))
504
+ {
505
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%u", get());
506
+ }
507
+ else if constexpr((Field::FieldTypes::uint64 == FIELDTYPE) ||
508
+ (Field::FieldTypes::fixed64 == FIELDTYPE))
509
+ {
510
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%lu", get());
511
+ }
512
+ else if constexpr(Field::FieldTypes::boolean == FIELDTYPE)
513
+ {
514
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%s",
515
+ get() ? "true" : "false");
516
+ }
517
+ else if constexpr(Field::FieldTypes::enumeration == FIELDTYPE)
518
+ {
519
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%d",
520
+ static_cast<uint32_t>(get()));
521
+ }
522
+ else if constexpr(Field::FieldTypes::floatfixed == FIELDTYPE)
523
+ {
524
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%f", get());
525
+ }
526
+ else if constexpr(Field::FieldTypes::doublefixed == FIELDTYPE)
527
+ {
528
+ n_chars_used = snprintf(left_chars.data, left_chars.size, "%lf", get());
529
+ }
530
+ else
531
+ {
532
+ // Should never get here.
533
+ }
534
+
535
+ if(0 < n_chars_used) {
536
+ // Update the character pointer and characters left in the array.
537
+ const int32_t actual_chars_used = EmbeddedProto::min(n_chars_used, left_chars.size);
538
+ left_chars.data += actual_chars_used;
539
+ left_chars.size -= actual_chars_used;
540
+ }
541
+
542
+ return left_chars;
543
+ }
544
+
545
+ #endif // End of MSG_TO_STRING
546
+
547
+ private:
548
+
549
+ VARIABLE_TYPE value_;
550
+
551
+
552
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::int32 == SER_FIELDTYPE, bool>::type = true>
553
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(static_cast<uint32_t>(get()), buffer); }
554
+
555
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::int64 == SER_FIELDTYPE, bool>::type = true>
556
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(static_cast<uint64_t>(get()), buffer); }
557
+
558
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::uint32 == SER_FIELDTYPE, bool>::type = true>
559
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(get(), buffer); }
560
+
561
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::uint64 == SER_FIELDTYPE, bool>::type = true>
562
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(get(), buffer); }
563
+
564
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sint32 == SER_FIELDTYPE, bool>::type = true>
565
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(WireFormatter::ZigZagEncode(get()), buffer); }
566
+
567
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sint64 == SER_FIELDTYPE, bool>::type = true>
568
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(WireFormatter::ZigZagEncode(get()), buffer); }
569
+
570
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::boolean == SER_FIELDTYPE, bool>::type = true>
571
+ inline Error serialize_(WriteBufferInterface& buffer) const
572
+ {
573
+ const uint8_t byte = get() ? 0x01 : 0x00;
574
+ return buffer.push(byte) ? Error::NO_ERRORS : Error::BUFFER_FULL;
575
+ }
576
+
577
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::enumeration == SER_FIELDTYPE, bool>::type = true>
578
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeVarint(static_cast<uint32_t>(get()), buffer); }
579
+
580
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::fixed32 == SER_FIELDTYPE, bool>::type = true>
581
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeFixedNoTag(get(), buffer); }
582
+
583
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::fixed64 == SER_FIELDTYPE, bool>::type = true>
584
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerializeFixedNoTag(get(), buffer); }
585
+
586
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sfixed32 == SER_FIELDTYPE, bool>::type = true>
587
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerialzieSFixedNoTag(get(), buffer); }
588
+
589
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sfixed64 == SER_FIELDTYPE, bool>::type = true>
590
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerialzieSFixedNoTag(get(), buffer); }
591
+
592
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::floatfixed == SER_FIELDTYPE, bool>::type = true>
593
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerialzieFloatNoTag(get(), buffer); }
594
+
595
+ template<Field::FieldTypes SER_FIELDTYPE, typename std::enable_if<Field::FieldTypes::doublefixed == SER_FIELDTYPE, bool>::type = true>
596
+ inline Error serialize_(WriteBufferInterface& buffer) const { return WireFormatter::SerialzieDoubleNoTag(get(), buffer); }
597
+
598
+
599
+
600
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::int32 == DES_FIELDTYPE, bool>::type = true>
601
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeInt(buffer, get()); }
602
+
603
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::int64 == DES_FIELDTYPE, bool>::type = true>
604
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeInt(buffer, get()); }
605
+
606
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::uint32 == DES_FIELDTYPE, bool>::type = true>
607
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeUInt(buffer, get()); }
608
+
609
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::uint64 == DES_FIELDTYPE, bool>::type = true>
610
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeUInt(buffer, get()); }
611
+
612
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sint32 == DES_FIELDTYPE, bool>::type = true>
613
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeSInt(buffer, get()); }
614
+
615
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sint64 == DES_FIELDTYPE, bool>::type = true>
616
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeSInt(buffer, get()); }
617
+
618
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::boolean == DES_FIELDTYPE, bool>::type = true>
619
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeBool(buffer, get()); }
620
+
621
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::enumeration == DES_FIELDTYPE, bool>::type = true>
622
+ inline Error deserialize_(ReadBufferInterface& buffer)
623
+ {
624
+ uint32_t value = 0;
625
+ const Error return_value = WireFormatter::DeserializeVarint(buffer, value);
626
+ if(::EmbeddedProto::Error::NO_ERRORS == return_value)
627
+ {
628
+ value_ = static_cast<VARIABLE_TYPE>(value);
629
+ }
630
+ return return_value;
631
+ }
632
+
633
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::fixed32 == DES_FIELDTYPE, bool>::type = true>
634
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeFixed(buffer, get()); }
635
+
636
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::fixed64 == DES_FIELDTYPE, bool>::type = true>
637
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeFixed(buffer, get()); }
638
+
639
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sfixed32 == DES_FIELDTYPE, bool>::type = true>
640
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeSFixed(buffer, get()); }
641
+
642
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::sfixed64 == DES_FIELDTYPE, bool>::type = true>
643
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeSFixed(buffer, get()); }
644
+
645
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::floatfixed == DES_FIELDTYPE, bool>::type = true>
646
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeFloat(buffer, get()); }
647
+
648
+ template<Field::FieldTypes DES_FIELDTYPE, typename std::enable_if<Field::FieldTypes::doublefixed == DES_FIELDTYPE, bool>::type = true>
649
+ inline Error deserialize_(ReadBufferInterface& buffer) { return WireFormatter::DeserializeDouble(buffer, get()); }
650
+
651
+ };
652
+
653
+
654
+ using int32 = FieldTemplate<Field::FieldTypes::int32, int32_t, WireFormatter::WireType::VARINT, 5>;
655
+ using int64 = FieldTemplate<Field::FieldTypes::int64, int64_t, WireFormatter::WireType::VARINT, 10>;
656
+ using uint32 = FieldTemplate<Field::FieldTypes::uint32, uint32_t, WireFormatter::WireType::VARINT, 5>;
657
+ using uint64 = FieldTemplate<Field::FieldTypes::uint64, uint64_t, WireFormatter::WireType::VARINT, 10>;
658
+ using sint32 = FieldTemplate<Field::FieldTypes::sint32, int32_t, WireFormatter::WireType::VARINT, 5>;
659
+ using sint64 = FieldTemplate<Field::FieldTypes::sint64, int64_t, WireFormatter::WireType::VARINT, 10>;
660
+ using boolean = FieldTemplate<Field::FieldTypes::boolean, bool, WireFormatter::WireType::VARINT, 1>;
661
+ using fixed32 = FieldTemplate<Field::FieldTypes::fixed32, uint32_t, WireFormatter::WireType::FIXED32, 4>;
662
+ using fixed64 = FieldTemplate<Field::FieldTypes::fixed64, uint64_t, WireFormatter::WireType::FIXED64, 8>;
663
+ using sfixed32 = FieldTemplate<Field::FieldTypes::sfixed32, int32_t, WireFormatter::WireType::FIXED32, 4>;
664
+ using sfixed64 = FieldTemplate<Field::FieldTypes::sfixed64, int64_t, WireFormatter::WireType::FIXED64, 8>;
665
+ using floatfixed = FieldTemplate<Field::FieldTypes::floatfixed, float, WireFormatter::WireType::FIXED32, 4>;
666
+ using doublefixed = FieldTemplate<Field::FieldTypes::doublefixed, double, WireFormatter::WireType::FIXED64, 8>;
667
+
668
+ template<class ENUM_TYPE, uint32_t MAX_SER_SIZE>
669
+ using enumeration = FieldTemplate<Field::FieldTypes::enumeration, ENUM_TYPE, WireFormatter::WireType::VARINT, MAX_SER_SIZE>;
670
+
671
+
672
+ // Definitions of the member functions declared above. The library is header only, so they are
673
+ // inline.
674
+
675
+ inline uint32_t Field::serialized_size() const
676
+ {
677
+ ::EmbeddedProto::MessageSizeCalculator calcBuffer;
678
+ this->serialize(calcBuffer);
679
+ return calcBuffer.get_size();
680
+ }
681
+
682
+ #ifdef PARTIAL_SERIALIZATION_ENABLED
683
+ inline Error Field::serialize_partial_tag_and_size(uint32_t field_number,
684
+ uint32_t size,
685
+ WriteBufferInterface& buffer,
686
+ MessageState& state,
687
+ bool optional) const
688
+ {
689
+ static_cast<void>(optional);
690
+ Error return_value = Error::NO_ERRORS;
691
+
692
+ if(::EmbeddedProto::FieldProcessingPhase::TAG == state.phase)
693
+ {
694
+ // Check if we have enough space for both tag and size atomically
695
+ const uint32_t tag = WireFormatter::MakeTag(field_number, WireFormatter::WireType::LENGTH_DELIMITED);
696
+ const uint32_t tag_size = WireFormatter::VarintSize(tag);
697
+ const uint32_t size_size = WireFormatter::VarintSize(size);
698
+ const uint32_t required_space = tag_size + size_size;
699
+
700
+ if(buffer.get_available_size() < required_space)
701
+ {
702
+ // Not enough space for atomic tag+size operation - rollback
703
+ return_value = Error::BUFFER_FULL;
704
+ return return_value;
705
+ }
706
+
707
+ // Write tag
708
+ return_value = WireFormatter::SerializeVarint(tag, buffer);
709
+ if(Error::NO_ERRORS == return_value)
710
+ {
711
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::SIZE;
712
+ }
713
+ }
714
+
715
+ if((Error::NO_ERRORS == return_value) && (::EmbeddedProto::FieldProcessingPhase::SIZE == state.phase))
716
+ {
717
+ // Write size
718
+ return_value = WireFormatter::SerializeVarint(size, buffer);
719
+ if(Error::NO_ERRORS == return_value)
720
+ {
721
+ state.bytes_remaining = size;
722
+ // For empty fields, skip DATA phase and go directly to COMPLETE
723
+ if(0 == size)
724
+ {
725
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::COMPLETE;
726
+ }
727
+ else
728
+ {
729
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::DATA;
730
+ }
731
+ }
732
+ }
733
+
734
+ return return_value;
735
+ }
736
+
737
+ inline Error Field::deserialize_partial_size_phase(ReadBufferInterface& buffer,
738
+ MessageState& state) const
739
+ {
740
+ Error return_value = Error::NO_ERRORS;
741
+
742
+ if(::EmbeddedProto::FieldProcessingPhase::SIZE == state.phase)
743
+ {
744
+ uint32_t size = 0;
745
+ return_value = WireFormatter::DeserializeVarint(buffer, size);
746
+ if(Error::NO_ERRORS == return_value)
747
+ {
748
+ state.size_value = size;
749
+ state.bytes_remaining = size;
750
+ state.phase = ::EmbeddedProto::FieldProcessingPhase::DATA;
751
+ }
752
+ else
753
+ {
754
+ // Keep state unchanged on incomplete/failed size decode.
755
+ }
756
+ }
757
+ else
758
+ {
759
+ return_value = Error::STATE_MISMATCH;
760
+ }
761
+
762
+ return return_value;
763
+ }
764
+ #endif
765
+
766
+ inline Error Field::serialize_len(const uint32_t field_number,
767
+ const uint32_t size,
768
+ WriteBufferInterface& buffer,
769
+ const bool optional) const
770
+ {
771
+ Error return_value = Error::NO_ERRORS;
772
+
773
+ // Skip serializing empty fields for non-optional fields (proto3 default behavior)
774
+ if(optional || (0 != size))
775
+ {
776
+ return_value = WireFormatter::SerializeVarint(
777
+ WireFormatter::MakeTag(field_number, WireFormatter::WireType::LENGTH_DELIMITED),
778
+ buffer);
779
+
780
+ if(Error::NO_ERRORS == return_value)
781
+ {
782
+ return_value = WireFormatter::SerializeVarint(size, buffer);
783
+
784
+ if(Error::NO_ERRORS == return_value)
785
+ {
786
+ // Check if there's enough space for the data after writing tag and size
787
+ if(size <= buffer.get_available_size())
788
+ {
789
+ // Only call serialize if there's actual data to write. When the buffer
790
+ // only counts bytes the payload size is already known, serializing it
791
+ // would just count the same bytes a second time.
792
+ if((0U < size) && !buffer.count_only(size))
793
+ {
794
+ return_value = serialize(buffer);
795
+ }
796
+ }
797
+ else
798
+ {
799
+ return_value = Error::BUFFER_FULL;
800
+ }
801
+ }
802
+ }
803
+ }
804
+
805
+ return return_value;
806
+ }
807
+
808
+ inline Error Field::serialize_scalar(const uint32_t field_number,
809
+ const WireFormatter::WireType wire_type,
810
+ const bool is_default,
811
+ WriteBufferInterface& buffer,
812
+ const bool optional) const
813
+ {
814
+ Error return_value = Error::NO_ERRORS;
815
+
816
+ if(optional || !is_default)
817
+ {
818
+ return_value = WireFormatter::SerializeVarint(
819
+ WireFormatter::MakeTag(field_number, wire_type),
820
+ buffer);
821
+
822
+ if(Error::NO_ERRORS == return_value)
823
+ {
824
+ return_value = serialize(buffer);
825
+ }
826
+ }
827
+
828
+ return return_value;
829
+ }
830
+
831
+ } // End of namespace EmbeddedProto.
832
+ #endif