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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {
  38. #endif
  39. upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f);
  40. /** upb_Message
  41. * *******************************************************************/
  42. /* Creates a new message of the given type in the given arena. */
  43. upb_Message* upb_Message_New(const upb_MessageDef* m, upb_Arena* a);
  44. /* Returns the value associated with this field. */
  45. upb_MessageValue upb_Message_Get(const upb_Message* msg, const upb_FieldDef* f);
  46. /* Returns a mutable pointer to a map, array, or submessage value. If the given
  47. * arena is non-NULL this will construct a new object if it was not previously
  48. * present. May not be called for primitive fields. */
  49. upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg, const upb_FieldDef* f, upb_Arena* a);
  50. /* May only be called for fields where upb_FieldDef_HasPresence(f) == true. */
  51. bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f);
  52. /* Returns the field that is set in the oneof, or NULL if none are set. */
  53. const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg, const upb_OneofDef* o);
  54. /* Sets the given field to the given value. For a msg/array/map/string, the
  55. * caller must ensure that the target data outlives |msg| (by living either in
  56. * the same arena or a different arena that outlives it).
  57. *
  58. * Returns false if allocation fails. */
  59. bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f, upb_MessageValue val, upb_Arena* a);
  60. /* Clears any field presence and sets the value back to its default. */
  61. void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f);
  62. /* Clear all data and unknown fields. */
  63. void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m);
  64. /* Iterate over present fields.
  65. *
  66. * size_t iter = kUpb_Message_Begin;
  67. * const upb_FieldDef *f;
  68. * upb_MessageValue val;
  69. * while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
  70. * process_field(f, val);
  71. * }
  72. *
  73. * If ext_pool is NULL, no extensions will be returned. If the given symtab
  74. * returns extensions that don't match what is in this message, those extensions
  75. * will be skipped.
  76. */
  77. #define kUpb_Message_Begin -1
  78. bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m, const upb_DefPool* ext_pool, const upb_FieldDef** f, upb_MessageValue* val, size_t* iter);
  79. /* Clears all unknown field data from this message and all submessages. */
  80. bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m, int maxdepth);
  81. #ifdef __cplusplus
  82. } /* extern "C" */
  83. #endif
  84. #include "upb/port_undef.inc"
  85. #endif /* UPB_REFLECTION_H_ */