| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #ifndef UPB_REFLECTION_H_
- #define UPB_REFLECTION_H_
- #include "upb/def.h"
- #include "upb/msg.h"
- #include "upb/upb.h"
- #include "upb/port_def.inc"
- typedef union {
- bool bool_val;
- float float_val;
- double double_val;
- int32_t int32_val;
- int64_t int64_val;
- uint32_t uint32_val;
- uint64_t uint64_val;
- const upb_map* map_val;
- const upb_msg* msg_val;
- const upb_array* array_val;
- upb_strview str_val;
- } upb_msgval;
- typedef union {
- upb_map* map;
- upb_msg* msg;
- upb_array* array;
- } upb_mutmsgval;
- /** upb_msg *******************************************************************/
- /* Creates a new message of the given type in the given arena. */
- upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a);
- /* Returns the value associated with this field. */
- upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f);
- /* Returns a mutable pointer to a map, array, or submessage value. If the given
- * arena is non-NULL this will construct a new object if it was not previously
- * present. May not be called for primitive fields. */
- upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f, upb_arena *a);
- /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
- bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f);
- /* Sets the given field to the given value. For a msg/array/map/string, the
- * value must be in the same arena. */
- void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
- upb_arena *a);
- /* Clears any field presence and sets the value back to its default. */
- void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f);
- /* Iterate over present fields.
- *
- * size_t iter = UPB_MSG_BEGIN;
- * const upb_fielddef *f;
- * upb_msgval val;
- * while (upb_msg_next(msg, m, ext_pool, &f, &val, &iter)) {
- * process_field(f, val);
- * }
- *
- * If ext_pool is NULL, no extensions will be returned. If the given symtab
- * returns extensions that don't match what is in this message, those extensions
- * will be skipped.
- */
- #define UPB_MSG_BEGIN -1
- bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
- const upb_symtab *ext_pool, const upb_fielddef **f,
- upb_msgval *val, size_t *iter);
- /* Adds unknown data (serialized protobuf data) to the given message. The data
- * is copied into the message instance. */
- void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
- upb_arena *arena);
- /* Returns a reference to the message's unknown data. */
- const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
- /** upb_array *****************************************************************/
- /* Creates a new array on the given arena that holds elements of this type. */
- upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type);
- /* Returns the size of the array. */
- size_t upb_array_size(const upb_array *arr);
- /* Returns the given element, which must be within the array's current size. */
- upb_msgval upb_array_get(const upb_array *arr, size_t i);
- /* Sets the given element, which must be within the array's current size. */
- void upb_array_set(upb_array *arr, size_t i, upb_msgval val);
- /* Appends an element to the array. Returns false on allocation failure. */
- bool upb_array_append(upb_array *array, upb_msgval val, upb_arena *arena);
- /* Changes the size of a vector. New elements are initialized to empty/0.
- * Returns false on allocation failure. */
- bool upb_array_resize(upb_array *array, size_t size, upb_arena *arena);
- /** upb_map *******************************************************************/
- /* Creates a new map on the given arena with the given key/value size. */
- upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
- upb_fieldtype_t value_type);
- /* Returns the number of entries in the map. */
- size_t upb_map_size(const upb_map *map);
- /* Stores a value for the given key into |*val| (or the zero value if the key is
- * not present). Returns whether the key was present. The |val| pointer may be
- * NULL, in which case the function tests whether the given key is present. */
- bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
- /* Removes all entries in the map. */
- void upb_map_clear(upb_map *map);
- /* Sets the given key to the given value. Returns true if this was a new key in
- * the map, or false if an existing key was replaced. */
- bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
- upb_arena *arena);
- /* Deletes this key from the table. Returns true if the key was present. */
- bool upb_map_delete(upb_map *map, upb_msgval key);
- /* Map iteration:
- *
- * size_t iter = UPB_MAP_BEGIN;
- * while (upb_mapiter_next(map, &iter)) {
- * upb_msgval key = upb_mapiter_key(map, iter);
- * upb_msgval val = upb_mapiter_value(map, iter);
- *
- * // If mutating is desired.
- * upb_mapiter_setvalue(map, iter, value2);
- * }
- */
- /* Advances to the next entry. Returns false if no more entries are present. */
- bool upb_mapiter_next(const upb_map *map, size_t *iter);
- /* Returns the key and value for this entry of the map. */
- upb_msgval upb_mapiter_key(const upb_map *map, size_t iter);
- upb_msgval upb_mapiter_value(const upb_map *map, size_t iter);
- /* Sets the value for this entry. The iterator must not be done, and the
- * iterator must not have been initialized const. */
- void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value);
- #include "upb/port_undef.inc"
- #endif /* UPB_REFLECTION_H_ */
|