upb: implement most of the non-universal setters

PiperOrigin-RevId: 631620526
pull/16771/head
Eric Salo 9 months ago committed by Copybara-Service
parent 4d3a98c27e
commit d923896299
  1. 255
      upb/message/internal/accessors.h
  2. 5
      upb/mini_table/internal/extension.h

@ -220,7 +220,6 @@ UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
// const upb_MiniTableField* field,
// bool value, upb_Arena* a) {
// UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bool);
// UPB_ASSUME(upb_MiniTableField_IsScalar(field));
// UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
// kUpb_FieldRep_1Byte);
// upb_Message_SetField(msg, field, &value, a);
@ -351,8 +350,8 @@ UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg,
const upb_MiniTableField* f,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_MessageValue def;
def.bool_val = default_val;
return upb_Message_GetField(msg, f, def).bool_val;
@ -362,8 +361,8 @@ UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg,
const upb_MiniTableField* f,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.double_val = default_val;
@ -374,8 +373,8 @@ UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg,
const upb_MiniTableField* f,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.float_val = default_val;
@ -387,8 +386,8 @@ UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.int32_val = default_val;
@ -399,8 +398,8 @@ UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.int64_val = default_val;
@ -551,7 +550,6 @@ upb_Message_GetString(const struct upb_Message* msg,
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.str_val = default_val;
@ -562,8 +560,8 @@ UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.uint32_val = default_val;
@ -574,23 +572,89 @@ UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.uint64_val = default_val;
return upb_Message_GetField(msg, f, def).uint64_val;
}
UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a) {
// BaseField Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
const upb_MiniTableField* f,
bool value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.bool_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(
@ -598,55 +662,132 @@ UPB_API_INLINE void upb_Message_SetClosedEnum(
const upb_MiniTableField* f, int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, f), value));
upb_Message_SetBaseField(msg, f, &value);
}
// Extension Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetExtensionBool(
struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_1Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionDouble(
struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionFloat(
struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionString(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_StringView);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
// Universal Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionBool(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldBool(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.double_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionDouble(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldDouble(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.float_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionFloat(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldFloat(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.int32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.int64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt64(msg, f, value), true);
}
// Sets the value of a message-typed field. The mini_tables of `msg` and
@ -666,36 +807,28 @@ UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg,
UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.str_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionString(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldString(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.uint32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.uint64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt64(msg, f, value), true);
}
UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,

@ -49,6 +49,11 @@ UPB_API_INLINE void upb_MiniTableExtension_SetSubMessage(
e->UPB_PRIVATE(sub).UPB_PRIVATE(submsg) = m;
}
UPB_INLINE upb_FieldRep UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(
const struct upb_MiniTableExtension* e) {
return UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field));
}
#ifdef __cplusplus
} /* extern "C" */
#endif

Loading…
Cancel
Save