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