Oneof.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. from .Field import Field
  31. class Oneof:
  32. def __init__(self, oneof_proto_descriptor, index, msg_descriptor, parent_msg):
  33. # A reference to the OneofDescriptorProto object which defines this field.
  34. self.descriptor = oneof_proto_descriptor
  35. # A reference to the parent message in which this oneof is defined.
  36. self.parent = parent_msg
  37. self.fields = []
  38. # Loop over all the fields in this oneof
  39. for f in msg_descriptor.field:
  40. if f.HasField('oneof_index') and index == f.oneof_index:
  41. new_field = Field.factory(f, self.parent, oneof=self)
  42. self.fields.append(new_field)
  43. def get_name(self):
  44. return self.descriptor.name
  45. def get_variable_name(self):
  46. return self.get_name() + "_"
  47. def get_which_oneof(self):
  48. return "which_" + self.get_name() + "_"
  49. def get_fields(self):
  50. return self.fields
  51. def match_field_with_definitions(self, all_types_definitions):
  52. for field in self.fields:
  53. field.match_field_with_definitions(all_types_definitions)
  54. def register_template_parameters(self):
  55. all_parameters_registered = True
  56. for field in self.fields:
  57. all_parameters_registered = field.register_template_parameters() and all_parameters_registered
  58. return all_parameters_registered
  59. # Returns true if in oneof.init the new& function needs to be call to initialize already allocated memory.
  60. def oneof_allocation_required(self):
  61. result = False
  62. for field in self.fields:
  63. result = field.oneof_allocation_required()
  64. if result:
  65. break
  66. return result