PageRenderTime 68ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/extension_set.cc

http://github.com/tomahawk-player/tomahawk
C++ | 1464 lines | 1170 code | 162 blank | 132 comment | 186 complexity | abfd54309523c64cda04e50283266534 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/stubs/once.h>
  36. #include <google/protobuf/extension_set.h>
  37. #include <google/protobuf/message_lite.h>
  38. #include <google/protobuf/io/coded_stream.h>
  39. #include <google/protobuf/io/zero_copy_stream_impl.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. // Registry stuff.
  55. typedef hash_map<pair<const MessageLite*, int>,
  56. ExtensionInfo> ExtensionRegistry;
  57. ExtensionRegistry* registry_ = NULL;
  58. GOOGLE_PROTOBUF_DECLARE_ONCE(registry_init_);
  59. void DeleteRegistry() {
  60. delete registry_;
  61. registry_ = NULL;
  62. }
  63. void InitRegistry() {
  64. registry_ = new ExtensionRegistry;
  65. internal::OnShutdown(&DeleteRegistry);
  66. }
  67. // This function is only called at startup, so there is no need for thread-
  68. // safety.
  69. void Register(const MessageLite* containing_type,
  70. int number, ExtensionInfo info) {
  71. ::google::protobuf::GoogleOnceInit(&registry_init_, &InitRegistry);
  72. if (!InsertIfNotPresent(registry_, make_pair(containing_type, number),
  73. info)) {
  74. GOOGLE_LOG(FATAL) << "Multiple extension registrations for type \""
  75. << containing_type->GetTypeName()
  76. << "\", field number " << number << ".";
  77. }
  78. }
  79. const ExtensionInfo* FindRegisteredExtension(
  80. const MessageLite* containing_type, int number) {
  81. return (registry_ == NULL) ? NULL :
  82. FindOrNull(*registry_, make_pair(containing_type, number));
  83. }
  84. } // namespace
  85. ExtensionFinder::~ExtensionFinder() {}
  86. bool GeneratedExtensionFinder::Find(int number, ExtensionInfo* output) {
  87. const ExtensionInfo* extension =
  88. FindRegisteredExtension(containing_type_, number);
  89. if (extension == NULL) {
  90. return false;
  91. } else {
  92. *output = *extension;
  93. return true;
  94. }
  95. }
  96. void ExtensionSet::RegisterExtension(const MessageLite* containing_type,
  97. int number, FieldType type,
  98. bool is_repeated, bool is_packed) {
  99. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_ENUM);
  100. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_MESSAGE);
  101. GOOGLE_CHECK_NE(type, WireFormatLite::TYPE_GROUP);
  102. ExtensionInfo info(type, is_repeated, is_packed);
  103. Register(containing_type, number, info);
  104. }
  105. static bool CallNoArgValidityFunc(const void* arg, int number) {
  106. // Note: Must use C-style cast here rather than reinterpret_cast because
  107. // the C++ standard at one point did not allow casts between function and
  108. // data pointers and some compilers enforce this for C++-style casts. No
  109. // compiler enforces it for C-style casts since lots of C-style code has
  110. // relied on these kinds of casts for a long time, despite being
  111. // technically undefined. See:
  112. // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#195
  113. // Also note: Some compilers do not allow function pointers to be "const".
  114. // Which makes sense, I suppose, because it's meaningless.
  115. return ((EnumValidityFunc*)arg)(number);
  116. }
  117. void ExtensionSet::RegisterEnumExtension(const MessageLite* containing_type,
  118. int number, FieldType type,
  119. bool is_repeated, bool is_packed,
  120. EnumValidityFunc* is_valid) {
  121. GOOGLE_CHECK_EQ(type, WireFormatLite::TYPE_ENUM);
  122. ExtensionInfo info(type, is_repeated, is_packed);
  123. info.enum_validity_check.func = CallNoArgValidityFunc;
  124. // See comment in CallNoArgValidityFunc() about why we use a c-style cast.
  125. info.enum_validity_check.arg = (void*)is_valid;
  126. Register(containing_type, number, info);
  127. }
  128. void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type,
  129. int number, FieldType type,
  130. bool is_repeated, bool is_packed,
  131. const MessageLite* prototype) {
  132. GOOGLE_CHECK(type == WireFormatLite::TYPE_MESSAGE ||
  133. type == WireFormatLite::TYPE_GROUP);
  134. ExtensionInfo info(type, is_repeated, is_packed);
  135. info.message_prototype = prototype;
  136. Register(containing_type, number, info);
  137. }
  138. // ===================================================================
  139. // Constructors and basic methods.
  140. ExtensionSet::ExtensionSet() {}
  141. ExtensionSet::~ExtensionSet() {
  142. for (map<int, Extension>::iterator iter = extensions_.begin();
  143. iter != extensions_.end(); ++iter) {
  144. iter->second.Free();
  145. }
  146. }
  147. // Defined in extension_set_heavy.cc.
  148. // void ExtensionSet::AppendToList(const Descriptor* containing_type,
  149. // const DescriptorPool* pool,
  150. // vector<const FieldDescriptor*>* output) const
  151. bool ExtensionSet::Has(int number) const {
  152. map<int, Extension>::const_iterator iter = extensions_.find(number);
  153. if (iter == extensions_.end()) return false;
  154. GOOGLE_DCHECK(!iter->second.is_repeated);
  155. return !iter->second.is_cleared;
  156. }
  157. int ExtensionSet::ExtensionSize(int number) const {
  158. map<int, Extension>::const_iterator iter = extensions_.find(number);
  159. if (iter == extensions_.end()) return false;
  160. return iter->second.GetSize();
  161. }
  162. FieldType ExtensionSet::ExtensionType(int number) const {
  163. map<int, Extension>::const_iterator iter = extensions_.find(number);
  164. if (iter == extensions_.end()) {
  165. GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (1). ";
  166. return 0;
  167. }
  168. if (iter->second.is_cleared) {
  169. GOOGLE_LOG(DFATAL) << "Don't lookup extension types if they aren't present (2). ";
  170. }
  171. return iter->second.type;
  172. }
  173. void ExtensionSet::ClearExtension(int number) {
  174. map<int, Extension>::iterator iter = extensions_.find(number);
  175. if (iter == extensions_.end()) return;
  176. iter->second.Clear();
  177. }
  178. // ===================================================================
  179. // Field accessors
  180. namespace {
  181. enum Cardinality {
  182. REPEATED,
  183. OPTIONAL
  184. };
  185. } // namespace
  186. #define GOOGLE_DCHECK_TYPE(EXTENSION, LABEL, CPPTYPE) \
  187. GOOGLE_DCHECK_EQ((EXTENSION).is_repeated ? REPEATED : OPTIONAL, LABEL); \
  188. GOOGLE_DCHECK_EQ(cpp_type((EXTENSION).type), WireFormatLite::CPPTYPE_##CPPTYPE)
  189. // -------------------------------------------------------------------
  190. // Primitives
  191. #define PRIMITIVE_ACCESSORS(UPPERCASE, LOWERCASE, CAMELCASE) \
  192. \
  193. LOWERCASE ExtensionSet::Get##CAMELCASE(int number, \
  194. LOWERCASE default_value) const { \
  195. map<int, Extension>::const_iterator iter = extensions_.find(number); \
  196. if (iter == extensions_.end() || iter->second.is_cleared) { \
  197. return default_value; \
  198. } else { \
  199. GOOGLE_DCHECK_TYPE(iter->second, OPTIONAL, UPPERCASE); \
  200. return iter->second.LOWERCASE##_value; \
  201. } \
  202. } \
  203. \
  204. void ExtensionSet::Set##CAMELCASE(int number, FieldType type, \
  205. LOWERCASE value, \
  206. const FieldDescriptor* descriptor) { \
  207. Extension* extension; \
  208. if (MaybeNewExtension(number, descriptor, &extension)) { \
  209. extension->type = type; \
  210. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
  211. extension->is_repeated = false; \
  212. } else { \
  213. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, UPPERCASE); \
  214. } \
  215. extension->is_cleared = false; \
  216. extension->LOWERCASE##_value = value; \
  217. } \
  218. \
  219. LOWERCASE ExtensionSet::GetRepeated##CAMELCASE(int number, int index) const { \
  220. map<int, Extension>::const_iterator iter = extensions_.find(number); \
  221. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty)."; \
  222. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, UPPERCASE); \
  223. return iter->second.repeated_##LOWERCASE##_value->Get(index); \
  224. } \
  225. \
  226. void ExtensionSet::SetRepeated##CAMELCASE( \
  227. int number, int index, LOWERCASE value) { \
  228. map<int, Extension>::iterator iter = extensions_.find(number); \
  229. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty)."; \
  230. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, UPPERCASE); \
  231. iter->second.repeated_##LOWERCASE##_value->Set(index, value); \
  232. } \
  233. \
  234. void ExtensionSet::Add##CAMELCASE(int number, FieldType type, \
  235. bool packed, LOWERCASE value, \
  236. const FieldDescriptor* descriptor) { \
  237. Extension* extension; \
  238. if (MaybeNewExtension(number, descriptor, &extension)) { \
  239. extension->type = type; \
  240. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
  241. extension->is_repeated = true; \
  242. extension->is_packed = packed; \
  243. extension->repeated_##LOWERCASE##_value = new RepeatedField<LOWERCASE>(); \
  244. } else { \
  245. GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
  246. GOOGLE_DCHECK_EQ(extension->is_packed, packed); \
  247. } \
  248. extension->repeated_##LOWERCASE##_value->Add(value); \
  249. }
  250. PRIMITIVE_ACCESSORS( INT32, int32, Int32)
  251. PRIMITIVE_ACCESSORS( INT64, int64, Int64)
  252. PRIMITIVE_ACCESSORS(UINT32, uint32, UInt32)
  253. PRIMITIVE_ACCESSORS(UINT64, uint64, UInt64)
  254. PRIMITIVE_ACCESSORS( FLOAT, float, Float)
  255. PRIMITIVE_ACCESSORS(DOUBLE, double, Double)
  256. PRIMITIVE_ACCESSORS( BOOL, bool, Bool)
  257. #undef PRIMITIVE_ACCESSORS
  258. // -------------------------------------------------------------------
  259. // Enums
  260. int ExtensionSet::GetEnum(int number, int default_value) const {
  261. map<int, Extension>::const_iterator iter = extensions_.find(number);
  262. if (iter == extensions_.end() || iter->second.is_cleared) {
  263. // Not present. Return the default value.
  264. return default_value;
  265. } else {
  266. GOOGLE_DCHECK_TYPE(iter->second, OPTIONAL, ENUM);
  267. return iter->second.enum_value;
  268. }
  269. }
  270. void ExtensionSet::SetEnum(int number, FieldType type, int 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_ENUM);
  276. extension->is_repeated = false;
  277. } else {
  278. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, ENUM);
  279. }
  280. extension->is_cleared = false;
  281. extension->enum_value = value;
  282. }
  283. int ExtensionSet::GetRepeatedEnum(int number, int index) const {
  284. map<int, Extension>::const_iterator iter = extensions_.find(number);
  285. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  286. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, ENUM);
  287. return iter->second.repeated_enum_value->Get(index);
  288. }
  289. void ExtensionSet::SetRepeatedEnum(int number, int index, int value) {
  290. map<int, Extension>::iterator iter = extensions_.find(number);
  291. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  292. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, ENUM);
  293. iter->second.repeated_enum_value->Set(index, value);
  294. }
  295. void ExtensionSet::AddEnum(int number, FieldType type,
  296. bool packed, int value,
  297. const FieldDescriptor* descriptor) {
  298. Extension* extension;
  299. if (MaybeNewExtension(number, descriptor, &extension)) {
  300. extension->type = type;
  301. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
  302. extension->is_repeated = true;
  303. extension->is_packed = packed;
  304. extension->repeated_enum_value = new RepeatedField<int>();
  305. } else {
  306. GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
  307. GOOGLE_DCHECK_EQ(extension->is_packed, packed);
  308. }
  309. extension->repeated_enum_value->Add(value);
  310. }
  311. // -------------------------------------------------------------------
  312. // Strings
  313. const string& ExtensionSet::GetString(int number,
  314. const string& default_value) const {
  315. map<int, Extension>::const_iterator iter = extensions_.find(number);
  316. if (iter == extensions_.end() || iter->second.is_cleared) {
  317. // Not present. Return the default value.
  318. return default_value;
  319. } else {
  320. GOOGLE_DCHECK_TYPE(iter->second, OPTIONAL, STRING);
  321. return *iter->second.string_value;
  322. }
  323. }
  324. string* ExtensionSet::MutableString(int number, FieldType type,
  325. const FieldDescriptor* descriptor) {
  326. Extension* extension;
  327. if (MaybeNewExtension(number, descriptor, &extension)) {
  328. extension->type = type;
  329. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
  330. extension->is_repeated = false;
  331. extension->string_value = new string;
  332. } else {
  333. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
  334. }
  335. extension->is_cleared = false;
  336. return extension->string_value;
  337. }
  338. const string& ExtensionSet::GetRepeatedString(int number, int index) const {
  339. map<int, Extension>::const_iterator iter = extensions_.find(number);
  340. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  341. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, STRING);
  342. return iter->second.repeated_string_value->Get(index);
  343. }
  344. string* ExtensionSet::MutableRepeatedString(int number, int index) {
  345. map<int, Extension>::iterator iter = extensions_.find(number);
  346. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  347. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, STRING);
  348. return iter->second.repeated_string_value->Mutable(index);
  349. }
  350. string* ExtensionSet::AddString(int number, FieldType type,
  351. const FieldDescriptor* descriptor) {
  352. Extension* extension;
  353. if (MaybeNewExtension(number, descriptor, &extension)) {
  354. extension->type = type;
  355. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
  356. extension->is_repeated = true;
  357. extension->is_packed = false;
  358. extension->repeated_string_value = new RepeatedPtrField<string>();
  359. } else {
  360. GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
  361. }
  362. return extension->repeated_string_value->Add();
  363. }
  364. // -------------------------------------------------------------------
  365. // Messages
  366. const MessageLite& ExtensionSet::GetMessage(
  367. int number, const MessageLite& default_value) const {
  368. map<int, Extension>::const_iterator iter = extensions_.find(number);
  369. if (iter == extensions_.end()) {
  370. // Not present. Return the default value.
  371. return default_value;
  372. } else {
  373. GOOGLE_DCHECK_TYPE(iter->second, OPTIONAL, MESSAGE);
  374. return *iter->second.message_value;
  375. }
  376. }
  377. // Defined in extension_set_heavy.cc.
  378. // const MessageLite& ExtensionSet::GetMessage(int number,
  379. // const Descriptor* message_type,
  380. // MessageFactory* factory) const
  381. MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
  382. const MessageLite& prototype,
  383. const FieldDescriptor* descriptor) {
  384. Extension* extension;
  385. if (MaybeNewExtension(number, descriptor, &extension)) {
  386. extension->type = type;
  387. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  388. extension->is_repeated = false;
  389. extension->message_value = prototype.New();
  390. } else {
  391. GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
  392. }
  393. extension->is_cleared = false;
  394. return extension->message_value;
  395. }
  396. // Defined in extension_set_heavy.cc.
  397. // MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
  398. // const Descriptor* message_type,
  399. // MessageFactory* factory)
  400. const MessageLite& ExtensionSet::GetRepeatedMessage(
  401. int number, int index) const {
  402. map<int, Extension>::const_iterator iter = extensions_.find(number);
  403. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  404. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, MESSAGE);
  405. return iter->second.repeated_message_value->Get(index);
  406. }
  407. MessageLite* ExtensionSet::MutableRepeatedMessage(int number, int index) {
  408. map<int, Extension>::iterator iter = extensions_.find(number);
  409. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  410. GOOGLE_DCHECK_TYPE(iter->second, REPEATED, MESSAGE);
  411. return iter->second.repeated_message_value->Mutable(index);
  412. }
  413. MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
  414. const MessageLite& prototype,
  415. const FieldDescriptor* descriptor) {
  416. Extension* extension;
  417. if (MaybeNewExtension(number, descriptor, &extension)) {
  418. extension->type = type;
  419. GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
  420. extension->is_repeated = true;
  421. extension->repeated_message_value =
  422. new RepeatedPtrField<MessageLite>();
  423. } else {
  424. GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
  425. }
  426. // RepeatedPtrField<MessageLite> does not know how to Add() since it cannot
  427. // allocate an abstract object, so we have to be tricky.
  428. MessageLite* result = extension->repeated_message_value
  429. ->AddFromCleared<internal::GenericTypeHandler<MessageLite> >();
  430. if (result == NULL) {
  431. result = prototype.New();
  432. extension->repeated_message_value->AddAllocated(result);
  433. }
  434. return result;
  435. }
  436. // Defined in extension_set_heavy.cc.
  437. // MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
  438. // const Descriptor* message_type,
  439. // MessageFactory* factory)
  440. #undef GOOGLE_DCHECK_TYPE
  441. void ExtensionSet::RemoveLast(int number) {
  442. map<int, Extension>::iterator iter = extensions_.find(number);
  443. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  444. Extension* extension = &iter->second;
  445. GOOGLE_DCHECK(extension->is_repeated);
  446. switch(cpp_type(extension->type)) {
  447. case WireFormatLite::CPPTYPE_INT32:
  448. extension->repeated_int32_value->RemoveLast();
  449. break;
  450. case WireFormatLite::CPPTYPE_INT64:
  451. extension->repeated_int64_value->RemoveLast();
  452. break;
  453. case WireFormatLite::CPPTYPE_UINT32:
  454. extension->repeated_uint32_value->RemoveLast();
  455. break;
  456. case WireFormatLite::CPPTYPE_UINT64:
  457. extension->repeated_uint64_value->RemoveLast();
  458. break;
  459. case WireFormatLite::CPPTYPE_FLOAT:
  460. extension->repeated_float_value->RemoveLast();
  461. break;
  462. case WireFormatLite::CPPTYPE_DOUBLE:
  463. extension->repeated_double_value->RemoveLast();
  464. break;
  465. case WireFormatLite::CPPTYPE_BOOL:
  466. extension->repeated_bool_value->RemoveLast();
  467. break;
  468. case WireFormatLite::CPPTYPE_ENUM:
  469. extension->repeated_enum_value->RemoveLast();
  470. break;
  471. case WireFormatLite::CPPTYPE_STRING:
  472. extension->repeated_string_value->RemoveLast();
  473. break;
  474. case WireFormatLite::CPPTYPE_MESSAGE:
  475. extension->repeated_message_value->RemoveLast();
  476. break;
  477. }
  478. }
  479. void ExtensionSet::SwapElements(int number, int index1, int index2) {
  480. map<int, Extension>::iterator iter = extensions_.find(number);
  481. GOOGLE_CHECK(iter != extensions_.end()) << "Index out-of-bounds (field is empty).";
  482. Extension* extension = &iter->second;
  483. GOOGLE_DCHECK(extension->is_repeated);
  484. switch(cpp_type(extension->type)) {
  485. case WireFormatLite::CPPTYPE_INT32:
  486. extension->repeated_int32_value->SwapElements(index1, index2);
  487. break;
  488. case WireFormatLite::CPPTYPE_INT64:
  489. extension->repeated_int64_value->SwapElements(index1, index2);
  490. break;
  491. case WireFormatLite::CPPTYPE_UINT32:
  492. extension->repeated_uint32_value->SwapElements(index1, index2);
  493. break;
  494. case WireFormatLite::CPPTYPE_UINT64:
  495. extension->repeated_uint64_value->SwapElements(index1, index2);
  496. break;
  497. case WireFormatLite::CPPTYPE_FLOAT:
  498. extension->repeated_float_value->SwapElements(index1, index2);
  499. break;
  500. case WireFormatLite::CPPTYPE_DOUBLE:
  501. extension->repeated_double_value->SwapElements(index1, index2);
  502. break;
  503. case WireFormatLite::CPPTYPE_BOOL:
  504. extension->repeated_bool_value->SwapElements(index1, index2);
  505. break;
  506. case WireFormatLite::CPPTYPE_ENUM:
  507. extension->repeated_enum_value->SwapElements(index1, index2);
  508. break;
  509. case WireFormatLite::CPPTYPE_STRING:
  510. extension->repeated_string_value->SwapElements(index1, index2);
  511. break;
  512. case WireFormatLite::CPPTYPE_MESSAGE:
  513. extension->repeated_message_value->SwapElements(index1, index2);
  514. break;
  515. }
  516. }
  517. // ===================================================================
  518. void ExtensionSet::Clear() {
  519. for (map<int, Extension>::iterator iter = extensions_.begin();
  520. iter != extensions_.end(); ++iter) {
  521. iter->second.Clear();
  522. }
  523. }
  524. void ExtensionSet::MergeFrom(const ExtensionSet& other) {
  525. for (map<int, Extension>::const_iterator iter = other.extensions_.begin();
  526. iter != other.extensions_.end(); ++iter) {
  527. const Extension& other_extension = iter->second;
  528. if (other_extension.is_repeated) {
  529. Extension* extension;
  530. bool is_new = MaybeNewExtension(iter->first, other_extension.descriptor,
  531. &extension);
  532. if (is_new) {
  533. // Extension did not already exist in set.
  534. extension->type = other_extension.type;
  535. extension->is_repeated = true;
  536. } else {
  537. GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
  538. GOOGLE_DCHECK(extension->is_repeated);
  539. }
  540. switch (cpp_type(other_extension.type)) {
  541. #define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \
  542. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  543. if (is_new) { \
  544. extension->repeated_##LOWERCASE##_value = \
  545. new REPEATED_TYPE; \
  546. } \
  547. extension->repeated_##LOWERCASE##_value->MergeFrom( \
  548. *other_extension.repeated_##LOWERCASE##_value); \
  549. break;
  550. HANDLE_TYPE( INT32, int32, RepeatedField < int32>);
  551. HANDLE_TYPE( INT64, int64, RepeatedField < int64>);
  552. HANDLE_TYPE( UINT32, uint32, RepeatedField < uint32>);
  553. HANDLE_TYPE( UINT64, uint64, RepeatedField < uint64>);
  554. HANDLE_TYPE( FLOAT, float, RepeatedField < float>);
  555. HANDLE_TYPE( DOUBLE, double, RepeatedField < double>);
  556. HANDLE_TYPE( BOOL, bool, RepeatedField < bool>);
  557. HANDLE_TYPE( ENUM, enum, RepeatedField < int>);
  558. HANDLE_TYPE( STRING, string, RepeatedPtrField< string>);
  559. #undef HANDLE_TYPE
  560. case WireFormatLite::CPPTYPE_MESSAGE:
  561. if (is_new) {
  562. extension->repeated_message_value =
  563. new RepeatedPtrField<MessageLite>();
  564. }
  565. // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
  566. // it would attempt to allocate new objects.
  567. RepeatedPtrField<MessageLite>* other_repeated_message =
  568. other_extension.repeated_message_value;
  569. for (int i = 0; i < other_repeated_message->size(); i++) {
  570. const MessageLite& other_message = other_repeated_message->Get(i);
  571. MessageLite* target = extension->repeated_message_value
  572. ->AddFromCleared<GenericTypeHandler<MessageLite> >();
  573. if (target == NULL) {
  574. target = other_message.New();
  575. extension->repeated_message_value->AddAllocated(target);
  576. }
  577. target->CheckTypeAndMergeFrom(other_message);
  578. }
  579. break;
  580. }
  581. } else {
  582. if (!other_extension.is_cleared) {
  583. switch (cpp_type(other_extension.type)) {
  584. #define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
  585. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  586. Set##CAMELCASE(iter->first, other_extension.type, \
  587. other_extension.LOWERCASE##_value, \
  588. other_extension.descriptor); \
  589. break;
  590. HANDLE_TYPE( INT32, int32, Int32);
  591. HANDLE_TYPE( INT64, int64, Int64);
  592. HANDLE_TYPE(UINT32, uint32, UInt32);
  593. HANDLE_TYPE(UINT64, uint64, UInt64);
  594. HANDLE_TYPE( FLOAT, float, Float);
  595. HANDLE_TYPE(DOUBLE, double, Double);
  596. HANDLE_TYPE( BOOL, bool, Bool);
  597. HANDLE_TYPE( ENUM, enum, Enum);
  598. #undef HANDLE_TYPE
  599. case WireFormatLite::CPPTYPE_STRING:
  600. SetString(iter->first, other_extension.type,
  601. *other_extension.string_value,
  602. other_extension.descriptor);
  603. break;
  604. case WireFormatLite::CPPTYPE_MESSAGE:
  605. MutableMessage(iter->first, other_extension.type,
  606. *other_extension.message_value,
  607. other_extension.descriptor)
  608. ->CheckTypeAndMergeFrom(*other_extension.message_value);
  609. break;
  610. }
  611. }
  612. }
  613. }
  614. }
  615. void ExtensionSet::Swap(ExtensionSet* x) {
  616. extensions_.swap(x->extensions_);
  617. }
  618. bool ExtensionSet::IsInitialized() const {
  619. // Extensions are never required. However, we need to check that all
  620. // embedded messages are initialized.
  621. for (map<int, Extension>::const_iterator iter = extensions_.begin();
  622. iter != extensions_.end(); ++iter) {
  623. const Extension& extension = iter->second;
  624. if (cpp_type(extension.type) == WireFormatLite::CPPTYPE_MESSAGE) {
  625. if (extension.is_repeated) {
  626. for (int i = 0; i < extension.repeated_message_value->size(); i++) {
  627. if (!extension.repeated_message_value->Get(i).IsInitialized()) {
  628. return false;
  629. }
  630. }
  631. } else {
  632. if (!extension.is_cleared) {
  633. if (!extension.message_value->IsInitialized()) return false;
  634. }
  635. }
  636. }
  637. }
  638. return true;
  639. }
  640. bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  641. ExtensionFinder* extension_finder,
  642. FieldSkipper* field_skipper) {
  643. int number = WireFormatLite::GetTagFieldNumber(tag);
  644. WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
  645. ExtensionInfo extension;
  646. bool is_unknown;
  647. if (!extension_finder->Find(number, &extension)) {
  648. is_unknown = true;
  649. } else if (extension.is_packed) {
  650. is_unknown = (wire_type != WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  651. } else {
  652. WireFormatLite::WireType expected_wire_type =
  653. WireFormatLite::WireTypeForFieldType(real_type(extension.type));
  654. is_unknown = (wire_type != expected_wire_type);
  655. }
  656. if (is_unknown) {
  657. field_skipper->SkipField(input, tag);
  658. } else if (extension.is_packed) {
  659. uint32 size;
  660. if (!input->ReadVarint32(&size)) return false;
  661. io::CodedInputStream::Limit limit = input->PushLimit(size);
  662. switch (extension.type) {
  663. #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
  664. case WireFormatLite::TYPE_##UPPERCASE: \
  665. while (input->BytesUntilLimit() > 0) { \
  666. CPP_LOWERCASE value; \
  667. if (!WireFormatLite::ReadPrimitive< \
  668. CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
  669. input, &value)) return false; \
  670. Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
  671. true, value, extension.descriptor); \
  672. } \
  673. break
  674. HANDLE_TYPE( INT32, Int32, int32);
  675. HANDLE_TYPE( INT64, Int64, int64);
  676. HANDLE_TYPE( UINT32, UInt32, uint32);
  677. HANDLE_TYPE( UINT64, UInt64, uint64);
  678. HANDLE_TYPE( SINT32, Int32, int32);
  679. HANDLE_TYPE( SINT64, Int64, int64);
  680. HANDLE_TYPE( FIXED32, UInt32, uint32);
  681. HANDLE_TYPE( FIXED64, UInt64, uint64);
  682. HANDLE_TYPE(SFIXED32, Int32, int32);
  683. HANDLE_TYPE(SFIXED64, Int64, int64);
  684. HANDLE_TYPE( FLOAT, Float, float);
  685. HANDLE_TYPE( DOUBLE, Double, double);
  686. HANDLE_TYPE( BOOL, Bool, bool);
  687. #undef HANDLE_TYPE
  688. case WireFormatLite::TYPE_ENUM:
  689. while (input->BytesUntilLimit() > 0) {
  690. int value;
  691. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  692. input, &value)) return false;
  693. if (extension.enum_validity_check.func(
  694. extension.enum_validity_check.arg, value)) {
  695. AddEnum(number, WireFormatLite::TYPE_ENUM, true, value,
  696. extension.descriptor);
  697. }
  698. }
  699. break;
  700. case WireFormatLite::TYPE_STRING:
  701. case WireFormatLite::TYPE_BYTES:
  702. case WireFormatLite::TYPE_GROUP:
  703. case WireFormatLite::TYPE_MESSAGE:
  704. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  705. break;
  706. }
  707. input->PopLimit(limit);
  708. } else {
  709. switch (extension.type) {
  710. #define HANDLE_TYPE(UPPERCASE, CPP_CAMELCASE, CPP_LOWERCASE) \
  711. case WireFormatLite::TYPE_##UPPERCASE: { \
  712. CPP_LOWERCASE value; \
  713. if (!WireFormatLite::ReadPrimitive< \
  714. CPP_LOWERCASE, WireFormatLite::TYPE_##UPPERCASE>( \
  715. input, &value)) return false; \
  716. if (extension.is_repeated) { \
  717. Add##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, \
  718. false, value, extension.descriptor); \
  719. } else { \
  720. Set##CPP_CAMELCASE(number, WireFormatLite::TYPE_##UPPERCASE, value, \
  721. extension.descriptor); \
  722. } \
  723. } break
  724. HANDLE_TYPE( INT32, Int32, int32);
  725. HANDLE_TYPE( INT64, Int64, int64);
  726. HANDLE_TYPE( UINT32, UInt32, uint32);
  727. HANDLE_TYPE( UINT64, UInt64, uint64);
  728. HANDLE_TYPE( SINT32, Int32, int32);
  729. HANDLE_TYPE( SINT64, Int64, int64);
  730. HANDLE_TYPE( FIXED32, UInt32, uint32);
  731. HANDLE_TYPE( FIXED64, UInt64, uint64);
  732. HANDLE_TYPE(SFIXED32, Int32, int32);
  733. HANDLE_TYPE(SFIXED64, Int64, int64);
  734. HANDLE_TYPE( FLOAT, Float, float);
  735. HANDLE_TYPE( DOUBLE, Double, double);
  736. HANDLE_TYPE( BOOL, Bool, bool);
  737. #undef HANDLE_TYPE
  738. case WireFormatLite::TYPE_ENUM: {
  739. int value;
  740. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  741. input, &value)) return false;
  742. if (!extension.enum_validity_check.func(
  743. extension.enum_validity_check.arg, value)) {
  744. // Invalid value. Treat as unknown.
  745. field_skipper->SkipUnknownEnum(number, value);
  746. } else if (extension.is_repeated) {
  747. AddEnum(number, WireFormatLite::TYPE_ENUM, false, value,
  748. extension.descriptor);
  749. } else {
  750. SetEnum(number, WireFormatLite::TYPE_ENUM, value,
  751. extension.descriptor);
  752. }
  753. break;
  754. }
  755. case WireFormatLite::TYPE_STRING: {
  756. string* value = extension.is_repeated ?
  757. AddString(number, WireFormatLite::TYPE_STRING, extension.descriptor) :
  758. MutableString(number, WireFormatLite::TYPE_STRING,
  759. extension.descriptor);
  760. if (!WireFormatLite::ReadString(input, value)) return false;
  761. break;
  762. }
  763. case WireFormatLite::TYPE_BYTES: {
  764. string* value = extension.is_repeated ?
  765. AddString(number, WireFormatLite::TYPE_STRING, extension.descriptor) :
  766. MutableString(number, WireFormatLite::TYPE_STRING,
  767. extension.descriptor);
  768. if (!WireFormatLite::ReadBytes(input, value)) return false;
  769. break;
  770. }
  771. case WireFormatLite::TYPE_GROUP: {
  772. MessageLite* value = extension.is_repeated ?
  773. AddMessage(number, WireFormatLite::TYPE_GROUP,
  774. *extension.message_prototype, extension.descriptor) :
  775. MutableMessage(number, WireFormatLite::TYPE_GROUP,
  776. *extension.message_prototype, extension.descriptor);
  777. if (!WireFormatLite::ReadGroup(number, input, value)) return false;
  778. break;
  779. }
  780. case WireFormatLite::TYPE_MESSAGE: {
  781. MessageLite* value = extension.is_repeated ?
  782. AddMessage(number, WireFormatLite::TYPE_MESSAGE,
  783. *extension.message_prototype, extension.descriptor) :
  784. MutableMessage(number, WireFormatLite::TYPE_MESSAGE,
  785. *extension.message_prototype, extension.descriptor);
  786. if (!WireFormatLite::ReadMessage(input, value)) return false;
  787. break;
  788. }
  789. }
  790. }
  791. return true;
  792. }
  793. bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  794. const MessageLite* containing_type) {
  795. FieldSkipper skipper;
  796. GeneratedExtensionFinder finder(containing_type);
  797. return ParseField(tag, input, &finder, &skipper);
  798. }
  799. // Defined in extension_set_heavy.cc.
  800. // bool ExtensionSet::ParseField(uint32 tag, io::CodedInputStream* input,
  801. // const MessageLite* containing_type,
  802. // UnknownFieldSet* unknown_fields)
  803. bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
  804. ExtensionFinder* extension_finder,
  805. FieldSkipper* field_skipper) {
  806. while (true) {
  807. uint32 tag = input->ReadTag();
  808. switch (tag) {
  809. case 0:
  810. return true;
  811. case WireFormatLite::kMessageSetItemStartTag:
  812. if (!ParseMessageSetItem(input, extension_finder, field_skipper)) {
  813. return false;
  814. }
  815. break;
  816. default:
  817. if (!ParseField(tag, input, extension_finder, field_skipper)) {
  818. return false;
  819. }
  820. break;
  821. }
  822. }
  823. }
  824. bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
  825. const MessageLite* containing_type) {
  826. FieldSkipper skipper;
  827. GeneratedExtensionFinder finder(containing_type);
  828. return ParseMessageSet(input, &finder, &skipper);
  829. }
  830. // Defined in extension_set_heavy.cc.
  831. // bool ExtensionSet::ParseMessageSet(io::CodedInputStream* input,
  832. // const MessageLite* containing_type,
  833. // UnknownFieldSet* unknown_fields);
  834. bool ExtensionSet::ParseMessageSetItem(io::CodedInputStream* input,
  835. ExtensionFinder* extension_finder,
  836. FieldSkipper* field_skipper) {
  837. // TODO(kenton): It would be nice to share code between this and
  838. // WireFormatLite::ParseAndMergeMessageSetItem(), but I think the
  839. // differences would be hard to factor out.
  840. // This method parses a group which should contain two fields:
  841. // required int32 type_id = 2;
  842. // required data message = 3;
  843. // Once we see a type_id, we'll construct a fake tag for this extension
  844. // which is the tag it would have had under the proto2 extensions wire
  845. // format.
  846. uint32 fake_tag = 0;
  847. // If we see message data before the type_id, we'll append it to this so
  848. // we can parse it later. This will probably never happen in practice,
  849. // as no MessageSet encoder I know of writes the message before the type ID.
  850. // But, it's technically valid so we should allow it.
  851. // TODO(kenton): Use a Cord instead? Do I care?
  852. string message_data;
  853. while (true) {
  854. uint32 tag = input->ReadTag();
  855. if (tag == 0) return false;
  856. switch (tag) {
  857. case WireFormatLite::kMessageSetTypeIdTag: {
  858. uint32 type_id;
  859. if (!input->ReadVarint32(&type_id)) return false;
  860. fake_tag = WireFormatLite::MakeTag(type_id,
  861. WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  862. if (!message_data.empty()) {
  863. // We saw some message data before the type_id. Have to parse it
  864. // now.
  865. io::CodedInputStream sub_input(
  866. reinterpret_cast<const uint8*>(message_data.data()),
  867. message_data.size());
  868. if (!ParseField(fake_tag, &sub_input,
  869. extension_finder, field_skipper)) {
  870. return false;
  871. }
  872. message_data.clear();
  873. }
  874. break;
  875. }
  876. case WireFormatLite::kMessageSetMessageTag: {
  877. if (fake_tag == 0) {
  878. // We haven't seen a type_id yet. Append this data to message_data.
  879. string temp;
  880. uint32 length;
  881. if (!input->ReadVarint32(&length)) return false;
  882. if (!input->ReadString(&temp, length)) return false;
  883. message_data.append(temp);
  884. } else {
  885. // Already saw type_id, so we can parse this directly.
  886. if (!ParseField(fake_tag, input,
  887. extension_finder, field_skipper)) {
  888. return false;
  889. }
  890. }
  891. break;
  892. }
  893. case WireFormatLite::kMessageSetItemEndTag: {
  894. return true;
  895. }
  896. default: {
  897. if (!field_skipper->SkipField(input, tag)) return false;
  898. }
  899. }
  900. }
  901. }
  902. void ExtensionSet::SerializeWithCachedSizes(
  903. int start_field_number, int end_field_number,
  904. io::CodedOutputStream* output) const {
  905. map<int, Extension>::const_iterator iter;
  906. for (iter = extensions_.lower_bound(start_field_number);
  907. iter != extensions_.end() && iter->first < end_field_number;
  908. ++iter) {
  909. iter->second.SerializeFieldWithCachedSizes(iter->first, output);
  910. }
  911. }
  912. void ExtensionSet::SerializeMessageSetWithCachedSizes(
  913. io::CodedOutputStream* output) const {
  914. map<int, Extension>::const_iterator iter;
  915. for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
  916. iter->second.SerializeMessageSetItemWithCachedSizes(iter->first, output);
  917. }
  918. }
  919. int ExtensionSet::ByteSize() const {
  920. int total_size = 0;
  921. for (map<int, Extension>::const_iterator iter = extensions_.begin();
  922. iter != extensions_.end(); ++iter) {
  923. total_size += iter->second.ByteSize(iter->first);
  924. }
  925. return total_size;
  926. }
  927. int ExtensionSet::MessageSetByteSize() const {
  928. int total_size = 0;
  929. for (map<int, Extension>::const_iterator iter = extensions_.begin();
  930. iter != extensions_.end(); ++iter) {
  931. total_size += iter->second.MessageSetItemByteSize(iter->first);
  932. }
  933. return total_size;
  934. }
  935. // Defined in extension_set_heavy.cc.
  936. // int ExtensionSet::SpaceUsedExcludingSelf() const
  937. bool ExtensionSet::MaybeNewExtension(int number,
  938. const FieldDescriptor* descriptor,
  939. Extension** result) {
  940. pair<map<int, Extension>::iterator, bool> insert_result =
  941. extensions_.insert(make_pair(number, Extension()));
  942. *result = &insert_result.first->second;
  943. (*result)->descriptor = descriptor;
  944. return insert_result.second;
  945. }
  946. // ===================================================================
  947. // Methods of ExtensionSet::Extension
  948. void ExtensionSet::Extension::Clear() {
  949. if (is_repeated) {
  950. switch (cpp_type(type)) {
  951. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  952. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  953. repeated_##LOWERCASE##_value->Clear(); \
  954. break
  955. HANDLE_TYPE( INT32, int32);
  956. HANDLE_TYPE( INT64, int64);
  957. HANDLE_TYPE( UINT32, uint32);
  958. HANDLE_TYPE( UINT64, uint64);
  959. HANDLE_TYPE( FLOAT, float);
  960. HANDLE_TYPE( DOUBLE, double);
  961. HANDLE_TYPE( BOOL, bool);
  962. HANDLE_TYPE( ENUM, enum);
  963. HANDLE_TYPE( STRING, string);
  964. HANDLE_TYPE(MESSAGE, message);
  965. #undef HANDLE_TYPE
  966. }
  967. } else {
  968. if (!is_cleared) {
  969. switch (cpp_type(type)) {
  970. case WireFormatLite::CPPTYPE_STRING:
  971. string_value->clear();
  972. break;
  973. case WireFormatLite::CPPTYPE_MESSAGE:
  974. message_value->Clear();
  975. break;
  976. default:
  977. // No need to do anything. Get*() will return the default value
  978. // as long as is_cleared is true and Set*() will overwrite the
  979. // previous value.
  980. break;
  981. }
  982. is_cleared = true;
  983. }
  984. }
  985. }
  986. void ExtensionSet::Extension::SerializeFieldWithCachedSizes(
  987. int number,
  988. io::CodedOutputStream* output) const {
  989. if (is_repeated) {
  990. if (is_packed) {
  991. if (cached_size == 0) return;
  992. WireFormatLite::WriteTag(number,
  993. WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
  994. output->WriteVarint32(cached_size);
  995. switch (real_type(type)) {
  996. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  997. case WireFormatLite::TYPE_##UPPERCASE: \
  998. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  999. WireFormatLite::Write##CAMELCASE##NoTag( \
  1000. repeated_##LOWERCASE##_value->Get(i), output); \
  1001. } \
  1002. break
  1003. HANDLE_TYPE( INT32, Int32, int32);
  1004. HANDLE_TYPE( INT64, Int64, int64);
  1005. HANDLE_TYPE( UINT32, UInt32, uint32);
  1006. HANDLE_TYPE( UINT64, UInt64, uint64);
  1007. HANDLE_TYPE( SINT32, SInt32, int32);
  1008. HANDLE_TYPE( SINT64, SInt64, int64);
  1009. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1010. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1011. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1012. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1013. HANDLE_TYPE( FLOAT, Float, float);
  1014. HANDLE_TYPE( DOUBLE, Double, double);
  1015. HANDLE_TYPE( BOOL, Bool, bool);
  1016. HANDLE_TYPE( ENUM, Enum, enum);
  1017. #undef HANDLE_TYPE
  1018. case WireFormatLite::TYPE_STRING:
  1019. case WireFormatLite::TYPE_BYTES:
  1020. case WireFormatLite::TYPE_GROUP:
  1021. case WireFormatLite::TYPE_MESSAGE:
  1022. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  1023. break;
  1024. }
  1025. } else {
  1026. switch (real_type(type)) {
  1027. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1028. case WireFormatLite::TYPE_##UPPERCASE: \
  1029. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1030. WireFormatLite::Write##CAMELCASE(number, \
  1031. repeated_##LOWERCASE##_value->Get(i), output); \
  1032. } \
  1033. break
  1034. HANDLE_TYPE( INT32, Int32, int32);
  1035. HANDLE_TYPE( INT64, Int64, int64);
  1036. HANDLE_TYPE( UINT32, UInt32, uint32);
  1037. HANDLE_TYPE( UINT64, UInt64, uint64);
  1038. HANDLE_TYPE( SINT32, SInt32, int32);
  1039. HANDLE_TYPE( SINT64, SInt64, int64);
  1040. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1041. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1042. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1043. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1044. HANDLE_TYPE( FLOAT, Float, float);
  1045. HANDLE_TYPE( DOUBLE, Double, double);
  1046. HANDLE_TYPE( BOOL, Bool, bool);
  1047. HANDLE_TYPE( STRING, String, string);
  1048. HANDLE_TYPE( BYTES, Bytes, string);
  1049. HANDLE_TYPE( ENUM, Enum, enum);
  1050. HANDLE_TYPE( GROUP, Group, message);
  1051. HANDLE_TYPE( MESSAGE, Message, message);
  1052. #undef HANDLE_TYPE
  1053. }
  1054. }
  1055. } else if (!is_cleared) {
  1056. switch (real_type(type)) {
  1057. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, VALUE) \
  1058. case WireFormatLite::TYPE_##UPPERCASE: \
  1059. WireFormatLite::Write##CAMELCASE(number, VALUE, output); \
  1060. break
  1061. HANDLE_TYPE( INT32, Int32, int32_value);
  1062. HANDLE_TYPE( INT64, Int64, int64_value);
  1063. HANDLE_TYPE( UINT32, UInt32, uint32_value);
  1064. HANDLE_TYPE( UINT64, UInt64, uint64_value);
  1065. HANDLE_TYPE( SINT32, SInt32, int32_value);
  1066. HANDLE_TYPE( SINT64, SInt64, int64_value);
  1067. HANDLE_TYPE( FIXED32, Fixed32, uint32_value);
  1068. HANDLE_TYPE( FIXED64, Fixed64, uint64_value);
  1069. HANDLE_TYPE(SFIXED32, SFixed32, int32_value);
  1070. HANDLE_TYPE(SFIXED64, SFixed64, int64_value);
  1071. HANDLE_TYPE( FLOAT, Float, float_value);
  1072. HANDLE_TYPE( DOUBLE, Double, double_value);
  1073. HANDLE_TYPE( BOOL, Bool, bool_value);
  1074. HANDLE_TYPE( STRING, String, *string_value);
  1075. HANDLE_TYPE( BYTES, Bytes, *string_value);
  1076. HANDLE_TYPE( ENUM, Enum, enum_value);
  1077. HANDLE_TYPE( GROUP, Group, *message_value);
  1078. HANDLE_TYPE( MESSAGE, Message, *message_value);
  1079. #undef HANDLE_TYPE
  1080. }
  1081. }
  1082. }
  1083. void ExtensionSet::Extension::SerializeMessageSetItemWithCachedSizes(
  1084. int number,
  1085. io::CodedOutputStream* output) const {
  1086. if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) {
  1087. // Not a valid MessageSet extension, but serialize it the normal way.
  1088. SerializeFieldWithCachedSizes(number, output);
  1089. return;
  1090. }
  1091. if (is_cleared) return;
  1092. // Start group.
  1093. output->WriteTag(WireFormatLite::kMessageSetItemStartTag);
  1094. // Write type ID.
  1095. WireFormatLite::WriteUInt32(WireFormatLite::kMessageSetTypeIdNumber,
  1096. number,
  1097. output);
  1098. // Write message.
  1099. WireFormatLite::WriteMessageMaybeToArray(
  1100. WireFormatLite::kMessageSetMessageNumber,
  1101. *message_value,
  1102. output);
  1103. // End group.
  1104. output->WriteTag(WireFormatLite::kMessageSetItemEndTag);
  1105. }
  1106. int ExtensionSet::Extension::ByteSize(int number) const {
  1107. int result = 0;
  1108. if (is_repeated) {
  1109. if (is_packed) {
  1110. switch (real_type(type)) {
  1111. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1112. case WireFormatLite::TYPE_##UPPERCASE: \
  1113. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1114. result += WireFormatLite::CAMELCASE##Size( \
  1115. repeated_##LOWERCASE##_value->Get(i)); \
  1116. } \
  1117. break
  1118. HANDLE_TYPE( INT32, Int32, int32);
  1119. HANDLE_TYPE( INT64, Int64, int64);
  1120. HANDLE_TYPE( UINT32, UInt32, uint32);
  1121. HANDLE_TYPE( UINT64, UInt64, uint64);
  1122. HANDLE_TYPE( SINT32, SInt32, int32);
  1123. HANDLE_TYPE( SINT64, SInt64, int64);
  1124. HANDLE_TYPE( ENUM, Enum, enum);
  1125. #undef HANDLE_TYPE
  1126. // Stuff with fixed size.
  1127. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1128. case WireFormatLite::TYPE_##UPPERCASE: \
  1129. result += WireFormatLite::k##CAMELCASE##Size * \
  1130. repeated_##LOWERCASE##_value->size(); \
  1131. break
  1132. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1133. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1134. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1135. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1136. HANDLE_TYPE( FLOAT, Float, float);
  1137. HANDLE_TYPE( DOUBLE, Double, double);
  1138. HANDLE_TYPE( BOOL, Bool, bool);
  1139. #undef HANDLE_TYPE
  1140. case WireFormatLite::TYPE_STRING:
  1141. case WireFormatLite::TYPE_BYTES:
  1142. case WireFormatLite::TYPE_GROUP:
  1143. case WireFormatLite::TYPE_MESSAGE:
  1144. GOOGLE_LOG(FATAL) << "Non-primitive types can't be packed.";
  1145. break;
  1146. }
  1147. cached_size = result;
  1148. if (result > 0) {
  1149. result += io::CodedOutputStream::VarintSize32(result);
  1150. result += io::CodedOutputStream::VarintSize32(
  1151. WireFormatLite::MakeTag(number,
  1152. WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
  1153. }
  1154. } else {
  1155. int tag_size = WireFormatLite::TagSize(number, real_type(type));
  1156. switch (real_type(type)) {
  1157. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1158. case WireFormatLite::TYPE_##UPPERCASE: \
  1159. result += tag_size * repeated_##LOWERCASE##_value->size(); \
  1160. for (int i = 0; i < repeated_##LOWERCASE##_value->size(); i++) { \
  1161. result += WireFormatLite::CAMELCASE##Size( \
  1162. repeated_##LOWERCASE##_value->Get(i)); \
  1163. } \
  1164. break
  1165. HANDLE_TYPE( INT32, Int32, int32);
  1166. HANDLE_TYPE( INT64, Int64, int64);
  1167. HANDLE_TYPE( UINT32, UInt32, uint32);
  1168. HANDLE_TYPE( UINT64, UInt64, uint64);
  1169. HANDLE_TYPE( SINT32, SInt32, int32);
  1170. HANDLE_TYPE( SINT64, SInt64, int64);
  1171. HANDLE_TYPE( STRING, String, string);
  1172. HANDLE_TYPE( BYTES, Bytes, string);
  1173. HANDLE_TYPE( ENUM, Enum, enum);
  1174. HANDLE_TYPE( GROUP, Group, message);
  1175. HANDLE_TYPE( MESSAGE, Message, message);
  1176. #undef HANDLE_TYPE
  1177. // Stuff with fixed size.
  1178. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1179. case WireFormatLite::TYPE_##UPPERCASE: \
  1180. result += (tag_size + WireFormatLite::k##CAMELCASE##Size) * \
  1181. repeated_##LOWERCASE##_value->size(); \
  1182. break
  1183. HANDLE_TYPE( FIXED32, Fixed32, uint32);
  1184. HANDLE_TYPE( FIXED64, Fixed64, uint64);
  1185. HANDLE_TYPE(SFIXED32, SFixed32, int32);
  1186. HANDLE_TYPE(SFIXED64, SFixed64, int64);
  1187. HANDLE_TYPE( FLOAT, Float, float);
  1188. HANDLE_TYPE( DOUBLE, Double, double);
  1189. HANDLE_TYPE( BOOL, Bool, bool);
  1190. #undef HANDLE_TYPE
  1191. }
  1192. }
  1193. } else if (!is_cleared) {
  1194. result += WireFormatLite::TagSize(number, real_type(type));
  1195. switch (real_type(type)) {
  1196. #define HANDLE_TYPE(UPPERCASE, CAMELCASE, LOWERCASE) \
  1197. case WireFormatLite::TYPE_##UPPERCASE: \
  1198. result += WireFormatLite::CAMELCASE##Size(LOWERCASE); \
  1199. break
  1200. HANDLE_TYPE( INT32, Int32, int32_value);
  1201. HANDLE_TYPE( INT64, Int64, int64_value);
  1202. HANDLE_TYPE( UINT32, UInt32, uint32_value);
  1203. HANDLE_TYPE( UINT64, UInt64, uint64_value);
  1204. HANDLE_TYPE( SINT32, SInt32, int32_value);
  1205. HANDLE_TYPE( SINT64, SInt64, int64_value);
  1206. HANDLE_TYPE( STRING, String, *string_value);
  1207. HANDLE_TYPE( BYTES, Bytes, *string_value);
  1208. HANDLE_TYPE( ENUM, Enum, enum_value);
  1209. HANDLE_TYPE( GROUP, Group, *message_value);
  1210. HANDLE_TYPE( MESSAGE, Message, *message_value);
  1211. #undef HANDLE_TYPE
  1212. // Stuff with fixed size.
  1213. #define HANDLE_TYPE(UPPERCASE, CAMELCASE) \
  1214. case WireFormatLite::TYPE_##UPPERCASE: \
  1215. result += WireFormatLite::k##CAMELCASE##Size; \
  1216. break
  1217. HANDLE_TYPE( FIXED32, Fixed32);
  1218. HANDLE_TYPE( FIXED64, Fixed64);
  1219. HANDLE_TYPE(SFIXED32, SFixed32);
  1220. HANDLE_TYPE(SFIXED64, SFixed64);
  1221. HANDLE_TYPE( FLOAT, Float);
  1222. HANDLE_TYPE( DOUBLE, Double);
  1223. HANDLE_TYPE( BOOL, Bool);
  1224. #undef HANDLE_TYPE
  1225. }
  1226. }
  1227. return result;
  1228. }
  1229. int ExtensionSet::Extension::MessageSetItemByteSize(int number) const {
  1230. if (type != WireFormatLite::TYPE_MESSAGE || is_repeated) {
  1231. // Not a valid MessageSet extension, but compute the byte size for it the
  1232. // normal way.
  1233. return ByteSize(number);
  1234. }
  1235. if (is_cleared) return 0;
  1236. int our_size = WireFormatLite::kMessageSetItemTagsSize;
  1237. // type_id
  1238. our_size += io::CodedOutputStream::VarintSize32(number);
  1239. // message
  1240. int message_size = message_value->ByteSize();
  1241. our_size += io::CodedOutputStream::VarintSize32(message_size);
  1242. our_size += message_size;
  1243. return our_size;
  1244. }
  1245. int ExtensionSet::Extension::GetSize() const {
  1246. GOOGLE_DCHECK(is_repeated);
  1247. switch (cpp_type(type)) {
  1248. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  1249. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  1250. return repeated_##LOWERCASE##_value->size()
  1251. HANDLE_TYPE( INT32, int32);
  1252. HANDLE_TYPE( INT64, int64);
  1253. HANDLE_TYPE( UINT32, uint32);
  1254. HANDLE_TYPE( UINT64, uint64);
  1255. HANDLE_TYPE( FLOAT, float);
  1256. HANDLE_TYPE( DOUBLE, double);
  1257. HANDLE_TYPE( BOOL, bool);
  1258. HANDLE_TYPE( ENUM, enum);
  1259. HANDLE_TYPE( STRING, string);
  1260. HANDLE_TYPE(MESSAGE, message);
  1261. #undef HANDLE_TYPE
  1262. }
  1263. GOOGLE_LOG(FATAL) << "Can't get here.";
  1264. return 0;
  1265. }
  1266. void ExtensionSet::Extension::Free() {
  1267. if (is_repeated) {
  1268. switch (cpp_type(type)) {
  1269. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  1270. case WireFormatLite::CPPTYPE_##UPPERCASE: \
  1271. delete repeated_##LOWERCASE##_value; \
  1272. break
  1273. HANDLE_TYPE( INT32, int32);
  1274. HANDLE_TYPE( INT64, int64);
  1275. HANDLE_TYPE( UINT32, uint32);
  1276. HANDLE_TYPE( UINT64, uint64);
  1277. HANDLE_TYPE( FLOAT, float);
  1278. HANDLE_TYPE( DOUBLE, double);
  1279. HANDLE_TYPE( BOOL, bool);
  1280. HANDLE_TYPE( ENUM, enum);
  1281. HANDLE_TYPE( STRING, string);
  1282. HANDLE_TYPE(MESSAGE, message);
  1283. #undef HANDLE_TYPE
  1284. }
  1285. } else {
  1286. switch (cpp_type(type)) {
  1287. case WireFormatLite::CPPTYPE_STRING:
  1288. delete string_value;
  1289. break;
  1290. case WireFormatLite::CPPTYPE_MESSAGE:
  1291. delete message_value;
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. }
  1297. }
  1298. // Defined in extension_set_heavy.cc.
  1299. // int ExtensionSet::Extension::SpaceUsedExcludingSelf() const
  1300. } // namespace internal
  1301. } // namespace protobuf
  1302. } // namespace google