reflection.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef UPB_REFLECTION_H_
  2. #define UPB_REFLECTION_H_
  3. #include "upb/def.h"
  4. #include "upb/msg.h"
  5. #include "upb/upb.h"
  6. #include "upb/port_def.inc"
  7. typedef union {
  8. bool bool_val;
  9. float float_val;
  10. double double_val;
  11. int32_t int32_val;
  12. int64_t int64_val;
  13. uint32_t uint32_val;
  14. uint64_t uint64_val;
  15. const upb_map* map_val;
  16. const upb_msg* msg_val;
  17. const upb_array* array_val;
  18. upb_strview str_val;
  19. } upb_msgval;
  20. typedef union {
  21. upb_map* map;
  22. upb_msg* msg;
  23. upb_array* array;
  24. } upb_mutmsgval;
  25. /** upb_msg *******************************************************************/
  26. /* Creates a new message of the given type in the given arena. */
  27. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a);
  28. /* Returns the value associated with this field. */
  29. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f);
  30. /* Returns a mutable pointer to a map, array, or submessage value. If the given
  31. * arena is non-NULL this will construct a new object if it was not previously
  32. * present. May not be called for primitive fields. */
  33. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f, upb_arena *a);
  34. /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
  35. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f);
  36. /* Sets the given field to the given value. For a msg/array/map/string, the
  37. * value must be in the same arena. */
  38. void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  39. upb_arena *a);
  40. /* Clears any field presence and sets the value back to its default. */
  41. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f);
  42. /* Iterate over present fields.
  43. *
  44. * size_t iter = UPB_MSG_BEGIN;
  45. * const upb_fielddef *f;
  46. * upb_msgval val;
  47. * while (upb_msg_next(msg, m, ext_pool, &f, &val, &iter)) {
  48. * process_field(f, val);
  49. * }
  50. *
  51. * If ext_pool is NULL, no extensions will be returned. If the given symtab
  52. * returns extensions that don't match what is in this message, those extensions
  53. * will be skipped.
  54. */
  55. #define UPB_MSG_BEGIN -1
  56. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  57. const upb_symtab *ext_pool, const upb_fielddef **f,
  58. upb_msgval *val, size_t *iter);
  59. /* Adds unknown data (serialized protobuf data) to the given message. The data
  60. * is copied into the message instance. */
  61. void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  62. upb_arena *arena);
  63. /* Returns a reference to the message's unknown data. */
  64. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
  65. /** upb_array *****************************************************************/
  66. /* Creates a new array on the given arena that holds elements of this type. */
  67. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type);
  68. /* Returns the size of the array. */
  69. size_t upb_array_size(const upb_array *arr);
  70. /* Returns the given element, which must be within the array's current size. */
  71. upb_msgval upb_array_get(const upb_array *arr, size_t i);
  72. /* Sets the given element, which must be within the array's current size. */
  73. void upb_array_set(upb_array *arr, size_t i, upb_msgval val);
  74. /* Appends an element to the array. Returns false on allocation failure. */
  75. bool upb_array_append(upb_array *array, upb_msgval val, upb_arena *arena);
  76. /* Changes the size of a vector. New elements are initialized to empty/0.
  77. * Returns false on allocation failure. */
  78. bool upb_array_resize(upb_array *array, size_t size, upb_arena *arena);
  79. /** upb_map *******************************************************************/
  80. /* Creates a new map on the given arena with the given key/value size. */
  81. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  82. upb_fieldtype_t value_type);
  83. /* Returns the number of entries in the map. */
  84. size_t upb_map_size(const upb_map *map);
  85. /* Stores a value for the given key into |*val| (or the zero value if the key is
  86. * not present). Returns whether the key was present. The |val| pointer may be
  87. * NULL, in which case the function tests whether the given key is present. */
  88. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
  89. /* Removes all entries in the map. */
  90. void upb_map_clear(upb_map *map);
  91. /* Sets the given key to the given value. Returns true if this was a new key in
  92. * the map, or false if an existing key was replaced. */
  93. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  94. upb_arena *arena);
  95. /* Deletes this key from the table. Returns true if the key was present. */
  96. bool upb_map_delete(upb_map *map, upb_msgval key);
  97. /* Map iteration:
  98. *
  99. * size_t iter = UPB_MAP_BEGIN;
  100. * while (upb_mapiter_next(map, &iter)) {
  101. * upb_msgval key = upb_mapiter_key(map, iter);
  102. * upb_msgval val = upb_mapiter_value(map, iter);
  103. *
  104. * // If mutating is desired.
  105. * upb_mapiter_setvalue(map, iter, value2);
  106. * }
  107. */
  108. /* Advances to the next entry. Returns false if no more entries are present. */
  109. bool upb_mapiter_next(const upb_map *map, size_t *iter);
  110. /* Returns the key and value for this entry of the map. */
  111. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter);
  112. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter);
  113. /* Sets the value for this entry. The iterator must not be done, and the
  114. * iterator must not have been initialized const. */
  115. void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value);
  116. #include "upb/port_undef.inc"
  117. #endif /* UPB_REFLECTION_H_ */