@ -227,6 +227,30 @@ std::string CTypeConst(const protobuf::FieldDescriptor* field) {
return CTypeInternal ( field , true ) ;
}
std : : string MapKeyCType ( const protobuf : : FieldDescriptor * map_field ) {
return CType ( map_field - > message_type ( ) - > map_key ( ) ) ;
}
std : : string MapValueCType ( const protobuf : : FieldDescriptor * map_field ) {
return CType ( map_field - > message_type ( ) - > map_value ( ) ) ;
}
std : : string MapKeySize ( const protobuf : : FieldDescriptor * map_field ,
absl : : string_view expr ) {
return map_field - > message_type ( ) - > map_key ( ) - > cpp_type ( ) = =
protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: absl : : StrCat ( " sizeof( " , expr , " ) " ) ;
}
std : : string MapValueSize ( const protobuf : : FieldDescriptor * map_field ,
absl : : string_view expr ) {
return map_field - > message_type ( ) - > map_value ( ) - > cpp_type ( ) = =
protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: absl : : StrCat ( " sizeof( " , expr , " ) " ) ;
}
std : : string FieldInitializer ( const FileLayout & layout ,
const protobuf : : FieldDescriptor * field ) ;
@ -436,38 +460,39 @@ void GenerateMapGetters(const protobuf::FieldDescriptor* field,
const FileLayout & layout , absl : : string_view msg_name ,
const NameToFieldDescriptorMap & field_names ,
Output & output ) {
const protobuf : : Descriptor * entry = field - > message_type ( ) ;
const protobuf : : FieldDescriptor * key = entry - > FindFieldByNumber ( 1 ) ;
const protobuf : : FieldDescriptor * val = entry - > FindFieldByNumber ( 2 ) ;
std : : string resolved_name = ResolveFieldName ( field , field_names ) ;
output (
R " cc(
UPB_INLINE size_t $ 0 _ $ 1 _size ( const $ 0 * msg ) {
return _upb_msg_map_size ( msg , $ 2 ) ;
const upb_MiniTableField field = $ 2 ;
const upb_Map * map = upb_Message_GetMap ( msg , & field ) ;
return map ? _upb_Map_Size ( map ) : 0 ;
}
) cc " ,
msg_name , resolved_name , layout . GetFieldOffset ( field ) ) ;
msg_name , resolved_name , FieldInitializer ( layout , field ) ) ;
output (
R " cc(
UPB_INLINE bool $ 0 _ $ 1 _get ( const $ 0 * msg , $ 2 key , $ 3 * val ) {
return _upb_msg_map_get ( msg , $ 4 , & key , $ 5 , val , $ 6 ) ;
const upb_MiniTableField field = $ 4 ;
const upb_Map * map = upb_Message_GetMap ( msg , & field ) ;
if ( ! map ) return false ;
return _upb_Map_Get ( map , & key , $ 5 , val , $ 6 ) ;
}
) cc " ,
msg_name , resolved_name , CType ( key ) , CType ( val ) ,
layout . GetFieldOffset ( field ) ,
key - > cpp_type ( ) = = protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: " sizeof(key) " ,
val - > cpp_type ( ) = = protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: " sizeof(*val) " ) ;
msg_name , resolved_name , MapKeyCType ( field ) , MapValueCType ( field ) ,
FieldInitializer ( layout , field ) , MapKeySize ( field , " key " ) ,
MapValueSize ( field , " *val " ) ) ;
output (
R " cc(
UPB_INLINE $ 0 $ 1 _ $ 2 _next ( const $ 1 * msg , size_t * iter ) {
return ( $ 0 ) _upb_msg_map_next ( msg , $ 3 , iter ) ;
const upb_MiniTableField field = $ 3 ;
const upb_Map * map = upb_Message_GetMap ( msg , & field ) ;
if ( ! map ) return NULL ;
return ( $ 0 ) _upb_map_next ( map , iter ) ;
}
) cc " ,
CTypeConst ( field ) , msg_name , resolved_name , layout . GetFieldOffset ( field ) ) ;
CTypeConst ( field ) , msg_name , resolved_name ,
FieldInitializer ( layout , field ) ) ;
}
void GenerateMapEntryGetters ( const protobuf : : FieldDescriptor * field ,
@ -539,46 +564,50 @@ void GenerateMapSetters(const protobuf::FieldDescriptor* field,
const FileLayout & layout , absl : : string_view msg_name ,
const NameToFieldDescriptorMap & field_names ,
Output & output ) {
const protobuf : : Descriptor * entry = field - > message_type ( ) ;
const protobuf : : FieldDescriptor * key = entry - > FindFieldByNumber ( 1 ) ;
const protobuf : : FieldDescriptor * val = entry - > FindFieldByNumber ( 2 ) ;
std : : string resolved_name = ResolveFieldName ( field , field_names ) ;
output (
R " cc(
UPB_INLINE void $ 0 _ $ 1 _clear ( $ 0 * msg ) { _upb_msg_map_clear ( msg , $ 2 ) ; }
UPB_INLINE void $ 0 _ $ 1 _clear ( $ 0 * msg ) {
const upb_MiniTableField field = $ 2 ;
upb_Map * map = ( upb_Map * ) upb_Message_GetMap ( msg , & field ) ;
if ( ! map ) return ;
_upb_Map_Clear ( map ) ;
}
) cc " ,
msg_name , resolved_name , layout . GetFieldOffset ( field ) ) ;
msg_name , resolved_name , FieldInitializer ( layout , field ) ) ;
output (
R " cc(
UPB_INLINE bool $ 0 _ $ 1 _set ( $ 0 * msg , $ 2 key , $ 3 val , upb_Arena * a ) {
return _upb_msg_map_set ( msg , $ 4 , & key , $ 5 , & val , $ 6 , a ) ;
const upb_MiniTableField field = $ 4 ;
upb_Map * map = _upb_MiniTable_GetOrCreateMutableMap ( msg , & field , $ 5 , $ 6 , a ) ;
return _upb_Map_Insert ( map , & key , $ 5 , & val , $ 6 , a ) ! =
kUpb_MapInsertStatus_OutOfMemory ;
}
) cc " ,
msg_name , resolved_name , CType ( key ) , CType ( val ) ,
layout . GetFieldOffset ( field ) ,
key - > cpp_type ( ) = = protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: " sizeof(key) " ,
val - > cpp_type ( ) = = protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: " sizeof(val) " ) ;
msg_name , resolved_name , MapKeyCType ( field ) , MapValueCType ( field ) ,
FieldInitializer ( layout , field ) , MapKeySize ( field , " key " ) ,
MapValueSize ( field , " val " ) ) ;
output (
R " cc(
UPB_INLINE bool $ 0 _ $ 1 _delete ( $ 0 * msg , $ 2 key ) {
return _upb_msg_map_delete ( msg , $ 3 , & key , $ 4 ) ;
const upb_MiniTableField field = $ 3 ;
upb_Map * map = ( upb_Map * ) upb_Message_GetMap ( msg , & field ) ;
if ( ! map ) return false ;
return _upb_Map_Delete ( map , & key , $ 4 ) ;
}
) cc " ,
msg_name , resolved_name , CType ( key ) , layout . GetFieldOffset ( field ) ,
key - > cpp_type ( ) = = protobuf : : FieldDescriptor : : CPPTYPE_STRING
? " 0 "
: " sizeof(key) " ) ;
msg_name , resolved_name , MapKeyCType ( field ) ,
FieldInitializer ( layout , field ) , MapKeySize ( field , " key " ) ) ;
output (
R " cc(
UPB_INLINE $ 0 $ 1 _ $ 2 _nextmutable ( $ 1 * msg , size_t * iter ) {
return ( $ 0 ) _upb_msg_map_next ( msg , $ 3 , iter ) ;
const upb_MiniTableField field = $ 3 ;
upb_Map * map = ( upb_Map * ) upb_Message_GetMap ( msg , & field ) ;
if ( ! map ) return NULL ;
return ( $ 0 ) _upb_map_next ( map , iter ) ;
}
) cc " ,
CType ( field ) , msg_name , resolved_name , layout . GetFieldOffset ( field ) ) ;
CType ( field ) , msg_name , resolved_name , FieldInitializer ( layout , field ) ) ;
}
void GenerateRepeatedSetters ( const protobuf : : FieldDescriptor * field ,