upb/upb/reflection.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of Google LLC nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_REFLECTION_H_
29 #define UPB_REFLECTION_H_
30 
31 #include "upb/def.h"
32 #include "upb/msg.h"
33 #include "upb/port_def.inc"
34 #include "upb/upb.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 typedef union {
41  bool bool_val;
42  float float_val;
43  double double_val;
48  const upb_Map* map_val;
53 
54 typedef union {
59 
61 
65 /* Creates a new message of the given type in the given arena. */
67 
68 /* Returns the value associated with this field. */
70 
71 /* Returns a mutable pointer to a map, array, or submessage value. If the given
72  * arena is non-NULL this will construct a new object if it was not previously
73  * present. May not be called for primitive fields. */
75  const upb_FieldDef* f,
76  upb_Arena* a);
77 
78 /* May only be called for fields where upb_FieldDef_HasPresence(f) == true. */
79 bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f);
80 
81 /* Returns the field that is set in the oneof, or NULL if none are set. */
83  const upb_OneofDef* o);
84 
85 /* Sets the given field to the given value. For a msg/array/map/string, the
86  * caller must ensure that the target data outlives |msg| (by living either in
87  * the same arena or a different arena that outlives it).
88  *
89  * Returns false if allocation fails. */
92 
93 /* Clears any field presence and sets the value back to its default. */
95 
96 /* Clear all data and unknown fields. */
98 
99 /* Iterate over present fields.
100  *
101  * size_t iter = kUpb_Message_Begin;
102  * const upb_FieldDef *f;
103  * upb_MessageValue val;
104  * while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
105  * process_field(f, val);
106  * }
107  *
108  * If ext_pool is NULL, no extensions will be returned. If the given symtab
109  * returns extensions that don't match what is in this message, those extensions
110  * will be skipped.
111  */
112 
113 #define kUpb_Message_Begin -1
114 bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
115  const upb_DefPool* ext_pool, const upb_FieldDef** f,
116  upb_MessageValue* val, size_t* iter);
117 
118 /* Clears all unknown field data from this message and all submessages. */
120  int maxdepth);
121 
124 /* Creates a new array on the given arena that holds elements of this type. */
126 
127 /* Returns the size of the array. */
128 size_t upb_Array_Size(const upb_Array* arr);
129 
130 /* Returns the given element, which must be within the array's current size. */
131 upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
132 
133 /* Sets the given element, which must be within the array's current size. */
134 void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
135 
136 /* Appends an element to the array. Returns false on allocation failure. */
138 
139 /* Moves elements within the array using memmove(). Like memmove(), the source
140  * and destination elements may be overlapping. */
141 void upb_Array_Move(upb_Array* array, size_t dst_idx, size_t src_idx,
142  size_t count);
143 
144 /* Inserts one or more empty elements into the array. Existing elements are
145  * shifted right. The new elements have undefined state and must be set with
146  * `upb_Array_Set()`.
147  * REQUIRES: `i <= upb_Array_Size(arr)` */
148 bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
149  upb_Arena* arena);
150 
151 /* Deletes one or more elements from the array. Existing elements are shifted
152  * left.
153  * REQUIRES: `i + count <= upb_Array_Size(arr)` */
154 void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
155 
156 /* Changes the size of a vector. New elements are initialized to empty/0.
157  * Returns false on allocation failure. */
159 
162 /* Creates a new map on the given arena with the given key/value size. */
164 
165 /* Returns the number of entries in the map. */
166 size_t upb_Map_Size(const upb_Map* map);
167 
168 /* Stores a value for the given key into |*val| (or the zero value if the key is
169  * not present). Returns whether the key was present. The |val| pointer may be
170  * NULL, in which case the function tests whether the given key is present. */
172  upb_MessageValue* val);
173 
174 /* Removes all entries in the map. */
175 void upb_Map_Clear(upb_Map* map);
176 
177 /* Sets the given key to the given value. Returns true if this was a new key in
178  * the map, or false if an existing key was replaced. */
180  upb_Arena* arena);
181 
182 /* Deletes this key from the table. Returns true if the key was present. */
184 
185 /* Map iteration:
186  *
187  * size_t iter = kUpb_Map_Begin;
188  * while (upb_MapIterator_Next(map, &iter)) {
189  * upb_MessageValue key = upb_MapIterator_Key(map, iter);
190  * upb_MessageValue val = upb_MapIterator_Value(map, iter);
191  *
192  * // If mutating is desired.
193  * upb_MapIterator_SetValue(map, iter, value2);
194  * }
195  */
196 
197 /* Advances to the next entry. Returns false if no more entries are present. */
198 bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
199 
200 /* Returns true if the iterator still points to a valid entry, or false if the
201  * iterator is past the last element. It is an error to call this function with
202  * kUpb_Map_Begin (you must call next() at least once first). */
203 bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
204 
205 /* Returns the key and value for this entry of the map. */
208 
209 /* Sets the value for this entry. The iterator must not be done, and the
210  * iterator must not have been initialized const. */
213 
214 #ifdef __cplusplus
215 } /* extern "C" */
216 #endif
217 
218 #include "upb/port_undef.inc"
219 
220 #endif /* UPB_REFLECTION_H_ */
upb_MutableMessageValue::array
upb_Array * array
Definition: upb/upb/reflection.h:57
upb_MessageValue::uint32_val
uint32_t uint32_val
Definition: upb/upb/reflection.h:46
upb_MessageValue::int64_val
int64_t int64_val
Definition: upb/upb/reflection.h:45
upb_CType
upb_CType
Definition: upb/upb/upb.h:286
upb_MapIterator_Value
upb_MessageValue upb_MapIterator_Value(const upb_Map *map, size_t iter)
Definition: reflection.c:470
upb_MutableMessageValue::map
upb_Map * map
Definition: upb/upb/reflection.h:55
upb_MessageDef
Definition: upb/upb/def.c:100
upb_Array_Size
size_t upb_Array_Size(const upb_Array *arr)
Definition: reflection.c:362
upb_MessageValue::str_val
upb_StringView str_val
Definition: upb/upb/reflection.h:51
upb_Message_ClearField
void upb_Message_ClearField(upb_Message *msg, const upb_FieldDef *f)
Definition: reflection.c:222
upb_OneofDef
Definition: upb/upb/def.c:157
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
upb_MessageValue::int32_val
int32_t int32_val
Definition: upb/upb/reflection.h:44
upb_Message_Next
bool upb_Message_Next(const upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *ext_pool, const upb_FieldDef **f, upb_MessageValue *val, size_t *iter)
Definition: reflection.c:245
upb_Array_Set
void upb_Array_Set(upb_Array *arr, size_t i, upb_MessageValue val)
Definition: reflection.c:373
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
upb_MutableMessageValue::msg
upb_Message * msg
Definition: upb/upb/reflection.h:56
upb_MessageValue::double_val
double double_val
Definition: upb/upb/reflection.h:43
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
msg.h
upb_MessageValue::array_val
const upb_Array * array_val
Definition: upb/upb/reflection.h:50
upb_Message_WhichOneof
const upb_FieldDef * upb_Message_WhichOneof(const upb_Message *msg, const upb_OneofDef *o)
Definition: reflection.c:131
upb_MessageValue::bool_val
bool bool_val
Definition: upb/upb/reflection.h:41
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
upb_Array_Resize
bool upb_Array_Resize(upb_Array *array, size_t size, upb_Arena *arena)
Definition: reflection.c:419
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
upb_MessageValue
Definition: upb/upb/reflection.h:40
upb_Map_Get
bool upb_Map_Get(const upb_Map *map, upb_MessageValue key, upb_MessageValue *val)
Definition: reflection.c:432
upb_Array_Move
void upb_Array_Move(upb_Array *array, size_t dst_idx, size_t src_idx, size_t count)
Definition: reflection.c:388
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
upb_FieldDef_Default
upb_MessageValue upb_FieldDef_Default(const upb_FieldDef *f)
Definition: upb/upb/def.c:581
array
Definition: undname.c:101
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
upb_Message_DiscardUnknown
bool upb_Message_DiscardUnknown(upb_Message *msg, const upb_MessageDef *m, int maxdepth)
Definition: reflection.c:351
upb.h
upb_MapIterator_Next
bool upb_MapIterator_Next(const upb_Map *map, size_t *iter)
Definition: reflection.c:448
upb_Array
Definition: msg_internal.h:424
upb_MessageValue::uint64_val
uint64_t uint64_val
Definition: upb/upb/reflection.h:47
upb_Map_Size
size_t upb_Map_Size(const upb_Map *map)
Definition: reflection.c:430
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
upb_Message_Has
bool upb_Message_Has(const upb_Message *msg, const upb_FieldDef *f)
Definition: reflection.c:112
upb_Message_Clear
void upb_Message_Clear(upb_Message *msg, const upb_MessageDef *m)
Definition: reflection.c:241
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
upb_Array_Delete
void upb_Array_Delete(upb_Array *array, size_t i, size_t count)
Definition: reflection.c:411
upb_MessageValue::msg_val
const upb_Message * msg_val
Definition: upb/upb/reflection.h:49
upb_MapIterator_Key
upb_MessageValue upb_MapIterator_Key(const upb_Map *map, size_t iter)
Definition: reflection.c:461
upb_Message
void upb_Message
Definition: msg.h:49
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
upb_Map_Clear
void upb_Map_Clear(upb_Map *map)
Definition: reflection.c:437
upb_FieldDef
Definition: upb/upb/def.c:56
upb_MessageValue::map_val
const upb_Map * map_val
Definition: upb/upb/reflection.h:48
value
const char * value
Definition: hpack_parser_table.cc:165
upb_Message_Set
bool upb_Message_Set(upb_Message *msg, const upb_FieldDef *f, upb_MessageValue val, upb_Arena *a)
Definition: reflection.c:202
key
const char * key
Definition: hpack_parser_table.cc:164
upb_Map_New
upb_Map * upb_Map_New(upb_Arena *a, upb_CType key_type, upb_CType value_type)
Definition: reflection.c:425
upb_StringView
Definition: upb/upb/upb.h:72
upb_Message_Get
upb_MessageValue upb_Message_Get(const upb_Message *msg, const upb_FieldDef *f)
Definition: reflection.c:146
upb_Array_Append
bool upb_Array_Append(upb_Array *array, upb_MessageValue val, upb_Arena *arena)
Definition: reflection.c:380
def.h
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
upb_Array_New
upb_Array * upb_Array_New(upb_Arena *a, upb_CType type)
Definition: reflection.c:358
upb_Map_Delete
bool upb_Map_Delete(upb_Map *map, upb_MessageValue key)
Definition: reflection.c:444
upb_Message_New
upb_Message * upb_Message_New(const upb_MessageDef *m, upb_Arena *a)
Definition: reflection.c:95
upb_MessageValue::float_val
float float_val
Definition: upb/upb/reflection.h:42
upb_MapIterator_SetValue
void upb_MapIterator_SetValue(upb_Map *map, size_t iter, upb_MessageValue value)
upb_Map_Set
bool upb_Map_Set(upb_Map *map, upb_MessageValue key, upb_MessageValue val, upb_Arena *arena)
Definition: reflection.c:439
upb_MutableMessageValue
Definition: upb/upb/reflection.h:54
upb_Map
Definition: msg_internal.h:581
upb_MapIterator_Done
bool upb_MapIterator_Done(const upb_Map *map, size_t iter)
Definition: reflection.c:452
iter
Definition: test_winkernel.cpp:47
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
key_type
upb_fieldtype_t key_type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1071
regress.m
m
Definition: regress/regress.py:25
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
upb_Array_Get
upb_MessageValue upb_Array_Get(const upb_Array *arr, size_t i)
Definition: reflection.c:364
upb_DefPool
Definition: upb/upb/def.c:217
upb_Message_Mutable
upb_MutableMessageValue upb_Message_Mutable(upb_Message *msg, const upb_FieldDef *f, upb_Arena *a)
Definition: reflection.c:164
upb_Arena
Definition: upb_internal.h:36
upb_Array_Insert
bool upb_Array_Insert(upb_Array *array, size_t i, size_t count, upb_Arena *arena)
Definition: reflection.c:395
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:01