| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- {% macro enum_macro(_enum) %}
- enum {{ _enum.name }}
- {
- {% for value in _enum.values() %}
- {{ value.name }} = {{ value.number }}{{ "," if not loop.last }}
- {% endfor %}
- };
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro oneof_init(_oneof) %}
- void init_{{_oneof.name}}(const id field_id)
- {
- if(id::NOT_SET != {{_oneof.which_oneof}})
- {
- // First delete the old object in the oneof.
- clear_{{_oneof.name}}();
- }
- // C++11 unions only support nontrivial members when you explicitly call the placement new statement.
- switch(field_id)
- {
- {% for field in _oneof.fields() %}
- case id::{{field.variable_id_name}}:
- {% if field.of_type_message or field.is_repeated_field or field.is_string or field.is_bytes %}
- new(&{{field.variable_full_name}}) {{field.type}};
- {{_oneof.which_oneof}} = id::{{field.variable_id_name}};
- {% endif %}
- break;
- {% endfor %}
- default:
- break;
- }
- }
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro oneof_assign(_oneof) %}
- if(rhs.get_which_{{_oneof.name}}() != {{_oneof.which_oneof}})
- {
- // First delete the old object in the oneof.
- clear_{{_oneof.name}}();
- }
- switch(rhs.get_which_{{_oneof.name}}())
- {
- {% for field in _oneof.fields() %}
- case id::{{field.variable_id_name}}:
- set_{{field.name}}(rhs.get_{{field.name}}());
- break;
- {% endfor %}
- default:
- break;
- }
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro oneof_clear(_oneof) %}
- void clear_{{_oneof.name}}()
- {
- switch({{_oneof.which_oneof}})
- {
- {% for field in _oneof.fields() %}
- case id::{{field.variable_id_name}}:
- {% if field.of_type_message or field.is_repeated_field or field.is_string or field.is_bytes%}
- {{field.variable_full_name}}.~{{field.short_type}}();
- {% else %}
- {{field.variable_full_name}}.set(0);
- {% endif %}
- break;
- {% endfor %}
- default:
- break;
- }
- {{_oneof.which_oneof}} = id::NOT_SET;
- }
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro field_get_set_macro(_field) %}
- {% if _field.is_string or _field.is_bytes %}
- inline const {{_field.repeated_type}}& {{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% if _field.which_oneof is defined %}
- inline void clear_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} == {{_field.which_oneof}})
- {
- {{_field.which_oneof}} = id::NOT_SET;
- {{_field.variable_full_name}}.~{{_field.short_type}}();
- }
- }
- inline {{_field.repeated_type}}& mutable_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} != {{_field.which_oneof}})
- {
- init_{{_field.oneof_name}}(id::{{_field.variable_id_name}});
- }
- return {{_field.variable_full_name}};
- }
- {% else %}
- inline void clear_{{_field.name}}() { {{_field.variable_full_name}}.clear(); }
- inline {{_field.repeated_type}}& mutable_{{_field.name}}() { return {{_field.variable_full_name}}; }
- {% endif %}
- {% if _field.is_string %}
- inline const char* get_{{_field.name}}() const { return {{_field.variable_full_name}}.get_const(); }
- {% else %}{# is bytes #}
- inline const uint8_t* get_{{_field.name}}() const { return {{_field.variable_full_name}}.get_const(); }
- {% endif %}
- {% elif _field.is_repeated_field %}
- inline const {{_field.type}}& {{_field.name}}(uint32_t index) const { return {{_field.variable_full_name}}[index]; }
- {% if _field.which_oneof is defined %}
- inline void clear_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} == {{_field.which_oneof}})
- {
- {{_field.which_oneof}} = id::NOT_SET;
- {{_field.variable_full_name}}.~{{_field.short_type}}();
- }
- }
- inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}}.set(index, value);
- }
- inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}}.set(index, value);
- }
- inline void set_{{_field.name}}(const {{_field.repeated_type}}& values)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}} = values;
- }
- inline void add_{{_field.name}}(const {{_field.type}}& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}}.add(value);
- }
- inline {{_field.repeated_type}}& mutable_{{_field.name}}()
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- return {{_field.variable_full_name}};
- }
- {% else %}
- inline void clear_{{_field.name}}() { {{_field.variable_full_name}}.clear(); }
- inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value) { {{_field.variable_full_name}}.set(index, value); }
- inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value) { {{_field.variable_full_name}}.set(index, value); }
- inline void set_{{_field.name}}(const {{_field.repeated_type}}& values) { {{_field.variable_full_name}} = values; }
- inline void add_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_full_name}}.add(value); }
- inline {{_field.repeated_type}}& mutable_{{_field.name}}() { return {{_field.variable_full_name}}; }
- {% endif %}
- inline const {{_field.repeated_type}}& get_{{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% elif _field.of_type_message %}
- inline const {{_field.type}}& {{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% if _field.which_oneof is defined %}
- inline void clear_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} == {{_field.which_oneof}})
- {
- {{_field.which_oneof}} = id::NOT_SET;
- {{_field.variable_full_name}}.~{{_field.short_type}}();
- }
- }
- inline void set_{{_field.name}}(const {{_field.type}}& value)
- {
- if(id::{{_field.variable_id_name}} != {{_field.which_oneof}})
- {
- init_{{_field.oneof_name}}(id::{{_field.variable_id_name}});
- }
- {{_field.variable_full_name}} = value;
- }
- inline void set_{{_field.name}}(const {{_field.type}}&& value)
- {
- if(id::{{_field.variable_id_name}} != {{_field.which_oneof}})
- {
- init_{{_field.oneof_name}}(id::{{_field.variable_id_name}});
- }
- {{_field.variable_full_name}} = value;
- }
- inline {{_field.type}}& mutable_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} != {{_field.which_oneof}})
- {
- init_{{_field.oneof_name}}(id::{{_field.variable_id_name}});
- }
- return {{_field.variable_full_name}};
- }
- {% else %}
- inline void clear_{{_field.name}}() { {{_field.variable_full_name}}.clear(); }
- inline void set_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_full_name}} = value; }
- inline void set_{{_field.name}}(const {{_field.type}}&& value) { {{_field.variable_full_name}} = value; }
- inline {{_field.type}}& mutable_{{_field.name}}() { return {{_field.variable_full_name}}; }
- {% endif %}
- inline const {{_field.type}}& get_{{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% elif _field.of_type_enum %}
- inline {{_field.type}} {{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% if _field.which_oneof is defined %}
- inline void clear_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} == {{_field.which_oneof}})
- {
- {{_field.which_oneof}} = id::NOT_SET;
- {{_field.variable_full_name}} = static_cast<{{_field.type}}>({{_field.default_value}});
- }
- }
- inline void set_{{_field.name}}(const {{_field.type}}& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}} = value;
- }
- inline void set_{{_field.name}}(const {{_field.type}}&& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}} = value;
- }
- {% else %}
- inline void clear_{{_field.name}}() { {{_field.variable_full_name}} = static_cast<{{_field.type}}>({{_field.default_value}}); }
- inline void set_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_full_name}} = value; }
- inline void set_{{_field.name}}(const {{_field.type}}&& value) { {{_field.variable_full_name}} = value; }
- {% endif %} inline {{_field.type}} get_{{_field.name}}() const { return {{_field.variable_full_name}}; }
- {% else %}
- inline {{_field.type}}::FIELD_TYPE {{_field.name}}() const { return {{_field.variable_full_name}}.get(); }
- {% if _field.which_oneof is defined %}
- inline void clear_{{_field.name}}()
- {
- if(id::{{_field.variable_id_name}} == {{_field.which_oneof}})
- {
- {{_field.which_oneof}} = id::NOT_SET;
- {{_field.variable_full_name}}.set({{_field.default_value}});
- }
- }
- inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}}.set(value);
- }
- inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- {{_field.variable_full_name}}.set(value);
- }
- {% else %}
- inline void clear_{{_field.name}}() { {{_field.variable_full_name}}.set({{_field.default_value}}); }
- inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE& value) { {{_field.variable_full_name}}.set(value); }
- inline void set_{{_field.name}}(const {{_field.type}}::FIELD_TYPE&& value) { {{_field.variable_full_name}}.set(value); }
- {% endif %}
- inline {{_field.type}}::FIELD_TYPE get_{{_field.name}}() const { return {{_field.variable_full_name}}.get(); }
- {% endif %}
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro field_serialize_macro(_field) %}
- {% if _field.is_repeated_field or _field.is_string or _field.is_bytes %}
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- return_value = {{_field.variable_full_name}}.serialize_with_id(static_cast<uint32_t>(id::{{_field.variable_id_name}}), buffer);
- }
- {% elif _field.of_type_message %}
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- return_value = {{_field.variable_full_name}}.serialize_with_id(static_cast<uint32_t>(id::{{_field.variable_id_name}}), buffer);
- }
- {% elif _field.of_type_enum %}
- if(({{_field.default_value}} != {{_field.variable_full_name}}) && (::EmbeddedProto::Error::NO_ERRORS == return_value))
- {
- EmbeddedProto::uint32 value;
- value.set(static_cast<uint32_t>({{_field.variable_full_name}}));
- return_value = value.serialize_with_id(static_cast<uint32_t>(id::{{_field.variable_id_name}}), buffer);
- }
- {% else %}
- if(({{_field.default_value}} != {{_field.variable_full_name}}.get()) && (::EmbeddedProto::Error::NO_ERRORS == return_value))
- {
- return_value = {{_field.variable_full_name}}.serialize_with_id(static_cast<uint32_t>(id::{{_field.variable_id_name}}), buffer);
- } {% endif %} {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro field_deserialize_macro(_field) %}
- {% if _field.is_repeated_field %}
- if(::EmbeddedProto::WireFormatter::WireType::LENGTH_DELIMITED == wire_type)
- {
- return_value = {{_field.variable_full_name}}.deserialize(buffer);
- }
- {% else %}
- if(::EmbeddedProto::WireFormatter::WireType::{{_field.wire_type}} == wire_type)
- {
- {% if _field.of_type_message %}
- uint32_t size;
- return_value = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
- ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- return_value = mutable_{{_field.name}}().deserialize(bufferSection);
- }
- {% elif _field.is_string or _field.is_bytes %}
- return_value = mutable_{{_field.name}}().deserialize(buffer);
- {% elif _field.of_type_enum %}
- uint32_t value;
- return_value = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, value);
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- set_{{_field.name}}(static_cast<{{_field.type}}>(value));
- }
- {% else %}
- return_value = {{_field.variable_full_name}}.deserialize(buffer);
- {% endif %}
- {% if _field.which_oneof is defined %}
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- {{_field.which_oneof}} = id::{{_field.variable_id_name}};
- }
- {% endif %}
- }
- {% endif %}
- else
- {
- // Wire type does not match field.
- return_value = ::EmbeddedProto::Error::INVALID_WIRETYPE;
- } {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- {% macro msg_macro(msg) %}
- {% if msg.templates is defined %}
- {% for template in msg.templates %}
- {{"template<" if loop.first}}{{template['type']}} {{template['name']}}{{", " if not loop.last}}{{">" if loop.last}}
- {% endfor %}
- {% endif %}
- class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
- {
- public:
- {{ msg.name }}(){% if (msg.has_fields or msg.has_oneofs) %} :
- {% endif %}
- {% for field in msg.fields() %}
- {% if field.of_type_enum %}
- {{field.variable_full_name}}({{field.default_value}}){{"," if not loop.last}}
- {% else %}
- {{field.variable_full_name}}(){{"," if not loop.last}}{{"," if loop.last and msg.has_oneofs}}
- {% endif %}
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- {{oneof.which_oneof}}(id::NOT_SET){{"," if not loop.last}}
- {% endfor %}
- {
- };
- ~{{ msg.name }}() override = default;
- {% for enum in msg.nested_enums() %}
- {{ enum_macro(enum) }}
- {% endfor %}
- enum class id
- {
- NOT_SET = 0,
- {% for id_set in msg.field_ids %}
- {{id_set[1]}} = {{id_set[0]}}{{ "," if not loop.last }}
- {% endfor %}
- };
- {{ msg.name }}& operator=(const {{ msg.name }}& rhs)
- {
- {% for field in msg.fields() %}
- set_{{ field.name }}(rhs.get_{{ field.name }}());
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- {{ oneof_assign(oneof)|indent(6) }}
- {% endfor %}
- return *this;
- }
- {% for field in msg.fields() %}
- {{ field_get_set_macro(field)|indent(4) }}
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- id get_which_{{oneof.name}}() const { return {{oneof.which_oneof}}; }
- {% for field in oneof.fields() %}
- {{ field_get_set_macro(field)|indent(4) }}
- {% endfor %}
- {% endfor %}
- ::EmbeddedProto::Error serialize(::EmbeddedProto::WriteBufferInterface& buffer) const final
- {
- ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
- {% for field in msg.fields() %}
- {{ field_serialize_macro(field)|indent(6) }}
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- switch({{oneof.which_oneof}})
- {
- {% for field in oneof.fields() %}
- case id::{{field.variable_id_name}}:
- {{ field_serialize_macro(field)|indent(12) }}
- break;
- {% endfor %}
- default:
- break;
- }
- {% endfor %}
- return return_value;
- };
- ::EmbeddedProto::Error deserialize(::EmbeddedProto::ReadBufferInterface& buffer) final
- {
- ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
- ::EmbeddedProto::WireFormatter::WireType wire_type;
- uint32_t id_number = 0;
- ::EmbeddedProto::Error tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
- while((::EmbeddedProto::Error::NO_ERRORS == return_value) && (::EmbeddedProto::Error::NO_ERRORS == tag_value))
- {
- switch(id_number)
- {
- {% for field in msg.fields() %}
- case static_cast<uint32_t>(id::{{field.variable_id_name}}):
- {
- {{ field_deserialize_macro(field)|indent(12) }}
- break;
- }
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- {% for field in oneof.fields() %}
- case static_cast<uint32_t>(id::{{field.variable_id_name}}):
- {
- {{ field_deserialize_macro(field)|indent(12) }}
- break;
- }
- {% endfor %}
- {% endfor %}
- default:
- break;
- }
-
- if(::EmbeddedProto::Error::NO_ERRORS == return_value)
- {
- // Read the next tag.
- tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
- }
- }
- // When an error was detect while reading the tag but no other errors where found, set it in the return value.
- if((::EmbeddedProto::Error::NO_ERRORS == return_value)
- && (::EmbeddedProto::Error::NO_ERRORS != tag_value)
- && (::EmbeddedProto::Error::END_OF_BUFFER != tag_value)) // The end of the buffer is not an array in this case.
- {
- return_value = tag_value;
- }
- return return_value;
- };
- void clear() final
- {
- {% for field in msg.fields() %}
- clear_{{field.name}}();
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- clear_{{oneof.name}}();
- {% endfor %}
- }
- private:
- {% for field in msg.fields() %}
- {% if field.is_repeated_field or field.is_string or field.is_bytes %}
- {{field.repeated_type}} {{field.variable_name}};
- {% else %}
- {{field.type}} {{field.variable_name}};
- {% endif %}
- {% endfor %}
- {% for oneof in msg.oneofs() %}
- id {{oneof.which_oneof}};
- union {{oneof.name}}
- {
- {{oneof.name}}() {}
- ~{{oneof.name}}() {}
- {% for field in oneof.fields() %}
- {% if field.is_repeated_field or field.is_string or field.is_bytes %}
- {{field.repeated_type}} {{field.variable_name}};
- {% else %}
- {{field.type}} {{field.variable_name}};
- {% endif %}
- {% endfor %}
- };
- {{oneof.name}} {{oneof.name}}_;
- {{ oneof_init(oneof)|indent(4) }}
- {{ oneof_clear(oneof)|indent(4) }}
- {% endfor %}
- };
- {% endmacro %}
- {# #}
- {# ------------------------------------------------------------------------------------------------------------------ #}
- {# #}
- /*
- * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
- *
- * This file is part of Embedded Proto.
- *
- * Embedded Proto is open source software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, version 3 of the license.
- *
- * Embedded Proto is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
- *
- * For commercial and closed source application please visit:
- * <https://EmbeddedProto.com/license/>.
- *
- * Embedded AMS B.V.
- * Info:
- * info at EmbeddedProto dot com
- *
- * Postal address:
- * Johan Huizingalaan 763a
- * 1066 VH, Amsterdam
- * the Netherlands
- */
- // This file is generated. Please do not edit!
- #ifndef _{{header_guard}}_H_
- #define _{{header_guard}}_H_
- #include <cstdint>
- {% if messages %}
- #include <MessageInterface.h>
- #include <WireFormatter.h>
- #include <Fields.h>
- #include <MessageSizeCalculator.h>
- #include <ReadBufferSection.h>
- #include <RepeatedFieldFixedSize.h>
- #include <FieldStringBytes.h>
- #include <Errors.h>
- {% endif %}
- {% if dependencies %}
- // Include external proto definitions
- {% for dependency in dependencies %}
- #include <{{dependency}}>
- {% endfor %}
- {% endif %}
- {% for namespace in namespaces %}
- namespace {{ namespace }} {
- {% endfor %}
- {% for enum in enums %}
- {{ enum_macro(enum) }}
- {% endfor %}
- {% for msg in messages %}
- {{ msg_macro(msg) }}
- {% endfor %}
- {% for namespace in namespaces|reverse %}
- } // End of namespace {{ namespace }}
- {% endfor %}
- #endif // _{{header_guard}}_H_
|