reflection.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/def.h"
  30. #include "upb/msg.h"
  31. #include "upb/port_def.inc"
  32. #include "upb/upb.h"
  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_Message* msg_val;
  46. const upb_Array* array_val;
  47. upb_StringView str_val;
  48. } upb_MessageValue;
  49. typedef union {
  50. upb_Map* map;
  51. upb_Message* msg;
  52. upb_Array* array;
  53. } upb_MutableMessageValue;
  54. upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f);
  55. /** upb_Message
  56. * *******************************************************************/
  57. /* Creates a new message of the given type in the given arena. */
  58. upb_Message* upb_Message_New(const upb_MessageDef* m, upb_Arena* a);
  59. /* Returns the value associated with this field. */
  60. upb_MessageValue upb_Message_Get(const upb_Message* msg, const upb_FieldDef* f);
  61. /* Returns a mutable pointer to a map, array, or submessage value. If the given
  62. * arena is non-NULL this will construct a new object if it was not previously
  63. * present. May not be called for primitive fields. */
  64. upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
  65. const upb_FieldDef* f,
  66. upb_Arena* a);
  67. /* May only be called for fields where upb_FieldDef_HasPresence(f) == true. */
  68. bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f);
  69. /* Returns the field that is set in the oneof, or NULL if none are set. */
  70. const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
  71. const upb_OneofDef* o);
  72. /* Sets the given field to the given value. For a msg/array/map/string, the
  73. * caller must ensure that the target data outlives |msg| (by living either in
  74. * the same arena or a different arena that outlives it).
  75. *
  76. * Returns false if allocation fails. */
  77. bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
  78. upb_MessageValue val, upb_Arena* a);
  79. /* Clears any field presence and sets the value back to its default. */
  80. void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f);
  81. /* Clear all data and unknown fields. */
  82. void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m);
  83. /* Iterate over present fields.
  84. *
  85. * size_t iter = kUpb_Message_Begin;
  86. * const upb_FieldDef *f;
  87. * upb_MessageValue val;
  88. * while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
  89. * process_field(f, val);
  90. * }
  91. *
  92. * If ext_pool is NULL, no extensions will be returned. If the given symtab
  93. * returns extensions that don't match what is in this message, those extensions
  94. * will be skipped.
  95. */
  96. #define kUpb_Message_Begin -1
  97. bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
  98. const upb_DefPool* ext_pool, const upb_FieldDef** f,
  99. upb_MessageValue* val, size_t* iter);
  100. /* Clears all unknown field data from this message and all submessages. */
  101. bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
  102. int maxdepth);
  103. /** upb_Array *****************************************************************/
  104. /* Creates a new array on the given arena that holds elements of this type. */
  105. upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
  106. /* Returns the size of the array. */
  107. size_t upb_Array_Size(const upb_Array* arr);
  108. /* Returns the given element, which must be within the array's current size. */
  109. upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
  110. /* Sets the given element, which must be within the array's current size. */
  111. void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
  112. /* Appends an element to the array. Returns false on allocation failure. */
  113. bool upb_Array_Append(upb_Array* array, upb_MessageValue val, upb_Arena* arena);
  114. /* Moves elements within the array using memmove(). Like memmove(), the source
  115. * and destination elements may be overlapping. */
  116. void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx,
  117. size_t count);
  118. /* Inserts one or more empty elements into the array. Existing elements are
  119. * shifted right. The new elements have undefined state and must be set with
  120. * `upb_Array_Set()`.
  121. * REQUIRES: `i <= upb_Array_Size(arr)` */
  122. bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
  123. upb_Arena* arena);
  124. /* Deletes one or more elements from the array. Existing elements are shifted
  125. * left.
  126. * REQUIRES: `i + count <= upb_Array_Size(arr)` */
  127. void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
  128. /* Changes the size of a vector. New elements are initialized to empty/0.
  129. * Returns false on allocation failure. */
  130. bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
  131. /** upb_Map *******************************************************************/
  132. /* Creates a new map on the given arena with the given key/value size. */
  133. upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type);
  134. /* Returns the number of entries in the map. */
  135. size_t upb_Map_Size(const upb_Map* map);
  136. /* Stores a value for the given key into |*val| (or the zero value if the key is
  137. * not present). Returns whether the key was present. The |val| pointer may be
  138. * NULL, in which case the function tests whether the given key is present. */
  139. bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
  140. upb_MessageValue* val);
  141. /* Removes all entries in the map. */
  142. void upb_Map_Clear(upb_Map* map);
  143. /* Sets the given key to the given value. Returns true if this was a new key in
  144. * the map, or false if an existing key was replaced. */
  145. bool upb_Map_Set(upb_Map* map, upb_MessageValue key, upb_MessageValue val,
  146. upb_Arena* arena);
  147. /* Deletes this key from the table. Returns true if the key was present. */
  148. bool upb_Map_Delete(upb_Map* map, upb_MessageValue key);
  149. /* Map iteration:
  150. *
  151. * size_t iter = kUpb_Map_Begin;
  152. * while (upb_MapIterator_Next(map, &iter)) {
  153. * upb_MessageValue key = upb_MapIterator_Key(map, iter);
  154. * upb_MessageValue val = upb_MapIterator_Value(map, iter);
  155. *
  156. * // If mutating is desired.
  157. * upb_MapIterator_SetValue(map, iter, value2);
  158. * }
  159. */
  160. /* Advances to the next entry. Returns false if no more entries are present. */
  161. bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
  162. /* Returns true if the iterator still points to a valid entry, or false if the
  163. * iterator is past the last element. It is an error to call this function with
  164. * kUpb_Map_Begin (you must call next() at least once first). */
  165. bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
  166. /* Returns the key and value for this entry of the map. */
  167. upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
  168. upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
  169. /* Sets the value for this entry. The iterator must not be done, and the
  170. * iterator must not have been initialized const. */
  171. void upb_MapIterator_SetValue(upb_Map* map, size_t iter,
  172. upb_MessageValue value);
  173. #ifdef __cplusplus
  174. } /* extern "C" */
  175. #endif
  176. #include "upb/port_undef.inc"
  177. #endif /* UPB_REFLECTION_H_ */