xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/message/map_gencode_util.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // These functions are only used by generated code.
9 
10 #ifndef UPB_MESSAGE_MAP_GENCODE_UTIL_H_
11 #define UPB_MESSAGE_MAP_GENCODE_UTIL_H_
12 
13 #include "upb/message/internal/map.h"
14 
15 // Must be last.
16 #include "upb/port/def.inc"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 // Message map operations, these get the map from the message first.
23 
_upb_msg_map_key(const void * msg,void * key,size_t size)24 UPB_INLINE void _upb_msg_map_key(const void* msg, void* key, size_t size) {
25   const upb_tabent* ent = (const upb_tabent*)msg;
26   uint32_t u32len;
27   upb_StringView k;
28   k.data = upb_tabstr(ent->key, &u32len);
29   k.size = u32len;
30   _upb_map_fromkey(k, key, size);
31 }
32 
_upb_msg_map_value(const void * msg,void * val,size_t size)33 UPB_INLINE void _upb_msg_map_value(const void* msg, void* val, size_t size) {
34   const upb_tabent* ent = (const upb_tabent*)msg;
35   upb_value v = {ent->val.val};
36   _upb_map_fromvalue(v, val, size);
37 }
38 
_upb_msg_map_set_value(void * msg,const void * val,size_t size)39 UPB_INLINE void _upb_msg_map_set_value(void* msg, const void* val,
40                                        size_t size) {
41   upb_tabent* ent = (upb_tabent*)msg;
42   // This is like _upb_map_tovalue() except the entry already exists
43   // so we can reuse the allocated upb_StringView for string fields.
44   if (size == UPB_MAPTYPE_STRING) {
45     upb_StringView* strp = (upb_StringView*)(uintptr_t)ent->val.val;
46     memcpy(strp, val, sizeof(*strp));
47   } else {
48     memcpy(&ent->val.val, val, size);
49   }
50 }
51 
52 #ifdef __cplusplus
53 } /* extern "C" */
54 #endif
55 
56 #include "upb/port/undef.inc"
57 
58 #endif /* UPB_MESSAGE_MAP_GENCODE_UTIL_H_ */
59