upb: implement upb_Message_FindExtensionByNumber()

PiperOrigin-RevId: 590420044
pull/15047/head
Eric Salo 1 year ago committed by Copybara-Service
parent e1d3008eaf
commit 088fee7f28
  1. 4
      upb/message/BUILD
  2. 30
      upb/message/compat.c
  3. 37
      upb/message/compat.h
  4. 4
      upb/message/internal/extension.h
  5. 2
      upb/message/message.h

@ -140,8 +140,12 @@ cc_library(
cc_library(
name = "message",
srcs = [
"compat.c",
],
hdrs = [
"array.h",
"compat.h",
"map.h",
"message.h",
],

@ -0,0 +1,30 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/message/compat.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/message/internal/extension.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
const upb_Message_Extension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number) {
size_t count = 0;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
while (count--) {
if (upb_MiniTableExtension_Number(ext->ext) == field_number) return ext;
ext++;
}
return NULL;
}

@ -0,0 +1,37 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_COMPAT_H_
#define UPB_MESSAGE_COMPAT_H_
#include <stdint.h>
#include "upb/message/message.h"
// Must be last.
#include "upb/port/def.inc"
// upb does not support mixing minitables from different sources but these
// functions are still used by some existing users so for now we make them
// available here. This may or may not change in the future so do not add
// them to new code.
#ifdef __cplusplus
extern "C" {
#endif
// Returns the extension with the given field number, or NULL on failure.
const upb_Message_Extension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_COMPAT_H_ */

@ -24,14 +24,14 @@
// This is rather wasteful for scalars (in the extreme case of bool,
// it wastes 15 bytes). We accept this because we expect messages to be
// the most common extension type.
typedef struct {
struct upb_Message_Extension {
const upb_MiniTableExtension* ext;
union {
upb_StringView str;
void* ptr;
char scalar_data[8];
} data;
} upb_Message_Extension;
};
#ifdef __cplusplus
extern "C" {

@ -21,6 +21,8 @@
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Message_Extension upb_Message_Extension;
#ifdef __cplusplus
extern "C" {
#endif

Loading…
Cancel
Save