Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-04 09:27:21

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
0009 #define UPB_MESSAGE_INTERNAL_EXTENSION_H_
0010 
0011 #include <stddef.h>
0012 
0013 #include "upb/mem/arena.h"
0014 #include "upb/message/internal/array.h"
0015 #include "upb/message/internal/map.h"
0016 #include "upb/message/internal/types.h"
0017 #include "upb/message/value.h"
0018 #include "upb/mini_table/extension.h"
0019 #include "upb/mini_table/internal/field.h"
0020 
0021 // Must be last.
0022 #include "upb/port/def.inc"
0023 
0024 // The internal representation of an extension is self-describing: it contains
0025 // enough information that we can serialize it to binary format without needing
0026 // to look it up in a upb_ExtensionRegistry.
0027 //
0028 // This representation allocates 16 bytes to data on 64-bit platforms.
0029 // This is rather wasteful for scalars (in the extreme case of bool,
0030 // it wastes 15 bytes). We accept this because we expect messages to be
0031 // the most common extension type.
0032 typedef struct {
0033   const upb_MiniTableExtension* ext;
0034   upb_MessageValue data;
0035 } upb_Extension;
0036 
0037 #ifdef __cplusplus
0038 extern "C" {
0039 #endif
0040 
0041 // Adds the given extension data to the given message.
0042 // |ext| is copied into the message instance.
0043 // This logically replaces any previously-added extension with this number.
0044 upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
0045     struct upb_Message* msg, const upb_MiniTableExtension* ext,
0046     upb_Arena* arena);
0047 
0048 // Returns an extension for a message with a given mini table,
0049 // or NULL if no extension exists with this mini table.
0050 const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
0051     const struct upb_Message* msg, const upb_MiniTableExtension* ext);
0052 
0053 UPB_INLINE bool UPB_PRIVATE(_upb_Extension_IsEmpty)(const upb_Extension* ext) {
0054   switch (
0055       UPB_PRIVATE(_upb_MiniTableField_Mode)(&ext->ext->UPB_PRIVATE(field))) {
0056     case kUpb_FieldMode_Scalar:
0057       return false;
0058     case kUpb_FieldMode_Array:
0059       return upb_Array_Size(ext->data.array_val) == 0;
0060     case kUpb_FieldMode_Map:
0061       return _upb_Map_Size(ext->data.map_val) == 0;
0062   }
0063   UPB_UNREACHABLE();
0064 }
0065 
0066 #ifdef __cplusplus
0067 } /* extern "C" */
0068 #endif
0069 
0070 #include "upb/port/undef.inc"
0071 
0072 #endif /* UPB_MESSAGE_INTERNAL_EXTENSION_H_ */