TypeOneof.h 3.0 KB

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