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