EmbeddedProto  2.0.0
EmbeddedProto is a C++ Protocol Buffer implementation specifically suitable for microcontrollers.
TypeOneof.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 {% macro assign(_oneof) %}
31 if(rhs.get_which_{{_oneof.get_name()}}() != {{_oneof.get_which_oneof()}})
32 {
33  // First delete the old object in the oneof.
34  clear_{{_oneof.get_name()}}();
35 }
36 
37 switch(rhs.get_which_{{_oneof.get_name()}}())
38 {
39  {% for field in _oneof.get_fields() %}
40  case id::{{field.get_variable_id_name()}}:
41  set_{{field.get_name()}}(rhs.get_{{field.name}}());
42  break;
43 
44  {% endfor %}
45  default:
46  break;
47 }
48 {% endmacro %}
49 {# #}
50 {# ------------------------------------------------------------------------------------------------------------------ #}
51 {# #}
52 {% macro init(_oneof) %}
53 void init_{{_oneof.get_name()}}(const id field_id)
54 {
55  if(id::NOT_SET != {{_oneof.get_which_oneof()}})
56  {
57  // First delete the old object in the oneof.
58  clear_{{_oneof.get_name()}}();
59  }
60 
61  {% if _oneof.oneof_allocation_required() %}
62  // C++11 unions only support nontrivial members when you explicitly call the placement new statement.
63  switch(field_id)
64  {
65  {% for field in _oneof.get_fields() %}
66  {% if field.oneof_allocation_required() %}
67  case id::{{field.get_variable_id_name()}}:
68  new(&{{field.get_variable_name()}}) {{field.get_type()}};
69  {{_oneof.get_which_oneof()}} = id::{{field.get_variable_id_name()}};
70  break;
71 
72  {% endif %}
73  {% endfor %}
74  default:
75  break;
76  }
77 
78  {% endif %}
79  {{_oneof.get_which_oneof()}} = field_id;
80 }
81 {% endmacro %}
82 {# #}
83 {# ------------------------------------------------------------------------------------------------------------------ #}
84 {# #}
85 {% macro clear(_oneof) %}
86 void clear_{{_oneof.get_name()}}()
87 {
88  switch({{_oneof.get_which_oneof()}})
89  {
90  {% for field in _oneof.get_fields() %}
91  case id::{{field.get_variable_id_name()}}:
92  {% if field.oneof_allocation_required() %}
93  {{field.get_variable_name()}}.~{{field.get_short_type()}}();
94  {% elif field.of_type_enum %}
95  {{field.get_variable_name()}} = {{field.get_default_value()}};
96  {% else %}
97  {{field.get_variable_name()}}.set(0);
98  {% endif %}
99  break;
100  {% endfor %}
101  default:
102  break;
103  }
104  {{_oneof.get_which_oneof()}} = id::NOT_SET;
105 }
106 {% endmacro %}
EmbeddedProto
Definition: Errors.h:34
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: TypeOneof.h:1