implement some simple array accessors for Dart

add upb_Message_GetOrCreateMutableArray()
add some array wrappers to upbc_so.c

PiperOrigin-RevId: 495976356
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 4f8734694c
commit abed1995a9
  1. 11
      upb/message/accessors.h
  2. 2
      upbc/BUILD
  3. 64
      upbc/upbc_so.c

@ -521,6 +521,17 @@ UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
return (upb_Array*)upb_Message_GetArray(msg, field);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
upb_Message* msg, const upb_MiniTableField* field, upb_CType ctype,
upb_Arena* arena) {
upb_Array* array = upb_Message_GetMutableArray(msg, field);
if (!array) {
array = upb_Array_New(arena, ctype);
_upb_Message_SetField(msg, field, &array, arena);
}
return array;
}
void* upb_Message_ResizeArray(upb_Message* msg, const upb_MiniTableField* field,
size_t len, upb_Arena* arena);

@ -75,10 +75,12 @@ cc_binary(
linkstatic = 1,
visibility = ["//visibility:public"],
deps = [
"//:collections",
"//:mem",
"//:message",
"//:message_accessors",
"//:mini_table",
"//:port",
"//upbc:upbdev",
],
)

@ -27,10 +27,72 @@
// This disables inlining and forces all public functions to be exported to the
// linker. It is used to generate bindings for FFIs from other languages.
#ifndef UPB_BUILD_API
#define UPB_BUILD_API
#endif
#include "upb/mem/arena.h"
#include "upb/collections/array.h"
#include "upb/message/accessors.h"
#include "upb/message/message.h"
#include "upb/mini_table/decode.h"
#include "upbc/upbdev.h"
// Must be last.
#include "upb/port/def.inc"
UPB_API bool upb_Array_AppendBool(upb_Array* array, bool val,
upb_Arena* arena) {
const upb_MessageValue mv = {.bool_val = val};
return upb_Array_Append(array, mv, arena);
}
UPB_API bool upb_Array_AppendDouble(upb_Array* array, double val,
upb_Arena* arena) {
const upb_MessageValue mv = {.double_val = val};
return upb_Array_Append(array, mv, arena);
}
UPB_API bool upb_Array_AppendFloat(upb_Array* array, float val,
upb_Arena* arena) {
const upb_MessageValue mv = {.float_val = val};
return upb_Array_Append(array, mv, arena);
}
UPB_API bool upb_Array_AppendInt32(upb_Array* array, int32_t val,
upb_Arena* arena) {
const upb_MessageValue mv = {.int32_val = val};
return upb_Array_Append(array, mv, arena);
}
UPB_API bool upb_Array_AppendUInt32(upb_Array* array, uint32_t val,
upb_Arena* arena) {
const upb_MessageValue mv = {.uint32_val = val};
return upb_Array_Append(array, mv, arena);
}
////////////////////////////////////////////////////////////////////////////////
UPB_API void upb_Array_SetBool(upb_Array* array, size_t i, bool val) {
const upb_MessageValue mv = {.bool_val = val};
upb_Array_Set(array, i, mv);
}
UPB_API void upb_Array_SetDouble(upb_Array* array, size_t i, double val) {
const upb_MessageValue mv = {.double_val = val};
upb_Array_Set(array, i, mv);
}
UPB_API void upb_Array_SetFloat(upb_Array* array, size_t i, float val) {
const upb_MessageValue mv = {.float_val = val};
upb_Array_Set(array, i, mv);
}
UPB_API void upb_Array_SetInt32(upb_Array* array, size_t i, int32_t val) {
const upb_MessageValue mv = {.int32_val = val};
upb_Array_Set(array, i, mv);
}
UPB_API void upb_Array_SetUInt32(upb_Array* array, size_t i, uint32_t val) {
const upb_MessageValue mv = {.uint32_val = val};
upb_Array_Set(array, i, mv);
}

Loading…
Cancel
Save