12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916 |
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // https://developers.google.com/protocol-buffers/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- // Author: kenton@google.com (Kenton Varda)
- // Based on original Protocol Buffers design by
- // Sanjay Ghemawat, Jeff Dean, and others.
- #include <google/protobuf/stubs/hash.h>
- #include <tuple>
- #include <utility>
- #include <google/protobuf/stubs/common.h>
- #include <google/protobuf/extension_set.h>
- #include <google/protobuf/message_lite.h>
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/wire_format_lite_inl.h>
- #include <google/protobuf/repeated_field.h>
- #include <google/protobuf/stubs/map_util.h>
- namespace google {
- namespace protobuf {
- namespace internal {
- namespace {
- inline WireFormatLite::FieldType real_type(FieldType type) {
- GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE);
- return static_cast<WireFormatLite::FieldType>(type);
- }
- inline WireFormatLite::CppType cpp_type(FieldType type) {
- return WireFormatLite::FieldTypeToCppType(real_type(type));
- }
- inline bool is_packable(WireFormatLite::WireType type) {
- switch (type) {
- case WireFormatLite::WIRETYPE_VARINT:
- case WireFormatLite::WIRETYPE_FIXED64:
- case WireFormatLite::WIRETYPE_FIXED32:
- return true;
- case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
- case WireFormatLite::WIRETYPE_START_GROUP:
- case WireFormatLite::WIRETYPE_END_GROUP:
- return false;
- // Do not add a default statement. Let the compiler complain when someone
- // adds a new wire type.
- }
- GOOGLE_LOG(FATAL) << "can't reach here.";
- return false;
- }
- // Registry stuff.
- typedef hash_map<std::pair<const MessageLite*, int>,
- ExtensionInfo> ExtensionRegistry;
- static const ExtensionRegistry* global_registry = nullptr;
- // This function is only called at startup, so there is no need for thread-
- // safety.
- void Register(const MessageLite* containing_type,
- int number, ExtensionInfo info) {
- static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry);
- global_registry = local_static_registry;
- if (!InsertIfNotPresent(local_static_registry,
- std::make_pair(containing_type, number), info)) {
- GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \""
- << containing_type->GetTypeName()
- << "\", field number " << number << ".";
- }
- }
- const ExtensionInfo* FindRegisteredExtension(
- const MessageLite* containing_type, int number) {
- return global_registry == nullptr
- ? nullptr
- : FindOrNull(*global_registry, std::make_pair(containing_type, number));
- }
- } // namespace
- ExtensionFinder::~ExtensionFinder() {}
- bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) {
- const ExtensionInfo* extension =
- FindRegisteredExtension(containing_type_, number);
- if (extension == NULL) {
- return false;
- } else {
- *output = *extension;
- return true;
- }
- }
- void ExtensionSet::RegisterExtension(const MessageLite* containing_type,
- int number, FieldType type,
- bool is_repeated, bool is_packed) {
- GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_ENUM);
- GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_MESSAGE);
- GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_GROUP);
- ExtensionInfo info(type, is_repeated, is_packed);
- Register(containing_type, number, info);
- }
- static bool CallNoArgValidityFunc(const void* arg, int number) {
- // Note: Must use C-style cast here rather than reinterpret_cast because
- // the C++ standard at one point did not allow casts between function and
- // data pointers and some compilers enforce this for C++-style casts. No
- // compiler enforces it for C-style casts since lots of C-style code has
- // relied on these kinds of casts for a long time, despite being
- // technically undefined. See:
- // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
- // Also note: Some compilers do not allow function pointers to be "const".
- // Which makes sense, I suppose, because it's meaningless.
- return ((EnumValidityFunc*)arg)(number);
- }
- void ExtensionSet::RegisterEnumExtension(const MessageLite* containing_type,
- int number, FieldType type,
- bool is_repeated, bool is_packed,
- EnumValidityFunc* is_valid) {
- GOOGLE_CHECK_EQ(type, WireFormatLite::TYPE_ENUM);
- ExtensionInfo info(type, is_repeated, is_packed);
- info.enum_validity_check.func = CallNoArgValidityFunc;
- // See comment in CallNoArgValidityFunc() about why we use a c-style cast.
- info.enum_validity_check.arg = (void*)is_valid;
- Register(containing_type, number, info);
- }
- void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type,
- int number, FieldType type,
- bool is_repeated, bool is_packed,
- const MessageLite* prototype) {
- GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE ||
- type == WireFormatLite::TYPE_GROUP);
- ExtensionInfo info(type, is_repeated, is_packed);
- info.message_prototype = prototype;
- Register(containing_type, number, info);
- }
- // ===================================================================
- // Constructors and basic methods.
- ExtensionSet::ExtensionSet(::google::protobuf::Arena* arena)
- : arena_(arena),
- flat_capacity_(0),
- flat_size_(0),
- map_{flat_capacity_ == 0 ? NULL
- : ::google::protobuf::Arena::CreateArray<KeyValue>(
- arena_, flat_capacity_)} {}
- ExtensionSet::ExtensionSet()
- : arena_(NULL),
- flat_capacity_(0),
- flat_size_(0),
- map_{flat_capacity_ == 0 ? NULL
- : ::google::protobuf::Arena::CreateArray<KeyValue>(
- arena_, flat_capacity_)} {}
- ExtensionSet::~ExtensionSet() {
- // Deletes all allocated extensions.
- if (arena_ == NULL) {
- ForEach([](int /* number */, Extension& ext) { ext.Free(); });
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- delete map_.large;
- } else {
- delete[] map_.flat;
- }
- }
- }
- // Defined in extension_set_heavy.cc.
- // void ExtensionSet::AppendToList(const Descriptor* containing_type,
- // const DescriptorPool* pool,
- // vector<const FieldDescriptor*>* output) const
- bool ExtensionSet::Has(int number) const {
- const Extension* ext = FindOrNull(number);
- if (ext == NULL) return false;
- GOOGLE_DCHECK(!ext->is_repeated);
- return !ext->is_cleared;
- }
- int ExtensionSet::NumExtensions() const {
- int result = 0;
- ForEach([&result](int /* number */, const Extension& ext) {
- if (!ext.is_cleared) {
- ++result;
- }
- });
- return result;
- }
- int ExtensionSet::ExtensionSize(int number) const {
- const Extension* ext = FindOrNull(number);
- return ext == NULL ? 0 : ext->GetSize();
- }
- FieldType ExtensionSet::ExtensionType(int number) const {
- const Extension* ext = FindOrNull(number);
- if (ext == NULL) {
- GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (1). ";
- return 0;
- }
- if (ext->is_cleared) {
- GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (2). ";
- }
- return ext->type;
- }
- void ExtensionSet::ClearExtension(int number) {
- Extension* ext = FindOrNull(number);
- if (ext == NULL) return;
- ext->Clear();
- }
- // ===================================================================
- // Field accessors
- namespace {
- enum Cardinality {
- REPEATED,
- OPTIONAL
- };
- } // namespace
- #define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \
- GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED : OPTIONAL, LABEL); \
- GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), WireFormatLite::CPPTYPE_##CPPTYPE)
- // -------------------------------------------------------------------
- // Primitives
- #define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \
- \
- LOWERCASE ExtensionSet::Get##CAMELCASE(int number, \
- LOWERCASE default_value) const { \
- const Extension* extension = FindOrNull(number); \
- if (extension == NULL || extension->is_cleared) { \
- return default_value; \
- } else { \
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, UPPERCASE); \
- return extension->LOWERCASE##_value; \
- } \
- } \
- \
- void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \
- LOWERCASE value, \
- const FieldDescriptor* descriptor) { \
- Extension* extension; \
- if (MaybeNewExtension(number, descriptor, &extension)) { \
- extension->type = type; \
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
- extension->is_repeated = false; \
- } else { \
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, UPPERCASE); \
- } \
- extension->is_cleared = false; \
- extension->LOWERCASE##_value = value; \
- } \
- \
- LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) const { \
- const Extension* extension = FindOrNull(number); \
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
- return extension->repeated_##LOWERCASE##_value->Get(index); \
- } \
- \
- void ExtensionSet::SetRepeated##CAMELCASE( \
- int number, int index, LOWERCASE value) { \
- Extension* extension = FindOrNull(number); \
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
- extension->repeated_##LOWERCASE##_value->Set(index, value); \
- } \
- \
- void ExtensionSet::Add##CAMELCASE(int number, FieldType type, \
- bool packed, LOWERCASE value, \
- const FieldDescriptor* descriptor) { \
- Extension* extension; \
- if (MaybeNewExtension(number, descriptor, &extension)) { \
- extension->type = type; \
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
- extension->is_repeated = true; \
- extension->is_packed = packed; \
- extension->repeated_##LOWERCASE##_value = \
- Arena::CreateMessage<RepeatedField<LOWERCASE> >(arena_); \
- } else { \
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
- GOOGLE_DCHECK_EQ(extension->is_packed, packed); \
- } \
- extension->repeated_##LOWERCASE##_value->Add(value); \
- }
- PRIMITIVE_ACCESSORS( INT32, int32, Int32)
- PRIMITIVE_ACCESSORS( INT64, int64, Int64)
- PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32)
- PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64)
- PRIMITIVE_ACCESSORS( FLOAT, float, Float)
- PRIMITIVE_ACCESSORS(DOUBLE, double, Double)
- PRIMITIVE_ACCESSORS( BOOL, bool, Bool)
- #undef PRIMITIVE_ACCESSORS
- const void* ExtensionSet::GetRawRepeatedField(int number,
- const void* default_value) const {
- const Extension* extension = FindOrNull(number);
- if (extension == NULL) {
- return default_value;
- }
- // We assume that all the RepeatedField<>* pointers have the same
- // size and alignment within the anonymous union in Extension.
- return extension->repeated_int32_value;
- }
- void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type,
- bool packed,
- const FieldDescriptor* desc) {
- Extension* extension;
- // We instantiate an empty Repeated{,Ptr}Field if one doesn't exist for this
- // extension.
- if (MaybeNewExtension(number, desc, &extension)) {
- extension->is_repeated = true;
- extension->type = field_type;
- extension->is_packed = packed;
- switch (WireFormatLite::FieldTypeToCppType(
- static_cast<WireFormatLite::FieldType>(field_type))) {
- case WireFormatLite::CPPTYPE_INT32:
- extension->repeated_int32_value =
- Arena::CreateMessage<RepeatedField<int32> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_INT64:
- extension->repeated_int64_value =
- Arena::CreateMessage<RepeatedField<int64> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_UINT32:
- extension->repeated_uint32_value =
- Arena::CreateMessage<RepeatedField<uint32> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_UINT64:
- extension->repeated_uint64_value =
- Arena::CreateMessage<RepeatedField<uint64> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_DOUBLE:
- extension->repeated_double_value =
- Arena::CreateMessage<RepeatedField<double> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_FLOAT:
- extension->repeated_float_value =
- Arena::CreateMessage<RepeatedField<float> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_BOOL:
- extension->repeated_bool_value =
- Arena::CreateMessage<RepeatedField<bool> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_ENUM:
- extension->repeated_enum_value =
- Arena::CreateMessage<RepeatedField<int> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_STRING:
- extension->repeated_string_value =
- Arena::CreateMessage<RepeatedPtrField<::std::string> >(arena_);
- break;
- case WireFormatLite::CPPTYPE_MESSAGE:
- extension->repeated_message_value =
- Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
- break;
- }
- }
- // We assume that all the RepeatedField<>* pointers have the same
- // size and alignment within the anonymous union in Extension.
- return extension->repeated_int32_value;
- }
- // Compatible version using old call signature. Does not create extensions when
- // the don't already exist; instead, just GOOGLE_CHECK-fails.
- void* ExtensionSet::MutableRawRepeatedField(int number) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Extension not found.";
- // We assume that all the RepeatedField<>* pointers have the same
- // size and alignment within the anonymous union in Extension.
- return extension->repeated_int32_value;
- }
- // -------------------------------------------------------------------
- // Enums
- int ExtensionSet::GetEnum(int number, int default_value) const {
- const Extension* extension = FindOrNull(number);
- if (extension == NULL || extension->is_cleared) {
- // Not present. Return the default value.
- return default_value;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM);
- return extension->enum_value;
- }
- }
- void ExtensionSet::SetEnum(int number, FieldType type, int value,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
- extension->is_repeated = false;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM);
- }
- extension->is_cleared = false;
- extension->enum_value = value;
- }
- int ExtensionSet::GetRepeatedEnum(int number, int index) const {
- const Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
- return extension->repeated_enum_value->Get(index);
- }
- void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
- extension->repeated_enum_value->Set(index, value);
- }
- void ExtensionSet::AddEnum(int number, FieldType type,
- bool packed, int value,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
- extension->is_repeated = true;
- extension->is_packed = packed;
- extension->repeated_enum_value =
- Arena::CreateMessage<RepeatedField<int> >(arena_);
- } else {
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
- GOOGLE_DCHECK_EQ(extension->is_packed, packed);
- }
- extension->repeated_enum_value->Add(value);
- }
- // -------------------------------------------------------------------
- // Strings
- const string& ExtensionSet::GetString(int number,
- const string& default_value) const {
- const Extension* extension = FindOrNull(number);
- if (extension == NULL || extension->is_cleared) {
- // Not present. Return the default value.
- return default_value;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
- return *extension->string_value;
- }
- }
- string* ExtensionSet::MutableString(int number, FieldType type,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
- extension->is_repeated = false;
- extension->string_value = Arena::Create<string>(arena_);
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
- }
- extension->is_cleared = false;
- return extension->string_value;
- }
- const string& ExtensionSet::GetRepeatedString(int number, int index) const {
- const Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
- return extension->repeated_string_value->Get(index);
- }
- string* ExtensionSet::MutableRepeatedString(int number, int index) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
- return extension->repeated_string_value->Mutable(index);
- }
- string* ExtensionSet::AddString(int number, FieldType type,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
- extension->is_repeated = true;
- extension->is_packed = false;
- extension->repeated_string_value =
- Arena::CreateMessage<RepeatedPtrField<string> >(arena_);
- } else {
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
- }
- return extension->repeated_string_value->Add();
- }
- // -------------------------------------------------------------------
- // Messages
- const MessageLite& ExtensionSet::GetMessage(
- int number, const MessageLite& default_value) const {
- const Extension* extension = FindOrNull(number);
- if (extension == NULL) {
- // Not present. Return the default value.
- return default_value;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- if (extension->is_lazy) {
- return extension->lazymessage_value->GetMessage(default_value);
- } else {
- return *extension->message_value;
- }
- }
- }
- // Defined in extension_set_heavy.cc.
- // const MessageLite& ExtensionSet::GetMessage(int number,
- // const Descriptor* message_type,
- // MessageFactory* factory) const
- MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
- const MessageLite& prototype,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
- extension->is_repeated = false;
- extension->is_lazy = false;
- extension->message_value = prototype.New(arena_);
- extension->is_cleared = false;
- return extension->message_value;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- extension->is_cleared = false;
- if (extension->is_lazy) {
- return extension->lazymessage_value->MutableMessage(prototype);
- } else {
- return extension->message_value;
- }
- }
- }
- // Defined in extension_set_heavy.cc.
- // MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
- // const Descriptor* message_type,
- // MessageFactory* factory)
- void ExtensionSet::SetAllocatedMessage(int number, FieldType type,
- const FieldDescriptor* descriptor,
- MessageLite* message) {
- if (message == NULL) {
- ClearExtension(number);
- return;
- }
- ::google::protobuf::Arena* message_arena = message->GetArena();
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
- extension->is_repeated = false;
- extension->is_lazy = false;
- if (message_arena == arena_) {
- extension->message_value = message;
- } else if (message_arena == NULL) {
- extension->message_value = message;
- arena_->Own(message); // not NULL because not equal to message_arena
- } else {
- extension->message_value = message->New(arena_);
- extension->message_value->CheckTypeAndMergeFrom(*message);
- }
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- if (extension->is_lazy) {
- extension->lazymessage_value->SetAllocatedMessage(message);
- } else {
- if (arena_ == NULL) {
- delete extension->message_value;
- }
- if (message_arena == arena_) {
- extension->message_value = message;
- } else if (message_arena == NULL) {
- extension->message_value = message;
- arena_->Own(message); // not NULL because not equal to message_arena
- } else {
- extension->message_value = message->New(arena_);
- extension->message_value->CheckTypeAndMergeFrom(*message);
- }
- }
- }
- extension->is_cleared = false;
- }
- void ExtensionSet::UnsafeArenaSetAllocatedMessage(
- int number, FieldType type, const FieldDescriptor* descriptor,
- MessageLite* message) {
- if (message == NULL) {
- ClearExtension(number);
- return;
- }
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
- extension->is_repeated = false;
- extension->is_lazy = false;
- extension->message_value = message;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- if (extension->is_lazy) {
- extension->lazymessage_value->UnsafeArenaSetAllocatedMessage(message);
- } else {
- if (arena_ == NULL) {
- delete extension->message_value;
- }
- extension->message_value = message;
- }
- }
- extension->is_cleared = false;
- }
- MessageLite* ExtensionSet::ReleaseMessage(int number,
- const MessageLite& prototype) {
- Extension* extension = FindOrNull(number);
- if (extension == NULL) {
- // Not present. Return NULL.
- return NULL;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- MessageLite* ret = NULL;
- if (extension->is_lazy) {
- ret = extension->lazymessage_value->ReleaseMessage(prototype);
- if (arena_ == NULL) {
- delete extension->lazymessage_value;
- }
- } else {
- if (arena_ == NULL) {
- ret = extension->message_value;
- } else {
- // ReleaseMessage() always returns a heap-allocated message, and we are
- // on an arena, so we need to make a copy of this message to return.
- ret = extension->message_value->New();
- ret->CheckTypeAndMergeFrom(*extension->message_value);
- }
- }
- Erase(number);
- return ret;
- }
- }
- MessageLite* ExtensionSet::UnsafeArenaReleaseMessage(
- int number, const MessageLite& prototype) {
- Extension* extension = FindOrNull(number);
- if (extension == NULL) {
- // Not present. Return NULL.
- return NULL;
- } else {
- GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
- MessageLite* ret = NULL;
- if (extension->is_lazy) {
- ret = extension->lazymessage_value->UnsafeArenaReleaseMessage(prototype);
- if (arena_ == NULL) {
- delete extension->lazymessage_value;
- }
- } else {
- ret = extension->message_value;
- }
- Erase(number);
- return ret;
- }
- }
- // Defined in extension_set_heavy.cc.
- // MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor,
- // MessageFactory* factory);
- const MessageLite& ExtensionSet::GetRepeatedMessage(
- int number, int index) const {
- const Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
- return extension->repeated_message_value->Get(index);
- }
- MessageLite* ExtensionSet::MutableRepeatedMessage(int number, int index) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
- return extension->repeated_message_value->Mutable(index);
- }
- MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
- const MessageLite& prototype,
- const FieldDescriptor* descriptor) {
- Extension* extension;
- if (MaybeNewExtension(number, descriptor, &extension)) {
- extension->type = type;
- GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
- extension->is_repeated = true;
- extension->repeated_message_value =
- Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
- } else {
- GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
- }
- // RepeatedPtrField<MessageLite> does not know how to Add() since it cannot
- // allocate an abstract object, so we have to be tricky.
- MessageLite* result =
- reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>(
- extension->repeated_message_value)
- ->AddFromCleared<GenericTypeHandler<MessageLite> >();
- if (result == NULL) {
- result = prototype.New(arena_);
- extension->repeated_message_value->AddAllocated(result);
- }
- return result;
- }
- // Defined in extension_set_heavy.cc.
- // MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
- // const Descriptor* message_type,
- // MessageFactory* factory)
- #undef GOOGLE_DCHECK_TYPE
- void ExtensionSet::RemoveLast(int number) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK(extension->is_repeated);
- switch(cpp_type(extension->type)) {
- case WireFormatLite::CPPTYPE_INT32:
- extension->repeated_int32_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_INT64:
- extension->repeated_int64_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_UINT32:
- extension->repeated_uint32_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_UINT64:
- extension->repeated_uint64_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_FLOAT:
- extension->repeated_float_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_DOUBLE:
- extension->repeated_double_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_BOOL:
- extension->repeated_bool_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_ENUM:
- extension->repeated_enum_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_STRING:
- extension->repeated_string_value->RemoveLast();
- break;
- case WireFormatLite::CPPTYPE_MESSAGE:
- extension->repeated_message_value->RemoveLast();
- break;
- }
- }
- MessageLite* ExtensionSet::ReleaseLast(int number) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK(extension->is_repeated);
- GOOGLE_DCHECK(cpp_type(extension->type) == WireFormatLite::CPPTYPE_MESSAGE);
- return extension->repeated_message_value->ReleaseLast();
- }
- void ExtensionSet::SwapElements(int number, int index1, int index2) {
- Extension* extension = FindOrNull(number);
- GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
- GOOGLE_DCHECK(extension->is_repeated);
- switch(cpp_type(extension->type)) {
- case WireFormatLite::CPPTYPE_INT32:
- extension->repeated_int32_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_INT64:
- extension->repeated_int64_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_UINT32:
- extension->repeated_uint32_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_UINT64:
- extension->repeated_uint64_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_FLOAT:
- extension->repeated_float_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_DOUBLE:
- extension->repeated_double_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_BOOL:
- extension->repeated_bool_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_ENUM:
- extension->repeated_enum_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_STRING:
- extension->repeated_string_value->SwapElements(index1, index2);
- break;
- case WireFormatLite::CPPTYPE_MESSAGE:
- extension->repeated_message_value->SwapElements(index1, index2);
- break;
- }
- }
- // ===================================================================
- void ExtensionSet::Clear() {
- ForEach([](int /* number */, Extension& ext) { ext.Clear(); });
- }
- namespace {
- // Computes the size of a std::set_union without constructing the union.
- template <typename ItX, typename ItY>
- size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) {
- size_t result = 0;
- while (it_xs != end_xs && it_ys != end_ys) {
- ++result;
- if (it_xs->first < it_ys->first) {
- ++it_xs;
- } else if (it_xs->first == it_ys->first) {
- ++it_xs;
- ++it_ys;
- } else {
- ++it_ys;
- }
- }
- result += std::distance(it_xs, end_xs);
- result += std::distance(it_ys, end_ys);
- return result;
- }
- } // namespace
- void ExtensionSet::MergeFrom(const ExtensionSet& other) {
- if (GOOGLE_PREDICT_TRUE(!is_large())) {
- if (GOOGLE_PREDICT_TRUE(!other.is_large())) {
- GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), other.flat_begin(),
- other.flat_end()));
- } else {
- GrowCapacity(SizeOfUnion(flat_begin(), flat_end(),
- other.map_.large->begin(),
- other.map_.large->end()));
- }
- }
- other.ForEach([this](int number, const Extension& ext) {
- this->InternalExtensionMergeFrom(number, ext);
- });
- }
- void ExtensionSet::InternalExtensionMergeFrom(
- int number, const Extension& other_extension) {
- if (other_extension.is_repeated) {
- Extension* extension;
- bool is_new = MaybeNewExtension(number, other_extension.descriptor,
- &extension);
- if (is_new) {
- // Extension did not already exist in set.
- extension->type = other_extension.type;
- extension->is_packed = other_extension.is_packed;
- extension->is_repeated = true;
- } else {
- GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
- GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
- GOOGLE_DCHECK(extension->is_repeated);
- }
- switch (cpp_type(other_extension.type)) {
- #define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- if (is_new) { \
- extension->repeated_##LOWERCASE##_value = \
- Arena::CreateMessage<REPEATED_TYPE >(arena_); \
- } \
- extension->repeated_##LOWERCASE##_value->MergeFrom( \
- *other_extension.repeated_##LOWERCASE##_value); \
- break;
- HANDLE_TYPE( INT32, int32, RepeatedField < int32>);
- HANDLE_TYPE( INT64, int64, RepeatedField < int64>);
- HANDLE_TYPE( UINT32, uint32, RepeatedField < uint32>);
- HANDLE_TYPE( UINT64, uint64, RepeatedField < uint64>);
- HANDLE_TYPE( FLOAT, float, RepeatedField < float>);
- HANDLE_TYPE( DOUBLE, double, RepeatedField < double>);
- HANDLE_TYPE( BOOL, bool, RepeatedField < bool>);
- HANDLE_TYPE( ENUM, enum, RepeatedField < int>);
- HANDLE_TYPE( STRING, string, RepeatedPtrField< string>);
- #undef HANDLE_TYPE
- case WireFormatLite::CPPTYPE_MESSAGE:
- if (is_new) {
- extension->repeated_message_value =
- Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
- }
- // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
- // it would attempt to allocate new objects.
- RepeatedPtrField<MessageLite>* other_repeated_message =
- other_extension.repeated_message_value;
- for (int i = 0; i < other_repeated_message->size(); i++) {
- const MessageLite& other_message = other_repeated_message->Get(i);
- MessageLite* target =
- reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>(
- extension->repeated_message_value)
- ->AddFromCleared<GenericTypeHandler<MessageLite> >();
- if (target == NULL) {
- target = other_message.New(arena_);
- extension->repeated_message_value->AddAllocated(target);
- }
- target->CheckTypeAndMergeFrom(other_message);
- }
- break;
- }
- } else {
- if (!other_extension.is_cleared) {
- switch (cpp_type(other_extension.type)) {
- #define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- Set##CAMELCASE(number, other_extension.type, \
- other_extension.LOWERCASE##_value, \
- other_extension.descriptor); \
- break;
- HANDLE_TYPE( INT32, int32, Int32);
- HANDLE_TYPE( INT64, int64, Int64);
- HANDLE_TYPE(UINT32, uint32, UInt32);
- HANDLE_TYPE(UINT64, uint64, UInt64);
- HANDLE_TYPE( FLOAT, float, Float);
- HANDLE_TYPE(DOUBLE, double, Double);
- HANDLE_TYPE( BOOL, bool, Bool);
- HANDLE_TYPE( ENUM, enum, Enum);
- #undef HANDLE_TYPE
- case WireFormatLite::CPPTYPE_STRING:
- SetString(number, other_extension.type,
- *other_extension.string_value,
- other_extension.descriptor);
- break;
- case WireFormatLite::CPPTYPE_MESSAGE: {
- Extension* extension;
- bool is_new = MaybeNewExtension(number,
- other_extension.descriptor,
- &extension);
- if (is_new) {
- extension->type = other_extension.type;
- extension->is_packed = other_extension.is_packed;
- extension->is_repeated = false;
- if (other_extension.is_lazy) {
- extension->is_lazy = true;
- extension->lazymessage_value =
- other_extension.lazymessage_value->New(arena_);
- extension->lazymessage_value->MergeFrom(
- *other_extension.lazymessage_value);
- } else {
- extension->is_lazy = false;
- extension->message_value =
- other_extension.message_value->New(arena_);
- extension->message_value->CheckTypeAndMergeFrom(
- *other_extension.message_value);
- }
- } else {
- GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
- GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed);
- GOOGLE_DCHECK(!extension->is_repeated);
- if (other_extension.is_lazy) {
- if (extension->is_lazy) {
- extension->lazymessage_value->MergeFrom(
- *other_extension.lazymessage_value);
- } else {
- extension->message_value->CheckTypeAndMergeFrom(
- other_extension.lazymessage_value->GetMessage(
- *extension->message_value));
- }
- } else {
- if (extension->is_lazy) {
- extension->lazymessage_value->MutableMessage(
- *other_extension.message_value)->CheckTypeAndMergeFrom(
- *other_extension.message_value);
- } else {
- extension->message_value->CheckTypeAndMergeFrom(
- *other_extension.message_value);
- }
- }
- }
- extension->is_cleared = false;
- break;
- }
- }
- }
- }
- }
- void ExtensionSet::Swap(ExtensionSet* x) {
- if (GetArenaNoVirtual() == x->GetArenaNoVirtual()) {
- using std::swap;
- swap(flat_capacity_, x->flat_capacity_);
- swap(flat_size_, x->flat_size_);
- swap(map_, x->map_);
- } else {
- // TODO(cfallin, rohananil): We maybe able to optimize a case where we are
- // swapping from heap to arena-allocated extension set, by just Own()'ing
- // the extensions.
- ExtensionSet extension_set;
- extension_set.MergeFrom(*x);
- x->Clear();
- x->MergeFrom(*this);
- Clear();
- MergeFrom(extension_set);
- }
- }
- void ExtensionSet::SwapExtension(ExtensionSet* other,
- int number) {
- if (this == other) return;
- Extension* this_ext = FindOrNull(number);
- Extension* other_ext = other->FindOrNull(number);
- if (this_ext == NULL && other_ext == NULL) {
- return;
- }
- if (this_ext != NULL && other_ext != NULL) {
- if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
- using std::swap;
- swap(*this_ext, *other_ext);
- } else {
- // TODO(cfallin, rohananil): We could further optimize these cases,
- // especially avoid creation of ExtensionSet, and move MergeFrom logic
- // into Extensions itself (which takes arena as an argument).
- // We do it this way to reuse the copy-across-arenas logic already
- // implemented in ExtensionSet's MergeFrom.
- ExtensionSet temp;
- temp.InternalExtensionMergeFrom(number, *other_ext);
- Extension* temp_ext = temp.FindOrNull(number);
- other_ext->Clear();
- other->InternalExtensionMergeFrom(number, *this_ext);
- this_ext->Clear();
- InternalExtensionMergeFrom(number, *temp_ext);
- }
- return;
- }
- if (this_ext == NULL) {
- if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
- *Insert(number).first = *other_ext;
- } else {
- InternalExtensionMergeFrom(number, *other_ext);
- }
- other->Erase(number);
- return;
- }
- if (other_ext == NULL) {
- if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
- *other->Insert(number).first = *this_ext;
- } else {
- other->InternalExtensionMergeFrom(number, *this_ext);
- }
- Erase(number);
- return;
- }
- }
- bool ExtensionSet::IsInitialized() const {
- // Extensions are never required. However, we need to check that all
- // embedded messages are initialized.
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- for (const auto& kv : *map_.large) {
- if (!kv.second.IsInitialized()) return false;
- }
- return true;
- }
- for (const KeyValue* it = flat_begin(); it != flat_end(); ++it) {
- if (!it->second.IsInitialized()) return false;
- }
- return true;
- }
- bool ExtensionSet::FindExtensionInfoFromTag(
- uint32 tag, ExtensionFinder* extension_finder, int* field_number,
- ExtensionInfo* extension, bool* was_packed_on_wire) {
- *field_number = WireFormatLite::GetTagFieldNumber(tag);
- WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
- return FindExtensionInfoFromFieldNumber(wire_type, *field_number,
- extension_finder, extension,
- was_packed_on_wire);
- }
- bool ExtensionSet::FindExtensionInfoFromFieldNumber(
- int wire_type, int field_number, ExtensionFinder* extension_finder,
- ExtensionInfo* extension, bool* was_packed_on_wire) {
- if (!extension_finder->Find(field_number, extension)) {
- return false;
- }
- WireFormatLite::WireType expected_wire_type =
- WireFormatLite::WireTypeForFieldType(real_type(extension->type));
- // Check if this is a packed field.
- *was_packed_on_wire = false;
- if (extension->is_repeated &&
- wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED &&
- is_packable(expected_wire_type)) {
- *was_packed_on_wire = true;
- return true;
- }
- // Otherwise the wire type must match.
- return expected_wire_type == wire_type;
- }
- bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
- ExtensionFinder* extension_finder,
- FieldSkipper* field_skipper) {
- int number;
- bool was_packed_on_wire;
- ExtensionInfo extension;
- if (!FindExtensionInfoFromTag(
- tag, extension_finder, &number, &extension, &was_packed_on_wire)) {
- return field_skipper->SkipField(input, tag);
- } else {
- return ParseFieldWithExtensionInfo(
- number, was_packed_on_wire, extension, input, field_skipper);
- }
- }
- bool ExtensionSet::ParseFieldWithExtensionInfo(
- int number, bool was_packed_on_wire, const ExtensionInfo& extension,
- io::CodedInputStream* input,
- FieldSkipper* field_skipper) {
- // Explicitly not read extension.is_packed, instead check whether the field
- // was encoded in packed form on the wire.
- if (was_packed_on_wire) {
- uint32 size;
- if (!input->ReadVarint32(&size)) return false;
- io::CodedInputStream::Limit limit = input->PushLimit(size);
- switch (extension.type) {
- #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- while (input->BytesUntilLimit() > 0) { \
- CPP_LOWERCASE value; \
- if (!WireFormatLite::ReadPrimitive< \
- CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
- input, &value)) return false; \
- Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
- extension.is_packed, value, \
- extension.descriptor); \
- } \
- break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, Int32, int32);
- HANDLE_TYPE( SINT64, Int64, int64);
- HANDLE_TYPE( FIXED32, UInt32, uint32);
- HANDLE_TYPE( FIXED64, UInt64, uint64);
- HANDLE_TYPE(SFIXED32, Int32, int32);
- HANDLE_TYPE(SFIXED64, Int64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_ENUM:
- while (input->BytesUntilLimit() > 0) {
- int value;
- if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
- input, &value)) return false;
- if (extension.enum_validity_check.func(
- extension.enum_validity_check.arg, value)) {
- AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed,
- value, extension.descriptor);
- } else {
- // Invalid value. Treat as unknown.
- field_skipper->SkipUnknownEnum(number, value);
- }
- }
- break;
- case WireFormatLite::TYPE_STRING:
- case WireFormatLite::TYPE_BYTES:
- case WireFormatLite::TYPE_GROUP:
- case WireFormatLite::TYPE_MESSAGE:
- GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
- break;
- }
- input->PopLimit(limit);
- } else {
- switch (extension.type) {
- #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: { \
- CPP_LOWERCASE value; \
- if (!WireFormatLite::ReadPrimitive< \
- CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
- input, &value)) return false; \
- if (extension.is_repeated) { \
- Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
- extension.is_packed, value, \
- extension.descriptor); \
- } else { \
- Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
- extension.descriptor); \
- } \
- } break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, Int32, int32);
- HANDLE_TYPE( SINT64, Int64, int64);
- HANDLE_TYPE( FIXED32, UInt32, uint32);
- HANDLE_TYPE( FIXED64, UInt64, uint64);
- HANDLE_TYPE(SFIXED32, Int32, int32);
- HANDLE_TYPE(SFIXED64, Int64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_ENUM: {
- int value;
- if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
- input, &value)) return false;
- if (!extension.enum_validity_check.func(
- extension.enum_validity_check.arg, value)) {
- // Invalid value. Treat as unknown.
- field_skipper->SkipUnknownEnum(number, value);
- } else if (extension.is_repeated) {
- AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, value,
- extension.descriptor);
- } else {
- SetEnum(number, WireFormatLite::TYPE_ENUM, value,
- extension.descriptor);
- }
- break;
- }
- case WireFormatLite::TYPE_STRING: {
- string* value = extension.is_repeated ?
- AddString(number, WireFormatLite::TYPE_STRING, extension.descriptor) :
- MutableString(number, WireFormatLite::TYPE_STRING,
- extension.descriptor);
- if (!WireFormatLite::ReadString(input, value)) return false;
- break;
- }
- case WireFormatLite::TYPE_BYTES: {
- string* value = extension.is_repeated ?
- AddString(number, WireFormatLite::TYPE_BYTES, extension.descriptor) :
- MutableString(number, WireFormatLite::TYPE_BYTES,
- extension.descriptor);
- if (!WireFormatLite::ReadBytes(input, value)) return false;
- break;
- }
- case WireFormatLite::TYPE_GROUP: {
- MessageLite* value = extension.is_repeated ?
- AddMessage(number, WireFormatLite::TYPE_GROUP,
- *extension.message_prototype, extension.descriptor) :
- MutableMessage(number, WireFormatLite::TYPE_GROUP,
- *extension.message_prototype, extension.descriptor);
- if (!WireFormatLite::ReadGroup(number, input, value)) return false;
- break;
- }
- case WireFormatLite::TYPE_MESSAGE: {
- MessageLite* value = extension.is_repeated ?
- AddMessage(number, WireFormatLite::TYPE_MESSAGE,
- *extension.message_prototype, extension.descriptor) :
- MutableMessage(number, WireFormatLite::TYPE_MESSAGE,
- *extension.message_prototype, extension.descriptor);
- if (!WireFormatLite::ReadMessage(input, value)) return false;
- break;
- }
- }
- }
- return true;
- }
- bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
- const MessageLite* containing_type) {
- FieldSkipper skipper;
- GeneratedExtensionFinder finder(containing_type);
- return ParseField(tag, input, &finder, &skipper);
- }
- bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
- const MessageLite* containing_type,
- io::CodedOutputStream* unknown_fields) {
- CodedOutputStreamFieldSkipper skipper(unknown_fields);
- GeneratedExtensionFinder finder(containing_type);
- return ParseField(tag, input, &finder, &skipper);
- }
- // Defined in extension_set_heavy.cc.
- // bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
- // const MessageLite* containing_type,
- // UnknownFieldSet* unknown_fields)
- // Defined in extension_set_heavy.cc.
- // bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
- // const MessageLite* containing_type,
- // UnknownFieldSet* unknown_fields);
- void ExtensionSet::SerializeWithCachedSizes(
- int start_field_number, int end_field_number,
- io::CodedOutputStream* output) const {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- const auto& end = map_.large->end();
- for (auto it = map_.large->lower_bound(start_field_number);
- it != end && it->first < end_field_number; ++it) {
- it->second.SerializeFieldWithCachedSizes(it->first, output);
- }
- return;
- }
- const KeyValue* end = flat_end();
- for (const KeyValue* it = std::lower_bound(
- flat_begin(), end, start_field_number, KeyValue::FirstComparator());
- it != end && it->first < end_field_number; ++it) {
- it->second.SerializeFieldWithCachedSizes(it->first, output);
- }
- }
- size_t ExtensionSet::ByteSize() const {
- size_t total_size = 0;
- ForEach([&total_size](int number, const Extension& ext) {
- total_size += ext.ByteSize(number);
- });
- return total_size;
- }
- // Defined in extension_set_heavy.cc.
- // int ExtensionSet::SpaceUsedExcludingSelf() const
- bool ExtensionSet::MaybeNewExtension(int number,
- const FieldDescriptor* descriptor,
- Extension** result) {
- bool extension_is_new = false;
- std::tie(*result, extension_is_new) = Insert(number);
- (*result)->descriptor = descriptor;
- return extension_is_new;
- }
- // ===================================================================
- // Methods of ExtensionSet::Extension
- void ExtensionSet::Extension::Clear() {
- if (is_repeated) {
- switch (cpp_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- repeated_##LOWERCASE##_value->Clear(); \
- break
- HANDLE_TYPE( INT32, int32);
- HANDLE_TYPE( INT64, int64);
- HANDLE_TYPE( UINT32, uint32);
- HANDLE_TYPE( UINT64, uint64);
- HANDLE_TYPE( FLOAT, float);
- HANDLE_TYPE( DOUBLE, double);
- HANDLE_TYPE( BOOL, bool);
- HANDLE_TYPE( ENUM, enum);
- HANDLE_TYPE( STRING, string);
- HANDLE_TYPE(MESSAGE, message);
- #undef HANDLE_TYPE
- }
- } else {
- if (!is_cleared) {
- switch (cpp_type(type)) {
- case WireFormatLite::CPPTYPE_STRING:
- string_value->clear();
- break;
- case WireFormatLite::CPPTYPE_MESSAGE:
- if (is_lazy) {
- lazymessage_value->Clear();
- } else {
- message_value->Clear();
- }
- break;
- default:
- // No need to do anything. Get*() will return the default value
- // as long as is_cleared is true and Set*() will overwrite the
- // previous value.
- break;
- }
- is_cleared = true;
- }
- }
- }
- void ExtensionSet::Extension::SerializeFieldWithCachedSizes(
- int number,
- io::CodedOutputStream* output) const {
- if (is_repeated) {
- if (is_packed) {
- if (cached_size == 0) return;
- WireFormatLite::WriteTag(number,
- WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
- output->WriteVarint32(cached_size);
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
- WireFormatLite::Write##CAMELCASE##NoTag( \
- repeated_##LOWERCASE##_value->Get(i), output); \
- } \
- break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, SInt32, int32);
- HANDLE_TYPE( SINT64, SInt64, int64);
- HANDLE_TYPE( FIXED32, Fixed32, uint32);
- HANDLE_TYPE( FIXED64, Fixed64, uint64);
- HANDLE_TYPE(SFIXED32, SFixed32, int32);
- HANDLE_TYPE(SFIXED64, SFixed64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- HANDLE_TYPE( ENUM, Enum, enum);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_STRING:
- case WireFormatLite::TYPE_BYTES:
- case WireFormatLite::TYPE_GROUP:
- case WireFormatLite::TYPE_MESSAGE:
- GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
- break;
- }
- } else {
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
- WireFormatLite::Write##CAMELCASE(number, \
- repeated_##LOWERCASE##_value->Get(i), output); \
- } \
- break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, SInt32, int32);
- HANDLE_TYPE( SINT64, SInt64, int64);
- HANDLE_TYPE( FIXED32, Fixed32, uint32);
- HANDLE_TYPE( FIXED64, Fixed64, uint64);
- HANDLE_TYPE(SFIXED32, SFixed32, int32);
- HANDLE_TYPE(SFIXED64, SFixed64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- HANDLE_TYPE( STRING, String, string);
- HANDLE_TYPE( BYTES, Bytes, string);
- HANDLE_TYPE( ENUM, Enum, enum);
- HANDLE_TYPE( GROUP, Group, message);
- HANDLE_TYPE( MESSAGE, Message, message);
- #undef HANDLE_TYPE
- }
- }
- } else if (!is_cleared) {
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- WireFormatLite::Write##CAMELCASE(number, VALUE, output); \
- break
- HANDLE_TYPE( INT32, Int32, int32_value);
- HANDLE_TYPE( INT64, Int64, int64_value);
- HANDLE_TYPE( UINT32, UInt32, uint32_value);
- HANDLE_TYPE( UINT64, UInt64, uint64_value);
- HANDLE_TYPE( SINT32, SInt32, int32_value);
- HANDLE_TYPE( SINT64, SInt64, int64_value);
- HANDLE_TYPE( FIXED32, Fixed32, uint32_value);
- HANDLE_TYPE( FIXED64, Fixed64, uint64_value);
- HANDLE_TYPE(SFIXED32, SFixed32, int32_value);
- HANDLE_TYPE(SFIXED64, SFixed64, int64_value);
- HANDLE_TYPE( FLOAT, Float, float_value);
- HANDLE_TYPE( DOUBLE, Double, double_value);
- HANDLE_TYPE( BOOL, Bool, bool_value);
- HANDLE_TYPE( STRING, String, *string_value);
- HANDLE_TYPE( BYTES, Bytes, *string_value);
- HANDLE_TYPE( ENUM, Enum, enum_value);
- HANDLE_TYPE( GROUP, Group, *message_value);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_MESSAGE:
- if (is_lazy) {
- lazymessage_value->WriteMessage(number, output);
- } else {
- WireFormatLite::WriteMessage(number, *message_value, output);
- }
- break;
- }
- }
- }
- size_t ExtensionSet::Extension::ByteSize(int number) const {
- size_t result = 0;
- if (is_repeated) {
- if (is_packed) {
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
- result += WireFormatLite::CAMELCASE##Size( \
- repeated_##LOWERCASE##_value->Get(i)); \
- } \
- break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, SInt32, int32);
- HANDLE_TYPE( SINT64, SInt64, int64);
- HANDLE_TYPE( ENUM, Enum, enum);
- #undef HANDLE_TYPE
- // Stuff with fixed size.
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- result += WireFormatLite::k##CAMELCASE##Size * \
- FromIntSize(repeated_##LOWERCASE##_value->size()); \
- break
- HANDLE_TYPE( FIXED32, Fixed32, uint32);
- HANDLE_TYPE( FIXED64, Fixed64, uint64);
- HANDLE_TYPE(SFIXED32, SFixed32, int32);
- HANDLE_TYPE(SFIXED64, SFixed64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_STRING:
- case WireFormatLite::TYPE_BYTES:
- case WireFormatLite::TYPE_GROUP:
- case WireFormatLite::TYPE_MESSAGE:
- GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
- break;
- }
- cached_size = ToCachedSize(result);
- if (result > 0) {
- result += io::CodedOutputStream::VarintSize32(result);
- result += io::CodedOutputStream::VarintSize32(
- WireFormatLite::MakeTag(number,
- WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
- }
- } else {
- size_t tag_size = WireFormatLite::TagSize(number, real_type(type));
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- result += tag_size * \
- FromIntSize(repeated_##LOWERCASE##_value->size()); \
- for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
- result += WireFormatLite::CAMELCASE##Size( \
- repeated_##LOWERCASE##_value->Get(i)); \
- } \
- break
- HANDLE_TYPE( INT32, Int32, int32);
- HANDLE_TYPE( INT64, Int64, int64);
- HANDLE_TYPE( UINT32, UInt32, uint32);
- HANDLE_TYPE( UINT64, UInt64, uint64);
- HANDLE_TYPE( SINT32, SInt32, int32);
- HANDLE_TYPE( SINT64, SInt64, int64);
- HANDLE_TYPE( STRING, String, string);
- HANDLE_TYPE( BYTES, Bytes, string);
- HANDLE_TYPE( ENUM, Enum, enum);
- HANDLE_TYPE( GROUP, Group, message);
- HANDLE_TYPE( MESSAGE, Message, message);
- #undef HANDLE_TYPE
- // Stuff with fixed size.
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \
- FromIntSize(repeated_##LOWERCASE##_value->size()); \
- break
- HANDLE_TYPE( FIXED32, Fixed32, uint32);
- HANDLE_TYPE( FIXED64, Fixed64, uint64);
- HANDLE_TYPE(SFIXED32, SFixed32, int32);
- HANDLE_TYPE(SFIXED64, SFixed64, int64);
- HANDLE_TYPE( FLOAT, Float, float);
- HANDLE_TYPE( DOUBLE, Double, double);
- HANDLE_TYPE( BOOL, Bool, bool);
- #undef HANDLE_TYPE
- }
- }
- } else if (!is_cleared) {
- result += WireFormatLite::TagSize(number, real_type(type));
- switch (real_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \
- break
- HANDLE_TYPE( INT32, Int32, int32_value);
- HANDLE_TYPE( INT64, Int64, int64_value);
- HANDLE_TYPE( UINT32, UInt32, uint32_value);
- HANDLE_TYPE( UINT64, UInt64, uint64_value);
- HANDLE_TYPE( SINT32, SInt32, int32_value);
- HANDLE_TYPE( SINT64, SInt64, int64_value);
- HANDLE_TYPE( STRING, String, *string_value);
- HANDLE_TYPE( BYTES, Bytes, *string_value);
- HANDLE_TYPE( ENUM, Enum, enum_value);
- HANDLE_TYPE( GROUP, Group, *message_value);
- #undef HANDLE_TYPE
- case WireFormatLite::TYPE_MESSAGE: {
- if (is_lazy) {
- size_t size = lazymessage_value->ByteSize();
- result += io::CodedOutputStream::VarintSize32(size) + size;
- } else {
- result += WireFormatLite::MessageSize(*message_value);
- }
- break;
- }
- // Stuff with fixed size.
- #define HANDLE_TYPE(UPPERCASE, CAMELCASE) \
- case WireFormatLite::TYPE_##UPPERCASE: \
- result += WireFormatLite::k##CAMELCASE##Size; \
- break
- HANDLE_TYPE( FIXED32, Fixed32);
- HANDLE_TYPE( FIXED64, Fixed64);
- HANDLE_TYPE(SFIXED32, SFixed32);
- HANDLE_TYPE(SFIXED64, SFixed64);
- HANDLE_TYPE( FLOAT, Float);
- HANDLE_TYPE( DOUBLE, Double);
- HANDLE_TYPE( BOOL, Bool);
- #undef HANDLE_TYPE
- }
- }
- return result;
- }
- int ExtensionSet::Extension::GetSize() const {
- GOOGLE_DCHECK(is_repeated);
- switch (cpp_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- return repeated_##LOWERCASE##_value->size()
- HANDLE_TYPE( INT32, int32);
- HANDLE_TYPE( INT64, int64);
- HANDLE_TYPE( UINT32, uint32);
- HANDLE_TYPE( UINT64, uint64);
- HANDLE_TYPE( FLOAT, float);
- HANDLE_TYPE( DOUBLE, double);
- HANDLE_TYPE( BOOL, bool);
- HANDLE_TYPE( ENUM, enum);
- HANDLE_TYPE( STRING, string);
- HANDLE_TYPE(MESSAGE, message);
- #undef HANDLE_TYPE
- }
- GOOGLE_LOG(FATAL) << "Can't get here.";
- return 0;
- }
- // This function deletes all allocated objects. This function should be only
- // called if the Extension was created with an arena.
- void ExtensionSet::Extension::Free() {
- if (is_repeated) {
- switch (cpp_type(type)) {
- #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- delete repeated_##LOWERCASE##_value; \
- break
- HANDLE_TYPE( INT32, int32);
- HANDLE_TYPE( INT64, int64);
- HANDLE_TYPE( UINT32, uint32);
- HANDLE_TYPE( UINT64, uint64);
- HANDLE_TYPE( FLOAT, float);
- HANDLE_TYPE( DOUBLE, double);
- HANDLE_TYPE( BOOL, bool);
- HANDLE_TYPE( ENUM, enum);
- HANDLE_TYPE( STRING, string);
- HANDLE_TYPE(MESSAGE, message);
- #undef HANDLE_TYPE
- }
- } else {
- switch (cpp_type(type)) {
- case WireFormatLite::CPPTYPE_STRING:
- delete string_value;
- break;
- case WireFormatLite::CPPTYPE_MESSAGE:
- if (is_lazy) {
- delete lazymessage_value;
- } else {
- delete message_value;
- }
- break;
- default:
- break;
- }
- }
- }
- // Defined in extension_set_heavy.cc.
- // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const
- bool ExtensionSet::Extension::IsInitialized() const {
- if (cpp_type(type) == WireFormatLite::CPPTYPE_MESSAGE) {
- if (is_repeated) {
- for (int i = 0; i < repeated_message_value->size(); i++) {
- if (!repeated_message_value->Get(i).IsInitialized()) {
- return false;
- }
- }
- } else {
- if (!is_cleared) {
- if (is_lazy) {
- if (!lazymessage_value->IsInitialized()) return false;
- } else {
- if (!message_value->IsInitialized()) return false;
- }
- }
- }
- }
- return true;
- }
- // Dummy key method to avoid weak vtable.
- void ExtensionSet::LazyMessageExtension::UnusedKeyMethod() {}
- const ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) const {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- return FindOrNullInLargeMap(key);
- }
- const KeyValue* end = flat_end();
- const KeyValue* it =
- std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
- if (it != end && it->first == key) {
- return &it->second;
- }
- return NULL;
- }
- const ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(
- int key) const {
- assert(is_large());
- LargeMap::const_iterator it = map_.large->find(key);
- if (it != map_.large->end()) {
- return &it->second;
- }
- return NULL;
- }
- ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- return FindOrNullInLargeMap(key);
- }
- KeyValue* end = flat_end();
- KeyValue* it =
- std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
- if (it != end && it->first == key) {
- return &it->second;
- }
- return NULL;
- }
- ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(int key) {
- assert(is_large());
- LargeMap::iterator it = map_.large->find(key);
- if (it != map_.large->end()) {
- return &it->second;
- }
- return NULL;
- }
- std::pair<ExtensionSet::Extension*, bool> ExtensionSet::Insert(int key) {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- auto maybe = map_.large->insert({key, Extension()});
- return {&maybe.first->second, maybe.second};
- }
- KeyValue* end = flat_end();
- KeyValue* it =
- std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
- if (it != end && it->first == key) {
- return {&it->second, false};
- }
- if (flat_size_ < flat_capacity_) {
- std::copy_backward(it, end, end + 1);
- ++flat_size_;
- it->first = key;
- it->second = Extension();
- return {&it->second, true};
- }
- GrowCapacity(flat_size_ + 1);
- return Insert(key);
- }
- void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- return; // LargeMap does not have a "reserve" method.
- }
- if (flat_capacity_ >= minimum_new_capacity) {
- return;
- }
- do {
- flat_capacity_ = flat_capacity_ == 0 ? 1 : flat_capacity_ * 4;
- } while (flat_capacity_ < minimum_new_capacity);
- const KeyValue* begin = flat_begin();
- const KeyValue* end = flat_end();
- if (flat_capacity_ > kMaximumFlatCapacity) {
- // Switch to LargeMap
- map_.large = ::google::protobuf::Arena::Create<LargeMap>(arena_);
- LargeMap::iterator hint = map_.large->begin();
- for (const KeyValue* it = begin; it != end; ++it) {
- hint = map_.large->insert(hint, {it->first, it->second});
- }
- flat_size_ = 0;
- } else {
- map_.flat = ::google::protobuf::Arena::CreateArray<KeyValue>(arena_, flat_capacity_);
- std::copy(begin, end, map_.flat);
- }
- if (arena_ == NULL) delete[] begin;
- }
- // static
- constexpr uint16 ExtensionSet::kMaximumFlatCapacity;
- void ExtensionSet::Erase(int key) {
- if (GOOGLE_PREDICT_FALSE(is_large())) {
- map_.large->erase(key);
- return;
- }
- KeyValue* end = flat_end();
- KeyValue* it =
- std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
- if (it != end && it->first == key) {
- std::copy(it + 1, end, it);
- --flat_size_;
- }
- }
- // ==================================================================
- // Default repeated field instances for iterator-compatible accessors
- const RepeatedPrimitiveDefaults* RepeatedPrimitiveDefaults::default_instance() {
- static auto instance = OnShutdownDelete(new RepeatedPrimitiveDefaults);
- return instance;
- }
- const RepeatedStringTypeTraits::RepeatedFieldType*
- RepeatedStringTypeTraits::GetDefaultRepeatedField() {
- static auto instance = OnShutdownDelete(new RepeatedFieldType);
- return instance;
- }
- } // namespace internal
- } // namespace protobuf
- } // namespace google
|