2 #ifndef UPB_LEGACY_MSG_REFLECTION_H_
3 #define UPB_LEGACY_MSG_REFLECTION_H_
8 #include "upb/port_def.inc"
11 typedef struct upb_map upb_map;
14 typedef struct upb_mapiter upb_mapiter;
16 /** upb_msgval ****************************************************************/
18 /* A union representing all possible protobuf values. Used for generic get/set
36 #define ACCESSORS(name, membername, ctype) \
37 UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
38 return v.membername; \
40 UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
41 v->membername = cval; \
43 UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
49 ACCESSORS(bool, b, bool)
50 ACCESSORS(float, flt, float)
51 ACCESSORS(double, dbl, double)
52 ACCESSORS(int32, i32, int32_t)
53 ACCESSORS(int64, i64, int64_t)
54 ACCESSORS(uint32, u32, uint32_t)
55 ACCESSORS(uint64, u64, uint64_t)
56 ACCESSORS(map, map, const upb_map*)
57 ACCESSORS(msg, msg, const upb_msg*)
58 ACCESSORS(ptr, ptr, const void*)
59 ACCESSORS(arr, arr, const upb_array*)
60 ACCESSORS(str, str, upb_strview)
64 UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
65 return upb_msgval_str(upb_strview_make(data, size));
68 /** upb_msg *******************************************************************/
70 /* A upb_msg represents a protobuf message. It always corresponds to a specific
71 * upb_msglayout, which describes how it is laid out in memory. */
73 /* Read-only message API. Can be safely called by anyone. */
75 /* Returns the value associated with this field:
76 * - for scalar fields (including strings), the value directly.
77 * - return upb_msg*, or upb_map* for msg/map.
78 * If the field is unset for these field types, returns NULL.
80 * TODO(haberman): should we let users store cached array/map/msg
81 * pointers here for fields that are unset? Could be useful for the
82 * strongly-owned submessage model (ie. generated C API that doesn't use
85 upb_msgval upb_msg_get(const upb_msg *msg,
87 const upb_msglayout *l);
89 /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
90 bool upb_msg_has(const upb_msg *msg,
92 const upb_msglayout *l);
94 /* Mutable message API. May only be called by the owner of the message who
95 * knows its ownership scheme and how to keep it consistent. */
97 /* Sets the given field to the given value. Does not perform any memory
98 * management: if you overwrite a pointer to a msg/array/map/string without
99 * cleaning it up (or using an arena) it will leak.
101 void upb_msg_set(upb_msg *msg,
104 const upb_msglayout *l);
106 /* For a primitive field, set it back to its default. For repeated, string, and
107 * submessage fields set it back to NULL. This could involve releasing some
108 * internal memory (for example, from an extension dictionary), but it is not
109 * recursive in any way and will not recover any memory that may be used by
110 * arrays/maps/strings/msgs that this field may have pointed to.
112 bool upb_msg_clearfield(upb_msg *msg,
114 const upb_msglayout *l);
116 /* TODO(haberman): copyfrom()/mergefrom()? */
118 /** upb_array *****************************************************************/
120 /* A upb_array stores data for a repeated field. The memory management
121 * semantics are the same as upb_msg. A upb_array allocates dynamic
122 * memory internally for the array elements. */
124 upb_fieldtype_t upb_array_type(const upb_array *arr);
126 /* Read-only interface. Safe for anyone to call. */
128 size_t upb_array_size(const upb_array *arr);
129 upb_msgval upb_array_get(const upb_array *arr, upb_fieldtype_t type, size_t i);
131 /* Write interface. May only be called by the message's owner who can enforce
132 * its memory management invariants. */
134 bool upb_array_set(upb_array *arr, upb_fieldtype_t type, size_t i,
135 upb_msgval val, upb_arena *arena);
137 /** upb_map *******************************************************************/
139 /* A upb_map stores data for a map field. The memory management semantics are
140 * the same as upb_msg, with one notable exception. upb_map will internally
141 * store a copy of all string keys, but *not* any string values or submessages.
142 * So you must ensure that any string or message values outlive the map, and you
143 * must delete them manually when they are no longer required. */
145 upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
148 /* Read-only interface. Safe for anyone to call. */
150 size_t upb_map_size(const upb_map *map);
151 upb_fieldtype_t upb_map_keytype(const upb_map *map);
152 upb_fieldtype_t upb_map_valuetype(const upb_map *map);
153 bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
155 /* Write interface. May only be called by the message's owner who can enforce
156 * its memory management invariants. */
158 /* Sets or overwrites an entry in the map. Return value indicates whether
159 * the operation succeeded or failed with OOM, and also whether an existing
160 * key was replaced or not. */
161 bool upb_map_set(upb_map *map,
162 upb_msgval key, upb_msgval val,
163 upb_msgval *valremoved);
165 /* Deletes an entry in the map. Returns true if the key was present. */
166 bool upb_map_del(upb_map *map, upb_msgval key);
168 /** upb_mapiter ***************************************************************/
170 /* For iterating over a map. Map iterators are invalidated by mutations to the
171 * map, but an invalidated iterator will never return junk or crash the process.
172 * An invalidated iterator may return entries that were already returned though,
173 * and if you keep invalidating the iterator during iteration, the program may
174 * enter an infinite loop. */
176 size_t upb_mapiter_sizeof(void);
178 void upb_mapiter_begin(upb_mapiter *i, const upb_map *t);
179 upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a);
180 void upb_mapiter_free(upb_mapiter *i, upb_alloc *a);
181 void upb_mapiter_next(upb_mapiter *i);
182 bool upb_mapiter_done(const upb_mapiter *i);
184 upb_msgval upb_mapiter_key(const upb_mapiter *i);
185 upb_msgval upb_mapiter_value(const upb_mapiter *i);
186 void upb_mapiter_setdone(upb_mapiter *i);
187 bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2);
189 #include "upb/port_undef.inc"
191 #endif /* UPB_LEGACY_MSG_REFLECTION_H_ */