extension_set.cc 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * 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.
  18. //
  19. // 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.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/stubs/hash.h>
  34. #include <tuple>
  35. #include <utility>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/extension_set.h>
  38. #include <google/protobuf/message_lite.h>
  39. #include <google/protobuf/io/coded_stream.h>
  40. #include <google/protobuf/wire_format_lite_inl.h>
  41. #include <google/protobuf/repeated_field.h>
  42. #include <google/protobuf/stubs/map_util.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace internal {
  46. namespace {
  47. inline WireFormatLite::FieldType real_type(FieldType type) {
  48. GOOGLE_DCHECK(type > 0 && type <= WireFormatLite::MAX_FIELD_TYPE);
  49. return static_cast<WireFormatLite::FieldType>(type);
  50. }
  51. inline WireFormatLite::CppType cpp_type(FieldType type) {
  52. return WireFormatLite::FieldTypeToCppType(real_type(type));
  53. }
  54. inline bool is_packable(WireFormatLite::WireType type) {
  55. switch (type) {
  56. case WireFormatLite::WIRETYPE_VARINT:
  57. case WireFormatLite::WIRETYPE_FIXED64:
  58. case WireFormatLite::WIRETYPE_FIXED32:
  59. return true;
  60. case WireFormatLite::WIRETYPE_LENGTH_DELIMITED:
  61. case WireFormatLite::WIRETYPE_START_GROUP:
  62. case WireFormatLite::WIRETYPE_END_GROUP:
  63. return false;
  64. // Do not add a default statement. Let the compiler complain when someone
  65. // adds a new wire type.
  66. }
  67. GOOGLE_LOG(FATAL) << "can't reach here.";
  68. return false;
  69. }
  70. // Registry stuff.
  71. typedef hash_map<std::pair<const MessageLite*, int>,
  72. ExtensionInfo> ExtensionRegistry;
  73. static const ExtensionRegistry* global_registry = nullptr;
  74. // This function is only called at startup, so there is no need for thread-
  75. // safety.
  76. void Register(const MessageLite* containing_type,
  77. int number, ExtensionInfo info) {
  78. static auto local_static_registry = OnShutdownDelete(new ExtensionRegistry);
  79. global_registry = local_static_registry;
  80. if (!InsertIfNotPresent(local_static_registry,
  81. std::make_pair(containing_type, number), info)) {
  82. GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \""
  83. << containing_type->GetTypeName()
  84. << "\", field number " << number << ".";
  85. }
  86. }
  87. const ExtensionInfo* FindRegisteredExtension(
  88. const MessageLite* containing_type, int number) {
  89. return global_registry == nullptr
  90. ? nullptr
  91. : FindOrNull(*global_registry, std::make_pair(containing_type, number));
  92. }
  93. } // namespace
  94. ExtensionFinder::~ExtensionFinder() {}
  95. bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) {
  96. const ExtensionInfo* extension =
  97. FindRegisteredExtension(containing_type_, number);
  98. if (extension == NULL) {
  99. return false;
  100. } else {
  101. *output = *extension;
  102. return true;
  103. }
  104. }
  105. void ExtensionSet::RegisterExtension(const MessageLite* containing_type,
  106. int number, FieldType type,
  107. bool is_repeated, bool is_packed) {
  108. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_ENUM);
  109. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_MESSAGE);
  110. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_GROUP);
  111. ExtensionInfo info(type, is_repeated, is_packed);
  112. Register(containing_type, number, info);
  113. }
  114. static bool CallNoArgValidityFunc(const void* arg, int number) {
  115. // Note: Must use C-style cast here rather than reinterpret_cast because
  116. // the C++ standard at one point did not allow casts between function and
  117. // data pointers and some compilers enforce this for C++-style casts. No
  118. // compiler enforces it for C-style casts since lots of C-style code has
  119. // relied on these kinds of casts for a long time, despite being
  120. // technically undefined. See:
  121. // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
  122. // Also note: Some compilers do not allow function pointers to be "const".
  123. // Which makes sense, I suppose, because it's meaningless.
  124. return ((EnumValidityFunc*)arg)(number);
  125. }
  126. void ExtensionSet::RegisterEnumExtension(const MessageLite* containing_type,
  127. int number, FieldType type,
  128. bool is_repeated, bool is_packed,
  129. EnumValidityFunc* is_valid) {
  130. GOOGLE_CHECK_EQ(type, WireFormatLite::TYPE_ENUM);
  131. ExtensionInfo info(type, is_repeated, is_packed);
  132. info.enum_validity_check.func = CallNoArgValidityFunc;
  133. // See comment in CallNoArgValidityFunc() about why we use a c-style cast.
  134. info.enum_validity_check.arg = (void*)is_valid;
  135. Register(containing_type, number, info);
  136. }
  137. void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type,
  138. int number, FieldType type,
  139. bool is_repeated, bool is_packed,
  140. const MessageLite* prototype) {
  141. GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE ||
  142. type == WireFormatLite::TYPE_GROUP);
  143. ExtensionInfo info(type, is_repeated, is_packed);
  144. info.message_prototype = prototype;
  145. Register(containing_type, number, info);
  146. }
  147. // ===================================================================
  148. // Constructors and basic methods.
  149. ExtensionSet::ExtensionSet(::google::protobuf::Arena* arena)
  150. : arena_(arena),
  151. flat_capacity_(0),
  152. flat_size_(0),
  153. map_{flat_capacity_ == 0 ? NULL
  154. : ::google::protobuf::Arena::CreateArray<KeyValue>(
  155. arena_, flat_capacity_)} {}
  156. ExtensionSet::ExtensionSet()
  157. : arena_(NULL),
  158. flat_capacity_(0),
  159. flat_size_(0),
  160. map_{flat_capacity_ == 0 ? NULL
  161. : ::google::protobuf::Arena::CreateArray<KeyValue>(
  162. arena_, flat_capacity_)} {}
  163. ExtensionSet::~ExtensionSet() {
  164. // Deletes all allocated extensions.
  165. if (arena_ == NULL) {
  166. ForEach([](int /* number */, Extension& ext) { ext.Free(); });
  167. if (GOOGLE_PREDICT_FALSE(is_large())) {
  168. delete map_.large;
  169. } else {
  170. delete[] map_.flat;
  171. }
  172. }
  173. }
  174. // Defined in extension_set_heavy.cc.
  175. // void ExtensionSet::AppendToList(const Descriptor* containing_type,
  176. // const DescriptorPool* pool,
  177. // vector<const FieldDescriptor*>* output) const
  178. bool ExtensionSet::Has(int number) const {
  179. const Extension* ext = FindOrNull(number);
  180. if (ext == NULL) return false;
  181. GOOGLE_DCHECK(!ext->is_repeated);
  182. return !ext->is_cleared;
  183. }
  184. int ExtensionSet::NumExtensions() const {
  185. int result = 0;
  186. ForEach([&result](int /* number */, const Extension& ext) {
  187. if (!ext.is_cleared) {
  188. ++result;
  189. }
  190. });
  191. return result;
  192. }
  193. int ExtensionSet::ExtensionSize(int number) const {
  194. const Extension* ext = FindOrNull(number);
  195. return ext == NULL ? 0 : ext->GetSize();
  196. }
  197. FieldType ExtensionSet::ExtensionType(int number) const {
  198. const Extension* ext = FindOrNull(number);
  199. if (ext == NULL) {
  200. GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (1). ";
  201. return 0;
  202. }
  203. if (ext->is_cleared) {
  204. GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (2). ";
  205. }
  206. return ext->type;
  207. }
  208. void ExtensionSet::ClearExtension(int number) {
  209. Extension* ext = FindOrNull(number);
  210. if (ext == NULL) return;
  211. ext->Clear();
  212. }
  213. // ===================================================================
  214. // Field accessors
  215. namespace {
  216. enum Cardinality {
  217. REPEATED,
  218. OPTIONAL
  219. };
  220. } // namespace
  221. #define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \
  222. GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED : OPTIONAL, LABEL); \
  223. GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), WireFormatLite::CPPTYPE_##CPPTYPE)
  224. // -------------------------------------------------------------------
  225. // Primitives
  226. #define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \
  227. \
  228. LOWERCASE ExtensionSet::Get##CAMELCASE(int number, \
  229. LOWERCASE default_value) const { \
  230. const Extension* extension = FindOrNull(number); \
  231. if (extension == NULL || extension->is_cleared) { \
  232. return default_value; \
  233. } else { \
  234. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, UPPERCASE); \
  235. return extension->LOWERCASE##_value; \
  236. } \
  237. } \
  238. \
  239. void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \
  240. LOWERCASE value, \
  241. const FieldDescriptor* descriptor) { \
  242. Extension* extension; \
  243. if (MaybeNewExtension(number, descriptor, &extension)) { \
  244. extension->type = type; \
  245. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
  246. extension->is_repeated = false; \
  247. } else { \
  248. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, UPPERCASE); \
  249. } \
  250. extension->is_cleared = false; \
  251. extension->LOWERCASE##_value = value; \
  252. } \
  253. \
  254. LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) const { \
  255. const Extension* extension = FindOrNull(number); \
  256. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
  257. GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
  258. return extension->repeated_##LOWERCASE##_value->Get(index); \
  259. } \
  260. \
  261. void ExtensionSet::SetRepeated##CAMELCASE( \
  262. int number, int index, LOWERCASE value) { \
  263. Extension* extension = FindOrNull(number); \
  264. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty)."; \
  265. GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
  266. extension->repeated_##LOWERCASE##_value->Set(index, value); \
  267. } \
  268. \
  269. void ExtensionSet::Add##CAMELCASE(int number, FieldType type, \
  270. bool packed, LOWERCASE value, \
  271. const FieldDescriptor* descriptor) { \
  272. Extension* extension; \
  273. if (MaybeNewExtension(number, descriptor, &extension)) { \
  274. extension->type = type; \
  275. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
  276. extension->is_repeated = true; \
  277. extension->is_packed = packed; \
  278. extension->repeated_##LOWERCASE##_value = \
  279. Arena::CreateMessage<RepeatedField<LOWERCASE> >(arena_); \
  280. } else { \
  281. GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
  282. GOOGLE_DCHECK_EQ(extension->is_packed, packed); \
  283. } \
  284. extension->repeated_##LOWERCASE##_value->Add(value); \
  285. }
  286. PRIMITIVE_ACCESSORS( INT32, int32, Int32)
  287. PRIMITIVE_ACCESSORS( INT64, int64, Int64)
  288. PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32)
  289. PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64)
  290. PRIMITIVE_ACCESSORS( FLOAT, float, Float)
  291. PRIMITIVE_ACCESSORS(DOUBLE, double, Double)
  292. PRIMITIVE_ACCESSORS( BOOL, bool, Bool)
  293. #undef PRIMITIVE_ACCESSORS
  294. const void* ExtensionSet::GetRawRepeatedField(int number,
  295. const void* default_value) const {
  296. const Extension* extension = FindOrNull(number);
  297. if (extension == NULL) {
  298. return default_value;
  299. }
  300. // We assume that all the RepeatedField<>* pointers have the same
  301. // size and alignment within the anonymous union in Extension.
  302. return extension->repeated_int32_value;
  303. }
  304. void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type,
  305. bool packed,
  306. const FieldDescriptor* desc) {
  307. Extension* extension;
  308. // We instantiate an empty Repeated{,Ptr}Field if one doesn't exist for this
  309. // extension.
  310. if (MaybeNewExtension(number, desc, &extension)) {
  311. extension->is_repeated = true;
  312. extension->type = field_type;
  313. extension->is_packed = packed;
  314. switch (WireFormatLite::FieldTypeToCppType(
  315. static_cast<WireFormatLite::FieldType>(field_type))) {
  316. case WireFormatLite::CPPTYPE_INT32:
  317. extension->repeated_int32_value =
  318. Arena::CreateMessage<RepeatedField<int32> >(arena_);
  319. break;
  320. case WireFormatLite::CPPTYPE_INT64:
  321. extension->repeated_int64_value =
  322. Arena::CreateMessage<RepeatedField<int64> >(arena_);
  323. break;
  324. case WireFormatLite::CPPTYPE_UINT32:
  325. extension->repeated_uint32_value =
  326. Arena::CreateMessage<RepeatedField<uint32> >(arena_);
  327. break;
  328. case WireFormatLite::CPPTYPE_UINT64:
  329. extension->repeated_uint64_value =
  330. Arena::CreateMessage<RepeatedField<uint64> >(arena_);
  331. break;
  332. case WireFormatLite::CPPTYPE_DOUBLE:
  333. extension->repeated_double_value =
  334. Arena::CreateMessage<RepeatedField<double> >(arena_);
  335. break;
  336. case WireFormatLite::CPPTYPE_FLOAT:
  337. extension->repeated_float_value =
  338. Arena::CreateMessage<RepeatedField<float> >(arena_);
  339. break;
  340. case WireFormatLite::CPPTYPE_BOOL:
  341. extension->repeated_bool_value =
  342. Arena::CreateMessage<RepeatedField<bool> >(arena_);
  343. break;
  344. case WireFormatLite::CPPTYPE_ENUM:
  345. extension->repeated_enum_value =
  346. Arena::CreateMessage<RepeatedField<int> >(arena_);
  347. break;
  348. case WireFormatLite::CPPTYPE_STRING:
  349. extension->repeated_string_value =
  350. Arena::CreateMessage<RepeatedPtrField<::std::string> >(arena_);
  351. break;
  352. case WireFormatLite::CPPTYPE_MESSAGE:
  353. extension->repeated_message_value =
  354. Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
  355. break;
  356. }
  357. }
  358. // We assume that all the RepeatedField<>* pointers have the same
  359. // size and alignment within the anonymous union in Extension.
  360. return extension->repeated_int32_value;
  361. }
  362. // Compatible version using old call signature. Does not create extensions when
  363. // the don't already exist; instead, just GOOGLE_CHECK-fails.
  364. void* ExtensionSet::MutableRawRepeatedField(int number) {
  365. Extension* extension = FindOrNull(number);
  366. GOOGLE_CHECK(extension != NULL) << "Extension not found.";
  367. // We assume that all the RepeatedField<>* pointers have the same
  368. // size and alignment within the anonymous union in Extension.
  369. return extension->repeated_int32_value;
  370. }
  371. // -------------------------------------------------------------------
  372. // Enums
  373. int ExtensionSet::GetEnum(int number, int default_value) const {
  374. const Extension* extension = FindOrNull(number);
  375. if (extension == NULL || extension->is_cleared) {
  376. // Not present. Return the default value.
  377. return default_value;
  378. } else {
  379. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM);
  380. return extension->enum_value;
  381. }
  382. }
  383. void ExtensionSet::SetEnum(int number, FieldType type, int value,
  384. const FieldDescriptor* descriptor) {
  385. Extension* extension;
  386. if (MaybeNewExtension(number, descriptor, &extension)) {
  387. extension->type = type;
  388. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
  389. extension->is_repeated = false;
  390. } else {
  391. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM);
  392. }
  393. extension->is_cleared = false;
  394. extension->enum_value = value;
  395. }
  396. int ExtensionSet::GetRepeatedEnum(int number, int index) const {
  397. const Extension* extension = FindOrNull(number);
  398. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  399. GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
  400. return extension->repeated_enum_value->Get(index);
  401. }
  402. void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
  403. Extension* extension = FindOrNull(number);
  404. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  405. GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
  406. extension->repeated_enum_value->Set(index, value);
  407. }
  408. void ExtensionSet::AddEnum(int number, FieldType type,
  409. bool packed, int value,
  410. const FieldDescriptor* descriptor) {
  411. Extension* extension;
  412. if (MaybeNewExtension(number, descriptor, &extension)) {
  413. extension->type = type;
  414. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
  415. extension->is_repeated = true;
  416. extension->is_packed = packed;
  417. extension->repeated_enum_value =
  418. Arena::CreateMessage<RepeatedField<int> >(arena_);
  419. } else {
  420. GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
  421. GOOGLE_DCHECK_EQ(extension->is_packed, packed);
  422. }
  423. extension->repeated_enum_value->Add(value);
  424. }
  425. // -------------------------------------------------------------------
  426. // Strings
  427. const string& ExtensionSet::GetString(int number,
  428. const string& default_value) const {
  429. const Extension* extension = FindOrNull(number);
  430. if (extension == NULL || extension->is_cleared) {
  431. // Not present. Return the default value.
  432. return default_value;
  433. } else {
  434. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
  435. return *extension->string_value;
  436. }
  437. }
  438. string* ExtensionSet::MutableString(int number, FieldType type,
  439. const FieldDescriptor* descriptor) {
  440. Extension* extension;
  441. if (MaybeNewExtension(number, descriptor, &extension)) {
  442. extension->type = type;
  443. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
  444. extension->is_repeated = false;
  445. extension->string_value = Arena::Create<string>(arena_);
  446. } else {
  447. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
  448. }
  449. extension->is_cleared = false;
  450. return extension->string_value;
  451. }
  452. const string& ExtensionSet::GetRepeatedString(int number, int index) const {
  453. const Extension* extension = FindOrNull(number);
  454. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  455. GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
  456. return extension->repeated_string_value->Get(index);
  457. }
  458. string* ExtensionSet::MutableRepeatedString(int number, int index) {
  459. Extension* extension = FindOrNull(number);
  460. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  461. GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
  462. return extension->repeated_string_value->Mutable(index);
  463. }
  464. string* ExtensionSet::AddString(int number, FieldType type,
  465. const FieldDescriptor* descriptor) {
  466. Extension* extension;
  467. if (MaybeNewExtension(number, descriptor, &extension)) {
  468. extension->type = type;
  469. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
  470. extension->is_repeated = true;
  471. extension->is_packed = false;
  472. extension->repeated_string_value =
  473. Arena::CreateMessage<RepeatedPtrField<string> >(arena_);
  474. } else {
  475. GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
  476. }
  477. return extension->repeated_string_value->Add();
  478. }
  479. // -------------------------------------------------------------------
  480. // Messages
  481. const MessageLite& ExtensionSet::GetMessage(
  482. int number, const MessageLite& default_value) const {
  483. const Extension* extension = FindOrNull(number);
  484. if (extension == NULL) {
  485. // Not present. Return the default value.
  486. return default_value;
  487. } else {
  488. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  489. if (extension->is_lazy) {
  490. return extension->lazymessage_value->GetMessage(default_value);
  491. } else {
  492. return *extension->message_value;
  493. }
  494. }
  495. }
  496. // Defined in extension_set_heavy.cc.
  497. // const MessageLite& ExtensionSet::GetMessage(int number,
  498. // const Descriptor* message_type,
  499. // MessageFactory* factory) const
  500. MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
  501. const MessageLite& prototype,
  502. const FieldDescriptor* descriptor) {
  503. Extension* extension;
  504. if (MaybeNewExtension(number, descriptor, &extension)) {
  505. extension->type = type;
  506. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  507. extension->is_repeated = false;
  508. extension->is_lazy = false;
  509. extension->message_value = prototype.New(arena_);
  510. extension->is_cleared = false;
  511. return extension->message_value;
  512. } else {
  513. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  514. extension->is_cleared = false;
  515. if (extension->is_lazy) {
  516. return extension->lazymessage_value->MutableMessage(prototype);
  517. } else {
  518. return extension->message_value;
  519. }
  520. }
  521. }
  522. // Defined in extension_set_heavy.cc.
  523. // MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
  524. // const Descriptor* message_type,
  525. // MessageFactory* factory)
  526. void ExtensionSet::SetAllocatedMessage(int number, FieldType type,
  527. const FieldDescriptor* descriptor,
  528. MessageLite* message) {
  529. if (message == NULL) {
  530. ClearExtension(number);
  531. return;
  532. }
  533. ::google::protobuf::Arena* message_arena = message->GetArena();
  534. Extension* extension;
  535. if (MaybeNewExtension(number, descriptor, &extension)) {
  536. extension->type = type;
  537. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  538. extension->is_repeated = false;
  539. extension->is_lazy = false;
  540. if (message_arena == arena_) {
  541. extension->message_value = message;
  542. } else if (message_arena == NULL) {
  543. extension->message_value = message;
  544. arena_->Own(message); // not NULL because not equal to message_arena
  545. } else {
  546. extension->message_value = message->New(arena_);
  547. extension->message_value->CheckTypeAndMergeFrom(*message);
  548. }
  549. } else {
  550. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  551. if (extension->is_lazy) {
  552. extension->lazymessage_value->SetAllocatedMessage(message);
  553. } else {
  554. if (arena_ == NULL) {
  555. delete extension->message_value;
  556. }
  557. if (message_arena == arena_) {
  558. extension->message_value = message;
  559. } else if (message_arena == NULL) {
  560. extension->message_value = message;
  561. arena_->Own(message); // not NULL because not equal to message_arena
  562. } else {
  563. extension->message_value = message->New(arena_);
  564. extension->message_value->CheckTypeAndMergeFrom(*message);
  565. }
  566. }
  567. }
  568. extension->is_cleared = false;
  569. }
  570. void ExtensionSet::UnsafeArenaSetAllocatedMessage(
  571. int number, FieldType type, const FieldDescriptor* descriptor,
  572. MessageLite* message) {
  573. if (message == NULL) {
  574. ClearExtension(number);
  575. return;
  576. }
  577. Extension* extension;
  578. if (MaybeNewExtension(number, descriptor, &extension)) {
  579. extension->type = type;
  580. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  581. extension->is_repeated = false;
  582. extension->is_lazy = false;
  583. extension->message_value = message;
  584. } else {
  585. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  586. if (extension->is_lazy) {
  587. extension->lazymessage_value->UnsafeArenaSetAllocatedMessage(message);
  588. } else {
  589. if (arena_ == NULL) {
  590. delete extension->message_value;
  591. }
  592. extension->message_value = message;
  593. }
  594. }
  595. extension->is_cleared = false;
  596. }
  597. MessageLite* ExtensionSet::ReleaseMessage(int number,
  598. const MessageLite& prototype) {
  599. Extension* extension = FindOrNull(number);
  600. if (extension == NULL) {
  601. // Not present. Return NULL.
  602. return NULL;
  603. } else {
  604. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  605. MessageLite* ret = NULL;
  606. if (extension->is_lazy) {
  607. ret = extension->lazymessage_value->ReleaseMessage(prototype);
  608. if (arena_ == NULL) {
  609. delete extension->lazymessage_value;
  610. }
  611. } else {
  612. if (arena_ == NULL) {
  613. ret = extension->message_value;
  614. } else {
  615. // ReleaseMessage() always returns a heap-allocated message, and we are
  616. // on an arena, so we need to make a copy of this message to return.
  617. ret = extension->message_value->New();
  618. ret->CheckTypeAndMergeFrom(*extension->message_value);
  619. }
  620. }
  621. Erase(number);
  622. return ret;
  623. }
  624. }
  625. MessageLite* ExtensionSet::UnsafeArenaReleaseMessage(
  626. int number, const MessageLite& prototype) {
  627. Extension* extension = FindOrNull(number);
  628. if (extension == NULL) {
  629. // Not present. Return NULL.
  630. return NULL;
  631. } else {
  632. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  633. MessageLite* ret = NULL;
  634. if (extension->is_lazy) {
  635. ret = extension->lazymessage_value->UnsafeArenaReleaseMessage(prototype);
  636. if (arena_ == NULL) {
  637. delete extension->lazymessage_value;
  638. }
  639. } else {
  640. ret = extension->message_value;
  641. }
  642. Erase(number);
  643. return ret;
  644. }
  645. }
  646. // Defined in extension_set_heavy.cc.
  647. // MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor,
  648. // MessageFactory* factory);
  649. const MessageLite& ExtensionSet::GetRepeatedMessage(
  650. int number, int index) const {
  651. const Extension* extension = FindOrNull(number);
  652. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  653. GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
  654. return extension->repeated_message_value->Get(index);
  655. }
  656. MessageLite* ExtensionSet::MutableRepeatedMessage(int number, int index) {
  657. Extension* extension = FindOrNull(number);
  658. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  659. GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
  660. return extension->repeated_message_value->Mutable(index);
  661. }
  662. MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
  663. const MessageLite& prototype,
  664. const FieldDescriptor* descriptor) {
  665. Extension* extension;
  666. if (MaybeNewExtension(number, descriptor, &extension)) {
  667. extension->type = type;
  668. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  669. extension->is_repeated = true;
  670. extension->repeated_message_value =
  671. Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
  672. } else {
  673. GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
  674. }
  675. // RepeatedPtrField<MessageLite> does not know how to Add() since it cannot
  676. // allocate an abstract object, so we have to be tricky.
  677. MessageLite* result =
  678. reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>(
  679. extension->repeated_message_value)
  680. ->AddFromCleared<GenericTypeHandler<MessageLite> >();
  681. if (result == NULL) {
  682. result = prototype.New(arena_);
  683. extension->repeated_message_value->AddAllocated(result);
  684. }
  685. return result;
  686. }
  687. // Defined in extension_set_heavy.cc.
  688. // MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
  689. // const Descriptor* message_type,
  690. // MessageFactory* factory)
  691. #undef GOOGLE_DCHECK_TYPE
  692. void ExtensionSet::RemoveLast(int number) {
  693. Extension* extension = FindOrNull(number);
  694. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  695. GOOGLE_DCHECK(extension->is_repeated);
  696. switch(cpp_type(extension->type)) {
  697. case WireFormatLite::CPPTYPE_INT32:
  698. extension->repeated_int32_value->RemoveLast();
  699. break;
  700. case WireFormatLite::CPPTYPE_INT64:
  701. extension->repeated_int64_value->RemoveLast();
  702. break;
  703. case WireFormatLite::CPPTYPE_UINT32:
  704. extension->repeated_uint32_value->RemoveLast();
  705. break;
  706. case WireFormatLite::CPPTYPE_UINT64:
  707. extension->repeated_uint64_value->RemoveLast();
  708. break;
  709. case WireFormatLite::CPPTYPE_FLOAT:
  710. extension->repeated_float_value->RemoveLast();
  711. break;
  712. case WireFormatLite::CPPTYPE_DOUBLE:
  713. extension->repeated_double_value->RemoveLast();
  714. break;
  715. case WireFormatLite::CPPTYPE_BOOL:
  716. extension->repeated_bool_value->RemoveLast();
  717. break;
  718. case WireFormatLite::CPPTYPE_ENUM:
  719. extension->repeated_enum_value->RemoveLast();
  720. break;
  721. case WireFormatLite::CPPTYPE_STRING:
  722. extension->repeated_string_value->RemoveLast();
  723. break;
  724. case WireFormatLite::CPPTYPE_MESSAGE:
  725. extension->repeated_message_value->RemoveLast();
  726. break;
  727. }
  728. }
  729. MessageLite* ExtensionSet::ReleaseLast(int number) {
  730. Extension* extension = FindOrNull(number);
  731. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  732. GOOGLE_DCHECK(extension->is_repeated);
  733. GOOGLE_DCHECK(cpp_type(extension->type) == WireFormatLite::CPPTYPE_MESSAGE);
  734. return extension->repeated_message_value->ReleaseLast();
  735. }
  736. void ExtensionSet::SwapElements(int number, int index1, int index2) {
  737. Extension* extension = FindOrNull(number);
  738. GOOGLE_CHECK(extension != NULL) << "Index out-of-bounds (field is empty).";
  739. GOOGLE_DCHECK(extension->is_repeated);
  740. switch(cpp_type(extension->type)) {
  741. case WireFormatLite::CPPTYPE_INT32:
  742. extension->repeated_int32_value->SwapElements(index1, index2);
  743. break;
  744. case WireFormatLite::CPPTYPE_INT64:
  745. extension->repeated_int64_value->SwapElements(index1, index2);
  746. break;
  747. case WireFormatLite::CPPTYPE_UINT32:
  748. extension->repeated_uint32_value->SwapElements(index1, index2);
  749. break;
  750. case WireFormatLite::CPPTYPE_UINT64:
  751. extension->repeated_uint64_value->SwapElements(index1, index2);
  752. break;
  753. case WireFormatLite::CPPTYPE_FLOAT:
  754. extension->repeated_float_value->SwapElements(index1, index2);
  755. break;
  756. case WireFormatLite::CPPTYPE_DOUBLE:
  757. extension->repeated_double_value->SwapElements(index1, index2);
  758. break;
  759. case WireFormatLite::CPPTYPE_BOOL:
  760. extension->repeated_bool_value->SwapElements(index1, index2);
  761. break;
  762. case WireFormatLite::CPPTYPE_ENUM:
  763. extension->repeated_enum_value->SwapElements(index1, index2);
  764. break;
  765. case WireFormatLite::CPPTYPE_STRING:
  766. extension->repeated_string_value->SwapElements(index1, index2);
  767. break;
  768. case WireFormatLite::CPPTYPE_MESSAGE:
  769. extension->repeated_message_value->SwapElements(index1, index2);
  770. break;
  771. }
  772. }
  773. // ===================================================================
  774. void ExtensionSet::Clear() {
  775. ForEach([](int /* number */, Extension& ext) { ext.Clear(); });
  776. }
  777. namespace {
  778. // Computes the size of a std::set_union without constructing the union.
  779. template <typename ItX, typename ItY>
  780. size_t SizeOfUnion(ItX it_xs, ItX end_xs, ItY it_ys, ItY end_ys) {
  781. size_t result = 0;
  782. while (it_xs != end_xs && it_ys != end_ys) {
  783. ++result;
  784. if (it_xs->first < it_ys->first) {
  785. ++it_xs;
  786. } else if (it_xs->first == it_ys->first) {
  787. ++it_xs;
  788. ++it_ys;
  789. } else {
  790. ++it_ys;
  791. }
  792. }
  793. result += std::distance(it_xs, end_xs);
  794. result += std::distance(it_ys, end_ys);
  795. return result;
  796. }
  797. } // namespace
  798. void ExtensionSet::MergeFrom(const ExtensionSet& other) {
  799. if (GOOGLE_PREDICT_TRUE(!is_large())) {
  800. if (GOOGLE_PREDICT_TRUE(!other.is_large())) {
  801. GrowCapacity(SizeOfUnion(flat_begin(), flat_end(), other.flat_begin(),
  802. other.flat_end()));
  803. } else {
  804. GrowCapacity(SizeOfUnion(flat_begin(), flat_end(),
  805. other.map_.large->begin(),
  806. other.map_.large->end()));
  807. }
  808. }
  809. other.ForEach([this](int number, const Extension& ext) {
  810. this->InternalExtensionMergeFrom(number, ext);
  811. });
  812. }
  813. void ExtensionSet::InternalExtensionMergeFrom(
  814. int number, const Extension& other_extension) {
  815. if (other_extension.is_repeated) {
  816. Extension* extension;
  817. bool is_new = MaybeNewExtension(number, other_extension.descriptor,
  818. &extension);
  819. if (is_new) {
  820. // Extension did not already exist in set.
  821. extension->type = other_extension.type;
  822. extension->is_packed = other_extension.is_packed;
  823. extension->is_repeated = true;
  824. } else {
  825. GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
  826. GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
  827. GOOGLE_DCHECK(extension->is_repeated);
  828. }
  829. switch (cpp_type(other_extension.type)) {
  830. #define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \
  831. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  832. if (is_new) { \
  833. extension->repeated_##LOWERCASE##_value = \
  834. Arena::CreateMessage<REPEATED_TYPE >(arena_); \
  835. } \
  836. extension->repeated_##LOWERCASE##_value->MergeFrom( \
  837. *other_extension.repeated_##LOWERCASE##_value); \
  838. break;
  839. HANDLE_TYPE( INT32, int32, RepeatedField < int32>);
  840. HANDLE_TYPE( INT64, int64, RepeatedField < int64>);
  841. HANDLE_TYPE( UINT32, uint32, RepeatedField < uint32>);
  842. HANDLE_TYPE( UINT64, uint64, RepeatedField < uint64>);
  843. HANDLE_TYPE( FLOAT, float, RepeatedField < float>);
  844. HANDLE_TYPE( DOUBLE, double, RepeatedField < double>);
  845. HANDLE_TYPE( BOOL, bool, RepeatedField < bool>);
  846. HANDLE_TYPE( ENUM, enum, RepeatedField < int>);
  847. HANDLE_TYPE( STRING, string, RepeatedPtrField< string>);
  848. #undef HANDLE_TYPE
  849. case WireFormatLite::CPPTYPE_MESSAGE:
  850. if (is_new) {
  851. extension->repeated_message_value =
  852. Arena::CreateMessage<RepeatedPtrField<MessageLite> >(arena_);
  853. }
  854. // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
  855. // it would attempt to allocate new objects.
  856. RepeatedPtrField<MessageLite>* other_repeated_message =
  857. other_extension.repeated_message_value;
  858. for (int i = 0; i < other_repeated_message->size(); i++) {
  859. const MessageLite& other_message = other_repeated_message->Get(i);
  860. MessageLite* target =
  861. reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>(
  862. extension->repeated_message_value)
  863. ->AddFromCleared<GenericTypeHandler<MessageLite> >();
  864. if (target == NULL) {
  865. target = other_message.New(arena_);
  866. extension->repeated_message_value->AddAllocated(target);
  867. }
  868. target->CheckTypeAndMergeFrom(other_message);
  869. }
  870. break;
  871. }
  872. } else {
  873. if (!other_extension.is_cleared) {
  874. switch (cpp_type(other_extension.type)) {
  875. #define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
  876. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  877. Set##CAMELCASE(number, other_extension.type, \
  878. other_extension.LOWERCASE##_value, \
  879. other_extension.descriptor); \
  880. break;
  881. HANDLE_TYPE( INT32, int32, Int32);
  882. HANDLE_TYPE( INT64, int64, Int64);
  883. HANDLE_TYPE(UINT32, uint32, UInt32);
  884. HANDLE_TYPE(UINT64, uint64, UInt64);
  885. HANDLE_TYPE( FLOAT, float, Float);
  886. HANDLE_TYPE(DOUBLE, double, Double);
  887. HANDLE_TYPE( BOOL, bool, Bool);
  888. HANDLE_TYPE( ENUM, enum, Enum);
  889. #undef HANDLE_TYPE
  890. case WireFormatLite::CPPTYPE_STRING:
  891. SetString(number, other_extension.type,
  892. *other_extension.string_value,
  893. other_extension.descriptor);
  894. break;
  895. case WireFormatLite::CPPTYPE_MESSAGE: {
  896. Extension* extension;
  897. bool is_new = MaybeNewExtension(number,
  898. other_extension.descriptor,
  899. &extension);
  900. if (is_new) {
  901. extension->type = other_extension.type;
  902. extension->is_packed = other_extension.is_packed;
  903. extension->is_repeated = false;
  904. if (other_extension.is_lazy) {
  905. extension->is_lazy = true;
  906. extension->lazymessage_value =
  907. other_extension.lazymessage_value->New(arena_);
  908. extension->lazymessage_value->MergeFrom(
  909. *other_extension.lazymessage_value);
  910. } else {
  911. extension->is_lazy = false;
  912. extension->message_value =
  913. other_extension.message_value->New(arena_);
  914. extension->message_value->CheckTypeAndMergeFrom(
  915. *other_extension.message_value);
  916. }
  917. } else {
  918. GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
  919. GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed);
  920. GOOGLE_DCHECK(!extension->is_repeated);
  921. if (other_extension.is_lazy) {
  922. if (extension->is_lazy) {
  923. extension->lazymessage_value->MergeFrom(
  924. *other_extension.lazymessage_value);
  925. } else {
  926. extension->message_value->CheckTypeAndMergeFrom(
  927. other_extension.lazymessage_value->GetMessage(
  928. *extension->message_value));
  929. }
  930. } else {
  931. if (extension->is_lazy) {
  932. extension->lazymessage_value->MutableMessage(
  933. *other_extension.message_value)->CheckTypeAndMergeFrom(
  934. *other_extension.message_value);
  935. } else {
  936. extension->message_value->CheckTypeAndMergeFrom(
  937. *other_extension.message_value);
  938. }
  939. }
  940. }
  941. extension->is_cleared = false;
  942. break;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. void ExtensionSet::Swap(ExtensionSet* x) {
  949. if (GetArenaNoVirtual() == x->GetArenaNoVirtual()) {
  950. using std::swap;
  951. swap(flat_capacity_, x->flat_capacity_);
  952. swap(flat_size_, x->flat_size_);
  953. swap(map_, x->map_);
  954. } else {
  955. // TODO(cfallin, rohananil): We maybe able to optimize a case where we are
  956. // swapping from heap to arena-allocated extension set, by just Own()'ing
  957. // the extensions.
  958. ExtensionSet extension_set;
  959. extension_set.MergeFrom(*x);
  960. x->Clear();
  961. x->MergeFrom(*this);
  962. Clear();
  963. MergeFrom(extension_set);
  964. }
  965. }
  966. void ExtensionSet::SwapExtension(ExtensionSet* other,
  967. int number) {
  968. if (this == other) return;
  969. Extension* this_ext = FindOrNull(number);
  970. Extension* other_ext = other->FindOrNull(number);
  971. if (this_ext == NULL && other_ext == NULL) {
  972. return;
  973. }
  974. if (this_ext != NULL && other_ext != NULL) {
  975. if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
  976. using std::swap;
  977. swap(*this_ext, *other_ext);
  978. } else {
  979. // TODO(cfallin, rohananil): We could further optimize these cases,
  980. // especially avoid creation of ExtensionSet, and move MergeFrom logic
  981. // into Extensions itself (which takes arena as an argument).
  982. // We do it this way to reuse the copy-across-arenas logic already
  983. // implemented in ExtensionSet's MergeFrom.
  984. ExtensionSet temp;
  985. temp.InternalExtensionMergeFrom(number, *other_ext);
  986. Extension* temp_ext = temp.FindOrNull(number);
  987. other_ext->Clear();
  988. other->InternalExtensionMergeFrom(number, *this_ext);
  989. this_ext->Clear();
  990. InternalExtensionMergeFrom(number, *temp_ext);
  991. }
  992. return;
  993. }
  994. if (this_ext == NULL) {
  995. if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
  996. *Insert(number).first = *other_ext;
  997. } else {
  998. InternalExtensionMergeFrom(number, *other_ext);
  999. }
  1000. other->Erase(number);
  1001. return;
  1002. }
  1003. if (other_ext == NULL) {
  1004. if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
  1005. *other->Insert(number).first = *this_ext;
  1006. } else {
  1007. other->InternalExtensionMergeFrom(number, *this_ext);
  1008. }
  1009. Erase(number);
  1010. return;
  1011. }
  1012. }
  1013. bool ExtensionSet::IsInitialized() const {
  1014. // Extensions are never required. However, we need to check that all
  1015. // embedded messages are initialized.
  1016. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1017. for (const auto& kv : *map_.large) {
  1018. if (!kv.second.IsInitialized()) return false;
  1019. }
  1020. return true;
  1021. }
  1022. for (const KeyValue* it = flat_begin(); it != flat_end(); ++it) {
  1023. if (!it->second.IsInitialized()) return false;
  1024. }
  1025. return true;
  1026. }
  1027. bool ExtensionSet::FindExtensionInfoFromTag(
  1028. uint32 tag, ExtensionFinder* extension_finder, int* field_number,
  1029. ExtensionInfo* extension, bool* was_packed_on_wire) {
  1030. *field_number = WireFormatLite::GetTagFieldNumber(tag);
  1031. WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
  1032. return FindExtensionInfoFromFieldNumber(wire_type, *field_number,
  1033. extension_finder, extension,
  1034. was_packed_on_wire);
  1035. }
  1036. bool ExtensionSet::FindExtensionInfoFromFieldNumber(
  1037. int wire_type, int field_number, ExtensionFinder* extension_finder,
  1038. ExtensionInfo* extension, bool* was_packed_on_wire) {
  1039. if (!extension_finder->Find(field_number, extension)) {
  1040. return false;
  1041. }
  1042. WireFormatLite::WireType expected_wire_type =
  1043. WireFormatLite::WireTypeForFieldType(real_type(extension->type));
  1044. // Check if this is a packed field.
  1045. *was_packed_on_wire = false;
  1046. if (extension->is_repeated &&
  1047. wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED &&
  1048. is_packable(expected_wire_type)) {
  1049. *was_packed_on_wire = true;
  1050. return true;
  1051. }
  1052. // Otherwise the wire type must match.
  1053. return expected_wire_type == wire_type;
  1054. }
  1055. bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  1056. ExtensionFinder* extension_finder,
  1057. FieldSkipper* field_skipper) {
  1058. int number;
  1059. bool was_packed_on_wire;
  1060. ExtensionInfo extension;
  1061. if (!FindExtensionInfoFromTag(
  1062. tag, extension_finder, &number, &extension, &was_packed_on_wire)) {
  1063. return field_skipper->SkipField(input, tag);
  1064. } else {
  1065. return ParseFieldWithExtensionInfo(
  1066. number, was_packed_on_wire, extension, input, field_skipper);
  1067. }
  1068. }
  1069. bool ExtensionSet::ParseFieldWithExtensionInfo(
  1070. int number, bool was_packed_on_wire, const ExtensionInfo& extension,
  1071. io::CodedInputStream* input,
  1072. FieldSkipper* field_skipper) {
  1073. // Explicitly not read extension.is_packed, instead check whether the field
  1074. // was encoded in packed form on the wire.
  1075. if (was_packed_on_wire) {
  1076. uint32 size;
  1077. if (!input->ReadVarint32(&size)) return false;
  1078. io::CodedInputStream::Limit limit = input->PushLimit(size);
  1079. switch (extension.type) {
  1080. #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
  1081. case WireFormatLite::TYPE_##UPPERCASE: \
  1082. while (input->BytesUntilLimit() > 0) { \
  1083. CPP_LOWERCASE value; \
  1084. if (!WireFormatLite::ReadPrimitive< \
  1085. CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
  1086. input, &value)) return false; \
  1087. Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
  1088. extension.is_packed, value, \
  1089. extension.descriptor); \
  1090. } \
  1091. break
  1092. HANDLE_TYPE( INT32, Int32, int32);
  1093. HANDLE_TYPE( INT64, Int64, int64);
  1094. HANDLE_TYPE( UINT32, UInt32, uint32);
  1095. HANDLE_TYPE( UINT64, UInt64, uint64);
  1096. HANDLE_TYPE( SINT32, Int32, int32);
  1097. HANDLE_TYPE( SINT64, Int64, int64);
  1098. HANDLE_TYPE( FIXED32, UInt32, uint32);
  1099. HANDLE_TYPE( FIXED64, UInt64, uint64);
  1100. HANDLE_TYPE(SFIXED32, Int32, int32);
  1101. HANDLE_TYPE(SFIXED64, Int64, int64);
  1102. HANDLE_TYPE( FLOAT, Float, float);
  1103. HANDLE_TYPE( DOUBLE, Double, double);
  1104. HANDLE_TYPE( BOOL, Bool, bool);
  1105. #undef HANDLE_TYPE
  1106. case WireFormatLite::TYPE_ENUM:
  1107. while (input->BytesUntilLimit() > 0) {
  1108. int value;
  1109. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  1110. input, &value)) return false;
  1111. if (extension.enum_validity_check.func(
  1112. extension.enum_validity_check.arg, value)) {
  1113. AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed,
  1114. value, extension.descriptor);
  1115. } else {
  1116. // Invalid value. Treat as unknown.
  1117. field_skipper->SkipUnknownEnum(number, value);
  1118. }
  1119. }
  1120. break;
  1121. case WireFormatLite::TYPE_STRING:
  1122. case WireFormatLite::TYPE_BYTES:
  1123. case WireFormatLite::TYPE_GROUP:
  1124. case WireFormatLite::TYPE_MESSAGE:
  1125. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  1126. break;
  1127. }
  1128. input->PopLimit(limit);
  1129. } else {
  1130. switch (extension.type) {
  1131. #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
  1132. case WireFormatLite::TYPE_##UPPERCASE: { \
  1133. CPP_LOWERCASE value; \
  1134. if (!WireFormatLite::ReadPrimitive< \
  1135. CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
  1136. input, &value)) return false; \
  1137. if (extension.is_repeated) { \
  1138. Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
  1139. extension.is_packed, value, \
  1140. extension.descriptor); \
  1141. } else { \
  1142. Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
  1143. extension.descriptor); \
  1144. } \
  1145. } break
  1146. HANDLE_TYPE( INT32, Int32, int32);
  1147. HANDLE_TYPE( INT64, Int64, int64);
  1148. HANDLE_TYPE( UINT32, UInt32, uint32);
  1149. HANDLE_TYPE( UINT64, UInt64, uint64);
  1150. HANDLE_TYPE( SINT32, Int32, int32);
  1151. HANDLE_TYPE( SINT64, Int64, int64);
  1152. HANDLE_TYPE( FIXED32, UInt32, uint32);
  1153. HANDLE_TYPE( FIXED64, UInt64, uint64);
  1154. HANDLE_TYPE(SFIXED32, Int32, int32);
  1155. HANDLE_TYPE(SFIXED64, Int64, int64);
  1156. HANDLE_TYPE( FLOAT, Float, float);
  1157. HANDLE_TYPE( DOUBLE, Double, double);
  1158. HANDLE_TYPE( BOOL, Bool, bool);
  1159. #undef HANDLE_TYPE
  1160. case WireFormatLite::TYPE_ENUM: {
  1161. int value;
  1162. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  1163. input, &value)) return false;
  1164. if (!extension.enum_validity_check.func(
  1165. extension.enum_validity_check.arg, value)) {
  1166. // Invalid value. Treat as unknown.
  1167. field_skipper->SkipUnknownEnum(number, value);
  1168. } else if (extension.is_repeated) {
  1169. AddEnum(number, WireFormatLite::TYPE_ENUM, extension.is_packed, value,
  1170. extension.descriptor);
  1171. } else {
  1172. SetEnum(number, WireFormatLite::TYPE_ENUM, value,
  1173. extension.descriptor);
  1174. }
  1175. break;
  1176. }
  1177. case WireFormatLite::TYPE_STRING: {
  1178. string* value = extension.is_repeated ?
  1179. AddString(number, WireFormatLite::TYPE_STRING, extension.descriptor) :
  1180. MutableString(number, WireFormatLite::TYPE_STRING,
  1181. extension.descriptor);
  1182. if (!WireFormatLite::ReadString(input, value)) return false;
  1183. break;
  1184. }
  1185. case WireFormatLite::TYPE_BYTES: {
  1186. string* value = extension.is_repeated ?
  1187. AddString(number, WireFormatLite::TYPE_BYTES, extension.descriptor) :
  1188. MutableString(number, WireFormatLite::TYPE_BYTES,
  1189. extension.descriptor);
  1190. if (!WireFormatLite::ReadBytes(input, value)) return false;
  1191. break;
  1192. }
  1193. case WireFormatLite::TYPE_GROUP: {
  1194. MessageLite* value = extension.is_repeated ?
  1195. AddMessage(number, WireFormatLite::TYPE_GROUP,
  1196. *extension.message_prototype, extension.descriptor) :
  1197. MutableMessage(number, WireFormatLite::TYPE_GROUP,
  1198. *extension.message_prototype, extension.descriptor);
  1199. if (!WireFormatLite::ReadGroup(number, input, value)) return false;
  1200. break;
  1201. }
  1202. case WireFormatLite::TYPE_MESSAGE: {
  1203. MessageLite* value = extension.is_repeated ?
  1204. AddMessage(number, WireFormatLite::TYPE_MESSAGE,
  1205. *extension.message_prototype, extension.descriptor) :
  1206. MutableMessage(number, WireFormatLite::TYPE_MESSAGE,
  1207. *extension.message_prototype, extension.descriptor);
  1208. if (!WireFormatLite::ReadMessage(input, value)) return false;
  1209. break;
  1210. }
  1211. }
  1212. }
  1213. return true;
  1214. }
  1215. bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  1216. const MessageLite* containing_type) {
  1217. FieldSkipper skipper;
  1218. GeneratedExtensionFinder finder(containing_type);
  1219. return ParseField(tag, input, &finder, &skipper);
  1220. }
  1221. bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  1222. const MessageLite* containing_type,
  1223. io::CodedOutputStream* unknown_fields) {
  1224. CodedOutputStreamFieldSkipper skipper(unknown_fields);
  1225. GeneratedExtensionFinder finder(containing_type);
  1226. return ParseField(tag, input, &finder, &skipper);
  1227. }
  1228. // Defined in extension_set_heavy.cc.
  1229. // bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  1230. // const MessageLite* containing_type,
  1231. // UnknownFieldSet* unknown_fields)
  1232. // Defined in extension_set_heavy.cc.
  1233. // bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
  1234. // const MessageLite* containing_type,
  1235. // UnknownFieldSet* unknown_fields);
  1236. void ExtensionSet::SerializeWithCachedSizes(
  1237. int start_field_number, int end_field_number,
  1238. io::CodedOutputStream* output) const {
  1239. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1240. const auto& end = map_.large->end();
  1241. for (auto it = map_.large->lower_bound(start_field_number);
  1242. it != end && it->first < end_field_number; ++it) {
  1243. it->second.SerializeFieldWithCachedSizes(it->first, output);
  1244. }
  1245. return;
  1246. }
  1247. const KeyValue* end = flat_end();
  1248. for (const KeyValue* it = std::lower_bound(
  1249. flat_begin(), end, start_field_number, KeyValue::FirstComparator());
  1250. it != end && it->first < end_field_number; ++it) {
  1251. it->second.SerializeFieldWithCachedSizes(it->first, output);
  1252. }
  1253. }
  1254. size_t ExtensionSet::ByteSize() const {
  1255. size_t total_size = 0;
  1256. ForEach([&total_size](int number, const Extension& ext) {
  1257. total_size += ext.ByteSize(number);
  1258. });
  1259. return total_size;
  1260. }
  1261. // Defined in extension_set_heavy.cc.
  1262. // int ExtensionSet::SpaceUsedExcludingSelf() const
  1263. bool ExtensionSet::MaybeNewExtension(int number,
  1264. const FieldDescriptor* descriptor,
  1265. Extension** result) {
  1266. bool extension_is_new = false;
  1267. std::tie(*result, extension_is_new) = Insert(number);
  1268. (*result)->descriptor = descriptor;
  1269. return extension_is_new;
  1270. }
  1271. // ===================================================================
  1272. // Methods of ExtensionSet::Extension
  1273. void ExtensionSet::Extension::Clear() {
  1274. if (is_repeated) {
  1275. switch (cpp_type(type)) {
  1276. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  1277. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  1278. repeated_##LOWERCASE##_value->Clear(); \
  1279. break
  1280. HANDLE_TYPE( INT32, int32);
  1281. HANDLE_TYPE( INT64, int64);
  1282. HANDLE_TYPE( UINT32, uint32);
  1283. HANDLE_TYPE( UINT64, uint64);
  1284. HANDLE_TYPE( FLOAT, float);
  1285. HANDLE_TYPE( DOUBLE, double);
  1286. HANDLE_TYPE( BOOL, bool);
  1287. HANDLE_TYPE( ENUM, enum);
  1288. HANDLE_TYPE( STRING, string);
  1289. HANDLE_TYPE(MESSAGE, message);
  1290. #undef HANDLE_TYPE
  1291. }
  1292. } else {
  1293. if (!is_cleared) {
  1294. switch (cpp_type(type)) {
  1295. case WireFormatLite::CPPTYPE_STRING:
  1296. string_value->clear();
  1297. break;
  1298. case WireFormatLite::CPPTYPE_MESSAGE:
  1299. if (is_lazy) {
  1300. lazymessage_value->Clear();
  1301. } else {
  1302. message_value->Clear();
  1303. }
  1304. break;
  1305. default:
  1306. // No need to do anything. Get*() will return the default value
  1307. // as long as is_cleared is true and Set*() will overwrite the
  1308. // previous value.
  1309. break;
  1310. }
  1311. is_cleared = true;
  1312. }
  1313. }
  1314. }
  1315. void ExtensionSet::Extension::SerializeFieldWithCachedSizes(
  1316. int number,
  1317. io::CodedOutputStream* output) const {
  1318. if (is_repeated) {
  1319. if (is_packed) {
  1320. if (cached_size == 0) return;
  1321. WireFormatLite::WriteTag(number,
  1322. WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
  1323. output->WriteVarint32(cached_size);
  1324. switch (real_type(type)) {
  1325. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1326. case WireFormatLite::TYPE_##UPPERCASE: \
  1327. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1328. WireFormatLite::Write##CAMELCASE##NoTag( \
  1329. repeated_##LOWERCASE##_value->Get(i), output); \
  1330. } \
  1331. break
  1332. HANDLE_TYPE( INT32, Int32, int32);
  1333. HANDLE_TYPE( INT64, Int64, int64);
  1334. HANDLE_TYPE( UINT32, UInt32, uint32);
  1335. HANDLE_TYPE( UINT64, UInt64, uint64);
  1336. HANDLE_TYPE( SINT32, SInt32, int32);
  1337. HANDLE_TYPE( SINT64, SInt64, int64);
  1338. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1339. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1340. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1341. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1342. HANDLE_TYPE( FLOAT, Float, float);
  1343. HANDLE_TYPE( DOUBLE, Double, double);
  1344. HANDLE_TYPE( BOOL, Bool, bool);
  1345. HANDLE_TYPE( ENUM, Enum, enum);
  1346. #undef HANDLE_TYPE
  1347. case WireFormatLite::TYPE_STRING:
  1348. case WireFormatLite::TYPE_BYTES:
  1349. case WireFormatLite::TYPE_GROUP:
  1350. case WireFormatLite::TYPE_MESSAGE:
  1351. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  1352. break;
  1353. }
  1354. } else {
  1355. switch (real_type(type)) {
  1356. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1357. case WireFormatLite::TYPE_##UPPERCASE: \
  1358. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1359. WireFormatLite::Write##CAMELCASE(number, \
  1360. repeated_##LOWERCASE##_value->Get(i), output); \
  1361. } \
  1362. break
  1363. HANDLE_TYPE( INT32, Int32, int32);
  1364. HANDLE_TYPE( INT64, Int64, int64);
  1365. HANDLE_TYPE( UINT32, UInt32, uint32);
  1366. HANDLE_TYPE( UINT64, UInt64, uint64);
  1367. HANDLE_TYPE( SINT32, SInt32, int32);
  1368. HANDLE_TYPE( SINT64, SInt64, int64);
  1369. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1370. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1371. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1372. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1373. HANDLE_TYPE( FLOAT, Float, float);
  1374. HANDLE_TYPE( DOUBLE, Double, double);
  1375. HANDLE_TYPE( BOOL, Bool, bool);
  1376. HANDLE_TYPE( STRING, String, string);
  1377. HANDLE_TYPE( BYTES, Bytes, string);
  1378. HANDLE_TYPE( ENUM, Enum, enum);
  1379. HANDLE_TYPE( GROUP, Group, message);
  1380. HANDLE_TYPE( MESSAGE, Message, message);
  1381. #undef HANDLE_TYPE
  1382. }
  1383. }
  1384. } else if (!is_cleared) {
  1385. switch (real_type(type)) {
  1386. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \
  1387. case WireFormatLite::TYPE_##UPPERCASE: \
  1388. WireFormatLite::Write##CAMELCASE(number, VALUE, output); \
  1389. break
  1390. HANDLE_TYPE( INT32, Int32, int32_value);
  1391. HANDLE_TYPE( INT64, Int64, int64_value);
  1392. HANDLE_TYPE( UINT32, UInt32, uint32_value);
  1393. HANDLE_TYPE( UINT64, UInt64, uint64_value);
  1394. HANDLE_TYPE( SINT32, SInt32, int32_value);
  1395. HANDLE_TYPE( SINT64, SInt64, int64_value);
  1396. HANDLE_TYPE( FIXED32, Fixed32, uint32_value);
  1397. HANDLE_TYPE( FIXED64, Fixed64, uint64_value);
  1398. HANDLE_TYPE(SFIXED32, SFixed32, int32_value);
  1399. HANDLE_TYPE(SFIXED64, SFixed64, int64_value);
  1400. HANDLE_TYPE( FLOAT, Float, float_value);
  1401. HANDLE_TYPE( DOUBLE, Double, double_value);
  1402. HANDLE_TYPE( BOOL, Bool, bool_value);
  1403. HANDLE_TYPE( STRING, String, *string_value);
  1404. HANDLE_TYPE( BYTES, Bytes, *string_value);
  1405. HANDLE_TYPE( ENUM, Enum, enum_value);
  1406. HANDLE_TYPE( GROUP, Group, *message_value);
  1407. #undef HANDLE_TYPE
  1408. case WireFormatLite::TYPE_MESSAGE:
  1409. if (is_lazy) {
  1410. lazymessage_value->WriteMessage(number, output);
  1411. } else {
  1412. WireFormatLite::WriteMessage(number, *message_value, output);
  1413. }
  1414. break;
  1415. }
  1416. }
  1417. }
  1418. size_t ExtensionSet::Extension::ByteSize(int number) const {
  1419. size_t result = 0;
  1420. if (is_repeated) {
  1421. if (is_packed) {
  1422. switch (real_type(type)) {
  1423. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1424. case WireFormatLite::TYPE_##UPPERCASE: \
  1425. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1426. result += WireFormatLite::CAMELCASE##Size( \
  1427. repeated_##LOWERCASE##_value->Get(i)); \
  1428. } \
  1429. break
  1430. HANDLE_TYPE( INT32, Int32, int32);
  1431. HANDLE_TYPE( INT64, Int64, int64);
  1432. HANDLE_TYPE( UINT32, UInt32, uint32);
  1433. HANDLE_TYPE( UINT64, UInt64, uint64);
  1434. HANDLE_TYPE( SINT32, SInt32, int32);
  1435. HANDLE_TYPE( SINT64, SInt64, int64);
  1436. HANDLE_TYPE( ENUM, Enum, enum);
  1437. #undef HANDLE_TYPE
  1438. // Stuff with fixed size.
  1439. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1440. case WireFormatLite::TYPE_##UPPERCASE: \
  1441. result += WireFormatLite::k##CAMELCASE##Size * \
  1442. FromIntSize(repeated_##LOWERCASE##_value->size()); \
  1443. break
  1444. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1445. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1446. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1447. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1448. HANDLE_TYPE( FLOAT, Float, float);
  1449. HANDLE_TYPE( DOUBLE, Double, double);
  1450. HANDLE_TYPE( BOOL, Bool, bool);
  1451. #undef HANDLE_TYPE
  1452. case WireFormatLite::TYPE_STRING:
  1453. case WireFormatLite::TYPE_BYTES:
  1454. case WireFormatLite::TYPE_GROUP:
  1455. case WireFormatLite::TYPE_MESSAGE:
  1456. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  1457. break;
  1458. }
  1459. cached_size = ToCachedSize(result);
  1460. if (result > 0) {
  1461. result += io::CodedOutputStream::VarintSize32(result);
  1462. result += io::CodedOutputStream::VarintSize32(
  1463. WireFormatLite::MakeTag(number,
  1464. WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
  1465. }
  1466. } else {
  1467. size_t tag_size = WireFormatLite::TagSize(number, real_type(type));
  1468. switch (real_type(type)) {
  1469. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1470. case WireFormatLite::TYPE_##UPPERCASE: \
  1471. result += tag_size * \
  1472. FromIntSize(repeated_##LOWERCASE##_value->size()); \
  1473. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1474. result += WireFormatLite::CAMELCASE##Size( \
  1475. repeated_##LOWERCASE##_value->Get(i)); \
  1476. } \
  1477. break
  1478. HANDLE_TYPE( INT32, Int32, int32);
  1479. HANDLE_TYPE( INT64, Int64, int64);
  1480. HANDLE_TYPE( UINT32, UInt32, uint32);
  1481. HANDLE_TYPE( UINT64, UInt64, uint64);
  1482. HANDLE_TYPE( SINT32, SInt32, int32);
  1483. HANDLE_TYPE( SINT64, SInt64, int64);
  1484. HANDLE_TYPE( STRING, String, string);
  1485. HANDLE_TYPE( BYTES, Bytes, string);
  1486. HANDLE_TYPE( ENUM, Enum, enum);
  1487. HANDLE_TYPE( GROUP, Group, message);
  1488. HANDLE_TYPE( MESSAGE, Message, message);
  1489. #undef HANDLE_TYPE
  1490. // Stuff with fixed size.
  1491. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1492. case WireFormatLite::TYPE_##UPPERCASE: \
  1493. result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \
  1494. FromIntSize(repeated_##LOWERCASE##_value->size()); \
  1495. break
  1496. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1497. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1498. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1499. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1500. HANDLE_TYPE( FLOAT, Float, float);
  1501. HANDLE_TYPE( DOUBLE, Double, double);
  1502. HANDLE_TYPE( BOOL, Bool, bool);
  1503. #undef HANDLE_TYPE
  1504. }
  1505. }
  1506. } else if (!is_cleared) {
  1507. result += WireFormatLite::TagSize(number, real_type(type));
  1508. switch (real_type(type)) {
  1509. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1510. case WireFormatLite::TYPE_##UPPERCASE: \
  1511. result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \
  1512. break
  1513. HANDLE_TYPE( INT32, Int32, int32_value);
  1514. HANDLE_TYPE( INT64, Int64, int64_value);
  1515. HANDLE_TYPE( UINT32, UInt32, uint32_value);
  1516. HANDLE_TYPE( UINT64, UInt64, uint64_value);
  1517. HANDLE_TYPE( SINT32, SInt32, int32_value);
  1518. HANDLE_TYPE( SINT64, SInt64, int64_value);
  1519. HANDLE_TYPE( STRING, String, *string_value);
  1520. HANDLE_TYPE( BYTES, Bytes, *string_value);
  1521. HANDLE_TYPE( ENUM, Enum, enum_value);
  1522. HANDLE_TYPE( GROUP, Group, *message_value);
  1523. #undef HANDLE_TYPE
  1524. case WireFormatLite::TYPE_MESSAGE: {
  1525. if (is_lazy) {
  1526. size_t size = lazymessage_value->ByteSize();
  1527. result += io::CodedOutputStream::VarintSize32(size) + size;
  1528. } else {
  1529. result += WireFormatLite::MessageSize(*message_value);
  1530. }
  1531. break;
  1532. }
  1533. // Stuff with fixed size.
  1534. #define HANDLE_TYPE(UPPERCASE, CAMELCASE) \
  1535. case WireFormatLite::TYPE_##UPPERCASE: \
  1536. result += WireFormatLite::k##CAMELCASE##Size; \
  1537. break
  1538. HANDLE_TYPE( FIXED32, Fixed32);
  1539. HANDLE_TYPE( FIXED64, Fixed64);
  1540. HANDLE_TYPE(SFIXED32, SFixed32);
  1541. HANDLE_TYPE(SFIXED64, SFixed64);
  1542. HANDLE_TYPE( FLOAT, Float);
  1543. HANDLE_TYPE( DOUBLE, Double);
  1544. HANDLE_TYPE( BOOL, Bool);
  1545. #undef HANDLE_TYPE
  1546. }
  1547. }
  1548. return result;
  1549. }
  1550. int ExtensionSet::Extension::GetSize() const {
  1551. GOOGLE_DCHECK(is_repeated);
  1552. switch (cpp_type(type)) {
  1553. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  1554. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  1555. return repeated_##LOWERCASE##_value->size()
  1556. HANDLE_TYPE( INT32, int32);
  1557. HANDLE_TYPE( INT64, int64);
  1558. HANDLE_TYPE( UINT32, uint32);
  1559. HANDLE_TYPE( UINT64, uint64);
  1560. HANDLE_TYPE( FLOAT, float);
  1561. HANDLE_TYPE( DOUBLE, double);
  1562. HANDLE_TYPE( BOOL, bool);
  1563. HANDLE_TYPE( ENUM, enum);
  1564. HANDLE_TYPE( STRING, string);
  1565. HANDLE_TYPE(MESSAGE, message);
  1566. #undef HANDLE_TYPE
  1567. }
  1568. GOOGLE_LOG(FATAL) << "Can't get here.";
  1569. return 0;
  1570. }
  1571. // This function deletes all allocated objects. This function should be only
  1572. // called if the Extension was created with an arena.
  1573. void ExtensionSet::Extension::Free() {
  1574. if (is_repeated) {
  1575. switch (cpp_type(type)) {
  1576. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  1577. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  1578. delete repeated_##LOWERCASE##_value; \
  1579. break
  1580. HANDLE_TYPE( INT32, int32);
  1581. HANDLE_TYPE( INT64, int64);
  1582. HANDLE_TYPE( UINT32, uint32);
  1583. HANDLE_TYPE( UINT64, uint64);
  1584. HANDLE_TYPE( FLOAT, float);
  1585. HANDLE_TYPE( DOUBLE, double);
  1586. HANDLE_TYPE( BOOL, bool);
  1587. HANDLE_TYPE( ENUM, enum);
  1588. HANDLE_TYPE( STRING, string);
  1589. HANDLE_TYPE(MESSAGE, message);
  1590. #undef HANDLE_TYPE
  1591. }
  1592. } else {
  1593. switch (cpp_type(type)) {
  1594. case WireFormatLite::CPPTYPE_STRING:
  1595. delete string_value;
  1596. break;
  1597. case WireFormatLite::CPPTYPE_MESSAGE:
  1598. if (is_lazy) {
  1599. delete lazymessage_value;
  1600. } else {
  1601. delete message_value;
  1602. }
  1603. break;
  1604. default:
  1605. break;
  1606. }
  1607. }
  1608. }
  1609. // Defined in extension_set_heavy.cc.
  1610. // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const
  1611. bool ExtensionSet::Extension::IsInitialized() const {
  1612. if (cpp_type(type) == WireFormatLite::CPPTYPE_MESSAGE) {
  1613. if (is_repeated) {
  1614. for (int i = 0; i < repeated_message_value->size(); i++) {
  1615. if (!repeated_message_value->Get(i).IsInitialized()) {
  1616. return false;
  1617. }
  1618. }
  1619. } else {
  1620. if (!is_cleared) {
  1621. if (is_lazy) {
  1622. if (!lazymessage_value->IsInitialized()) return false;
  1623. } else {
  1624. if (!message_value->IsInitialized()) return false;
  1625. }
  1626. }
  1627. }
  1628. }
  1629. return true;
  1630. }
  1631. // Dummy key method to avoid weak vtable.
  1632. void ExtensionSet::LazyMessageExtension::UnusedKeyMethod() {}
  1633. const ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) const {
  1634. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1635. return FindOrNullInLargeMap(key);
  1636. }
  1637. const KeyValue* end = flat_end();
  1638. const KeyValue* it =
  1639. std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
  1640. if (it != end && it->first == key) {
  1641. return &it->second;
  1642. }
  1643. return NULL;
  1644. }
  1645. const ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(
  1646. int key) const {
  1647. assert(is_large());
  1648. LargeMap::const_iterator it = map_.large->find(key);
  1649. if (it != map_.large->end()) {
  1650. return &it->second;
  1651. }
  1652. return NULL;
  1653. }
  1654. ExtensionSet::Extension* ExtensionSet::FindOrNull(int key) {
  1655. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1656. return FindOrNullInLargeMap(key);
  1657. }
  1658. KeyValue* end = flat_end();
  1659. KeyValue* it =
  1660. std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
  1661. if (it != end && it->first == key) {
  1662. return &it->second;
  1663. }
  1664. return NULL;
  1665. }
  1666. ExtensionSet::Extension* ExtensionSet::FindOrNullInLargeMap(int key) {
  1667. assert(is_large());
  1668. LargeMap::iterator it = map_.large->find(key);
  1669. if (it != map_.large->end()) {
  1670. return &it->second;
  1671. }
  1672. return NULL;
  1673. }
  1674. std::pair<ExtensionSet::Extension*, bool> ExtensionSet::Insert(int key) {
  1675. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1676. auto maybe = map_.large->insert({key, Extension()});
  1677. return {&maybe.first->second, maybe.second};
  1678. }
  1679. KeyValue* end = flat_end();
  1680. KeyValue* it =
  1681. std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
  1682. if (it != end && it->first == key) {
  1683. return {&it->second, false};
  1684. }
  1685. if (flat_size_ < flat_capacity_) {
  1686. std::copy_backward(it, end, end + 1);
  1687. ++flat_size_;
  1688. it->first = key;
  1689. it->second = Extension();
  1690. return {&it->second, true};
  1691. }
  1692. GrowCapacity(flat_size_ + 1);
  1693. return Insert(key);
  1694. }
  1695. void ExtensionSet::GrowCapacity(size_t minimum_new_capacity) {
  1696. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1697. return; // LargeMap does not have a "reserve" method.
  1698. }
  1699. if (flat_capacity_ >= minimum_new_capacity) {
  1700. return;
  1701. }
  1702. do {
  1703. flat_capacity_ = flat_capacity_ == 0 ? 1 : flat_capacity_ * 4;
  1704. } while (flat_capacity_ < minimum_new_capacity);
  1705. const KeyValue* begin = flat_begin();
  1706. const KeyValue* end = flat_end();
  1707. if (flat_capacity_ > kMaximumFlatCapacity) {
  1708. // Switch to LargeMap
  1709. map_.large = ::google::protobuf::Arena::Create<LargeMap>(arena_);
  1710. LargeMap::iterator hint = map_.large->begin();
  1711. for (const KeyValue* it = begin; it != end; ++it) {
  1712. hint = map_.large->insert(hint, {it->first, it->second});
  1713. }
  1714. flat_size_ = 0;
  1715. } else {
  1716. map_.flat = ::google::protobuf::Arena::CreateArray<KeyValue>(arena_, flat_capacity_);
  1717. std::copy(begin, end, map_.flat);
  1718. }
  1719. if (arena_ == NULL) delete[] begin;
  1720. }
  1721. // static
  1722. constexpr uint16 ExtensionSet::kMaximumFlatCapacity;
  1723. void ExtensionSet::Erase(int key) {
  1724. if (GOOGLE_PREDICT_FALSE(is_large())) {
  1725. map_.large->erase(key);
  1726. return;
  1727. }
  1728. KeyValue* end = flat_end();
  1729. KeyValue* it =
  1730. std::lower_bound(flat_begin(), end, key, KeyValue::FirstComparator());
  1731. if (it != end && it->first == key) {
  1732. std::copy(it + 1, end, it);
  1733. --flat_size_;
  1734. }
  1735. }
  1736. // ==================================================================
  1737. // Default repeated field instances for iterator-compatible accessors
  1738. const RepeatedPrimitiveDefaults* RepeatedPrimitiveDefaults::default_instance() {
  1739. static auto instance = OnShutdownDelete(new RepeatedPrimitiveDefaults);
  1740. return instance;
  1741. }
  1742. const RepeatedStringTypeTraits::RepeatedFieldType*
  1743. RepeatedStringTypeTraits::GetDefaultRepeatedField() {
  1744. static auto instance = OnShutdownDelete(new RepeatedFieldType);
  1745. return instance;
  1746. }
  1747. } // namespace internal
  1748. } // namespace protobuf
  1749. } // namespace google