11/*
2- * Copyright (C) 2020-2024 Embedded AMS B.V. - All Rights Reserved
2+ * Copyright (C) 2020-2025 Embedded AMS B.V. - All Rights Reserved
33 *
44 * This file is part of Embedded Proto.
55 *
1616 * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
1717 *
1818 * For commercial and closed source application please visit:
19- * <https://EmbeddedProto .com/license />.
19+ * <https://embeddedproto .com/pricing />.
2020 *
2121 * Embedded AMS B.V.
2222 * Info:
@@ -119,12 +119,12 @@ namespace EmbeddedProto
119119#endif // End of MSG_TO_STRING
120120 };
121121
122- template <Field::FieldTypes FIELDTYPE, class VARIABLE_TYPE , WireFormatter::WireType WIRETYPE>
122+ template <Field::FieldTypes FIELDTYPE, class VARIABLE_TYPE , WireFormatter::WireType WIRETYPE, uint32_t MAX_SER_SIZE >
123123 class FieldTemplate
124124 {
125125 public:
126126 using TYPE = VARIABLE_TYPE;
127- using CLASS_TYPE = FieldTemplate<FIELDTYPE, VARIABLE_TYPE, WIRETYPE>;
127+ using CLASS_TYPE = FieldTemplate<FIELDTYPE, VARIABLE_TYPE, WIRETYPE, MAX_SER_SIZE >;
128128
129129 FieldTemplate () = default ;
130130 FieldTemplate (const VARIABLE_TYPE& v) : value_(v) { };
@@ -209,18 +209,18 @@ namespace EmbeddedProto
209209 bool operator >=(const VARIABLE_TYPE& rhs) { return value_ >= rhs; }
210210 bool operator <=(const VARIABLE_TYPE& rhs) { return value_ <= rhs; }
211211
212- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
213- bool operator ==(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ == rhs.get (); }
214- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
215- bool operator !=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ != rhs.get (); }
216- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
217- bool operator >(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ > rhs.get (); }
218- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
219- bool operator <(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ < rhs.get (); }
220- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
221- bool operator >=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ >= rhs.get (); }
222- template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS>
223- bool operator <=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS>& rhs) { return value_ <= rhs.get (); }
212+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
213+ bool operator ==(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ == rhs.get (); }
214+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
215+ bool operator !=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ != rhs.get (); }
216+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
217+ bool operator >(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ > rhs.get (); }
218+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
219+ bool operator <(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ < rhs.get (); }
220+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
221+ bool operator >=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ >= rhs.get (); }
222+ template <Field::FieldTypes FIELDTYPE_RHS, class TYPE_RHS , WireFormatter::WireType WIRETYPE_RHS, uint32_t SIZE_RHS >
223+ bool operator <=(const FieldTemplate<FIELDTYPE_RHS, TYPE_RHS, WIRETYPE_RHS, SIZE_RHS >& rhs) { return value_ <= rhs.get (); }
224224
225225 void clear () { value_ = static_cast <VARIABLE_TYPE>(0 ); }
226226
@@ -231,6 +231,22 @@ namespace EmbeddedProto
231231 return calcBuffer.get_size ();
232232 }
233233
234+ // ! When serialized with the most unfavrouble value how much bytes does this field need.
235+ /* !
236+ This function takes into account the field number and tag combination.
237+ \param[in] field_number We need to include the field number. This because large field numbers require more bytes.
238+ \return The number of bytes required at most.
239+ */
240+ static constexpr uint32_t max_serialized_size (const uint32_t field_number)
241+ {
242+ return MAX_SER_SIZE + WireFormatter::VarintSize (WireFormatter::MakeTag (field_number, WIRETYPE));
243+ }
244+
245+ static constexpr uint32_t max_serialized_size ()
246+ {
247+ return MAX_SER_SIZE;
248+ }
249+
234250#ifdef MSG_TO_STRING
235251
236252 // ! Write all the data in this field to a human readable string.
@@ -438,22 +454,22 @@ namespace EmbeddedProto
438454 };
439455
440456
441- using int32 = FieldTemplate<Field::FieldTypes::int32, int32_t , WireFormatter::WireType::VARINT>;
442- using int64 = FieldTemplate<Field::FieldTypes::int64, int64_t , WireFormatter::WireType::VARINT>;
443- using uint32 = FieldTemplate<Field::FieldTypes::uint32, uint32_t , WireFormatter::WireType::VARINT>;
444- using uint64 = FieldTemplate<Field::FieldTypes::uint64, uint64_t , WireFormatter::WireType::VARINT>;
445- using sint32 = FieldTemplate<Field::FieldTypes::sint32, int32_t , WireFormatter::WireType::VARINT>;
446- using sint64 = FieldTemplate<Field::FieldTypes::sint64, int64_t , WireFormatter::WireType::VARINT>;
447- using boolean = FieldTemplate<Field::FieldTypes::boolean, bool , WireFormatter::WireType::VARINT>;
448- using fixed32 = FieldTemplate<Field::FieldTypes::fixed32, uint32_t , WireFormatter::WireType::FIXED32>;
449- using fixed64 = FieldTemplate<Field::FieldTypes::fixed64, uint64_t , WireFormatter::WireType::FIXED64>;
450- using sfixed32 = FieldTemplate<Field::FieldTypes::sfixed32, int32_t , WireFormatter::WireType::FIXED32>;
451- using sfixed64 = FieldTemplate<Field::FieldTypes::sfixed64, int64_t , WireFormatter::WireType::FIXED64>;
452- using floatfixed = FieldTemplate<Field::FieldTypes::floatfixed, float , WireFormatter::WireType::FIXED32>;
453- using doublefixed = FieldTemplate<Field::FieldTypes::doublefixed, double , WireFormatter::WireType::FIXED64>;
454-
455- template <class ENUM_TYPE >
456- using enumeration = FieldTemplate<Field::FieldTypes::enumeration, ENUM_TYPE, WireFormatter::WireType::VARINT>;
457+ using int32 = FieldTemplate<Field::FieldTypes::int32, int32_t , WireFormatter::WireType::VARINT, 5 >;
458+ using int64 = FieldTemplate<Field::FieldTypes::int64, int64_t , WireFormatter::WireType::VARINT, 10 >;
459+ using uint32 = FieldTemplate<Field::FieldTypes::uint32, uint32_t , WireFormatter::WireType::VARINT, 5 >;
460+ using uint64 = FieldTemplate<Field::FieldTypes::uint64, uint64_t , WireFormatter::WireType::VARINT, 10 >;
461+ using sint32 = FieldTemplate<Field::FieldTypes::sint32, int32_t , WireFormatter::WireType::VARINT, 5 >;
462+ using sint64 = FieldTemplate<Field::FieldTypes::sint64, int64_t , WireFormatter::WireType::VARINT, 10 >;
463+ using boolean = FieldTemplate<Field::FieldTypes::boolean, bool , WireFormatter::WireType::VARINT, 1 >;
464+ using fixed32 = FieldTemplate<Field::FieldTypes::fixed32, uint32_t , WireFormatter::WireType::FIXED32, 4 >;
465+ using fixed64 = FieldTemplate<Field::FieldTypes::fixed64, uint64_t , WireFormatter::WireType::FIXED64, 8 >;
466+ using sfixed32 = FieldTemplate<Field::FieldTypes::sfixed32, int32_t , WireFormatter::WireType::FIXED32, 4 >;
467+ using sfixed64 = FieldTemplate<Field::FieldTypes::sfixed64, int64_t , WireFormatter::WireType::FIXED64, 8 >;
468+ using floatfixed = FieldTemplate<Field::FieldTypes::floatfixed, float , WireFormatter::WireType::FIXED32, 4 >;
469+ using doublefixed = FieldTemplate<Field::FieldTypes::doublefixed, double , WireFormatter::WireType::FIXED64, 8 >;
470+
471+ template <class ENUM_TYPE , uint32_t MAX_SER_SIZE >
472+ using enumeration = FieldTemplate<Field::FieldTypes::enumeration, ENUM_TYPE, WireFormatter::WireType::VARINT, MAX_SER_SIZE >;
457473
458474} // End of namespace EmbeddedProto.
459475#endif
0 commit comments