cpp_map_field.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <google/protobuf/compiler/cpp/cpp_map_field.h>
  31. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  32. #include <google/protobuf/io/printer.h>
  33. #include <google/protobuf/stubs/strutil.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace compiler {
  37. namespace cpp {
  38. bool IsProto3Field(const FieldDescriptor* field_descriptor) {
  39. const FileDescriptor* file_descriptor = field_descriptor->file();
  40. return file_descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
  41. }
  42. void SetMessageVariables(const FieldDescriptor* descriptor,
  43. map<string, string>* variables,
  44. const Options& options) {
  45. SetCommonFieldVariables(descriptor, variables, options);
  46. (*variables)["type"] = FieldMessageTypeName(descriptor);
  47. (*variables)["stream_writer"] = (*variables)["declared_type"] +
  48. (HasFastArraySerialization(descriptor->message_type()->file()) ?
  49. "MaybeToArray" :
  50. "");
  51. (*variables)["full_name"] = descriptor->full_name();
  52. const FieldDescriptor* key =
  53. descriptor->message_type()->FindFieldByName("key");
  54. const FieldDescriptor* val =
  55. descriptor->message_type()->FindFieldByName("value");
  56. (*variables)["key_cpp"] = PrimitiveTypeName(key->cpp_type());
  57. switch (val->cpp_type()) {
  58. case FieldDescriptor::CPPTYPE_MESSAGE:
  59. (*variables)["val_cpp"] = FieldMessageTypeName(val);
  60. (*variables)["wrapper"] = "EntryWrapper";
  61. break;
  62. case FieldDescriptor::CPPTYPE_ENUM:
  63. (*variables)["val_cpp"] = ClassName(val->enum_type(), false);
  64. (*variables)["wrapper"] = "EnumEntryWrapper";
  65. break;
  66. default:
  67. (*variables)["val_cpp"] = PrimitiveTypeName(val->cpp_type());
  68. (*variables)["wrapper"] = "EntryWrapper";
  69. }
  70. (*variables)["key_type"] =
  71. "::google::protobuf::FieldDescriptor::TYPE_" +
  72. ToUpper(DeclaredTypeMethodName(key->type()));
  73. (*variables)["val_type"] =
  74. "::google::protobuf::FieldDescriptor::TYPE_" +
  75. ToUpper(DeclaredTypeMethodName(val->type()));
  76. (*variables)["map_classname"] = ClassName(descriptor->message_type(), false);
  77. (*variables)["number"] = Int32ToString(descriptor->number());
  78. if (!IsProto3Field(descriptor) &&
  79. val->type() == FieldDescriptor::TYPE_ENUM) {
  80. const EnumValueDescriptor* default_value = val->default_value_enum();
  81. (*variables)["default_enum_value"] = Int32ToString(default_value->number());
  82. } else {
  83. (*variables)["default_enum_value"] = "0";
  84. }
  85. }
  86. MapFieldGenerator::
  87. MapFieldGenerator(const FieldDescriptor* descriptor,
  88. const Options& options)
  89. : descriptor_(descriptor) {
  90. SetMessageVariables(descriptor, &variables_, options);
  91. }
  92. MapFieldGenerator::~MapFieldGenerator() {}
  93. void MapFieldGenerator::
  94. GeneratePrivateMembers(io::Printer* printer) const {
  95. printer->Print(variables_,
  96. "typedef ::google::protobuf::internal::MapEntry<\n"
  97. " $key_cpp$, $val_cpp$,\n"
  98. " $key_type$,\n"
  99. " $val_type$, $default_enum_value$>\n"
  100. " $map_classname$;\n"
  101. "::google::protobuf::internal::MapField< $key_cpp$, $val_cpp$,"
  102. "$key_type$, $val_type$, $default_enum_value$ > $name$_;\n");
  103. }
  104. void MapFieldGenerator::
  105. GenerateAccessorDeclarations(io::Printer* printer) const {
  106. printer->Print(variables_,
  107. "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n"
  108. " $name$() const$deprecation$;\n"
  109. "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n"
  110. " mutable_$name$()$deprecation$;\n");
  111. }
  112. void MapFieldGenerator::
  113. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  114. printer->Print(variables_,
  115. "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n"
  116. "$classname$::$name$() const {\n"
  117. " // @@protoc_insertion_point(field_map:$full_name$)\n"
  118. " return $name$_.GetMap();\n"
  119. "}\n"
  120. "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n"
  121. "$classname$::mutable_$name$() {\n"
  122. " // @@protoc_insertion_point(field_mutable_map:$full_name$)\n"
  123. " return $name$_.MutableMap();\n"
  124. "}\n");
  125. }
  126. void MapFieldGenerator::
  127. GenerateClearingCode(io::Printer* printer) const {
  128. printer->Print(variables_, "$name$_.Clear();\n");
  129. }
  130. void MapFieldGenerator::
  131. GenerateMergingCode(io::Printer* printer) const {
  132. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  133. }
  134. void MapFieldGenerator::
  135. GenerateSwappingCode(io::Printer* printer) const {
  136. printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
  137. }
  138. void MapFieldGenerator::
  139. GenerateConstructorCode(io::Printer* printer) const {
  140. if (HasDescriptorMethods(descriptor_->file())) {
  141. printer->Print(variables_,
  142. "$name$_.SetAssignDescriptorCallback(\n"
  143. " protobuf_AssignDescriptorsOnce);\n"
  144. "$name$_.SetEntryDescriptor(\n"
  145. " &$type$_descriptor_);\n");
  146. }
  147. }
  148. void MapFieldGenerator::
  149. GenerateMergeFromCodedStream(io::Printer* printer) const {
  150. const FieldDescriptor* value_field =
  151. descriptor_->message_type()->FindFieldByName("value");
  152. printer->Print(variables_,
  153. "::google::protobuf::scoped_ptr<$map_classname$> entry($name$_.NewEntry());\n");
  154. if (IsProto3Field(descriptor_) ||
  155. value_field->type() != FieldDescriptor::TYPE_ENUM) {
  156. printer->Print(variables_,
  157. "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n"
  158. " input, entry.get()));\n");
  159. switch (value_field->cpp_type()) {
  160. case FieldDescriptor::CPPTYPE_MESSAGE:
  161. printer->Print(variables_,
  162. "(*mutable_$name$())[entry->key()].Swap("
  163. "entry->mutable_value());\n");
  164. break;
  165. case FieldDescriptor::CPPTYPE_ENUM:
  166. printer->Print(variables_,
  167. "(*mutable_$name$())[entry->key()] =\n"
  168. " static_cast<$val_cpp$>(*entry->mutable_value());\n");
  169. break;
  170. default:
  171. printer->Print(variables_,
  172. "(*mutable_$name$())[entry->key()] = *entry->mutable_value();\n");
  173. break;
  174. }
  175. } else {
  176. printer->Print(variables_,
  177. "{\n"
  178. " ::std::string data;\n"
  179. " DO_(::google::protobuf::internal::WireFormatLite::ReadString(input, &data));\n"
  180. " DO_(entry->ParseFromString(data));\n"
  181. " if ($val_cpp$_IsValid(*entry->mutable_value())) {\n"
  182. " (*mutable_$name$())[entry->key()] =\n"
  183. " static_cast<$val_cpp$>(*entry->mutable_value());\n"
  184. " } else {\n"
  185. " mutable_unknown_fields()->AddLengthDelimited($number$, data);\n"
  186. " }\n"
  187. "}\n");
  188. }
  189. }
  190. void MapFieldGenerator::
  191. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  192. printer->Print(variables_,
  193. "{\n"
  194. " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
  195. " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
  196. " it = $name$().begin(); it != $name$().end(); ++it) {\n"
  197. " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
  198. " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
  199. " $number$, *entry, output);\n"
  200. " }\n"
  201. "}\n");
  202. }
  203. void MapFieldGenerator::
  204. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  205. printer->Print(variables_,
  206. "{\n"
  207. " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
  208. " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
  209. " it = $name$().begin(); it != $name$().end(); ++it) {\n"
  210. " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
  211. " target = ::google::protobuf::internal::WireFormatLite::\n"
  212. " Write$declared_type$NoVirtualToArray(\n"
  213. " $number$, *entry, target);\n"
  214. " }\n"
  215. "}\n");
  216. }
  217. void MapFieldGenerator::
  218. GenerateByteSize(io::Printer* printer) const {
  219. printer->Print(variables_,
  220. "total_size += $tag_size$ * this->$name$_size();\n"
  221. "{\n"
  222. " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
  223. " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
  224. " it = $name$().begin(); it != $name$().end(); ++it) {\n"
  225. " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
  226. " total_size += ::google::protobuf::internal::WireFormatLite::\n"
  227. " $declared_type$SizeNoVirtual(*entry);\n"
  228. " }\n"
  229. "}\n");
  230. }
  231. } // namespace cpp
  232. } // namespace compiler
  233. } // namespace protobuf
  234. } // namespace google