Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

91 lines
4.1 KiB

/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/mini_table_accessors.h"
#include "upb/mini_table.h"
#include "upb/mini_table_accessors_internal.h"
#include "upb/msg_internal.h"
// Must be last.
#include "upb/port_def.inc"
size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
static unsigned char sizes[] = {
0, /* 0 */
8, /* kUpb_FieldType_Double */
4, /* kUpb_FieldType_Float */
8, /* kUpb_FieldType_Int64 */
8, /* kUpb_FieldType_UInt64 */
4, /* kUpb_FieldType_Int32 */
8, /* kUpb_FieldType_Fixed64 */
4, /* kUpb_FieldType_Fixed32 */
1, /* kUpb_FieldType_Bool */
sizeof(upb_StringView), /* kUpb_FieldType_String */
sizeof(void*), /* kUpb_FieldType_Group */
sizeof(void*), /* kUpb_FieldType_Message */
sizeof(upb_StringView), /* kUpb_FieldType_Bytes */
4, /* kUpb_FieldType_UInt32 */
4, /* kUpb_FieldType_Enum */
4, /* kUpb_FieldType_SFixed32 */
8, /* kUpb_FieldType_SFixed64 */
4, /* kUpb_FieldType_SInt32 */
8, /* kUpb_FieldType_SInt64 */
};
return upb_IsRepeatedOrMap(f) ? sizeof(void*) : sizes[f->descriptortype];
}
UPB_INLINE upb_Message* upb_MiniTable_GetMessage(
const upb_Message* msg, const upb_MiniTable_Field* field) {
return UPB_PTR_AT(msg, field->offset, upb_Message);
}
bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTable_Field* field) {
if (_upb_MiniTable_Field_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
} else if (field->presence > 0) {
return _upb_hasbit_field(msg, field);
} else {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
field->descriptortype == kUpb_FieldType_Group);
return upb_MiniTable_GetMessage(msg, field) != NULL;
}
}
void upb_MiniTable_ClearField(upb_Message* msg,
const upb_MiniTable_Field* field) {
char* mem = UPB_PTR_AT(msg, field->offset, char);
if (field->presence > 0) {
_upb_clearhas_field(msg, field);
} else if (_upb_MiniTable_Field_InOneOf(field)) {
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (*oneof_case != field->number) return;
*oneof_case = 0;
}
memset(mem, 0, upb_MiniTable_Field_GetSize(field));
}