blob: bad23a68b6c12170197ede59a2e758ffbaf94e5e [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
[email protected]24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
[email protected]24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
[email protected]24bf56f2008-09-24 20:31:01 +00009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
temporal40ee5512008-07-10 02:12:20 +000018//
[email protected]24bf56f2008-09-24 20:31:01 +000019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: [email protected] (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
Feng Xiao6bbe1972018-08-08 17:00:41 -070035#include <google/protobuf/extension_set.h>
36
Adam Cozzette0400cca2018-03-13 16:37:29 -070037#include <tuple>
Feng Xiao6bbe1972018-08-08 17:00:41 -070038#include <unordered_map>
Adam Cozzette92a7e772017-12-01 10:05:10 -080039#include <utility>
[email protected]d37d46d2009-04-25 02:53:47 +000040#include <google/protobuf/stubs/common.h>
Adam Cozzette0894e072018-11-09 11:28:22 -080041#include <google/protobuf/extension_set_inl.h>
42#include <google/protobuf/parse_context.h>
temporal40ee5512008-07-10 02:12:20 +000043#include <google/protobuf/io/coded_stream.h>
Feng Xiao6bbe1972018-08-08 17:00:41 -070044#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
45#include <google/protobuf/message_lite.h>
46#include <google/protobuf/metadata_lite.h>
temporal40ee5512008-07-10 02:12:20 +000047#include <google/protobuf/repeated_field.h>
[email protected]4de8f552014-07-18 00:47:59 +000048#include <google/protobuf/stubs/map_util.h>
Feng Xiao6bbe1972018-08-08 17:00:41 -070049#include <google/protobuf/stubs/hash.h>
50
51#include <google/protobuf/port_def.inc>
temporal40ee5512008-07-10 02:12:20 +000052
53namespace google {
54namespace protobuf {
55namespace internal {
56
[email protected]d37d46d2009-04-25 02:53:47 +000057namespace {
temporal40ee5512008-07-10 02:12:20 +000058
[email protected]80b1d622009-07-29 01:13:20 +000059inline WireFormatLite::FieldType real_type(FieldType type) {
60 GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE);
61 return static_cast<WireFormatLite::FieldType>(type);
temporal40ee5512008-07-10 02:12:20 +000062}
63
[email protected]80b1d622009-07-29 01:13:20 +000064inline WireFormatLite::CppType cpp_type(FieldType type) {
65 return WireFormatLite::FieldTypeToCppType(real_type(type));
[email protected]d37d46d2009-04-25 02:53:47 +000066}
67
[email protected]4de8f552014-07-18 00:47:59 +000068inline bool is_packable(WireFormatLite::WireType type) {
69 switch (type) {
70 case WireFormatLite::WIRETYPE_VARINT:
71 case WireFormatLite::WIRETYPE_FIXED64:
72 case WireFormatLite::WIRETYPE_FIXED32:
73 return true;
74 case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
75 case WireFormatLite::WIRETYPE_START_GROUP:
76 case WireFormatLite::WIRETYPE_END_GROUP:
77 return false;
78
Hao Nguyen176f7db2019-04-09 06:23:32 -070079 // Do not add a default statement. Let the compiler complain when someone
80 // adds a new wire type.
[email protected]4de8f552014-07-18 00:47:59 +000081 }
Feng Xiao9173ba22014-12-02 15:28:11 -080082 GOOGLE_LOG(FATAL) << "can't reach here.";
83 return false;
[email protected]4de8f552014-07-18 00:47:59 +000084}
85
[email protected]d37d46d2009-04-25 02:53:47 +000086// Registry stuff.
Feng Xiao6bbe1972018-08-08 17:00:41 -070087struct ExtensionHasher {
88 std::size_t operator()(const std::pair<const MessageLite*, int>& p) const {
89 return std::hash<const MessageLite*>{}(p.first) ^
90 std::hash<int>{}(p.second);
91 }
92};
93
94typedef std::unordered_map<std::pair<const MessageLite*, int>, ExtensionInfo,
95 ExtensionHasher>
96 ExtensionRegistry;
[email protected]63e646b2009-05-06 19:27:03 +000097
Adam Cozzettea9abc782018-07-06 14:12:33 -070098static const ExtensionRegistry* global_registry = nullptr;
[email protected]d37d46d2009-04-25 02:53:47 +000099
100// This function is only called at startup, so there is no need for thread-
101// safety.
Hao Nguyen176f7db2019-04-09 06:23:32 -0700102void Register(const MessageLite* containing_type, int number,
103 ExtensionInfo info) {
Adam Cozzettea9abc782018-07-06 14:12:33 -0700104 static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry);
105 global_registry = local_static_registry;
106 if (!InsertIfNotPresent(local_static_registry,
Feng Xiao6bbe1972018-08-08 17:00:41 -0700107 std::make_pair(containing_type, number), info)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000108 GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \""
Hao Nguyen176f7db2019-04-09 06:23:32 -0700109 << containing_type->GetTypeName() << "\", field number "
110 << number << ".";
[email protected]d37d46d2009-04-25 02:53:47 +0000111 }
112}
113
Hao Nguyen176f7db2019-04-09 06:23:32 -0700114const ExtensionInfo* FindRegisteredExtension(const MessageLite* containing_type,
115 int number) {
Adam Cozzettea9abc782018-07-06 14:12:33 -0700116 return global_registry == nullptr
Feng Xiao6bbe1972018-08-08 17:00:41 -0700117 ? nullptr
118 : FindOrNull(*global_registry,
119 std::make_pair(containing_type, number));
[email protected]d37d46d2009-04-25 02:53:47 +0000120}
121
122} // namespace
123
[email protected]fccb1462009-12-18 02:11:36 +0000124ExtensionFinder::~ExtensionFinder() {}
125
126bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) {
127 const ExtensionInfo* extension =
128 FindRegisteredExtension(containing_type_, number);
129 if (extension == NULL) {
130 return false;
131 } else {
132 *output = *extension;
133 return true;
134 }
135}
136
[email protected]80b1d622009-07-29 01:13:20 +0000137void ExtensionSet::RegisterExtension(const MessageLite* containing_type,
[email protected]d37d46d2009-04-25 02:53:47 +0000138 int number, FieldType type,
139 bool is_repeated, bool is_packed) {
[email protected]80b1d622009-07-29 01:13:20 +0000140 GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_ENUM);
141 GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_MESSAGE);
142 GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_GROUP);
[email protected]d37d46d2009-04-25 02:53:47 +0000143 ExtensionInfo info(type, is_repeated, is_packed);
144 Register(containing_type, number, info);
145}
146
[email protected]fccb1462009-12-18 02:11:36 +0000147static bool CallNoArgValidityFunc(const void* arg, int number) {
[email protected]91218af2009-12-18 07:20:43 +0000148 // Note: Must use C-style cast here rather than reinterpret_cast because
149 // the C++ standard at one point did not allow casts between function and
150 // data pointers and some compilers enforce this for C++-style casts. No
151 // compiler enforces it for C-style casts since lots of C-style code has
152 // relied on these kinds of casts for a long time, despite being
153 // technically undefined. See:
154 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
[email protected]f2a73292009-12-21 19:44:57 +0000155 // Also note: Some compilers do not allow function pointers to be "const".
156 // Which makes sense, I suppose, because it's meaningless.
157 return ((EnumValidityFunc*)arg)(number);
[email protected]fccb1462009-12-18 02:11:36 +0000158}
159
[email protected]80b1d622009-07-29 01:13:20 +0000160void ExtensionSet::RegisterEnumExtension(const MessageLite* containing_type,
[email protected]d37d46d2009-04-25 02:53:47 +0000161 int number, FieldType type,
162 bool is_repeated, bool is_packed,
163 EnumValidityFunc* is_valid) {
[email protected]80b1d622009-07-29 01:13:20 +0000164 GOOGLE_CHECK_EQ(type, WireFormatLite::TYPE_ENUM);
[email protected]d37d46d2009-04-25 02:53:47 +0000165 ExtensionInfo info(type, is_repeated, is_packed);
[email protected]f9c59782009-12-22 18:04:23 +0000166 info.enum_validity_check.func = CallNoArgValidityFunc;
[email protected]91218af2009-12-18 07:20:43 +0000167 // See comment in CallNoArgValidityFunc() about why we use a c-style cast.
[email protected]f9c59782009-12-22 18:04:23 +0000168 info.enum_validity_check.arg = (void*)is_valid;
[email protected]d37d46d2009-04-25 02:53:47 +0000169 Register(containing_type, number, info);
170}
171
[email protected]80b1d622009-07-29 01:13:20 +0000172void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type,
[email protected]d37d46d2009-04-25 02:53:47 +0000173 int number, FieldType type,
174 bool is_repeated, bool is_packed,
[email protected]80b1d622009-07-29 01:13:20 +0000175 const MessageLite* prototype) {
176 GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE ||
177 type == WireFormatLite::TYPE_GROUP);
[email protected]d37d46d2009-04-25 02:53:47 +0000178 ExtensionInfo info(type, is_repeated, is_packed);
Adam Cozzette0894e072018-11-09 11:28:22 -0800179 info.message_info = {prototype};
[email protected]d37d46d2009-04-25 02:53:47 +0000180 Register(containing_type, number, info);
temporal40ee5512008-07-10 02:12:20 +0000181}
182
temporal40ee5512008-07-10 02:12:20 +0000183// ===================================================================
184// Constructors and basic methods.
185
Feng Xiao6bbe1972018-08-08 17:00:41 -0700186ExtensionSet::ExtensionSet(Arena* arena)
Adam Cozzette0400cca2018-03-13 16:37:29 -0700187 : arena_(arena),
188 flat_capacity_(0),
189 flat_size_(0),
Feng Xiao6bbe1972018-08-08 17:00:41 -0700190 map_{flat_capacity_ == 0
191 ? NULL
192 : Arena::CreateArray<KeyValue>(arena_, flat_capacity_)} {}
Feng Xiao6ef984a2014-11-10 17:34:54 -0800193
temporal40ee5512008-07-10 02:12:20 +0000194ExtensionSet::~ExtensionSet() {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800195 // Deletes all allocated extensions.
196 if (arena_ == NULL) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700197 ForEach([](int /* number */, Extension& ext) { ext.Free(); });
Josh Habermand61aede2018-09-04 10:58:54 -0700198 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700199 delete map_.large;
200 } else {
Feng Xiao6bbe1972018-08-08 17:00:41 -0700201 DeleteFlatMap(map_.flat, flat_capacity_);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800202 }
temporal40ee5512008-07-10 02:12:20 +0000203 }
204}
205
Hao Nguyen176f7db2019-04-09 06:23:32 -0700206void ExtensionSet::DeleteFlatMap(const ExtensionSet::KeyValue* flat,
207 uint16 flat_capacity) {
Feng Xiao6bbe1972018-08-08 17:00:41 -0700208#ifdef __cpp_sized_deallocation
209 // Arena::CreateArray already requires a trivially destructible type, but
210 // ensure this constraint is not violated in the future.
211 static_assert(std::is_trivially_destructible<KeyValue>::value,
212 "CreateArray requires a trivially destructible type");
213 // A const-cast is needed, but this is safe as we are about to deallocate the
214 // array.
Hao Nguyen176f7db2019-04-09 06:23:32 -0700215 ::operator delete[](const_cast<ExtensionSet::KeyValue*>(flat),
216 sizeof(*flat) * flat_capacity);
217#else // !__cpp_sized_deallocation
Feng Xiao6bbe1972018-08-08 17:00:41 -0700218 delete[] flat;
219#endif // !__cpp_sized_deallocation
220}
221
[email protected]80b1d622009-07-29 01:13:20 +0000222// Defined in extension_set_heavy.cc.
223// void ExtensionSet::AppendToList(const Descriptor* containing_type,
224// const DescriptorPool* pool,
225// vector<const FieldDescriptor*>* output) const
temporal40ee5512008-07-10 02:12:20 +0000226
227bool ExtensionSet::Has(int number) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700228 const Extension* ext = FindOrNull(number);
229 if (ext == NULL) return false;
230 GOOGLE_DCHECK(!ext->is_repeated);
231 return !ext->is_cleared;
temporal40ee5512008-07-10 02:12:20 +0000232}
233
[email protected]b55a20f2012-09-22 02:40:50 +0000234int ExtensionSet::NumExtensions() const {
235 int result = 0;
Adam Cozzette0400cca2018-03-13 16:37:29 -0700236 ForEach([&result](int /* number */, const Extension& ext) {
237 if (!ext.is_cleared) {
[email protected]b55a20f2012-09-22 02:40:50 +0000238 ++result;
239 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700240 });
[email protected]b55a20f2012-09-22 02:40:50 +0000241 return result;
242}
243
temporal40ee5512008-07-10 02:12:20 +0000244int ExtensionSet::ExtensionSize(int number) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700245 const Extension* ext = FindOrNull(number);
246 return ext == NULL ? 0 : ext->GetSize();
temporal40ee5512008-07-10 02:12:20 +0000247}
248
[email protected]33165fe2010-11-02 13:14:58 +0000249FieldType ExtensionSet::ExtensionType(int number) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700250 const Extension* ext = FindOrNull(number);
251 if (ext == NULL) {
[email protected]33165fe2010-11-02 13:14:58 +0000252 GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (1). ";
253 return 0;
254 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700255 if (ext->is_cleared) {
[email protected]33165fe2010-11-02 13:14:58 +0000256 GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (2). ";
257 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700258 return ext->type;
[email protected]33165fe2010-11-02 13:14:58 +0000259}
260
temporal40ee5512008-07-10 02:12:20 +0000261void ExtensionSet::ClearExtension(int number) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700262 Extension* ext = FindOrNull(number);
263 if (ext == NULL) return;
264 ext->Clear();
temporal40ee5512008-07-10 02:12:20 +0000265}
266
267// ===================================================================
268// Field accessors
269
[email protected]80b1d622009-07-29 01:13:20 +0000270namespace {
271
Hao Nguyen176f7db2019-04-09 06:23:32 -0700272enum { REPEATED_FIELD, OPTIONAL_FIELD };
[email protected]80b1d622009-07-29 01:13:20 +0000273
274} // namespace
275
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700276#define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \
277 GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED_FIELD : OPTIONAL_FIELD, LABEL); \
[email protected]80b1d622009-07-29 01:13:20 +0000278 GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), WireFormatLite::CPPTYPE_##CPPTYPE)
temporal40ee5512008-07-10 02:12:20 +0000279
280// -------------------------------------------------------------------
281// Primitives
282
Hao Nguyen176f7db2019-04-09 06:23:32 -0700283#define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \
284 \
285 LOWERCASE ExtensionSet::Get##CAMELCASE(int number, LOWERCASE default_value) \
286 const { \
287 const Extension* extension = FindOrNull(number); \
288 if (extension == NULL || extension->is_cleared) { \
289 return default_value; \
290 } else { \
291 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \
292 return extension->LOWERCASE##_value; \
293 } \
294 } \
295 \
296 void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \
297 LOWERCASE value, \
298 const FieldDescriptor* descriptor) { \
299 Extension* extension; \
300 if (MaybeNewExtension(number, descriptor, &extension)) { \
301 extension->type = type; \
302 GOOGLE_DCHECK_EQ(cpp_type(extension->type), \
303 WireFormatLite::CPPTYPE_##UPPERCASE); \
304 extension->is_repeated = false; \
305 } else { \
306 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, UPPERCASE); \
307 } \
308 extension->is_cleared = false; \
309 extension->LOWERCASE##_value = value; \
310 } \
311 \
312 LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) \
313 const { \
314 const Extension* extension = FindOrNull(number); \
315 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
316 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \
317 return extension->repeated_##LOWERCASE##_value->Get(index); \
318 } \
319 \
320 void ExtensionSet::SetRepeated##CAMELCASE(int number, int index, \
321 LOWERCASE value) { \
322 Extension* extension = FindOrNull(number); \
323 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
324 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \
325 extension->repeated_##LOWERCASE##_value->Set(index, value); \
326 } \
327 \
328 void ExtensionSet::Add##CAMELCASE(int number, FieldType type, bool packed, \
329 LOWERCASE value, \
330 const FieldDescriptor* descriptor) { \
331 Extension* extension; \
332 if (MaybeNewExtension(number, descriptor, &extension)) { \
333 extension->type = type; \
334 GOOGLE_DCHECK_EQ(cpp_type(extension->type), \
335 WireFormatLite::CPPTYPE_##UPPERCASE); \
336 extension->is_repeated = true; \
337 extension->is_packed = packed; \
338 extension->repeated_##LOWERCASE##_value = \
339 Arena::CreateMessage<RepeatedField<LOWERCASE>>(arena_); \
340 } else { \
341 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, UPPERCASE); \
342 GOOGLE_DCHECK_EQ(extension->is_packed, packed); \
343 } \
344 extension->repeated_##LOWERCASE##_value->Add(value); \
345 }
temporal40ee5512008-07-10 02:12:20 +0000346
Hao Nguyen176f7db2019-04-09 06:23:32 -0700347PRIMITIVE_ACCESSORS(INT32, int32, Int32)
348PRIMITIVE_ACCESSORS(INT64, int64, Int64)
temporal40ee5512008-07-10 02:12:20 +0000349PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32)
350PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64)
Hao Nguyen176f7db2019-04-09 06:23:32 -0700351PRIMITIVE_ACCESSORS(FLOAT, float, Float)
temporal40ee5512008-07-10 02:12:20 +0000352PRIMITIVE_ACCESSORS(DOUBLE, double, Double)
Hao Nguyen176f7db2019-04-09 06:23:32 -0700353PRIMITIVE_ACCESSORS(BOOL, bool, Bool)
temporal40ee5512008-07-10 02:12:20 +0000354
355#undef PRIMITIVE_ACCESSORS
356
[email protected]4de8f552014-07-18 00:47:59 +0000357const void* ExtensionSet::GetRawRepeatedField(int number,
358 const void* default_value) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700359 const Extension* extension = FindOrNull(number);
360 if (extension == NULL) {
[email protected]4de8f552014-07-18 00:47:59 +0000361 return default_value;
362 }
[email protected]b55a20f2012-09-22 02:40:50 +0000363 // We assume that all the RepeatedField<>* pointers have the same
364 // size and alignment within the anonymous union in Extension.
Adam Cozzette0400cca2018-03-13 16:37:29 -0700365 return extension->repeated_int32_value;
[email protected]b55a20f2012-09-22 02:40:50 +0000366}
367
[email protected]4de8f552014-07-18 00:47:59 +0000368void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type,
369 bool packed,
370 const FieldDescriptor* desc) {
371 Extension* extension;
372
373 // We instantiate an empty Repeated{,Ptr}Field if one doesn't exist for this
374 // extension.
375 if (MaybeNewExtension(number, desc, &extension)) {
376 extension->is_repeated = true;
377 extension->type = field_type;
378 extension->is_packed = packed;
379
380 switch (WireFormatLite::FieldTypeToCppType(
381 static_cast<WireFormatLite::FieldType>(field_type))) {
382 case WireFormatLite::CPPTYPE_INT32:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800383 extension->repeated_int32_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700384 Arena::CreateMessage<RepeatedField<int32>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000385 break;
386 case WireFormatLite::CPPTYPE_INT64:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800387 extension->repeated_int64_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700388 Arena::CreateMessage<RepeatedField<int64>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000389 break;
390 case WireFormatLite::CPPTYPE_UINT32:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800391 extension->repeated_uint32_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700392 Arena::CreateMessage<RepeatedField<uint32>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000393 break;
394 case WireFormatLite::CPPTYPE_UINT64:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800395 extension->repeated_uint64_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700396 Arena::CreateMessage<RepeatedField<uint64>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000397 break;
398 case WireFormatLite::CPPTYPE_DOUBLE:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800399 extension->repeated_double_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700400 Arena::CreateMessage<RepeatedField<double>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000401 break;
402 case WireFormatLite::CPPTYPE_FLOAT:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800403 extension->repeated_float_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700404 Arena::CreateMessage<RepeatedField<float>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000405 break;
406 case WireFormatLite::CPPTYPE_BOOL:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800407 extension->repeated_bool_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700408 Arena::CreateMessage<RepeatedField<bool>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000409 break;
410 case WireFormatLite::CPPTYPE_ENUM:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800411 extension->repeated_enum_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700412 Arena::CreateMessage<RepeatedField<int>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000413 break;
414 case WireFormatLite::CPPTYPE_STRING:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800415 extension->repeated_string_value =
Hao Nguyend0f91c82019-03-06 12:39:12 -0800416 Arena::CreateMessage<RepeatedPtrField<std::string>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000417 break;
418 case WireFormatLite::CPPTYPE_MESSAGE:
Feng Xiao6ef984a2014-11-10 17:34:54 -0800419 extension->repeated_message_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700420 Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_);
[email protected]4de8f552014-07-18 00:47:59 +0000421 break;
422 }
423 }
424
425 // We assume that all the RepeatedField<>* pointers have the same
426 // size and alignment within the anonymous union in Extension.
427 return extension->repeated_int32_value;
428}
429
430// Compatible version using old call signature. Does not create extensions when
431// the don't already exist; instead, just GOOGLE_CHECK-fails.
432void* ExtensionSet::MutableRawRepeatedField(int number) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700433 Extension* extension = FindOrNull(number);
434 GOOGLE_CHECK(extension != NULL) << "Extension not found.";
[email protected]4de8f552014-07-18 00:47:59 +0000435 // We assume that all the RepeatedField<>* pointers have the same
436 // size and alignment within the anonymous union in Extension.
Adam Cozzette0400cca2018-03-13 16:37:29 -0700437 return extension->repeated_int32_value;
[email protected]4de8f552014-07-18 00:47:59 +0000438}
439
temporal40ee5512008-07-10 02:12:20 +0000440// -------------------------------------------------------------------
441// Enums
442
[email protected]d37d46d2009-04-25 02:53:47 +0000443int ExtensionSet::GetEnum(int number, int default_value) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700444 const Extension* extension = FindOrNull(number);
445 if (extension == NULL || extension->is_cleared) {
temporal40ee5512008-07-10 02:12:20 +0000446 // Not present. Return the default value.
[email protected]d37d46d2009-04-25 02:53:47 +0000447 return default_value;
temporal40ee5512008-07-10 02:12:20 +0000448 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700449 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700450 return extension->enum_value;
temporal40ee5512008-07-10 02:12:20 +0000451 }
452}
453
[email protected]fccb1462009-12-18 02:11:36 +0000454void ExtensionSet::SetEnum(int number, FieldType type, int value,
455 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000456 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000457 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000458 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000459 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
[email protected]d37d46d2009-04-25 02:53:47 +0000460 extension->is_repeated = false;
temporal40ee5512008-07-10 02:12:20 +0000461 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700462 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, ENUM);
temporal40ee5512008-07-10 02:12:20 +0000463 }
[email protected]d37d46d2009-04-25 02:53:47 +0000464 extension->is_cleared = false;
temporal40ee5512008-07-10 02:12:20 +0000465 extension->enum_value = value;
466}
467
468int ExtensionSet::GetRepeatedEnum(int number, int index) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700469 const Extension* extension = FindOrNull(number);
470 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700471 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700472 return extension->repeated_enum_value->Get(index);
temporal40ee5512008-07-10 02:12:20 +0000473}
474
475void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700476 Extension* extension = FindOrNull(number);
477 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700478 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700479 extension->repeated_enum_value->Set(index, value);
temporal40ee5512008-07-10 02:12:20 +0000480}
481
Hao Nguyen176f7db2019-04-09 06:23:32 -0700482void ExtensionSet::AddEnum(int number, FieldType type, bool packed, int value,
[email protected]fccb1462009-12-18 02:11:36 +0000483 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000484 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000485 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000486 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000487 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
[email protected]d37d46d2009-04-25 02:53:47 +0000488 extension->is_repeated = true;
489 extension->is_packed = packed;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800490 extension->repeated_enum_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700491 Arena::CreateMessage<RepeatedField<int>>(arena_);
temporal40ee5512008-07-10 02:12:20 +0000492 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700493 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, ENUM);
[email protected]d37d46d2009-04-25 02:53:47 +0000494 GOOGLE_DCHECK_EQ(extension->is_packed, packed);
temporal40ee5512008-07-10 02:12:20 +0000495 }
temporal40ee5512008-07-10 02:12:20 +0000496 extension->repeated_enum_value->Add(value);
497}
498
499// -------------------------------------------------------------------
500// Strings
501
Hao Nguyend0f91c82019-03-06 12:39:12 -0800502const std::string& ExtensionSet::GetString(
503 int number, const std::string& default_value) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700504 const Extension* extension = FindOrNull(number);
505 if (extension == NULL || extension->is_cleared) {
temporal40ee5512008-07-10 02:12:20 +0000506 // Not present. Return the default value.
[email protected]d37d46d2009-04-25 02:53:47 +0000507 return default_value;
temporal40ee5512008-07-10 02:12:20 +0000508 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700509 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700510 return *extension->string_value;
temporal40ee5512008-07-10 02:12:20 +0000511 }
512}
513
Hao Nguyend0f91c82019-03-06 12:39:12 -0800514std::string* ExtensionSet::MutableString(int number, FieldType type,
515 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000516 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000517 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000518 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000519 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
[email protected]d37d46d2009-04-25 02:53:47 +0000520 extension->is_repeated = false;
Hao Nguyend0f91c82019-03-06 12:39:12 -0800521 extension->string_value = Arena::Create<std::string>(arena_);
temporal40ee5512008-07-10 02:12:20 +0000522 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700523 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, STRING);
temporal40ee5512008-07-10 02:12:20 +0000524 }
[email protected]d37d46d2009-04-25 02:53:47 +0000525 extension->is_cleared = false;
temporal40ee5512008-07-10 02:12:20 +0000526 return extension->string_value;
527}
528
Hao Nguyend0f91c82019-03-06 12:39:12 -0800529const std::string& ExtensionSet::GetRepeatedString(int number,
530 int index) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700531 const Extension* extension = FindOrNull(number);
532 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700533 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700534 return extension->repeated_string_value->Get(index);
temporal40ee5512008-07-10 02:12:20 +0000535}
536
Hao Nguyend0f91c82019-03-06 12:39:12 -0800537std::string* ExtensionSet::MutableRepeatedString(int number, int index) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700538 Extension* extension = FindOrNull(number);
539 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700540 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700541 return extension->repeated_string_value->Mutable(index);
temporal40ee5512008-07-10 02:12:20 +0000542}
543
Hao Nguyend0f91c82019-03-06 12:39:12 -0800544std::string* ExtensionSet::AddString(int number, FieldType type,
545 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000546 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000547 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000548 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000549 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
[email protected]d37d46d2009-04-25 02:53:47 +0000550 extension->is_repeated = true;
551 extension->is_packed = false;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800552 extension->repeated_string_value =
Hao Nguyend0f91c82019-03-06 12:39:12 -0800553 Arena::CreateMessage<RepeatedPtrField<std::string>>(arena_);
temporal40ee5512008-07-10 02:12:20 +0000554 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700555 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, STRING);
temporal40ee5512008-07-10 02:12:20 +0000556 }
557 return extension->repeated_string_value->Add();
558}
559
560// -------------------------------------------------------------------
561// Messages
562
[email protected]80b1d622009-07-29 01:13:20 +0000563const MessageLite& ExtensionSet::GetMessage(
564 int number, const MessageLite& default_value) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700565 const Extension* extension = FindOrNull(number);
566 if (extension == NULL) {
temporal40ee5512008-07-10 02:12:20 +0000567 // Not present. Return the default value.
[email protected]d37d46d2009-04-25 02:53:47 +0000568 return default_value;
temporal40ee5512008-07-10 02:12:20 +0000569 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700570 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700571 if (extension->is_lazy) {
572 return extension->lazymessage_value->GetMessage(default_value);
[email protected]b55a20f2012-09-22 02:40:50 +0000573 } else {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700574 return *extension->message_value;
[email protected]b55a20f2012-09-22 02:40:50 +0000575 }
temporal40ee5512008-07-10 02:12:20 +0000576 }
577}
578
[email protected]80b1d622009-07-29 01:13:20 +0000579// Defined in extension_set_heavy.cc.
580// const MessageLite& ExtensionSet::GetMessage(int number,
581// const Descriptor* message_type,
582// MessageFactory* factory) const
[email protected]d37d46d2009-04-25 02:53:47 +0000583
[email protected]80b1d622009-07-29 01:13:20 +0000584MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
[email protected]fccb1462009-12-18 02:11:36 +0000585 const MessageLite& prototype,
586 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000587 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000588 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000589 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000590 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
[email protected]d37d46d2009-04-25 02:53:47 +0000591 extension->is_repeated = false;
[email protected]b55a20f2012-09-22 02:40:50 +0000592 extension->is_lazy = false;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800593 extension->message_value = prototype.New(arena_);
[email protected]b55a20f2012-09-22 02:40:50 +0000594 extension->is_cleared = false;
595 return extension->message_value;
[email protected]d37d46d2009-04-25 02:53:47 +0000596 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700597 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
[email protected]b55a20f2012-09-22 02:40:50 +0000598 extension->is_cleared = false;
599 if (extension->is_lazy) {
600 return extension->lazymessage_value->MutableMessage(prototype);
601 } else {
602 return extension->message_value;
603 }
[email protected]d37d46d2009-04-25 02:53:47 +0000604 }
[email protected]d37d46d2009-04-25 02:53:47 +0000605}
606
[email protected]80b1d622009-07-29 01:13:20 +0000607// Defined in extension_set_heavy.cc.
608// MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
609// const Descriptor* message_type,
610// MessageFactory* factory)
temporal40ee5512008-07-10 02:12:20 +0000611
[email protected]b55a20f2012-09-22 02:40:50 +0000612void ExtensionSet::SetAllocatedMessage(int number, FieldType type,
613 const FieldDescriptor* descriptor,
614 MessageLite* message) {
615 if (message == NULL) {
616 ClearExtension(number);
617 return;
618 }
Deanna Garcia380e3522021-05-14 17:50:31 +0000619#ifdef PROTOBUF_INTERNAL_USE_MUST_USE_RESULT
620 GOOGLE_DCHECK(message->GetOwningArena() == nullptr ||
621 message->GetOwningArena() == arena_);
622#endif // PROTOBUF_INTERNAL_USE_MUST_USE_RESULT
Joshua Habermanf5fd8972021-04-30 16:12:50 -0700623 Arena* message_arena = message->GetOwningArena();
[email protected]b55a20f2012-09-22 02:40:50 +0000624 Extension* extension;
625 if (MaybeNewExtension(number, descriptor, &extension)) {
626 extension->type = type;
627 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
628 extension->is_repeated = false;
629 extension->is_lazy = false;
Jisi Liu46e8ff62015-10-05 11:59:43 -0700630 if (message_arena == arena_) {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800631 extension->message_value = message;
Jisi Liu46e8ff62015-10-05 11:59:43 -0700632 } else if (message_arena == NULL) {
633 extension->message_value = message;
634 arena_->Own(message); // not NULL because not equal to message_arena
Feng Xiao6ef984a2014-11-10 17:34:54 -0800635 } else {
636 extension->message_value = message->New(arena_);
637 extension->message_value->CheckTypeAndMergeFrom(*message);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800638 }
[email protected]b55a20f2012-09-22 02:40:50 +0000639 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700640 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
[email protected]b55a20f2012-09-22 02:40:50 +0000641 if (extension->is_lazy) {
642 extension->lazymessage_value->SetAllocatedMessage(message);
643 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800644 if (arena_ == NULL) {
645 delete extension->message_value;
646 }
Jisi Liu46e8ff62015-10-05 11:59:43 -0700647 if (message_arena == arena_) {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800648 extension->message_value = message;
Jisi Liu46e8ff62015-10-05 11:59:43 -0700649 } else if (message_arena == NULL) {
650 extension->message_value = message;
651 arena_->Own(message); // not NULL because not equal to message_arena
Feng Xiao6ef984a2014-11-10 17:34:54 -0800652 } else {
653 extension->message_value = message->New(arena_);
654 extension->message_value->CheckTypeAndMergeFrom(*message);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800655 }
[email protected]b55a20f2012-09-22 02:40:50 +0000656 }
657 }
658 extension->is_cleared = false;
659}
660
Bo Yang5db21732015-05-21 14:28:59 -0700661void ExtensionSet::UnsafeArenaSetAllocatedMessage(
662 int number, FieldType type, const FieldDescriptor* descriptor,
663 MessageLite* message) {
664 if (message == NULL) {
665 ClearExtension(number);
666 return;
667 }
668 Extension* extension;
669 if (MaybeNewExtension(number, descriptor, &extension)) {
670 extension->type = type;
671 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
672 extension->is_repeated = false;
673 extension->is_lazy = false;
674 extension->message_value = message;
675 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700676 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
Bo Yang5db21732015-05-21 14:28:59 -0700677 if (extension->is_lazy) {
678 extension->lazymessage_value->UnsafeArenaSetAllocatedMessage(message);
679 } else {
680 if (arena_ == NULL) {
681 delete extension->message_value;
682 }
683 extension->message_value = message;
684 }
685 }
686 extension->is_cleared = false;
687}
688
[email protected]b55a20f2012-09-22 02:40:50 +0000689MessageLite* ExtensionSet::ReleaseMessage(int number,
690 const MessageLite& prototype) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700691 Extension* extension = FindOrNull(number);
692 if (extension == NULL) {
[email protected]b55a20f2012-09-22 02:40:50 +0000693 // Not present. Return NULL.
694 return NULL;
695 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700696 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
[email protected]b55a20f2012-09-22 02:40:50 +0000697 MessageLite* ret = NULL;
Adam Cozzette0400cca2018-03-13 16:37:29 -0700698 if (extension->is_lazy) {
699 ret = extension->lazymessage_value->ReleaseMessage(prototype);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800700 if (arena_ == NULL) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700701 delete extension->lazymessage_value;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800702 }
703 } else {
704 if (arena_ == NULL) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700705 ret = extension->message_value;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800706 } else {
707 // ReleaseMessage() always returns a heap-allocated message, and we are
708 // on an arena, so we need to make a copy of this message to return.
Adam Cozzette0400cca2018-03-13 16:37:29 -0700709 ret = extension->message_value->New();
710 ret->CheckTypeAndMergeFrom(*extension->message_value);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800711 }
712 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700713 Erase(number);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800714 return ret;
715 }
716}
717
718MessageLite* ExtensionSet::UnsafeArenaReleaseMessage(
719 int number, const MessageLite& prototype) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700720 Extension* extension = FindOrNull(number);
721 if (extension == NULL) {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800722 // Not present. Return NULL.
723 return NULL;
724 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700725 GOOGLE_DCHECK_TYPE(*extension, OPTIONAL_FIELD, MESSAGE);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800726 MessageLite* ret = NULL;
Adam Cozzette0400cca2018-03-13 16:37:29 -0700727 if (extension->is_lazy) {
728 ret = extension->lazymessage_value->UnsafeArenaReleaseMessage(prototype);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800729 if (arena_ == NULL) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700730 delete extension->lazymessage_value;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800731 }
[email protected]b55a20f2012-09-22 02:40:50 +0000732 } else {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700733 ret = extension->message_value;
[email protected]b55a20f2012-09-22 02:40:50 +0000734 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700735 Erase(number);
[email protected]b55a20f2012-09-22 02:40:50 +0000736 return ret;
737 }
738}
739
740// Defined in extension_set_heavy.cc.
741// MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor,
742// MessageFactory* factory);
743
Hao Nguyen176f7db2019-04-09 06:23:32 -0700744const MessageLite& ExtensionSet::GetRepeatedMessage(int number,
745 int index) const {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700746 const Extension* extension = FindOrNull(number);
747 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700748 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700749 return extension->repeated_message_value->Get(index);
temporal40ee5512008-07-10 02:12:20 +0000750}
751
[email protected]80b1d622009-07-29 01:13:20 +0000752MessageLite* ExtensionSet::MutableRepeatedMessage(int number, int index) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700753 Extension* extension = FindOrNull(number);
754 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700755 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE);
Adam Cozzette0400cca2018-03-13 16:37:29 -0700756 return extension->repeated_message_value->Mutable(index);
temporal40ee5512008-07-10 02:12:20 +0000757}
758
[email protected]80b1d622009-07-29 01:13:20 +0000759MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
[email protected]fccb1462009-12-18 02:11:36 +0000760 const MessageLite& prototype,
761 const FieldDescriptor* descriptor) {
[email protected]d37d46d2009-04-25 02:53:47 +0000762 Extension* extension;
[email protected]fccb1462009-12-18 02:11:36 +0000763 if (MaybeNewExtension(number, descriptor, &extension)) {
[email protected]d37d46d2009-04-25 02:53:47 +0000764 extension->type = type;
[email protected]80b1d622009-07-29 01:13:20 +0000765 GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
[email protected]d37d46d2009-04-25 02:53:47 +0000766 extension->is_repeated = true;
temporal40ee5512008-07-10 02:12:20 +0000767 extension->repeated_message_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700768 Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_);
temporal40ee5512008-07-10 02:12:20 +0000769 } else {
Hao Nguyen2f864fd2019-03-20 11:45:01 -0700770 GOOGLE_DCHECK_TYPE(*extension, REPEATED_FIELD, MESSAGE);
[email protected]d37d46d2009-04-25 02:53:47 +0000771 }
[email protected]80b1d622009-07-29 01:13:20 +0000772
773 // RepeatedPtrField<MessageLite> does not know how to Add() since it cannot
774 // allocate an abstract object, so we have to be tricky.
Feng Xiao6bbe1972018-08-08 17:00:41 -0700775 MessageLite* result = reinterpret_cast<internal::RepeatedPtrFieldBase*>(
776 extension->repeated_message_value)
777 ->AddFromCleared<GenericTypeHandler<MessageLite>>();
[email protected]80b1d622009-07-29 01:13:20 +0000778 if (result == NULL) {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800779 result = prototype.New(arena_);
[email protected]80b1d622009-07-29 01:13:20 +0000780 extension->repeated_message_value->AddAllocated(result);
781 }
782 return result;
[email protected]d37d46d2009-04-25 02:53:47 +0000783}
784
[email protected]80b1d622009-07-29 01:13:20 +0000785// Defined in extension_set_heavy.cc.
786// MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
787// const Descriptor* message_type,
788// MessageFactory* factory)
temporal40ee5512008-07-10 02:12:20 +0000789
790#undef GOOGLE_DCHECK_TYPE
791
[email protected]ceb561d2009-06-25 19:05:36 +0000792void ExtensionSet::RemoveLast(int number) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700793 Extension* extension = FindOrNull(number);
794 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
[email protected]ceb561d2009-06-25 19:05:36 +0000795 GOOGLE_DCHECK(extension->is_repeated);
796
Hao Nguyen176f7db2019-04-09 06:23:32 -0700797 switch (cpp_type(extension->type)) {
[email protected]80b1d622009-07-29 01:13:20 +0000798 case WireFormatLite::CPPTYPE_INT32:
799 extension->repeated_int32_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000800 break;
[email protected]80b1d622009-07-29 01:13:20 +0000801 case WireFormatLite::CPPTYPE_INT64:
802 extension->repeated_int64_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000803 break;
[email protected]80b1d622009-07-29 01:13:20 +0000804 case WireFormatLite::CPPTYPE_UINT32:
805 extension->repeated_uint32_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000806 break;
[email protected]80b1d622009-07-29 01:13:20 +0000807 case WireFormatLite::CPPTYPE_UINT64:
808 extension->repeated_uint64_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000809 break;
[email protected]80b1d622009-07-29 01:13:20 +0000810 case WireFormatLite::CPPTYPE_FLOAT:
811 extension->repeated_float_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000812 break;
[email protected]80b1d622009-07-29 01:13:20 +0000813 case WireFormatLite::CPPTYPE_DOUBLE:
814 extension->repeated_double_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000815 break;
[email protected]80b1d622009-07-29 01:13:20 +0000816 case WireFormatLite::CPPTYPE_BOOL:
817 extension->repeated_bool_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000818 break;
[email protected]80b1d622009-07-29 01:13:20 +0000819 case WireFormatLite::CPPTYPE_ENUM:
820 extension->repeated_enum_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000821 break;
[email protected]80b1d622009-07-29 01:13:20 +0000822 case WireFormatLite::CPPTYPE_STRING:
823 extension->repeated_string_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000824 break;
[email protected]80b1d622009-07-29 01:13:20 +0000825 case WireFormatLite::CPPTYPE_MESSAGE:
826 extension->repeated_message_value->RemoveLast();
[email protected]ceb561d2009-06-25 19:05:36 +0000827 break;
828 }
829}
830
[email protected]b55a20f2012-09-22 02:40:50 +0000831MessageLite* ExtensionSet::ReleaseLast(int number) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700832 Extension* extension = FindOrNull(number);
833 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
[email protected]b55a20f2012-09-22 02:40:50 +0000834 GOOGLE_DCHECK(extension->is_repeated);
835 GOOGLE_DCHECK(cpp_type(extension->type) == WireFormatLite::CPPTYPE_MESSAGE);
836 return extension->repeated_message_value->ReleaseLast();
837}
838
[email protected]ceb561d2009-06-25 19:05:36 +0000839void ExtensionSet::SwapElements(int number, int index1, int index2) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700840 Extension* extension = FindOrNull(number);
841 GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
[email protected]ceb561d2009-06-25 19:05:36 +0000842 GOOGLE_DCHECK(extension->is_repeated);
843
Hao Nguyen176f7db2019-04-09 06:23:32 -0700844 switch (cpp_type(extension->type)) {
[email protected]80b1d622009-07-29 01:13:20 +0000845 case WireFormatLite::CPPTYPE_INT32:
[email protected]ceb561d2009-06-25 19:05:36 +0000846 extension->repeated_int32_value->SwapElements(index1, index2);
847 break;
[email protected]80b1d622009-07-29 01:13:20 +0000848 case WireFormatLite::CPPTYPE_INT64:
849 extension->repeated_int64_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000850 break;
[email protected]80b1d622009-07-29 01:13:20 +0000851 case WireFormatLite::CPPTYPE_UINT32:
[email protected]ceb561d2009-06-25 19:05:36 +0000852 extension->repeated_uint32_value->SwapElements(index1, index2);
853 break;
[email protected]80b1d622009-07-29 01:13:20 +0000854 case WireFormatLite::CPPTYPE_UINT64:
[email protected]ceb561d2009-06-25 19:05:36 +0000855 extension->repeated_uint64_value->SwapElements(index1, index2);
856 break;
[email protected]80b1d622009-07-29 01:13:20 +0000857 case WireFormatLite::CPPTYPE_FLOAT:
858 extension->repeated_float_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000859 break;
[email protected]80b1d622009-07-29 01:13:20 +0000860 case WireFormatLite::CPPTYPE_DOUBLE:
861 extension->repeated_double_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000862 break;
[email protected]80b1d622009-07-29 01:13:20 +0000863 case WireFormatLite::CPPTYPE_BOOL:
864 extension->repeated_bool_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000865 break;
[email protected]80b1d622009-07-29 01:13:20 +0000866 case WireFormatLite::CPPTYPE_ENUM:
867 extension->repeated_enum_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000868 break;
[email protected]80b1d622009-07-29 01:13:20 +0000869 case WireFormatLite::CPPTYPE_STRING:
870 extension->repeated_string_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000871 break;
[email protected]80b1d622009-07-29 01:13:20 +0000872 case WireFormatLite::CPPTYPE_MESSAGE:
873 extension->repeated_message_value->SwapElements(index1, index2);
[email protected]ceb561d2009-06-25 19:05:36 +0000874 break;
875 }
876}
877
temporal40ee5512008-07-10 02:12:20 +0000878// ===================================================================
879
880void ExtensionSet::Clear() {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700881 ForEach([](int /* number */, Extension& ext) { ext.Clear(); });
temporal40ee5512008-07-10 02:12:20 +0000882}
883
Adam Cozzette0400cca2018-03-13 16:37:29 -0700884namespace {
885// Computes the size of a std::set_union without constructing the union.
886template <typename ItX, typename ItY>
887size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) {
888 size_t result = 0;
889 while (it_xs != end_xs && it_ys != end_ys) {
890 ++result;
891 if (it_xs->first < it_ys->first) {
892 ++it_xs;
893 } else if (it_xs->first == it_ys->first) {
894 ++it_xs;
895 ++it_ys;
896 } else {
897 ++it_ys;
898 }
Feng Xiao6ef984a2014-11-10 17:34:54 -0800899 }
Adam Cozzette0400cca2018-03-13 16:37:29 -0700900 result += std::distance(it_xs, end_xs);
901 result += std::distance(it_ys, end_ys);
902 return result;
903}
904} // namespace
905
906void ExtensionSet::MergeFrom(const ExtensionSet& other) {
Josh Habermand61aede2018-09-04 10:58:54 -0700907 if (PROTOBUF_PREDICT_TRUE(!is_large())) {
908 if (PROTOBUF_PREDICT_TRUE(!other.is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -0700909 GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), other.flat_begin(),
910 other.flat_end()));
911 } else {
912 GrowCapacity(SizeOfUnion(flat_begin(), flat_end(),
913 other.map_.large->begin(),
914 other.map_.large->end()));
915 }
916 }
917 other.ForEach([this](int number, const Extension& ext) {
918 this->InternalExtensionMergeFrom(number, ext);
919 });
Feng Xiao6ef984a2014-11-10 17:34:54 -0800920}
[email protected]d37d46d2009-04-25 02:53:47 +0000921
Feng Xiao6ef984a2014-11-10 17:34:54 -0800922void ExtensionSet::InternalExtensionMergeFrom(
Hao Nguyen176f7db2019-04-09 06:23:32 -0700923 int number, const Extension& other_extension) {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800924 if (other_extension.is_repeated) {
925 Extension* extension;
Hao Nguyen176f7db2019-04-09 06:23:32 -0700926 bool is_new =
927 MaybeNewExtension(number, other_extension.descriptor, &extension);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800928 if (is_new) {
929 // Extension did not already exist in set.
930 extension->type = other_extension.type;
931 extension->is_packed = other_extension.is_packed;
932 extension->is_repeated = true;
temporal40ee5512008-07-10 02:12:20 +0000933 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800934 GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
935 GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
936 GOOGLE_DCHECK(extension->is_repeated);
937 }
temporal40ee5512008-07-10 02:12:20 +0000938
Feng Xiao6ef984a2014-11-10 17:34:54 -0800939 switch (cpp_type(other_extension.type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -0700940#define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \
941 case WireFormatLite::CPPTYPE_##UPPERCASE: \
942 if (is_new) { \
943 extension->repeated_##LOWERCASE##_value = \
944 Arena::CreateMessage<REPEATED_TYPE>(arena_); \
945 } \
946 extension->repeated_##LOWERCASE##_value->MergeFrom( \
947 *other_extension.repeated_##LOWERCASE##_value); \
948 break;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800949
Hao Nguyen176f7db2019-04-09 06:23:32 -0700950 HANDLE_TYPE(INT32, int32, RepeatedField<int32>);
951 HANDLE_TYPE(INT64, int64, RepeatedField<int64>);
952 HANDLE_TYPE(UINT32, uint32, RepeatedField<uint32>);
953 HANDLE_TYPE(UINT64, uint64, RepeatedField<uint64>);
954 HANDLE_TYPE(FLOAT, float, RepeatedField<float>);
955 HANDLE_TYPE(DOUBLE, double, RepeatedField<double>);
956 HANDLE_TYPE(BOOL, bool, RepeatedField<bool>);
957 HANDLE_TYPE(ENUM, enum, RepeatedField<int>);
Hao Nguyend0f91c82019-03-06 12:39:12 -0800958 HANDLE_TYPE(STRING, string, RepeatedPtrField<std::string>);
temporal40ee5512008-07-10 02:12:20 +0000959#undef HANDLE_TYPE
Feng Xiao6ef984a2014-11-10 17:34:54 -0800960
961 case WireFormatLite::CPPTYPE_MESSAGE:
962 if (is_new) {
963 extension->repeated_message_value =
Hao Nguyen176f7db2019-04-09 06:23:32 -0700964 Arena::CreateMessage<RepeatedPtrField<MessageLite>>(arena_);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800965 }
966 // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
967 // it would attempt to allocate new objects.
968 RepeatedPtrField<MessageLite>* other_repeated_message =
969 other_extension.repeated_message_value;
970 for (int i = 0; i < other_repeated_message->size(); i++) {
971 const MessageLite& other_message = other_repeated_message->Get(i);
Adam Cozzette92a7e772017-12-01 10:05:10 -0800972 MessageLite* target =
Feng Xiao6bbe1972018-08-08 17:00:41 -0700973 reinterpret_cast<internal::RepeatedPtrFieldBase*>(
Adam Cozzette92a7e772017-12-01 10:05:10 -0800974 extension->repeated_message_value)
Feng Xiao6bbe1972018-08-08 17:00:41 -0700975 ->AddFromCleared<GenericTypeHandler<MessageLite>>();
Feng Xiao6ef984a2014-11-10 17:34:54 -0800976 if (target == NULL) {
977 target = other_message.New(arena_);
978 extension->repeated_message_value->AddAllocated(target);
979 }
980 target->CheckTypeAndMergeFrom(other_message);
981 }
982 break;
983 }
984 } else {
985 if (!other_extension.is_cleared) {
986 switch (cpp_type(other_extension.type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -0700987#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
988 case WireFormatLite::CPPTYPE_##UPPERCASE: \
989 Set##CAMELCASE(number, other_extension.type, \
990 other_extension.LOWERCASE##_value, \
991 other_extension.descriptor); \
992 break;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800993
Hao Nguyen176f7db2019-04-09 06:23:32 -0700994 HANDLE_TYPE(INT32, int32, Int32);
995 HANDLE_TYPE(INT64, int64, Int64);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800996 HANDLE_TYPE(UINT32, uint32, UInt32);
997 HANDLE_TYPE(UINT64, uint64, UInt64);
Hao Nguyen176f7db2019-04-09 06:23:32 -0700998 HANDLE_TYPE(FLOAT, float, Float);
Feng Xiao6ef984a2014-11-10 17:34:54 -0800999 HANDLE_TYPE(DOUBLE, double, Double);
Hao Nguyen176f7db2019-04-09 06:23:32 -07001000 HANDLE_TYPE(BOOL, bool, Bool);
1001 HANDLE_TYPE(ENUM, enum, Enum);
Feng Xiao6ef984a2014-11-10 17:34:54 -08001002#undef HANDLE_TYPE
1003 case WireFormatLite::CPPTYPE_STRING:
Hao Nguyen176f7db2019-04-09 06:23:32 -07001004 SetString(number, other_extension.type, *other_extension.string_value,
Feng Xiao6ef984a2014-11-10 17:34:54 -08001005 other_extension.descriptor);
1006 break;
1007 case WireFormatLite::CPPTYPE_MESSAGE: {
1008 Extension* extension;
Hao Nguyen176f7db2019-04-09 06:23:32 -07001009 bool is_new =
1010 MaybeNewExtension(number, other_extension.descriptor, &extension);
Feng Xiao6ef984a2014-11-10 17:34:54 -08001011 if (is_new) {
1012 extension->type = other_extension.type;
1013 extension->is_packed = other_extension.is_packed;
1014 extension->is_repeated = false;
1015 if (other_extension.is_lazy) {
1016 extension->is_lazy = true;
1017 extension->lazymessage_value =
1018 other_extension.lazymessage_value->New(arena_);
1019 extension->lazymessage_value->MergeFrom(
1020 *other_extension.lazymessage_value);
1021 } else {
1022 extension->is_lazy = false;
1023 extension->message_value =
1024 other_extension.message_value->New(arena_);
1025 extension->message_value->CheckTypeAndMergeFrom(
1026 *other_extension.message_value);
1027 }
1028 } else {
1029 GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
Hao Nguyen176f7db2019-04-09 06:23:32 -07001030 GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
Feng Xiao6ef984a2014-11-10 17:34:54 -08001031 GOOGLE_DCHECK(!extension->is_repeated);
1032 if (other_extension.is_lazy) {
1033 if (extension->is_lazy) {
[email protected]b55a20f2012-09-22 02:40:50 +00001034 extension->lazymessage_value->MergeFrom(
1035 *other_extension.lazymessage_value);
1036 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -08001037 extension->message_value->CheckTypeAndMergeFrom(
1038 other_extension.lazymessage_value->GetMessage(
1039 *extension->message_value));
1040 }
1041 } else {
1042 if (extension->is_lazy) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001043 extension->lazymessage_value
1044 ->MutableMessage(*other_extension.message_value)
1045 ->CheckTypeAndMergeFrom(*other_extension.message_value);
Feng Xiao6ef984a2014-11-10 17:34:54 -08001046 } else {
[email protected]b55a20f2012-09-22 02:40:50 +00001047 extension->message_value->CheckTypeAndMergeFrom(
1048 *other_extension.message_value);
1049 }
[email protected]b55a20f2012-09-22 02:40:50 +00001050 }
[email protected]b55a20f2012-09-22 02:40:50 +00001051 }
Feng Xiao6ef984a2014-11-10 17:34:54 -08001052 extension->is_cleared = false;
1053 break;
temporal40ee5512008-07-10 02:12:20 +00001054 }
1055 }
1056 }
1057 }
1058}
1059
[email protected]26bd9ee2008-11-21 00:06:27 +00001060void ExtensionSet::Swap(ExtensionSet* x) {
Deanna Garcia380e3522021-05-14 17:50:31 +00001061#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
1062 if (GetArena() != nullptr && GetArena() == x->GetArena()) {
1063#else // PROTOBUF_FORCE_COPY_IN_SWAP
Joshua Habermanb99994d2020-03-31 16:25:37 -07001064 if (GetArena() == x->GetArena()) {
Deanna Garcia380e3522021-05-14 17:50:31 +00001065#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
Joshua Habermance560632021-04-15 15:53:17 -07001066 InternalSwap(x);
Feng Xiao6ef984a2014-11-10 17:34:54 -08001067 } else {
1068 // TODO(cfallin, rohananil): We maybe able to optimize a case where we are
1069 // swapping from heap to arena-allocated extension set, by just Own()'ing
1070 // the extensions.
1071 ExtensionSet extension_set;
1072 extension_set.MergeFrom(*x);
1073 x->Clear();
1074 x->MergeFrom(*this);
1075 Clear();
1076 MergeFrom(extension_set);
1077 }
[email protected]26bd9ee2008-11-21 00:06:27 +00001078}
1079
Joshua Habermance560632021-04-15 15:53:17 -07001080void ExtensionSet::InternalSwap(ExtensionSet* other) {
1081 using std::swap;
Joshua Habermanf5fd8972021-04-30 16:12:50 -07001082 swap(arena_, other->arena_);
Joshua Habermance560632021-04-15 15:53:17 -07001083 swap(flat_capacity_, other->flat_capacity_);
1084 swap(flat_size_, other->flat_size_);
1085 swap(map_, other->map_);
1086}
1087
Hao Nguyen176f7db2019-04-09 06:23:32 -07001088void ExtensionSet::SwapExtension(ExtensionSet* other, int number) {
[email protected]4de8f552014-07-18 00:47:59 +00001089 if (this == other) return;
Deanna Garcia380e3522021-05-14 17:50:31 +00001090
1091 if (GetArena() == other->GetArena()) {
1092 UnsafeShallowSwapExtension(other, number);
1093 return;
1094 }
1095
Adam Cozzette0400cca2018-03-13 16:37:29 -07001096 Extension* this_ext = FindOrNull(number);
1097 Extension* other_ext = other->FindOrNull(number);
[email protected]4de8f552014-07-18 00:47:59 +00001098
Deanna Garcia380e3522021-05-14 17:50:31 +00001099 if (this_ext == other_ext) return;
[email protected]4de8f552014-07-18 00:47:59 +00001100
Deanna Garcia380e3522021-05-14 17:50:31 +00001101 if (this_ext != nullptr && other_ext != nullptr) {
1102 // TODO(cfallin, rohananil): We could further optimize these cases,
1103 // especially avoid creation of ExtensionSet, and move MergeFrom logic
1104 // into Extensions itself (which takes arena as an argument).
1105 // We do it this way to reuse the copy-across-arenas logic already
1106 // implemented in ExtensionSet's MergeFrom.
1107 ExtensionSet temp;
1108 temp.InternalExtensionMergeFrom(number, *other_ext);
1109 Extension* temp_ext = temp.FindOrNull(number);
1110 other_ext->Clear();
1111 other->InternalExtensionMergeFrom(number, *this_ext);
1112 this_ext->Clear();
1113 InternalExtensionMergeFrom(number, *temp_ext);
1114 } else if (this_ext == nullptr) {
1115 InternalExtensionMergeFrom(number, *other_ext);
1116 if (other->GetArena() == nullptr) other_ext->Free();
Adam Cozzette0400cca2018-03-13 16:37:29 -07001117 other->Erase(number);
Deanna Garcia380e3522021-05-14 17:50:31 +00001118 } else {
1119 other->InternalExtensionMergeFrom(number, *this_ext);
1120 if (GetArena() == nullptr) this_ext->Free();
Adam Cozzette0400cca2018-03-13 16:37:29 -07001121 Erase(number);
Deanna Garcia380e3522021-05-14 17:50:31 +00001122 }
1123}
1124
1125void ExtensionSet::UnsafeShallowSwapExtension(ExtensionSet* other, int number) {
1126 if (this == other) return;
1127
1128 Extension* this_ext = FindOrNull(number);
1129 Extension* other_ext = other->FindOrNull(number);
1130
1131 if (this_ext == other_ext) return;
1132
1133 GOOGLE_DCHECK_EQ(GetArena(), other->GetArena());
1134
1135 if (this_ext != nullptr && other_ext != nullptr) {
1136 std::swap(*this_ext, *other_ext);
1137 } else if (this_ext == nullptr) {
1138 *Insert(number).first = *other_ext;
1139 other->Erase(number);
1140 } else {
1141 *other->Insert(number).first = *this_ext;
1142 Erase(number);
[email protected]4de8f552014-07-18 00:47:59 +00001143 }
1144}
1145
temporal40ee5512008-07-10 02:12:20 +00001146bool ExtensionSet::IsInitialized() const {
[email protected]d37d46d2009-04-25 02:53:47 +00001147 // Extensions are never required. However, we need to check that all
temporal40ee5512008-07-10 02:12:20 +00001148 // embedded messages are initialized.
Josh Habermand61aede2018-09-04 10:58:54 -07001149 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001150 for (const auto& kv : *map_.large) {
1151 if (!kv.second.IsInitialized()) return false;
temporal40ee5512008-07-10 02:12:20 +00001152 }
Adam Cozzette0400cca2018-03-13 16:37:29 -07001153 return true;
temporal40ee5512008-07-10 02:12:20 +00001154 }
Adam Cozzette0400cca2018-03-13 16:37:29 -07001155 for (const KeyValue* it = flat_begin(); it != flat_end(); ++it) {
1156 if (!it->second.IsInitialized()) return false;
1157 }
temporal40ee5512008-07-10 02:12:20 +00001158 return true;
1159}
1160
Hao Nguyen176f7db2019-04-09 06:23:32 -07001161bool ExtensionSet::FindExtensionInfoFromTag(uint32 tag,
1162 ExtensionFinder* extension_finder,
1163 int* field_number,
1164 ExtensionInfo* extension,
1165 bool* was_packed_on_wire) {
[email protected]b55a20f2012-09-22 02:40:50 +00001166 *field_number = WireFormatLite::GetTagFieldNumber(tag);
[email protected]80b1d622009-07-29 01:13:20 +00001167 WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
[email protected]4de8f552014-07-18 00:47:59 +00001168 return FindExtensionInfoFromFieldNumber(wire_type, *field_number,
1169 extension_finder, extension,
1170 was_packed_on_wire);
1171}
temporal40ee5512008-07-10 02:12:20 +00001172
[email protected]4de8f552014-07-18 00:47:59 +00001173bool ExtensionSet::FindExtensionInfoFromFieldNumber(
1174 int wire_type, int field_number, ExtensionFinder* extension_finder,
1175 ExtensionInfo* extension, bool* was_packed_on_wire) {
1176 if (!extension_finder->Find(field_number, extension)) {
1177 return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001178 }
[email protected]4de8f552014-07-18 00:47:59 +00001179
1180 WireFormatLite::WireType expected_wire_type =
1181 WireFormatLite::WireTypeForFieldType(real_type(extension->type));
1182
1183 // Check if this is a packed field.
1184 *was_packed_on_wire = false;
1185 if (extension->is_repeated &&
1186 wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED &&
1187 is_packable(expected_wire_type)) {
1188 *was_packed_on_wire = true;
1189 return true;
1190 }
1191 // Otherwise the wire type must match.
1192 return expected_wire_type == wire_type;
[email protected]b55a20f2012-09-22 02:40:50 +00001193}
[email protected]d37d46d2009-04-25 02:53:47 +00001194
[email protected]b55a20f2012-09-22 02:40:50 +00001195bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
1196 ExtensionFinder* extension_finder,
1197 FieldSkipper* field_skipper) {
1198 int number;
[email protected]4de8f552014-07-18 00:47:59 +00001199 bool was_packed_on_wire;
[email protected]b55a20f2012-09-22 02:40:50 +00001200 ExtensionInfo extension;
Hao Nguyen176f7db2019-04-09 06:23:32 -07001201 if (!FindExtensionInfoFromTag(tag, extension_finder, &number, &extension,
1202 &was_packed_on_wire)) {
[email protected]b55a20f2012-09-22 02:40:50 +00001203 return field_skipper->SkipField(input, tag);
1204 } else {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001205 return ParseFieldWithExtensionInfo(number, was_packed_on_wire, extension,
1206 input, field_skipper);
[email protected]b55a20f2012-09-22 02:40:50 +00001207 }
1208}
1209
Joshua Habermanb99994d2020-03-31 16:25:37 -07001210const char* ExtensionSet::ParseField(uint64 tag, const char* ptr,
1211 const MessageLite* containing_type,
1212 internal::InternalMetadata* metadata,
1213 internal::ParseContext* ctx) {
Feng Xiao6bbe1972018-08-08 17:00:41 -07001214 GeneratedExtensionFinder finder(containing_type);
Josh Habermand61aede2018-09-04 10:58:54 -07001215 int number = tag >> 3;
Feng Xiao6bbe1972018-08-08 17:00:41 -07001216 bool was_packed_on_wire;
1217 ExtensionInfo extension;
Josh Habermand61aede2018-09-04 10:58:54 -07001218 if (!FindExtensionInfoFromFieldNumber(tag & 7, number, &finder, &extension,
1219 &was_packed_on_wire)) {
Joshua Habermanb99994d2020-03-31 16:25:37 -07001220 return UnknownFieldParse(
1221 tag, metadata->mutable_unknown_fields<std::string>(), ptr, ctx);
Feng Xiao6bbe1972018-08-08 17:00:41 -07001222 }
Joshua Habermanb99994d2020-03-31 16:25:37 -07001223 return ParseFieldWithExtensionInfo<std::string>(
1224 number, was_packed_on_wire, extension, metadata, ptr, ctx);
Feng Xiao6bbe1972018-08-08 17:00:41 -07001225}
Yilun Chongd8c25012019-02-22 18:13:33 +08001226
1227const char* ExtensionSet::ParseMessageSetItem(
1228 const char* ptr, const MessageLite* containing_type,
Joshua Habermanb99994d2020-03-31 16:25:37 -07001229 internal::InternalMetadata* metadata, internal::ParseContext* ctx) {
1230 return ParseMessageSetItemTmpl<MessageLite, std::string>(ptr, containing_type,
1231 metadata, ctx);
Yilun Chongd8c25012019-02-22 18:13:33 +08001232}
1233
Hao Nguyen176f7db2019-04-09 06:23:32 -07001234bool ExtensionSet::ParseFieldWithExtensionInfo(int number,
1235 bool was_packed_on_wire,
1236 const ExtensionInfo& extension,
1237 io::CodedInputStream* input,
1238 FieldSkipper* field_skipper) {
[email protected]4de8f552014-07-18 00:47:59 +00001239 // Explicitly not read extension.is_packed, instead check whether the field
1240 // was encoded in packed form on the wire.
1241 if (was_packed_on_wire) {
[email protected]d37d46d2009-04-25 02:53:47 +00001242 uint32 size;
1243 if (!input->ReadVarint32(&size)) return false;
1244 io::CodedInputStream::Limit limit = input->PushLimit(size);
1245
[email protected]fccb1462009-12-18 02:11:36 +00001246 switch (extension.type) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001247#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
1248 case WireFormatLite::TYPE_##UPPERCASE: \
1249 while (input->BytesUntilLimit() > 0) { \
1250 CPP_LOWERCASE value; \
1251 if (!WireFormatLite::ReadPrimitive<CPP_LOWERCASE, \
1252 WireFormatLite::TYPE_##UPPERCASE>( \
1253 input, &value)) \
1254 return false; \
1255 Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
1256 extension.is_packed, value, extension.descriptor); \
1257 } \
1258 break
[email protected]d37d46d2009-04-25 02:53:47 +00001259
Hao Nguyen176f7db2019-04-09 06:23:32 -07001260 HANDLE_TYPE(INT32, Int32, int32);
1261 HANDLE_TYPE(INT64, Int64, int64);
1262 HANDLE_TYPE(UINT32, UInt32, uint32);
1263 HANDLE_TYPE(UINT64, UInt64, uint64);
1264 HANDLE_TYPE(SINT32, Int32, int32);
1265 HANDLE_TYPE(SINT64, Int64, int64);
1266 HANDLE_TYPE(FIXED32, UInt32, uint32);
1267 HANDLE_TYPE(FIXED64, UInt64, uint64);
1268 HANDLE_TYPE(SFIXED32, Int32, int32);
1269 HANDLE_TYPE(SFIXED64, Int64, int64);
1270 HANDLE_TYPE(FLOAT, Float, float);
1271 HANDLE_TYPE(DOUBLE, Double, double);
1272 HANDLE_TYPE(BOOL, Bool, bool);
[email protected]d37d46d2009-04-25 02:53:47 +00001273#undef HANDLE_TYPE
1274
[email protected]80b1d622009-07-29 01:13:20 +00001275 case WireFormatLite::TYPE_ENUM:
[email protected]d37d46d2009-04-25 02:53:47 +00001276 while (input->BytesUntilLimit() > 0) {
1277 int value;
[email protected]fccb1462009-12-18 02:11:36 +00001278 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
Hao Nguyen176f7db2019-04-09 06:23:32 -07001279 input, &value))
1280 return false;
[email protected]f9c59782009-12-22 18:04:23 +00001281 if (extension.enum_validity_check.func(
1282 extension.enum_validity_check.arg, value)) {
[email protected]4de8f552014-07-18 00:47:59 +00001283 AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed,
1284 value, extension.descriptor);
Jisi Liu885b6122015-02-28 14:51:22 -08001285 } else {
1286 // Invalid value. Treat as unknown.
1287 field_skipper->SkipUnknownEnum(number, value);
[email protected]d37d46d2009-04-25 02:53:47 +00001288 }
1289 }
1290 break;
1291
[email protected]80b1d622009-07-29 01:13:20 +00001292 case WireFormatLite::TYPE_STRING:
1293 case WireFormatLite::TYPE_BYTES:
1294 case WireFormatLite::TYPE_GROUP:
1295 case WireFormatLite::TYPE_MESSAGE:
[email protected]d37d46d2009-04-25 02:53:47 +00001296 GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
1297 break;
1298 }
1299
1300 input->PopLimit(limit);
1301 } else {
[email protected]fccb1462009-12-18 02:11:36 +00001302 switch (extension.type) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001303#define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
1304 case WireFormatLite::TYPE_##UPPERCASE: { \
1305 CPP_LOWERCASE value; \
1306 if (!WireFormatLite::ReadPrimitive<CPP_LOWERCASE, \
1307 WireFormatLite::TYPE_##UPPERCASE>( \
1308 input, &value)) \
1309 return false; \
1310 if (extension.is_repeated) { \
1311 Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
1312 extension.is_packed, value, extension.descriptor); \
1313 } else { \
1314 Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
1315 extension.descriptor); \
1316 } \
1317 } break
[email protected]d37d46d2009-04-25 02:53:47 +00001318
Hao Nguyen176f7db2019-04-09 06:23:32 -07001319 HANDLE_TYPE(INT32, Int32, int32);
1320 HANDLE_TYPE(INT64, Int64, int64);
1321 HANDLE_TYPE(UINT32, UInt32, uint32);
1322 HANDLE_TYPE(UINT64, UInt64, uint64);
1323 HANDLE_TYPE(SINT32, Int32, int32);
1324 HANDLE_TYPE(SINT64, Int64, int64);
1325 HANDLE_TYPE(FIXED32, UInt32, uint32);
1326 HANDLE_TYPE(FIXED64, UInt64, uint64);
1327 HANDLE_TYPE(SFIXED32, Int32, int32);
1328 HANDLE_TYPE(SFIXED64, Int64, int64);
1329 HANDLE_TYPE(FLOAT, Float, float);
1330 HANDLE_TYPE(DOUBLE, Double, double);
1331 HANDLE_TYPE(BOOL, Bool, bool);
[email protected]d37d46d2009-04-25 02:53:47 +00001332#undef HANDLE_TYPE
1333
[email protected]80b1d622009-07-29 01:13:20 +00001334 case WireFormatLite::TYPE_ENUM: {
[email protected]d37d46d2009-04-25 02:53:47 +00001335 int value;
[email protected]fccb1462009-12-18 02:11:36 +00001336 if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
Hao Nguyen176f7db2019-04-09 06:23:32 -07001337 input, &value))
1338 return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001339
[email protected]f9c59782009-12-22 18:04:23 +00001340 if (!extension.enum_validity_check.func(
1341 extension.enum_validity_check.arg, value)) {
[email protected]d37d46d2009-04-25 02:53:47 +00001342 // Invalid value. Treat as unknown.
[email protected]80b1d622009-07-29 01:13:20 +00001343 field_skipper->SkipUnknownEnum(number, value);
[email protected]fccb1462009-12-18 02:11:36 +00001344 } else if (extension.is_repeated) {
[email protected]4de8f552014-07-18 00:47:59 +00001345 AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, value,
[email protected]fccb1462009-12-18 02:11:36 +00001346 extension.descriptor);
[email protected]d37d46d2009-04-25 02:53:47 +00001347 } else {
[email protected]fccb1462009-12-18 02:11:36 +00001348 SetEnum(number, WireFormatLite::TYPE_ENUM, value,
1349 extension.descriptor);
[email protected]d37d46d2009-04-25 02:53:47 +00001350 }
1351 break;
1352 }
1353
Hao Nguyen176f7db2019-04-09 06:23:32 -07001354 case WireFormatLite::TYPE_STRING: {
Hao Nguyend0f91c82019-03-06 12:39:12 -08001355 std::string* value =
1356 extension.is_repeated
1357 ? AddString(number, WireFormatLite::TYPE_STRING,
1358 extension.descriptor)
1359 : MutableString(number, WireFormatLite::TYPE_STRING,
1360 extension.descriptor);
[email protected]80b1d622009-07-29 01:13:20 +00001361 if (!WireFormatLite::ReadString(input, value)) return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001362 break;
1363 }
1364
Hao Nguyen176f7db2019-04-09 06:23:32 -07001365 case WireFormatLite::TYPE_BYTES: {
Hao Nguyend0f91c82019-03-06 12:39:12 -08001366 std::string* value =
1367 extension.is_repeated
1368 ? AddString(number, WireFormatLite::TYPE_BYTES,
1369 extension.descriptor)
1370 : MutableString(number, WireFormatLite::TYPE_BYTES,
1371 extension.descriptor);
[email protected]80b1d622009-07-29 01:13:20 +00001372 if (!WireFormatLite::ReadBytes(input, value)) return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001373 break;
1374 }
1375
[email protected]80b1d622009-07-29 01:13:20 +00001376 case WireFormatLite::TYPE_GROUP: {
Adam Cozzette0894e072018-11-09 11:28:22 -08001377 MessageLite* value =
1378 extension.is_repeated
1379 ? AddMessage(number, WireFormatLite::TYPE_GROUP,
1380 *extension.message_info.prototype,
1381 extension.descriptor)
1382 : MutableMessage(number, WireFormatLite::TYPE_GROUP,
1383 *extension.message_info.prototype,
1384 extension.descriptor);
[email protected]80b1d622009-07-29 01:13:20 +00001385 if (!WireFormatLite::ReadGroup(number, input, value)) return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001386 break;
1387 }
1388
[email protected]80b1d622009-07-29 01:13:20 +00001389 case WireFormatLite::TYPE_MESSAGE: {
Adam Cozzette0894e072018-11-09 11:28:22 -08001390 MessageLite* value =
1391 extension.is_repeated
1392 ? AddMessage(number, WireFormatLite::TYPE_MESSAGE,
1393 *extension.message_info.prototype,
1394 extension.descriptor)
1395 : MutableMessage(number, WireFormatLite::TYPE_MESSAGE,
1396 *extension.message_info.prototype,
1397 extension.descriptor);
[email protected]80b1d622009-07-29 01:13:20 +00001398 if (!WireFormatLite::ReadMessage(input, value)) return false;
[email protected]d37d46d2009-04-25 02:53:47 +00001399 break;
1400 }
temporal40ee5512008-07-10 02:12:20 +00001401 }
1402 }
1403
1404 return true;
1405}
1406
[email protected]80b1d622009-07-29 01:13:20 +00001407bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
1408 const MessageLite* containing_type) {
1409 FieldSkipper skipper;
[email protected]fccb1462009-12-18 02:11:36 +00001410 GeneratedExtensionFinder finder(containing_type);
1411 return ParseField(tag, input, &finder, &skipper);
[email protected]80b1d622009-07-29 01:13:20 +00001412}
1413
[email protected]4de8f552014-07-18 00:47:59 +00001414bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
1415 const MessageLite* containing_type,
1416 io::CodedOutputStream* unknown_fields) {
1417 CodedOutputStreamFieldSkipper skipper(unknown_fields);
1418 GeneratedExtensionFinder finder(containing_type);
1419 return ParseField(tag, input, &finder, &skipper);
1420}
1421
Feng Xiao6bbe1972018-08-08 17:00:41 -07001422bool ExtensionSet::ParseMessageSetLite(io::CodedInputStream* input,
1423 ExtensionFinder* extension_finder,
1424 FieldSkipper* field_skipper) {
1425 while (true) {
1426 const uint32 tag = input->ReadTag();
1427 switch (tag) {
1428 case 0:
1429 return true;
1430 case WireFormatLite::kMessageSetItemStartTag:
1431 if (!ParseMessageSetItemLite(input, extension_finder, field_skipper)) {
1432 return false;
1433 }
1434 break;
1435 default:
1436 if (!ParseField(tag, input, extension_finder, field_skipper)) {
1437 return false;
1438 }
1439 break;
1440 }
1441 }
1442}
[email protected]80b1d622009-07-29 01:13:20 +00001443
Feng Xiao6bbe1972018-08-08 17:00:41 -07001444bool ExtensionSet::ParseMessageSetItemLite(io::CodedInputStream* input,
1445 ExtensionFinder* extension_finder,
1446 FieldSkipper* field_skipper) {
1447 struct MSLite {
1448 bool ParseField(int type_id, io::CodedInputStream* input) {
1449 return me->ParseField(
1450 WireFormatLite::WIRETYPE_LENGTH_DELIMITED + 8 * type_id, input,
1451 extension_finder, field_skipper);
1452 }
1453
1454 bool SkipField(uint32 tag, io::CodedInputStream* input) {
1455 return field_skipper->SkipField(input, tag);
1456 }
1457
1458 ExtensionSet* me;
1459 ExtensionFinder* extension_finder;
1460 FieldSkipper* field_skipper;
1461 };
1462
1463 return ParseMessageSetItemImpl(input,
1464 MSLite{this, extension_finder, field_skipper});
1465}
1466
1467bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
1468 const MessageLite* containing_type,
Hao Nguyend0f91c82019-03-06 12:39:12 -08001469 std::string* unknown_fields) {
Feng Xiao6bbe1972018-08-08 17:00:41 -07001470 io::StringOutputStream zcis(unknown_fields);
1471 io::CodedOutputStream output(&zcis);
1472 CodedOutputStreamFieldSkipper skipper(&output);
1473 GeneratedExtensionFinder finder(containing_type);
1474 return ParseMessageSetLite(input, &finder, &skipper);
1475}
[email protected]80b1d622009-07-29 01:13:20 +00001476
Deanna Garciaa0012502021-01-22 00:24:30 +00001477uint8* ExtensionSet::_InternalSerializeImpl(
1478 int start_field_number, int end_field_number, uint8* target,
1479 io::EpsCopyOutputStream* stream) const {
Josh Habermand61aede2018-09-04 10:58:54 -07001480 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001481 const auto& end = map_.large->end();
1482 for (auto it = map_.large->lower_bound(start_field_number);
1483 it != end && it->first < end_field_number; ++it) {
Hao Nguyen51026d92019-06-26 11:01:34 -07001484 target = it->second.InternalSerializeFieldWithCachedSizesToArray(
1485 it->first, target, stream);
Adam Cozzette0400cca2018-03-13 16:37:29 -07001486 }
Hao Nguyen51026d92019-06-26 11:01:34 -07001487 return target;
Adam Cozzette0400cca2018-03-13 16:37:29 -07001488 }
1489 const KeyValue* end = flat_end();
1490 for (const KeyValue* it = std::lower_bound(
1491 flat_begin(), end, start_field_number, KeyValue::FirstComparator());
1492 it != end && it->first < end_field_number; ++it) {
Hao Nguyen51026d92019-06-26 11:01:34 -07001493 target = it->second.InternalSerializeFieldWithCachedSizesToArray(
1494 it->first, target, stream);
[email protected]d37d46d2009-04-25 02:53:47 +00001495 }
Hao Nguyen51026d92019-06-26 11:01:34 -07001496 return target;
1497}
1498
1499uint8* ExtensionSet::InternalSerializeMessageSetWithCachedSizesToArray(
1500 uint8* target, io::EpsCopyOutputStream* stream) const {
1501 ForEach([&target, stream](int number, const Extension& ext) {
1502 target = ext.InternalSerializeMessageSetItemWithCachedSizesToArray(
1503 number, target, stream);
1504 });
1505 return target;
[email protected]d37d46d2009-04-25 02:53:47 +00001506}
1507
Bo Yangcc8ca5b2016-09-19 13:45:07 -07001508size_t ExtensionSet::ByteSize() const {
1509 size_t total_size = 0;
Adam Cozzette0400cca2018-03-13 16:37:29 -07001510 ForEach([&total_size](int number, const Extension& ext) {
1511 total_size += ext.ByteSize(number);
1512 });
temporal40ee5512008-07-10 02:12:20 +00001513 return total_size;
1514}
1515
[email protected]80b1d622009-07-29 01:13:20 +00001516// Defined in extension_set_heavy.cc.
1517// int ExtensionSet::SpaceUsedExcludingSelf() const
1518
[email protected]fccb1462009-12-18 02:11:36 +00001519bool ExtensionSet::MaybeNewExtension(int number,
1520 const FieldDescriptor* descriptor,
1521 Extension** result) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001522 bool extension_is_new = false;
1523 std::tie(*result, extension_is_new) = Insert(number);
[email protected]fccb1462009-12-18 02:11:36 +00001524 (*result)->descriptor = descriptor;
Adam Cozzette0400cca2018-03-13 16:37:29 -07001525 return extension_is_new;
[email protected]d37d46d2009-04-25 02:53:47 +00001526}
1527
temporal40ee5512008-07-10 02:12:20 +00001528// ===================================================================
1529// Methods of ExtensionSet::Extension
1530
1531void ExtensionSet::Extension::Clear() {
[email protected]d37d46d2009-04-25 02:53:47 +00001532 if (is_repeated) {
1533 switch (cpp_type(type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001534#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
1535 case WireFormatLite::CPPTYPE_##UPPERCASE: \
1536 repeated_##LOWERCASE##_value->Clear(); \
1537 break
temporal40ee5512008-07-10 02:12:20 +00001538
Hao Nguyen176f7db2019-04-09 06:23:32 -07001539 HANDLE_TYPE(INT32, int32);
1540 HANDLE_TYPE(INT64, int64);
1541 HANDLE_TYPE(UINT32, uint32);
1542 HANDLE_TYPE(UINT64, uint64);
1543 HANDLE_TYPE(FLOAT, float);
1544 HANDLE_TYPE(DOUBLE, double);
1545 HANDLE_TYPE(BOOL, bool);
1546 HANDLE_TYPE(ENUM, enum);
1547 HANDLE_TYPE(STRING, string);
temporal40ee5512008-07-10 02:12:20 +00001548 HANDLE_TYPE(MESSAGE, message);
1549#undef HANDLE_TYPE
1550 }
1551 } else {
1552 if (!is_cleared) {
[email protected]d37d46d2009-04-25 02:53:47 +00001553 switch (cpp_type(type)) {
[email protected]80b1d622009-07-29 01:13:20 +00001554 case WireFormatLite::CPPTYPE_STRING:
[email protected]d37d46d2009-04-25 02:53:47 +00001555 string_value->clear();
temporal40ee5512008-07-10 02:12:20 +00001556 break;
[email protected]80b1d622009-07-29 01:13:20 +00001557 case WireFormatLite::CPPTYPE_MESSAGE:
[email protected]b55a20f2012-09-22 02:40:50 +00001558 if (is_lazy) {
1559 lazymessage_value->Clear();
1560 } else {
1561 message_value->Clear();
1562 }
temporal40ee5512008-07-10 02:12:20 +00001563 break;
[email protected]d37d46d2009-04-25 02:53:47 +00001564 default:
1565 // No need to do anything. Get*() will return the default value
1566 // as long as is_cleared is true and Set*() will overwrite the
1567 // previous value.
1568 break;
temporal40ee5512008-07-10 02:12:20 +00001569 }
1570
1571 is_cleared = true;
1572 }
1573 }
1574}
1575
Bo Yangcc8ca5b2016-09-19 13:45:07 -07001576size_t ExtensionSet::Extension::ByteSize(int number) const {
1577 size_t result = 0;
[email protected]d37d46d2009-04-25 02:53:47 +00001578
1579 if (is_repeated) {
1580 if (is_packed) {
1581 switch (real_type(type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001582#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1583 case WireFormatLite::TYPE_##UPPERCASE: \
1584 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
1585 result += WireFormatLite::CAMELCASE##Size( \
1586 repeated_##LOWERCASE##_value->Get(i)); \
1587 } \
1588 break
[email protected]d37d46d2009-04-25 02:53:47 +00001589
Hao Nguyen176f7db2019-04-09 06:23:32 -07001590 HANDLE_TYPE(INT32, Int32, int32);
1591 HANDLE_TYPE(INT64, Int64, int64);
1592 HANDLE_TYPE(UINT32, UInt32, uint32);
1593 HANDLE_TYPE(UINT64, UInt64, uint64);
1594 HANDLE_TYPE(SINT32, SInt32, int32);
1595 HANDLE_TYPE(SINT64, SInt64, int64);
1596 HANDLE_TYPE(ENUM, Enum, enum);
[email protected]d37d46d2009-04-25 02:53:47 +00001597#undef HANDLE_TYPE
1598
1599 // Stuff with fixed size.
Hao Nguyen176f7db2019-04-09 06:23:32 -07001600#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1601 case WireFormatLite::TYPE_##UPPERCASE: \
1602 result += WireFormatLite::k##CAMELCASE##Size * \
1603 FromIntSize(repeated_##LOWERCASE##_value->size()); \
1604 break
1605 HANDLE_TYPE(FIXED32, Fixed32, uint32);
1606 HANDLE_TYPE(FIXED64, Fixed64, uint64);
1607 HANDLE_TYPE(SFIXED32, SFixed32, int32);
1608 HANDLE_TYPE(SFIXED64, SFixed64, int64);
1609 HANDLE_TYPE(FLOAT, Float, float);
1610 HANDLE_TYPE(DOUBLE, Double, double);
1611 HANDLE_TYPE(BOOL, Bool, bool);
[email protected]d37d46d2009-04-25 02:53:47 +00001612#undef HANDLE_TYPE
1613
[email protected]80b1d622009-07-29 01:13:20 +00001614 case WireFormatLite::TYPE_STRING:
1615 case WireFormatLite::TYPE_BYTES:
1616 case WireFormatLite::TYPE_GROUP:
1617 case WireFormatLite::TYPE_MESSAGE:
[email protected]d37d46d2009-04-25 02:53:47 +00001618 GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
1619 break;
1620 }
1621
Bo Yangcc8ca5b2016-09-19 13:45:07 -07001622 cached_size = ToCachedSize(result);
[email protected]d37d46d2009-04-25 02:53:47 +00001623 if (result > 0) {
1624 result += io::CodedOutputStream::VarintSize32(result);
Hao Nguyen176f7db2019-04-09 06:23:32 -07001625 result += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
1626 number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
[email protected]d37d46d2009-04-25 02:53:47 +00001627 }
1628 } else {
Bo Yangcc8ca5b2016-09-19 13:45:07 -07001629 size_t tag_size = WireFormatLite::TagSize(number, real_type(type));
[email protected]d37d46d2009-04-25 02:53:47 +00001630
1631 switch (real_type(type)) {
1632#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
Hao Nguyen176f7db2019-04-09 06:23:32 -07001633 case WireFormatLite::TYPE_##UPPERCASE: \
1634 result += tag_size * FromIntSize(repeated_##LOWERCASE##_value->size()); \
1635 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
1636 result += WireFormatLite::CAMELCASE##Size( \
1637 repeated_##LOWERCASE##_value->Get(i)); \
1638 } \
1639 break
[email protected]d37d46d2009-04-25 02:53:47 +00001640
Hao Nguyen176f7db2019-04-09 06:23:32 -07001641 HANDLE_TYPE(INT32, Int32, int32);
1642 HANDLE_TYPE(INT64, Int64, int64);
1643 HANDLE_TYPE(UINT32, UInt32, uint32);
1644 HANDLE_TYPE(UINT64, UInt64, uint64);
1645 HANDLE_TYPE(SINT32, SInt32, int32);
1646 HANDLE_TYPE(SINT64, SInt64, int64);
1647 HANDLE_TYPE(STRING, String, string);
1648 HANDLE_TYPE(BYTES, Bytes, string);
1649 HANDLE_TYPE(ENUM, Enum, enum);
1650 HANDLE_TYPE(GROUP, Group, message);
1651 HANDLE_TYPE(MESSAGE, Message, message);
[email protected]d37d46d2009-04-25 02:53:47 +00001652#undef HANDLE_TYPE
1653
1654 // Stuff with fixed size.
Hao Nguyen176f7db2019-04-09 06:23:32 -07001655#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1656 case WireFormatLite::TYPE_##UPPERCASE: \
1657 result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \
1658 FromIntSize(repeated_##LOWERCASE##_value->size()); \
1659 break
1660 HANDLE_TYPE(FIXED32, Fixed32, uint32);
1661 HANDLE_TYPE(FIXED64, Fixed64, uint64);
1662 HANDLE_TYPE(SFIXED32, SFixed32, int32);
1663 HANDLE_TYPE(SFIXED64, SFixed64, int64);
1664 HANDLE_TYPE(FLOAT, Float, float);
1665 HANDLE_TYPE(DOUBLE, Double, double);
1666 HANDLE_TYPE(BOOL, Bool, bool);
[email protected]d37d46d2009-04-25 02:53:47 +00001667#undef HANDLE_TYPE
1668 }
1669 }
1670 } else if (!is_cleared) {
[email protected]80b1d622009-07-29 01:13:20 +00001671 result += WireFormatLite::TagSize(number, real_type(type));
[email protected]d37d46d2009-04-25 02:53:47 +00001672 switch (real_type(type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001673#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1674 case WireFormatLite::TYPE_##UPPERCASE: \
1675 result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \
1676 break
[email protected]d37d46d2009-04-25 02:53:47 +00001677
Hao Nguyen176f7db2019-04-09 06:23:32 -07001678 HANDLE_TYPE(INT32, Int32, int32_value);
1679 HANDLE_TYPE(INT64, Int64, int64_value);
1680 HANDLE_TYPE(UINT32, UInt32, uint32_value);
1681 HANDLE_TYPE(UINT64, UInt64, uint64_value);
1682 HANDLE_TYPE(SINT32, SInt32, int32_value);
1683 HANDLE_TYPE(SINT64, SInt64, int64_value);
1684 HANDLE_TYPE(STRING, String, *string_value);
1685 HANDLE_TYPE(BYTES, Bytes, *string_value);
1686 HANDLE_TYPE(ENUM, Enum, enum_value);
1687 HANDLE_TYPE(GROUP, Group, *message_value);
[email protected]d37d46d2009-04-25 02:53:47 +00001688#undef HANDLE_TYPE
[email protected]b55a20f2012-09-22 02:40:50 +00001689 case WireFormatLite::TYPE_MESSAGE: {
1690 if (is_lazy) {
Paul Yang763c3582019-09-12 11:03:27 -07001691 size_t size = lazymessage_value->ByteSizeLong();
[email protected]b55a20f2012-09-22 02:40:50 +00001692 result += io::CodedOutputStream::VarintSize32(size) + size;
1693 } else {
1694 result += WireFormatLite::MessageSize(*message_value);
1695 }
1696 break;
1697 }
[email protected]d37d46d2009-04-25 02:53:47 +00001698
1699 // Stuff with fixed size.
Hao Nguyen176f7db2019-04-09 06:23:32 -07001700#define HANDLE_TYPE(UPPERCASE, CAMELCASE) \
1701 case WireFormatLite::TYPE_##UPPERCASE: \
1702 result += WireFormatLite::k##CAMELCASE##Size; \
1703 break
1704 HANDLE_TYPE(FIXED32, Fixed32);
1705 HANDLE_TYPE(FIXED64, Fixed64);
1706 HANDLE_TYPE(SFIXED32, SFixed32);
1707 HANDLE_TYPE(SFIXED64, SFixed64);
1708 HANDLE_TYPE(FLOAT, Float);
1709 HANDLE_TYPE(DOUBLE, Double);
1710 HANDLE_TYPE(BOOL, Bool);
[email protected]d37d46d2009-04-25 02:53:47 +00001711#undef HANDLE_TYPE
1712 }
temporal40ee5512008-07-10 02:12:20 +00001713 }
[email protected]d37d46d2009-04-25 02:53:47 +00001714
1715 return result;
temporal40ee5512008-07-10 02:12:20 +00001716}
1717
1718int ExtensionSet::Extension::GetSize() const {
[email protected]d37d46d2009-04-25 02:53:47 +00001719 GOOGLE_DCHECK(is_repeated);
1720 switch (cpp_type(type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001721#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
1722 case WireFormatLite::CPPTYPE_##UPPERCASE: \
1723 return repeated_##LOWERCASE##_value->size()
temporal40ee5512008-07-10 02:12:20 +00001724
Hao Nguyen176f7db2019-04-09 06:23:32 -07001725 HANDLE_TYPE(INT32, int32);
1726 HANDLE_TYPE(INT64, int64);
1727 HANDLE_TYPE(UINT32, uint32);
1728 HANDLE_TYPE(UINT64, uint64);
1729 HANDLE_TYPE(FLOAT, float);
1730 HANDLE_TYPE(DOUBLE, double);
1731 HANDLE_TYPE(BOOL, bool);
1732 HANDLE_TYPE(ENUM, enum);
1733 HANDLE_TYPE(STRING, string);
temporal40ee5512008-07-10 02:12:20 +00001734 HANDLE_TYPE(MESSAGE, message);
1735#undef HANDLE_TYPE
1736 }
1737
1738 GOOGLE_LOG(FATAL) << "Can't get here.";
1739 return 0;
1740}
1741
Feng Xiao6ef984a2014-11-10 17:34:54 -08001742// This function deletes all allocated objects. This function should be only
Deanna Garcia380e3522021-05-14 17:50:31 +00001743// called if the Extension was created without an arena.
temporal40ee5512008-07-10 02:12:20 +00001744void ExtensionSet::Extension::Free() {
[email protected]d37d46d2009-04-25 02:53:47 +00001745 if (is_repeated) {
1746 switch (cpp_type(type)) {
Hao Nguyen176f7db2019-04-09 06:23:32 -07001747#define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
1748 case WireFormatLite::CPPTYPE_##UPPERCASE: \
1749 delete repeated_##LOWERCASE##_value; \
1750 break
temporal40ee5512008-07-10 02:12:20 +00001751
Hao Nguyen176f7db2019-04-09 06:23:32 -07001752 HANDLE_TYPE(INT32, int32);
1753 HANDLE_TYPE(INT64, int64);
1754 HANDLE_TYPE(UINT32, uint32);
1755 HANDLE_TYPE(UINT64, uint64);
1756 HANDLE_TYPE(FLOAT, float);
1757 HANDLE_TYPE(DOUBLE, double);
1758 HANDLE_TYPE(BOOL, bool);
1759 HANDLE_TYPE(ENUM, enum);
1760 HANDLE_TYPE(STRING, string);
temporal40ee5512008-07-10 02:12:20 +00001761 HANDLE_TYPE(MESSAGE, message);
1762#undef HANDLE_TYPE
1763 }
1764 } else {
[email protected]d37d46d2009-04-25 02:53:47 +00001765 switch (cpp_type(type)) {
[email protected]80b1d622009-07-29 01:13:20 +00001766 case WireFormatLite::CPPTYPE_STRING:
temporal40ee5512008-07-10 02:12:20 +00001767 delete string_value;
1768 break;
[email protected]80b1d622009-07-29 01:13:20 +00001769 case WireFormatLite::CPPTYPE_MESSAGE:
[email protected]b55a20f2012-09-22 02:40:50 +00001770 if (is_lazy) {
1771 delete lazymessage_value;
1772 } else {
1773 delete message_value;
1774 }
temporal40ee5512008-07-10 02:12:20 +00001775 break;
1776 default:
1777 break;
1778 }
1779 }
1780}
1781
[email protected]80b1d622009-07-29 01:13:20 +00001782// Defined in extension_set_heavy.cc.
1783// int ExtensionSet::Extension::SpaceUsedExcludingSelf() const
[email protected]26bd9ee2008-11-21 00:06:27 +00001784
Adam Cozzette0400cca2018-03-13 16:37:29 -07001785bool ExtensionSet::Extension::IsInitialized() const {
1786 if (cpp_type(type) == WireFormatLite::CPPTYPE_MESSAGE) {
1787 if (is_repeated) {
1788 for (int i = 0; i < repeated_message_value->size(); i++) {
1789 if (!repeated_message_value->Get(i).IsInitialized()) {
1790 return false;
1791 }
1792 }
1793 } else {
1794 if (!is_cleared) {
1795 if (is_lazy) {
1796 if (!lazymessage_value->IsInitialized()) return false;
1797 } else {
1798 if (!message_value->IsInitialized()) return false;
1799 }
1800 }
1801 }
1802 }
1803 return true;
1804}
1805
Adam Cozzette92a7e772017-12-01 10:05:10 -08001806// Dummy key method to avoid weak vtable.
1807void ExtensionSet::LazyMessageExtension::UnusedKeyMethod() {}
1808
Adam Cozzette0400cca2018-03-13 16:37:29 -07001809const ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) const {
Josh Habermand61aede2018-09-04 10:58:54 -07001810 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001811 return FindOrNullInLargeMap(key);
1812 }
1813 const KeyValue* end = flat_end();
1814 const KeyValue* it =
1815 std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
1816 if (it != end && it->first == key) {
1817 return &it->second;
1818 }
1819 return NULL;
1820}
1821
1822const ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(
1823 int key) const {
1824 assert(is_large());
1825 LargeMap::const_iterator it = map_.large->find(key);
1826 if (it != map_.large->end()) {
1827 return &it->second;
1828 }
1829 return NULL;
1830}
1831
1832ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) {
Josh Habermand61aede2018-09-04 10:58:54 -07001833 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001834 return FindOrNullInLargeMap(key);
1835 }
1836 KeyValue* end = flat_end();
1837 KeyValue* it =
1838 std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
1839 if (it != end && it->first == key) {
1840 return &it->second;
1841 }
1842 return NULL;
1843}
1844
1845ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(int key) {
1846 assert(is_large());
1847 LargeMap::iterator it = map_.large->find(key);
1848 if (it != map_.large->end()) {
1849 return &it->second;
1850 }
1851 return NULL;
1852}
1853
1854std::pair<ExtensionSet::Extension*, bool> ExtensionSet::Insert(int key) {
Josh Habermand61aede2018-09-04 10:58:54 -07001855 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001856 auto maybe = map_.large->insert({key, Extension()});
1857 return {&maybe.first->second, maybe.second};
1858 }
1859 KeyValue* end = flat_end();
1860 KeyValue* it =
1861 std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
1862 if (it != end && it->first == key) {
1863 return {&it->second, false};
1864 }
1865 if (flat_size_ < flat_capacity_) {
1866 std::copy_backward(it, end, end + 1);
1867 ++flat_size_;
1868 it->first = key;
1869 it->second = Extension();
1870 return {&it->second, true};
1871 }
1872 GrowCapacity(flat_size_ + 1);
1873 return Insert(key);
1874}
1875
1876void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) {
Josh Habermand61aede2018-09-04 10:58:54 -07001877 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001878 return; // LargeMap does not have a "reserve" method.
1879 }
1880 if (flat_capacity_ >= minimum_new_capacity) {
1881 return;
1882 }
1883
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001884 auto new_flat_capacity = flat_capacity_;
Adam Cozzette0400cca2018-03-13 16:37:29 -07001885 do {
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001886 new_flat_capacity = new_flat_capacity == 0 ? 1 : new_flat_capacity * 4;
1887 } while (new_flat_capacity < minimum_new_capacity);
Adam Cozzette0400cca2018-03-13 16:37:29 -07001888
1889 const KeyValue* begin = flat_begin();
1890 const KeyValue* end = flat_end();
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001891 AllocatedData new_map;
1892 if (new_flat_capacity > kMaximumFlatCapacity) {
1893 new_map.large = Arena::Create<LargeMap>(arena_);
1894 LargeMap::iterator hint = new_map.large->begin();
Adam Cozzette0400cca2018-03-13 16:37:29 -07001895 for (const KeyValue* it = begin; it != end; ++it) {
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001896 hint = new_map.large->insert(hint, {it->first, it->second});
Adam Cozzette0400cca2018-03-13 16:37:29 -07001897 }
Adam Cozzette0400cca2018-03-13 16:37:29 -07001898 } else {
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001899 new_map.flat = Arena::CreateArray<KeyValue>(arena_, new_flat_capacity);
1900 std::copy(begin, end, new_map.flat);
Adam Cozzette0400cca2018-03-13 16:37:29 -07001901 }
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001902
Feng Xiao6bbe1972018-08-08 17:00:41 -07001903 if (arena_ == nullptr) {
Joshua Haberman95e6c5b2020-08-17 15:26:13 -07001904 DeleteFlatMap(begin, flat_capacity_);
1905 }
1906 flat_capacity_ = new_flat_capacity;
1907 map_ = new_map;
Adam Cozzette0400cca2018-03-13 16:37:29 -07001908}
1909
1910// static
1911constexpr uint16 ExtensionSet::kMaximumFlatCapacity;
1912
1913void ExtensionSet::Erase(int key) {
Josh Habermand61aede2018-09-04 10:58:54 -07001914 if (PROTOBUF_PREDICT_FALSE(is_large())) {
Adam Cozzette0400cca2018-03-13 16:37:29 -07001915 map_.large->erase(key);
1916 return;
1917 }
1918 KeyValue* end = flat_end();
1919 KeyValue* it =
1920 std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
1921 if (it != end && it->first == key) {
1922 std::copy(it + 1, end, it);
1923 --flat_size_;
1924 }
1925}
1926
[email protected]4de8f552014-07-18 00:47:59 +00001927// ==================================================================
1928// Default repeated field instances for iterator-compatible accessors
1929
Adam Cozzettea9abc782018-07-06 14:12:33 -07001930const RepeatedPrimitiveDefaults* RepeatedPrimitiveDefaults::default_instance() {
1931 static auto instance = OnShutdownDelete(new RepeatedPrimitiveDefaults);
1932 return instance;
Peter Kasting4f3bead2015-08-27 20:16:33 -07001933}
1934
[email protected]4de8f552014-07-18 00:47:59 +00001935const RepeatedStringTypeTraits::RepeatedFieldType*
Adam Cozzettea9abc782018-07-06 14:12:33 -07001936RepeatedStringTypeTraits::GetDefaultRepeatedField() {
1937 static auto instance = OnShutdownDelete(new RepeatedFieldType);
1938 return instance;
1939}
[email protected]4de8f552014-07-18 00:47:59 +00001940
Hao Nguyen51026d92019-06-26 11:01:34 -07001941uint8* ExtensionSet::Extension::InternalSerializeFieldWithCachedSizesToArray(
1942 int number, uint8* target, io::EpsCopyOutputStream* stream) const {
1943 if (is_repeated) {
1944 if (is_packed) {
1945 if (cached_size == 0) return target;
1946
Paul Yang763c3582019-09-12 11:03:27 -07001947 target = stream->EnsureSpace(target);
Hao Nguyen51026d92019-06-26 11:01:34 -07001948 target = WireFormatLite::WriteTagToArray(
1949 number, WireFormatLite::WIRETYPE_LENGTH_DELIMITED, target);
1950 target = WireFormatLite::WriteInt32NoTagToArray(cached_size, target);
1951
1952 switch (real_type(type)) {
1953#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1954 case WireFormatLite::TYPE_##UPPERCASE: \
1955 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
Paul Yang763c3582019-09-12 11:03:27 -07001956 target = stream->EnsureSpace(target); \
Hao Nguyen51026d92019-06-26 11:01:34 -07001957 target = WireFormatLite::Write##CAMELCASE##NoTagToArray( \
1958 repeated_##LOWERCASE##_value->Get(i), target); \
1959 } \
1960 break
1961
1962 HANDLE_TYPE(INT32, Int32, int32);
1963 HANDLE_TYPE(INT64, Int64, int64);
1964 HANDLE_TYPE(UINT32, UInt32, uint32);
1965 HANDLE_TYPE(UINT64, UInt64, uint64);
1966 HANDLE_TYPE(SINT32, SInt32, int32);
1967 HANDLE_TYPE(SINT64, SInt64, int64);
1968 HANDLE_TYPE(FIXED32, Fixed32, uint32);
1969 HANDLE_TYPE(FIXED64, Fixed64, uint64);
1970 HANDLE_TYPE(SFIXED32, SFixed32, int32);
1971 HANDLE_TYPE(SFIXED64, SFixed64, int64);
1972 HANDLE_TYPE(FLOAT, Float, float);
1973 HANDLE_TYPE(DOUBLE, Double, double);
1974 HANDLE_TYPE(BOOL, Bool, bool);
1975 HANDLE_TYPE(ENUM, Enum, enum);
1976#undef HANDLE_TYPE
1977
1978 case WireFormatLite::TYPE_STRING:
1979 case WireFormatLite::TYPE_BYTES:
1980 case WireFormatLite::TYPE_GROUP:
1981 case WireFormatLite::TYPE_MESSAGE:
1982 GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
1983 break;
1984 }
1985 } else {
1986 switch (real_type(type)) {
1987#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
1988 case WireFormatLite::TYPE_##UPPERCASE: \
1989 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
Paul Yang763c3582019-09-12 11:03:27 -07001990 target = stream->EnsureSpace(target); \
Hao Nguyen51026d92019-06-26 11:01:34 -07001991 target = WireFormatLite::Write##CAMELCASE##ToArray( \
1992 number, repeated_##LOWERCASE##_value->Get(i), target); \
1993 } \
1994 break
1995
1996 HANDLE_TYPE(INT32, Int32, int32);
1997 HANDLE_TYPE(INT64, Int64, int64);
1998 HANDLE_TYPE(UINT32, UInt32, uint32);
1999 HANDLE_TYPE(UINT64, UInt64, uint64);
2000 HANDLE_TYPE(SINT32, SInt32, int32);
2001 HANDLE_TYPE(SINT64, SInt64, int64);
2002 HANDLE_TYPE(FIXED32, Fixed32, uint32);
2003 HANDLE_TYPE(FIXED64, Fixed64, uint64);
2004 HANDLE_TYPE(SFIXED32, SFixed32, int32);
2005 HANDLE_TYPE(SFIXED64, SFixed64, int64);
2006 HANDLE_TYPE(FLOAT, Float, float);
2007 HANDLE_TYPE(DOUBLE, Double, double);
2008 HANDLE_TYPE(BOOL, Bool, bool);
2009 HANDLE_TYPE(ENUM, Enum, enum);
2010#undef HANDLE_TYPE
2011#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
2012 case WireFormatLite::TYPE_##UPPERCASE: \
2013 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
Paul Yang763c3582019-09-12 11:03:27 -07002014 target = stream->EnsureSpace(target); \
Hao Nguyen51026d92019-06-26 11:01:34 -07002015 target = stream->WriteString( \
2016 number, repeated_##LOWERCASE##_value->Get(i), target); \
2017 } \
2018 break
2019 HANDLE_TYPE(STRING, String, string);
2020 HANDLE_TYPE(BYTES, Bytes, string);
2021#undef HANDLE_TYPE
2022#define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
2023 case WireFormatLite::TYPE_##UPPERCASE: \
2024 for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
Paul Yang763c3582019-09-12 11:03:27 -07002025 target = stream->EnsureSpace(target); \
Rafi Kamal58d44202019-11-11 17:06:56 -08002026 target = WireFormatLite::InternalWrite##CAMELCASE( \
Hao Nguyen51026d92019-06-26 11:01:34 -07002027 number, repeated_##LOWERCASE##_value->Get(i), target, stream); \
2028 } \
2029 break
2030
2031 HANDLE_TYPE(GROUP, Group, message);
2032 HANDLE_TYPE(MESSAGE, Message, message);
2033#undef HANDLE_TYPE
2034 }
2035 }
2036 } else if (!is_cleared) {
2037 switch (real_type(type)) {
2038#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \
2039 case WireFormatLite::TYPE_##UPPERCASE: \
Paul Yang763c3582019-09-12 11:03:27 -07002040 target = stream->EnsureSpace(target); \
Hao Nguyen51026d92019-06-26 11:01:34 -07002041 target = WireFormatLite::Write##CAMELCASE##ToArray(number, VALUE, target); \
2042 break
2043
2044 HANDLE_TYPE(INT32, Int32, int32_value);
2045 HANDLE_TYPE(INT64, Int64, int64_value);
2046 HANDLE_TYPE(UINT32, UInt32, uint32_value);
2047 HANDLE_TYPE(UINT64, UInt64, uint64_value);
2048 HANDLE_TYPE(SINT32, SInt32, int32_value);
2049 HANDLE_TYPE(SINT64, SInt64, int64_value);
2050 HANDLE_TYPE(FIXED32, Fixed32, uint32_value);
2051 HANDLE_TYPE(FIXED64, Fixed64, uint64_value);
2052 HANDLE_TYPE(SFIXED32, SFixed32, int32_value);
2053 HANDLE_TYPE(SFIXED64, SFixed64, int64_value);
2054 HANDLE_TYPE(FLOAT, Float, float_value);
2055 HANDLE_TYPE(DOUBLE, Double, double_value);
2056 HANDLE_TYPE(BOOL, Bool, bool_value);
2057 HANDLE_TYPE(ENUM, Enum, enum_value);
2058#undef HANDLE_TYPE
2059#define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \
2060 case WireFormatLite::TYPE_##UPPERCASE: \
Paul Yang763c3582019-09-12 11:03:27 -07002061 target = stream->EnsureSpace(target); \
Hao Nguyen51026d92019-06-26 11:01:34 -07002062 target = stream->WriteString(number, VALUE, target); \
2063 break
2064 HANDLE_TYPE(STRING, String, *string_value);
2065 HANDLE_TYPE(BYTES, Bytes, *string_value);
2066#undef HANDLE_TYPE
2067 case WireFormatLite::TYPE_GROUP:
Paul Yang763c3582019-09-12 11:03:27 -07002068 target = stream->EnsureSpace(target);
Rafi Kamal58d44202019-11-11 17:06:56 -08002069 target = WireFormatLite::InternalWriteGroup(number, *message_value,
2070 target, stream);
Hao Nguyen51026d92019-06-26 11:01:34 -07002071 break;
2072 case WireFormatLite::TYPE_MESSAGE:
2073 if (is_lazy) {
2074 target =
2075 lazymessage_value->WriteMessageToArray(number, target, stream);
2076 } else {
Paul Yang763c3582019-09-12 11:03:27 -07002077 target = stream->EnsureSpace(target);
Rafi Kamal58d44202019-11-11 17:06:56 -08002078 target = WireFormatLite::InternalWriteMessage(number, *message_value,
2079 target, stream);
Hao Nguyen51026d92019-06-26 11:01:34 -07002080 }
2081 break;
2082 }
2083 }
2084 return target;
2085}
2086
2087uint8*
2088ExtensionSet::Extension::InternalSerializeMessageSetItemWithCachedSizesToArray(
2089 int number, uint8* target, io::EpsCopyOutputStream* stream) const {
Josh Habermand61aede2018-09-04 10:58:54 -07002090 if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) {
2091 // Not a valid MessageSet extension, but serialize it the normal way.
Hao Nguyen51026d92019-06-26 11:01:34 -07002092 GOOGLE_LOG(WARNING) << "Invalid message set extension.";
2093 return InternalSerializeFieldWithCachedSizesToArray(number, target, stream);
Josh Habermand61aede2018-09-04 10:58:54 -07002094 }
2095
Hao Nguyen51026d92019-06-26 11:01:34 -07002096 if (is_cleared) return target;
Josh Habermand61aede2018-09-04 10:58:54 -07002097
Paul Yang763c3582019-09-12 11:03:27 -07002098 target = stream->EnsureSpace(target);
Josh Habermand61aede2018-09-04 10:58:54 -07002099 // Start group.
Hao Nguyen51026d92019-06-26 11:01:34 -07002100 target = io::CodedOutputStream::WriteTagToArray(
2101 WireFormatLite::kMessageSetItemStartTag, target);
Josh Habermand61aede2018-09-04 10:58:54 -07002102 // Write type ID.
Hao Nguyen51026d92019-06-26 11:01:34 -07002103 target = WireFormatLite::WriteUInt32ToArray(
2104 WireFormatLite::kMessageSetTypeIdNumber, number, target);
Josh Habermand61aede2018-09-04 10:58:54 -07002105 // Write message.
2106 if (is_lazy) {
Hao Nguyen51026d92019-06-26 11:01:34 -07002107 target = lazymessage_value->WriteMessageToArray(
2108 WireFormatLite::kMessageSetMessageNumber, target, stream);
Josh Habermand61aede2018-09-04 10:58:54 -07002109 } else {
Rafi Kamal58d44202019-11-11 17:06:56 -08002110 target = WireFormatLite::InternalWriteMessage(
Hao Nguyen51026d92019-06-26 11:01:34 -07002111 WireFormatLite::kMessageSetMessageNumber, *message_value, target,
2112 stream);
Josh Habermand61aede2018-09-04 10:58:54 -07002113 }
Josh Habermand61aede2018-09-04 10:58:54 -07002114 // End group.
Paul Yang763c3582019-09-12 11:03:27 -07002115 target = stream->EnsureSpace(target);
Hao Nguyen51026d92019-06-26 11:01:34 -07002116 target = io::CodedOutputStream::WriteTagToArray(
2117 WireFormatLite::kMessageSetItemEndTag, target);
2118 return target;
Josh Habermand61aede2018-09-04 10:58:54 -07002119}
2120
2121size_t ExtensionSet::Extension::MessageSetItemByteSize(int number) const {
2122 if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) {
2123 // Not a valid MessageSet extension, but compute the byte size for it the
2124 // normal way.
2125 return ByteSize(number);
2126 }
2127
2128 if (is_cleared) return 0;
2129
2130 size_t our_size = WireFormatLite::kMessageSetItemTagsSize;
2131
2132 // type_id
2133 our_size += io::CodedOutputStream::VarintSize32(number);
2134
2135 // message
2136 size_t message_size = 0;
2137 if (is_lazy) {
2138 message_size = lazymessage_value->ByteSizeLong();
2139 } else {
2140 message_size = message_value->ByteSizeLong();
2141 }
2142
2143 our_size += io::CodedOutputStream::VarintSize32(message_size);
2144 our_size += message_size;
2145
2146 return our_size;
2147}
2148
Josh Habermand61aede2018-09-04 10:58:54 -07002149size_t ExtensionSet::MessageSetByteSize() const {
2150 size_t total_size = 0;
2151 ForEach([&total_size](int number, const Extension& ext) {
2152 total_size += ext.MessageSetItemByteSize(number);
2153 });
2154 return total_size;
2155}
2156
temporal40ee5512008-07-10 02:12:20 +00002157} // namespace internal
2158} // namespace protobuf
2159} // namespace google
Joshua Haberman5c028d62020-11-20 10:44:58 -08002160
2161#include <google/protobuf/port_undef.inc>