Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / upb / upb / legacy_msg_reflection.h
1
2 #ifndef UPB_LEGACY_MSG_REFLECTION_H_
3 #define UPB_LEGACY_MSG_REFLECTION_H_
4
5 #include "upb/upb.h"
6 #include "upb/msg.h"
7
8 #include "upb/port_def.inc"
9
10 struct upb_map;
11 typedef struct upb_map upb_map;
12
13 struct upb_mapiter;
14 typedef struct upb_mapiter upb_mapiter;
15
16 /** upb_msgval ****************************************************************/
17
18 /* A union representing all possible protobuf values.  Used for generic get/set
19  * operations. */
20
21 typedef union {
22   bool b;
23   float flt;
24   double dbl;
25   int32_t i32;
26   int64_t i64;
27   uint32_t u32;
28   uint64_t u64;
29   const upb_map* map;
30   const upb_msg* msg;
31   const upb_array* arr;
32   const void* ptr;
33   upb_strview str;
34 } upb_msgval;
35
36 #define ACCESSORS(name, membername, ctype) \
37   UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
38     return v.membername; \
39   } \
40   UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
41     v->membername = cval; \
42   } \
43   UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
44     upb_msgval ret; \
45     ret.membername = v; \
46     return ret; \
47   }
48
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)
61
62 #undef ACCESSORS
63
64 UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
65   return upb_msgval_str(upb_strview_make(data, size));
66 }
67
68 /** upb_msg *******************************************************************/
69
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.  */
72
73 /* Read-only message API.  Can be safely called by anyone. */
74
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.
79  *
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
83  * arenas).
84  */
85 upb_msgval upb_msg_get(const upb_msg *msg,
86                        int field_index,
87                        const upb_msglayout *l);
88
89 /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
90 bool upb_msg_has(const upb_msg *msg,
91                  int field_index,
92                  const upb_msglayout *l);
93
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. */
96
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.
100  */
101 void upb_msg_set(upb_msg *msg,
102                  int field_index,
103                  upb_msgval val,
104                  const upb_msglayout *l);
105
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.
111  */
112 bool upb_msg_clearfield(upb_msg *msg,
113                         int field_index,
114                         const upb_msglayout *l);
115
116 /* TODO(haberman): copyfrom()/mergefrom()? */
117
118 /** upb_array *****************************************************************/
119
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. */
123
124 upb_fieldtype_t upb_array_type(const upb_array *arr);
125
126 /* Read-only interface.  Safe for anyone to call. */
127
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);
130
131 /* Write interface.  May only be called by the message's owner who can enforce
132  * its memory management invariants. */
133
134 bool upb_array_set(upb_array *arr, upb_fieldtype_t type, size_t i,
135                    upb_msgval val, upb_arena *arena);
136
137 /** upb_map *******************************************************************/
138
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. */
144
145 upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
146                      upb_arena *a);
147
148 /* Read-only interface.  Safe for anyone to call. */
149
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);
154
155 /* Write interface.  May only be called by the message's owner who can enforce
156  * its memory management invariants. */
157
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);
164
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);
167
168 /** upb_mapiter ***************************************************************/
169
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. */
175
176 size_t upb_mapiter_sizeof(void);
177
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);
183
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);
188
189 #include "upb/port_undef.inc"
190
191 #endif /* UPB_LEGACY_MSG_REFLECTION_H_ */