TypeDefMsg.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {#
  2. Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. This file is part of Embedded Proto.
  4. Embedded Proto is open source software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation, version 3 of the license.
  7. Embedded Proto is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial and closed source application please visit:
  14. <https://EmbeddedProto.com/license/>.
  15. Embedded AMS B.V.
  16. Info:
  17. info at EmbeddedProto dot com
  18. Postal address:
  19. Johan Huizingalaan 763a
  20. 1066 VH, Amsterdam
  21. the Netherlands
  22. #}
  23. {% import 'TypeOneof.h' as TypeOneof %}
  24. {% for tmpl_param in typedef.get_templates() %}
  25. {{"template<" if loop.first}}{{tmpl_param['type']}} {{tmpl_param['name']}}{{", " if not loop.last}}{{">" if loop.last}}
  26. {% endfor %}
  27. class {{ typedef.get_name() }} final: public ::EmbeddedProto::MessageInterface
  28. {
  29. public:
  30. {{ typedef.get_name() }}(){% if (typedef.fields or typedef.oneofs) %} :
  31. {% endif %}
  32. {% for field in typedef.fields %}
  33. {{field.get_variable_name()}}({{field.get_default_value()}}){{"," if not loop.last}}{{"," if loop.last and typedef.oneofs}}
  34. {% endfor %}
  35. {% for oneof in typedef.oneofs %}
  36. {{oneof.get_which_oneof()}}(id::NOT_SET){{"," if not loop.last}}
  37. {% endfor %}
  38. {
  39. };
  40. ~{{ typedef.get_name() }}() override = default;
  41. {% for enum in typedef.nested_enum_definitions %}
  42. {{ enum.render(environment)|indent(4) }}
  43. {% endfor %}
  44. {% for msg in typedef.nested_msg_definitions %}
  45. {{ msg.render(environment)|indent(4) }}
  46. {% endfor %}
  47. enum class id
  48. {
  49. NOT_SET = 0,
  50. {% for id_set in typedef.field_ids %}
  51. {{id_set[1]}} = {{id_set[0]}}{{ "," if not loop.last }}
  52. {% endfor %}
  53. };
  54. {{ typedef.name }}& operator=(const {{ typedef.name }}& rhs)
  55. {
  56. {% for field in typedef.fields %}
  57. set_{{ field.get_name() }}(rhs.get_{{ field.get_name() }}());
  58. {% endfor %}
  59. {% for oneof in typedef.oneofs %}
  60. {{ TypeOneof.assign(oneof)|indent(6) }}
  61. {% endfor %}
  62. return *this;
  63. }
  64. {% for field in typedef.fields %}
  65. {{ field.render_get_set(environment)|indent(4) }}
  66. {% endfor %}
  67. {% for oneof in typedef.oneofs %}
  68. id get_which_{{oneof.get_name()}}() const { return {{oneof.get_which_oneof()}}; }
  69. {% for field in oneof.fields %}
  70. {{ field.render_get_set(environment)|indent(4) }}
  71. {% endfor %}
  72. {% endfor %}
  73. ::EmbeddedProto::Error serialize(::EmbeddedProto::WriteBufferInterface& buffer) const final
  74. {
  75. ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
  76. {% for field in typedef.fields %}
  77. {{ field.render_serialize(environment)|indent(6) }}
  78. {% endfor %}
  79. {% for oneof in typedef.oneofs %}
  80. switch({{oneof.get_which_oneof()}})
  81. {
  82. {% for field in oneof.get_fields() %}
  83. case id::{{field.variable_id_name}}:
  84. {{ field.render_serialize(environment)|indent(10) }}
  85. break;
  86. {% endfor %}
  87. default:
  88. break;
  89. }
  90. {% endfor %}
  91. return return_value;
  92. };
  93. ::EmbeddedProto::Error deserialize(::EmbeddedProto::ReadBufferInterface& buffer) final
  94. {
  95. ::EmbeddedProto::Error return_value = ::EmbeddedProto::Error::NO_ERRORS;
  96. ::EmbeddedProto::WireFormatter::WireType wire_type;
  97. uint32_t id_number = 0;
  98. ::EmbeddedProto::Error tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
  99. while((::EmbeddedProto::Error::NO_ERRORS == return_value) && (::EmbeddedProto::Error::NO_ERRORS == tag_value))
  100. {
  101. switch(id_number)
  102. {
  103. {% for field in typedef.fields %}
  104. case static_cast<uint32_t>(id::{{field.get_variable_id_name()}}):
  105. {
  106. {{ field.render_deserialize(environment)|indent(12) }}
  107. break;
  108. }
  109. {% endfor %}
  110. {% for oneof in typedef.oneofs %}
  111. {% for field in oneof.get_fields() %}
  112. case static_cast<uint32_t>(id::{{field.get_variable_id_name()}}):
  113. {
  114. {{ field.render_deserialize(environment)|indent(12) }}
  115. break;
  116. }
  117. {% endfor %}
  118. {% endfor %}
  119. default:
  120. break;
  121. }
  122. if(::EmbeddedProto::Error::NO_ERRORS == return_value)
  123. {
  124. // Read the next tag.
  125. tag_value = ::EmbeddedProto::WireFormatter::DeserializeTag(buffer, wire_type, id_number);
  126. }
  127. }
  128. // When an error was detect while reading the tag but no other errors where found, set it in the return value.
  129. if((::EmbeddedProto::Error::NO_ERRORS == return_value)
  130. && (::EmbeddedProto::Error::NO_ERRORS != tag_value)
  131. && (::EmbeddedProto::Error::END_OF_BUFFER != tag_value)) // The end of the buffer is not an array in this case.
  132. {
  133. return_value = tag_value;
  134. }
  135. return return_value;
  136. };
  137. void clear() final
  138. {
  139. {% for field in typedef.fields %}
  140. clear_{{field.get_name()}}();
  141. {% endfor %}
  142. {% for oneof in typedef.oneofs %}
  143. clear_{{oneof.get_name()}}();
  144. {% endfor %}
  145. }
  146. private:
  147. {% for field in typedef.fields %}
  148. {{field.get_type()}} {{field.get_variable_name()}};
  149. {% endfor %}
  150. {% for oneof in typedef.oneofs %}
  151. id {{oneof.get_which_oneof()}};
  152. union {{oneof.get_name()}}
  153. {
  154. {{oneof.get_name()}}() {}
  155. ~{{oneof.get_name()}}() {}
  156. {% for field in oneof.fields %}
  157. {# Here we use the field name variable instead of the get_ function as the get function will add the oneof
  158. name. This is the only place where this is required. #}
  159. {{field.get_type()}} {{field.variable_name}};
  160. {% endfor %}
  161. };
  162. {{oneof.get_name()}} {{oneof.get_variable_name()}};
  163. {{ TypeOneof.init(oneof)|indent(6) }}
  164. {{ TypeOneof.clear(oneof)|indent(6) }}
  165. {% endfor %}
  166. };