reflection.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  20. * DIRECT, 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/def.h"
  30. #include "upb/msg.h"
  31. #include "upb/upb.h"
  32. #include "upb/port_def.inc"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. typedef union {
  37. bool bool_val;
  38. float float_val;
  39. double double_val;
  40. int32_t int32_val;
  41. int64_t int64_val;
  42. uint32_t uint32_val;
  43. uint64_t uint64_val;
  44. const upb_map* map_val;
  45. const upb_msg* msg_val;
  46. const upb_array* array_val;
  47. upb_strview str_val;
  48. } upb_msgval;
  49. typedef union {
  50. upb_map* map;
  51. upb_msg* msg;
  52. upb_array* array;
  53. } upb_mutmsgval;
  54. upb_msgval upb_fielddef_default(const upb_fielddef *f);
  55. /** upb_msg *******************************************************************/
  56. /* Creates a new message of the given type in the given arena. */
  57. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a);
  58. /* Returns the value associated with this field. */
  59. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f);
  60. /* Returns a mutable pointer to a map, array, or submessage value. If the given
  61. * arena is non-NULL this will construct a new object if it was not previously
  62. * present. May not be called for primitive fields. */
  63. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f, upb_arena *a);
  64. /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
  65. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f);
  66. /* Returns the field that is set in the oneof, or NULL if none are set. */
  67. const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
  68. const upb_oneofdef *o);
  69. /* Sets the given field to the given value. For a msg/array/map/string, the
  70. * caller must ensure that the target data outlives |msg| (by living either in
  71. * the same arena or a different arena that outlives it).
  72. *
  73. * Returns false if allocation fails. */
  74. bool upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  75. upb_arena *a);
  76. /* Clears any field presence and sets the value back to its default. */
  77. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f);
  78. /* Clear all data and unknown fields. */
  79. void upb_msg_clear(upb_msg *msg, const upb_msgdef *m);
  80. /* Iterate over present fields.
  81. *
  82. * size_t iter = UPB_MSG_BEGIN;
  83. * const upb_fielddef *f;
  84. * upb_msgval val;
  85. * while (upb_msg_next(msg, m, ext_pool, &f, &val, &iter)) {
  86. * process_field(f, val);
  87. * }
  88. *
  89. * If ext_pool is NULL, no extensions will be returned. If the given symtab
  90. * returns extensions that don't match what is in this message, those extensions
  91. * will be skipped.
  92. */
  93. #define UPB_MSG_BEGIN -1
  94. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  95. const upb_symtab *ext_pool, const upb_fielddef **f,
  96. upb_msgval *val, size_t *iter);
  97. /* Clears all unknown field data from this message and all submessages. */
  98. bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth);
  99. /** upb_array *****************************************************************/
  100. /* Creates a new array on the given arena that holds elements of this type. */
  101. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type);
  102. /* Returns the size of the array. */
  103. size_t upb_array_size(const upb_array *arr);
  104. /* Returns the given element, which must be within the array's current size. */
  105. upb_msgval upb_array_get(const upb_array *arr, size_t i);
  106. /* Sets the given element, which must be within the array's current size. */
  107. void upb_array_set(upb_array *arr, size_t i, upb_msgval val);
  108. /* Appends an element to the array. Returns false on allocation failure. */
  109. bool upb_array_append(upb_array *array, upb_msgval val, upb_arena *arena);
  110. /* Changes the size of a vector. New elements are initialized to empty/0.
  111. * Returns false on allocation failure. */
  112. bool upb_array_resize(upb_array *array, size_t size, upb_arena *arena);
  113. /** upb_map *******************************************************************/
  114. /* Creates a new map on the given arena with the given key/value size. */
  115. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  116. upb_fieldtype_t value_type);
  117. /* Returns the number of entries in the map. */
  118. size_t upb_map_size(const upb_map *map);
  119. /* Stores a value for the given key into |*val| (or the zero value if the key is
  120. * not present). Returns whether the key was present. The |val| pointer may be
  121. * NULL, in which case the function tests whether the given key is present. */
  122. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
  123. /* Removes all entries in the map. */
  124. void upb_map_clear(upb_map *map);
  125. /* Sets the given key to the given value. Returns true if this was a new key in
  126. * the map, or false if an existing key was replaced. */
  127. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  128. upb_arena *arena);
  129. /* Deletes this key from the table. Returns true if the key was present. */
  130. bool upb_map_delete(upb_map *map, upb_msgval key);
  131. /* Map iteration:
  132. *
  133. * size_t iter = UPB_MAP_BEGIN;
  134. * while (upb_mapiter_next(map, &iter)) {
  135. * upb_msgval key = upb_mapiter_key(map, iter);
  136. * upb_msgval val = upb_mapiter_value(map, iter);
  137. *
  138. * // If mutating is desired.
  139. * upb_mapiter_setvalue(map, iter, value2);
  140. * }
  141. */
  142. /* Advances to the next entry. Returns false if no more entries are present. */
  143. bool upb_mapiter_next(const upb_map *map, size_t *iter);
  144. /* Returns true if the iterator still points to a valid entry, or false if the
  145. * iterator is past the last element. It is an error to call this function with
  146. * UPB_MAP_BEGIN (you must call next() at least once first). */
  147. bool upb_mapiter_done(const upb_map *map, size_t iter);
  148. /* Returns the key and value for this entry of the map. */
  149. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter);
  150. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter);
  151. /* Sets the value for this entry. The iterator must not be done, and the
  152. * iterator must not have been initialized const. */
  153. void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value);
  154. #ifdef __cplusplus
  155. } /* extern "C" */
  156. #endif
  157. #include "upb/port_undef.inc"
  158. #endif /* UPB_REFLECTION_H_ */