You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reflection.h 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPB_REFLECTION_H_
  28. #define UPB_REFLECTION_H_
  29. #include "upb/array.h"
  30. #include "upb/def.h"
  31. #include "upb/map.h"
  32. #include "upb/msg.h"
  33. #include "upb/port_def.inc"
  34. #include "upb/upb.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f);
  39. /** upb_Message
  40. * *******************************************************************/
  41. /* Creates a new message of the given type in the given arena. */
  42. upb_Message* upb_Message_New(const upb_MessageDef* m, upb_Arena* a);
  43. /* Returns the value associated with this field. */
  44. upb_MessageValue upb_Message_Get(const upb_Message* msg, const upb_FieldDef* f);
  45. /* Returns a mutable pointer to a map, array, or submessage value. If the given
  46. * arena is non-NULL this will construct a new object if it was not previously
  47. * present. May not be called for primitive fields. */
  48. upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
  49. const upb_FieldDef* f,
  50. upb_Arena* a);
  51. /* May only be called for fields where upb_FieldDef_HasPresence(f) == true. */
  52. bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f);
  53. /* Returns the field that is set in the oneof, or NULL if none are set. */
  54. const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
  55. const upb_OneofDef* o);
  56. /* Sets the given field to the given value. For a msg/array/map/string, the
  57. * caller must ensure that the target data outlives |msg| (by living either in
  58. * the same arena or a different arena that outlives it).
  59. *
  60. * Returns false if allocation fails. */
  61. bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
  62. upb_MessageValue val, upb_Arena* a);
  63. /* Clears any field presence and sets the value back to its default. */
  64. void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f);
  65. /* Clear all data and unknown fields. */
  66. void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m);
  67. /* Iterate over present fields.
  68. *
  69. * size_t iter = kUpb_Message_Begin;
  70. * const upb_FieldDef *f;
  71. * upb_MessageValue val;
  72. * while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
  73. * process_field(f, val);
  74. * }
  75. *
  76. * If ext_pool is NULL, no extensions will be returned. If the given symtab
  77. * returns extensions that don't match what is in this message, those extensions
  78. * will be skipped.
  79. */
  80. #define kUpb_Message_Begin -1
  81. bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
  82. const upb_DefPool* ext_pool, const upb_FieldDef** f,
  83. upb_MessageValue* val, size_t* iter);
  84. /* Clears all unknown field data from this message and all submessages. */
  85. bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
  86. int maxdepth);
  87. #ifdef __cplusplus
  88. } /* extern "C" */
  89. #endif
  90. #include "upb/port_undef.inc"
  91. #endif /* UPB_REFLECTION_H_ */