• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
9 #define UPB_MESSAGE_INTERNAL_EXTENSION_H_
10 
11 #include <stddef.h>
12 
13 #include "upb/mem/arena.h"
14 #include "upb/message/value.h"
15 #include "upb/mini_table/extension.h"
16 
17 // Must be last.
18 #include "upb/port/def.inc"
19 
20 // The internal representation of an extension is self-describing: it contains
21 // enough information that we can serialize it to binary format without needing
22 // to look it up in a upb_ExtensionRegistry.
23 //
24 // This representation allocates 16 bytes to data on 64-bit platforms.
25 // This is rather wasteful for scalars (in the extreme case of bool,
26 // it wastes 15 bytes). We accept this because we expect messages to be
27 // the most common extension type.
28 typedef struct {
29   const upb_MiniTableExtension* ext;
30   upb_MessageValue data;
31 } upb_Extension;
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 // Adds the given extension data to the given message.
38 // |ext| is copied into the message instance.
39 // This logically replaces any previously-added extension with this number.
40 upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
41     struct upb_Message* msg, const upb_MiniTableExtension* ext,
42     upb_Arena* arena);
43 
44 // Returns an array of extensions for this message.
45 // Note: the array is ordered in reverse relative to the order of creation.
46 const upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
47     const struct upb_Message* msg, size_t* count);
48 
49 // Returns an extension for a message with a given mini table,
50 // or NULL if no extension exists with this mini table.
51 const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
52     const struct upb_Message* msg, const upb_MiniTableExtension* ext);
53 
54 #ifdef __cplusplus
55 } /* extern "C" */
56 #endif
57 
58 #include "upb/port/undef.inc"
59 
60 #endif /* UPB_MESSAGE_INTERNAL_EXTENSION_H_ */
61