Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: third_party/protobuf/src/google/protobuf/generated_message_reflection.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Update to new HEAD (b7632464b4) + restore GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \ 166 #define USAGE_CHECK_ALL(METHOD, LABEL, CPPTYPE) \
167 USAGE_CHECK_MESSAGE_TYPE(METHOD); \ 167 USAGE_CHECK_MESSAGE_TYPE(METHOD); \
168 USAGE_CHECK_##LABEL(METHOD); \ 168 USAGE_CHECK_##LABEL(METHOD); \
169 USAGE_CHECK_TYPE(METHOD, CPPTYPE) 169 USAGE_CHECK_TYPE(METHOD, CPPTYPE)
170 170
171 } // namespace 171 } // namespace
172 172
173 // =================================================================== 173 // ===================================================================
174 174
175 GeneratedMessageReflection::GeneratedMessageReflection( 175 GeneratedMessageReflection::GeneratedMessageReflection(
176 const Descriptor* descriptor, 176 const Descriptor* descriptor, const Message* default_instance,
177 const Message* default_instance, 177 const int offsets[], int has_bits_offset, int unknown_fields_offset,
178 const int offsets[], 178 int extensions_offset, const DescriptorPool* descriptor_pool,
179 int has_bits_offset, 179 MessageFactory* factory, int object_size, int arena_offset)
180 int unknown_fields_offset, 180 : descriptor_(descriptor),
181 int extensions_offset, 181 default_instance_(default_instance),
182 const DescriptorPool* descriptor_pool, 182 offsets_(offsets),
183 MessageFactory* factory, 183 has_bits_offset_(has_bits_offset),
184 int object_size, 184 unknown_fields_offset_(unknown_fields_offset),
185 int arena_offset, 185 extensions_offset_(extensions_offset),
186 int is_default_instance_offset) 186 arena_offset_(arena_offset),
187 : descriptor_ (descriptor), 187 object_size_(object_size),
188 default_instance_ (default_instance), 188 descriptor_pool_((descriptor_pool == NULL)
189 offsets_ (offsets), 189 ? DescriptorPool::generated_pool()
190 has_bits_offset_ (has_bits_offset), 190 : descriptor_pool),
191 unknown_fields_offset_(unknown_fields_offset), 191 message_factory_(factory) {}
192 extensions_offset_(extensions_offset),
193 arena_offset_ (arena_offset),
194 is_default_instance_offset_(is_default_instance_offset),
195 object_size_ (object_size),
196 descriptor_pool_ ((descriptor_pool == NULL) ?
197 DescriptorPool::generated_pool() :
198 descriptor_pool),
199 message_factory_ (factory) {
200 }
201 192
202 GeneratedMessageReflection::GeneratedMessageReflection( 193 GeneratedMessageReflection::GeneratedMessageReflection(
203 const Descriptor* descriptor, 194 const Descriptor* descriptor, const Message* default_instance,
204 const Message* default_instance, 195 const int offsets[], int has_bits_offset, int unknown_fields_offset,
205 const int offsets[], 196 int extensions_offset, const void* default_oneof_instance,
206 int has_bits_offset, 197 int oneof_case_offset, const DescriptorPool* descriptor_pool,
207 int unknown_fields_offset, 198 MessageFactory* factory, int object_size, int arena_offset)
208 int extensions_offset, 199 : descriptor_(descriptor),
209 const void* default_oneof_instance, 200 default_instance_(default_instance),
210 int oneof_case_offset, 201 default_oneof_instance_(default_oneof_instance),
211 const DescriptorPool* descriptor_pool, 202 offsets_(offsets),
212 MessageFactory* factory, 203 has_bits_offset_(has_bits_offset),
213 int object_size, 204 oneof_case_offset_(oneof_case_offset),
214 int arena_offset, 205 unknown_fields_offset_(unknown_fields_offset),
215 int is_default_instance_offset) 206 extensions_offset_(extensions_offset),
216 : descriptor_ (descriptor), 207 arena_offset_(arena_offset),
217 default_instance_ (default_instance), 208 object_size_(object_size),
218 default_oneof_instance_ (default_oneof_instance), 209 descriptor_pool_((descriptor_pool == NULL)
219 offsets_ (offsets), 210 ? DescriptorPool::generated_pool()
220 has_bits_offset_ (has_bits_offset), 211 : descriptor_pool),
221 oneof_case_offset_(oneof_case_offset), 212 message_factory_(factory) {}
222 unknown_fields_offset_(unknown_fields_offset),
223 extensions_offset_(extensions_offset),
224 arena_offset_ (arena_offset),
225 is_default_instance_offset_(is_default_instance_offset),
226 object_size_ (object_size),
227 descriptor_pool_ ((descriptor_pool == NULL) ?
228 DescriptorPool::generated_pool() :
229 descriptor_pool),
230 message_factory_ (factory) {
231 }
232 213
233 GeneratedMessageReflection::~GeneratedMessageReflection() {} 214 GeneratedMessageReflection::~GeneratedMessageReflection() {}
234 215
235 namespace { 216 namespace {
236 UnknownFieldSet* empty_unknown_field_set_ = NULL; 217 UnknownFieldSet* empty_unknown_field_set_ = NULL;
237 GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_); 218 GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_);
238 219
239 void DeleteEmptyUnknownFieldSet() { 220 void DeleteEmptyUnknownFieldSet() {
240 delete empty_unknown_field_set_; 221 delete empty_unknown_field_set_;
241 empty_unknown_field_set_ = NULL; 222 empty_unknown_field_set_ = NULL;
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 } 1000 }
1020 1001
1021 namespace { 1002 namespace {
1022 // Comparison functor for sorting FieldDescriptors by field number. 1003 // Comparison functor for sorting FieldDescriptors by field number.
1023 struct FieldNumberSorter { 1004 struct FieldNumberSorter {
1024 bool operator()(const FieldDescriptor* left, 1005 bool operator()(const FieldDescriptor* left,
1025 const FieldDescriptor* right) const { 1006 const FieldDescriptor* right) const {
1026 return left->number() < right->number(); 1007 return left->number() < right->number();
1027 } 1008 }
1028 }; 1009 };
1010
1011 inline bool IsIndexInHasBitSet(const uint32* has_bit_set, uint32 index) {
1012 return ((has_bit_set[index / 32] >> (index % 32)) & static_cast<uint32>(1))
1013 != 0;
1014 }
1029 } // namespace 1015 } // namespace
1030 1016
1031 void GeneratedMessageReflection::ListFields( 1017 void GeneratedMessageReflection::ListFields(
1032 const Message& message, 1018 const Message& message,
1033 vector<const FieldDescriptor*>* output) const { 1019 vector<const FieldDescriptor*>* output) const {
1034 output->clear(); 1020 output->clear();
1035 1021
1036 // Optimization: The default instance never has any fields set. 1022 // Optimization: The default instance never has any fields set.
1037 if (&message == default_instance_) return; 1023 if (&message == default_instance_) return;
1038 1024
1025 // Optimization: Avoid calling GetHasBits() and HasOneofField() many times
1026 // within the field loop.
1027 const uint32* const has_bits = GetHasBits(message);
1028 const uint32* const oneof_case_array = reinterpret_cast<const uint32*>(
1029 reinterpret_cast<const uint8*>(&message) + oneof_case_offset_);
1030
1039 output->reserve(descriptor_->field_count()); 1031 output->reserve(descriptor_->field_count());
1040 for (int i = 0; i < descriptor_->field_count(); i++) { 1032 for (int i = 0; i < descriptor_->field_count(); i++) {
1041 const FieldDescriptor* field = descriptor_->field(i); 1033 const FieldDescriptor* field = descriptor_->field(i);
1042 if (field->is_repeated()) { 1034 if (field->is_repeated()) {
1043 if (FieldSize(message, field) > 0) { 1035 if (FieldSize(message, field) > 0) {
1044 output->push_back(field); 1036 output->push_back(field);
1045 } 1037 }
1046 } else { 1038 } else {
1047 if (field->containing_oneof()) { 1039 const OneofDescriptor* containing_oneof = field->containing_oneof();
1048 if (HasOneofField(message, field)) { 1040 if (containing_oneof) {
1041 // Equivalent to: HasOneofField(message, field)
1042 if (oneof_case_array[containing_oneof->index()] == field->number()) {
1049 output->push_back(field); 1043 output->push_back(field);
1050 } 1044 }
1051 } else if (HasBit(message, field)) { 1045 } else if (has_bits) {
1046 // Equivalent to: HasBit(message, field)
1047 if (IsIndexInHasBitSet(has_bits, i)) {
1048 output->push_back(field);
1049 }
1050 } else if (HasBit(message, field)) { // Fall back on proto3-style HasBit.
1052 output->push_back(field); 1051 output->push_back(field);
1053 } 1052 }
1054 } 1053 }
1055 } 1054 }
1056 1055
1057 if (extensions_offset_ != -1) { 1056 if (extensions_offset_ != -1) {
1058 GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_, 1057 GetExtensionSet(message).AppendToList(descriptor_, descriptor_pool_,
1059 output); 1058 output);
1060 } 1059 }
1061 1060
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
1572 Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage( 1571 Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage(
1573 Message* message, 1572 Message* message,
1574 const FieldDescriptor* field, 1573 const FieldDescriptor* field,
1575 MessageFactory* factory) const { 1574 MessageFactory* factory) const {
1576 USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE); 1575 USAGE_CHECK_ALL(ReleaseMessage, SINGULAR, MESSAGE);
1577 1576
1578 if (factory == NULL) factory = message_factory_; 1577 if (factory == NULL) factory = message_factory_;
1579 1578
1580 if (field->is_extension()) { 1579 if (field->is_extension()) {
1581 return static_cast<Message*>( 1580 return static_cast<Message*>(
1582 MutableExtensionSet(message)->ReleaseMessage(field, factory)); 1581 MutableExtensionSet(message)->UnsafeArenaReleaseMessage(field,
1582 factory));
1583 } else { 1583 } else {
1584 ClearBit(message, field); 1584 ClearBit(message, field);
1585 if (field->containing_oneof()) { 1585 if (field->containing_oneof()) {
1586 if (HasOneofField(*message, field)) { 1586 if (HasOneofField(*message, field)) {
1587 *MutableOneofCase(message, field->containing_oneof()) = 0; 1587 *MutableOneofCase(message, field->containing_oneof()) = 0;
1588 } else { 1588 } else {
1589 return NULL; 1589 return NULL;
1590 } 1590 }
1591 } 1591 }
1592 Message** result = MutableRaw<Message*>(message, field); 1592 Message** result = MutableRaw<Message*>(message, field);
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
1993 inline InternalMetadataWithArena* 1993 inline InternalMetadataWithArena*
1994 GeneratedMessageReflection::MutableInternalMetadataWithArena( 1994 GeneratedMessageReflection::MutableInternalMetadataWithArena(
1995 Message* message) const { 1995 Message* message) const {
1996 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_; 1996 void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_;
1997 return reinterpret_cast<InternalMetadataWithArena*>(ptr); 1997 return reinterpret_cast<InternalMetadataWithArena*>(ptr);
1998 } 1998 }
1999 1999
2000 inline bool 2000 inline bool
2001 GeneratedMessageReflection::GetIsDefaultInstance( 2001 GeneratedMessageReflection::GetIsDefaultInstance(
2002 const Message& message) const { 2002 const Message& message) const {
2003 if (is_default_instance_offset_ == kHasNoDefaultInstanceField) { 2003 return &message == default_instance_;
2004 return false;
2005 }
2006 const void* ptr = reinterpret_cast<const uint8*>(&message) +
2007 is_default_instance_offset_;
2008 return *reinterpret_cast<const bool*>(ptr);
2009 } 2004 }
2010 2005
2011 // Simple accessors for manipulating has_bits_. 2006 // Simple accessors for manipulating has_bits_.
2012 inline bool GeneratedMessageReflection::HasBit( 2007 inline bool GeneratedMessageReflection::HasBit(
2013 const Message& message, const FieldDescriptor* field) const { 2008 const Message& message, const FieldDescriptor* field) const {
2014 if (has_bits_offset_ == -1) { 2009 if (has_bits_offset_ == -1) {
2015 // proto3: no has-bits. All fields present except messages, which are 2010 // proto3: no has-bits. All fields present except messages, which are
2016 // present only if their message-field pointer is non-NULL. 2011 // present only if their message-field pointer is non-NULL.
2017 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 2012 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
2018 return !GetIsDefaultInstance(message) && 2013 return !GetIsDefaultInstance(message) &&
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2055 return GetRaw<double>(message, field) != 0.0; 2050 return GetRaw<double>(message, field) != 0.0;
2056 case FieldDescriptor::CPPTYPE_ENUM: 2051 case FieldDescriptor::CPPTYPE_ENUM:
2057 return GetRaw<int>(message, field) != 0; 2052 return GetRaw<int>(message, field) != 0;
2058 case FieldDescriptor::CPPTYPE_MESSAGE: 2053 case FieldDescriptor::CPPTYPE_MESSAGE:
2059 // handled above; avoid warning 2054 // handled above; avoid warning
2060 GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit()."; 2055 GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit().";
2061 break; 2056 break;
2062 } 2057 }
2063 } 2058 }
2064 } 2059 }
2065 return GetHasBits(message)[field->index() / 32] & 2060 return IsIndexInHasBitSet(GetHasBits(message), field->index());
2066 (1 << (field->index() % 32));
2067 } 2061 }
2068 2062
2069 inline void GeneratedMessageReflection::SetBit( 2063 inline void GeneratedMessageReflection::SetBit(
2070 Message* message, const FieldDescriptor* field) const { 2064 Message* message, const FieldDescriptor* field) const {
2071 if (has_bits_offset_ == -1) { 2065 if (has_bits_offset_ == -1) {
2072 return; 2066 return;
2073 } 2067 }
2074 MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32)); 2068 const uint32 index = static_cast<uint32>(field->index());
2069 MutableHasBits(message)[index / 32] |=
2070 (static_cast<uint32>(1) << (index % 32));
2075 } 2071 }
2076 2072
2077 inline void GeneratedMessageReflection::ClearBit( 2073 inline void GeneratedMessageReflection::ClearBit(
2078 Message* message, const FieldDescriptor* field) const { 2074 Message* message, const FieldDescriptor* field) const {
2079 if (has_bits_offset_ == -1) { 2075 if (has_bits_offset_ == -1) {
2080 return; 2076 return;
2081 } 2077 }
2082 MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32)); 2078 const uint32 index = static_cast<uint32>(field->index());
2079 MutableHasBits(message)[index / 32] &=
2080 ~(static_cast<uint32>(1) << (index % 32));
2083 } 2081 }
2084 2082
2085 inline void GeneratedMessageReflection::SwapBit( 2083 inline void GeneratedMessageReflection::SwapBit(
2086 Message* message1, Message* message2, const FieldDescriptor* field) const { 2084 Message* message1, Message* message2, const FieldDescriptor* field) const {
2087 if (has_bits_offset_ == -1) { 2085 if (has_bits_offset_ == -1) {
2088 return; 2086 return;
2089 } 2087 }
2090 bool temp_has_bit = HasBit(*message1, field); 2088 bool temp_has_bit = HasBit(*message1, field);
2091 if (HasBit(*message2, field)) { 2089 if (HasBit(*message2, field)) {
2092 SetBit(message1, field); 2090 SetBit(message1, field);
(...skipping 25 matching lines...) Expand all
2118 inline void GeneratedMessageReflection::ClearOneofField( 2116 inline void GeneratedMessageReflection::ClearOneofField(
2119 Message* message, const FieldDescriptor* field) const { 2117 Message* message, const FieldDescriptor* field) const {
2120 if (HasOneofField(*message, field)) { 2118 if (HasOneofField(*message, field)) {
2121 ClearOneof(message, field->containing_oneof()); 2119 ClearOneof(message, field->containing_oneof());
2122 } 2120 }
2123 } 2121 }
2124 2122
2125 inline void GeneratedMessageReflection::ClearOneof( 2123 inline void GeneratedMessageReflection::ClearOneof(
2126 Message* message, const OneofDescriptor* oneof_descriptor) const { 2124 Message* message, const OneofDescriptor* oneof_descriptor) const {
2127 // TODO(jieluo): Consider to cache the unused object instead of deleting 2125 // TODO(jieluo): Consider to cache the unused object instead of deleting
2128 // it. It will be much faster if an aplication switches a lot from 2126 // it. It will be much faster if an application switches a lot from
2129 // a few oneof fields. Time/space tradeoff 2127 // a few oneof fields. Time/space tradeoff
2130 uint32 oneof_case = GetOneofCase(*message, oneof_descriptor); 2128 uint32 oneof_case = GetOneofCase(*message, oneof_descriptor);
2131 if (oneof_case > 0) { 2129 if (oneof_case > 0) {
2132 const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case); 2130 const FieldDescriptor* field = descriptor_->FindFieldByNumber(oneof_case);
2133 if (GetArena(message) == NULL) { 2131 if (GetArena(message) == NULL) {
2134 switch (field->cpp_type()) { 2132 switch (field->cpp_type()) {
2135 case FieldDescriptor::CPPTYPE_STRING: { 2133 case FieldDescriptor::CPPTYPE_STRING: {
2136 switch (field->options().ctype()) { 2134 switch (field->options().ctype()) {
2137 default: // TODO(kenton): Support other string reps. 2135 default: // TODO(kenton): Support other string reps.
2138 case FieldOptions::STRING: { 2136 case FieldOptions::STRING: {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
2267 const Message* default_instance, 2265 const Message* default_instance,
2268 const int offsets[], 2266 const int offsets[],
2269 int has_bits_offset, 2267 int has_bits_offset,
2270 int unknown_fields_offset, 2268 int unknown_fields_offset,
2271 int extensions_offset, 2269 int extensions_offset,
2272 const void* default_oneof_instance, 2270 const void* default_oneof_instance,
2273 int oneof_case_offset, 2271 int oneof_case_offset,
2274 int object_size, 2272 int object_size,
2275 int arena_offset, 2273 int arena_offset,
2276 int is_default_instance_offset) { 2274 int is_default_instance_offset) {
2277 return new GeneratedMessageReflection(descriptor, 2275 return new GeneratedMessageReflection(
2278 default_instance, 2276 descriptor, default_instance, offsets, has_bits_offset,
2279 offsets, 2277 unknown_fields_offset, extensions_offset, default_oneof_instance,
2280 has_bits_offset, 2278 oneof_case_offset, DescriptorPool::generated_pool(),
2281 unknown_fields_offset, 2279 MessageFactory::generated_factory(), object_size, arena_offset);
2282 extensions_offset,
2283 default_oneof_instance,
2284 oneof_case_offset,
2285 DescriptorPool::generated_pool(),
2286 MessageFactory::generated_factory(),
2287 object_size,
2288 arena_offset,
2289 is_default_instance_offset);
2290 } 2280 }
2291 2281
2292 GeneratedMessageReflection* 2282 GeneratedMessageReflection*
2293 GeneratedMessageReflection::NewGeneratedMessageReflection( 2283 GeneratedMessageReflection::NewGeneratedMessageReflection(
2294 const Descriptor* descriptor, 2284 const Descriptor* descriptor,
2295 const Message* default_instance, 2285 const Message* default_instance,
2296 const int offsets[], 2286 const int offsets[],
2297 int has_bits_offset, 2287 int has_bits_offset,
2298 int unknown_fields_offset, 2288 int unknown_fields_offset,
2299 int extensions_offset, 2289 int extensions_offset,
2300 int object_size, 2290 int object_size,
2301 int arena_offset, 2291 int arena_offset,
2302 int is_default_instance_offset) { 2292 int is_default_instance_offset) {
2303 return new GeneratedMessageReflection(descriptor, 2293 return new GeneratedMessageReflection(
2304 default_instance, 2294 descriptor, default_instance, offsets, has_bits_offset,
2305 offsets, 2295 unknown_fields_offset, extensions_offset,
2306 has_bits_offset, 2296 DescriptorPool::generated_pool(), MessageFactory::generated_factory(),
2307 unknown_fields_offset, 2297 object_size, arena_offset);
2308 extensions_offset,
2309 DescriptorPool::generated_pool(),
2310 MessageFactory::generated_factory(),
2311 object_size,
2312 arena_offset,
2313 is_default_instance_offset);
2314 } 2298 }
2315 2299
2316 } // namespace internal 2300 } // namespace internal
2317 } // namespace protobuf 2301 } // namespace protobuf
2318 } // namespace google 2302 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698