/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/cpp/cpp_message_field.cc

http://github.com/tomahawk-player/tomahawk · C++ · 277 lines · 207 code · 35 blank · 35 comment · 6 complexity · e99750caca0ec6ee11281ba8c22e1e18 MD5 · raw file

  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/compiler/cpp/cpp_message_field.h>
  34. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace cpp {
  41. namespace {
  42. void SetMessageVariables(const FieldDescriptor* descriptor,
  43. map<string, string>* variables) {
  44. SetCommonFieldVariables(descriptor, variables);
  45. (*variables)["type"] = FieldMessageTypeName(descriptor);
  46. (*variables)["stream_writer"] = (*variables)["declared_type"] +
  47. (HasFastArraySerialization(descriptor->message_type()->file()) ?
  48. "MaybeToArray" :
  49. "");
  50. }
  51. } // namespace
  52. // ===================================================================
  53. MessageFieldGenerator::
  54. MessageFieldGenerator(const FieldDescriptor* descriptor)
  55. : descriptor_(descriptor) {
  56. SetMessageVariables(descriptor, &variables_);
  57. }
  58. MessageFieldGenerator::~MessageFieldGenerator() {}
  59. void MessageFieldGenerator::
  60. GeneratePrivateMembers(io::Printer* printer) const {
  61. printer->Print(variables_, "$type$* $name$_;\n");
  62. }
  63. void MessageFieldGenerator::
  64. GenerateAccessorDeclarations(io::Printer* printer) const {
  65. printer->Print(variables_,
  66. "inline const $type$& $name$() const$deprecation$;\n"
  67. "inline $type$* mutable_$name$()$deprecation$;\n"
  68. "inline $type$* release_$name$()$deprecation$;\n");
  69. }
  70. void MessageFieldGenerator::
  71. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  72. printer->Print(variables_,
  73. "inline const $type$& $classname$::$name$() const {\n"
  74. " return $name$_ != NULL ? *$name$_ : *default_instance_->$name$_;\n"
  75. "}\n"
  76. "inline $type$* $classname$::mutable_$name$() {\n"
  77. " set_has_$name$();\n"
  78. " if ($name$_ == NULL) $name$_ = new $type$;\n"
  79. " return $name$_;\n"
  80. "}\n"
  81. "inline $type$* $classname$::release_$name$() {\n"
  82. " clear_has_$name$();\n"
  83. " $type$* temp = $name$_;\n"
  84. " $name$_ = NULL;\n"
  85. " return temp;\n"
  86. "}\n");
  87. }
  88. void MessageFieldGenerator::
  89. GenerateClearingCode(io::Printer* printer) const {
  90. printer->Print(variables_,
  91. "if ($name$_ != NULL) $name$_->$type$::Clear();\n");
  92. }
  93. void MessageFieldGenerator::
  94. GenerateMergingCode(io::Printer* printer) const {
  95. printer->Print(variables_,
  96. "mutable_$name$()->$type$::MergeFrom(from.$name$());\n");
  97. }
  98. void MessageFieldGenerator::
  99. GenerateSwappingCode(io::Printer* printer) const {
  100. printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
  101. }
  102. void MessageFieldGenerator::
  103. GenerateConstructorCode(io::Printer* printer) const {
  104. printer->Print(variables_, "$name$_ = NULL;\n");
  105. }
  106. void MessageFieldGenerator::
  107. GenerateMergeFromCodedStream(io::Printer* printer) const {
  108. if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
  109. printer->Print(variables_,
  110. "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n"
  111. " input, mutable_$name$()));\n");
  112. } else {
  113. printer->Print(variables_,
  114. "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n"
  115. " $number$, input, mutable_$name$()));\n");
  116. }
  117. }
  118. void MessageFieldGenerator::
  119. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  120. printer->Print(variables_,
  121. "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
  122. " $number$, this->$name$(), output);\n");
  123. }
  124. void MessageFieldGenerator::
  125. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  126. printer->Print(variables_,
  127. "target = ::google::protobuf::internal::WireFormatLite::\n"
  128. " Write$declared_type$NoVirtualToArray(\n"
  129. " $number$, this->$name$(), target);\n");
  130. }
  131. void MessageFieldGenerator::
  132. GenerateByteSize(io::Printer* printer) const {
  133. printer->Print(variables_,
  134. "total_size += $tag_size$ +\n"
  135. " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n"
  136. " this->$name$());\n");
  137. }
  138. // ===================================================================
  139. RepeatedMessageFieldGenerator::
  140. RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor)
  141. : descriptor_(descriptor) {
  142. SetMessageVariables(descriptor, &variables_);
  143. }
  144. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  145. void RepeatedMessageFieldGenerator::
  146. GeneratePrivateMembers(io::Printer* printer) const {
  147. printer->Print(variables_,
  148. "::google::protobuf::RepeatedPtrField< $type$ > $name$_;\n");
  149. }
  150. void RepeatedMessageFieldGenerator::
  151. GenerateAccessorDeclarations(io::Printer* printer) const {
  152. printer->Print(variables_,
  153. "inline const $type$& $name$(int index) const$deprecation$;\n"
  154. "inline $type$* mutable_$name$(int index)$deprecation$;\n"
  155. "inline $type$* add_$name$()$deprecation$;\n");
  156. printer->Print(variables_,
  157. "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  158. " $name$() const$deprecation$;\n"
  159. "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
  160. " mutable_$name$()$deprecation$;\n");
  161. }
  162. void RepeatedMessageFieldGenerator::
  163. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  164. printer->Print(variables_,
  165. "inline const $type$& $classname$::$name$(int index) const {\n"
  166. " return $name$_.Get(index);\n"
  167. "}\n"
  168. "inline $type$* $classname$::mutable_$name$(int index) {\n"
  169. " return $name$_.Mutable(index);\n"
  170. "}\n"
  171. "inline $type$* $classname$::add_$name$() {\n"
  172. " return $name$_.Add();\n"
  173. "}\n");
  174. printer->Print(variables_,
  175. "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  176. "$classname$::$name$() const {\n"
  177. " return $name$_;\n"
  178. "}\n"
  179. "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
  180. "$classname$::mutable_$name$() {\n"
  181. " return &$name$_;\n"
  182. "}\n");
  183. }
  184. void RepeatedMessageFieldGenerator::
  185. GenerateClearingCode(io::Printer* printer) const {
  186. printer->Print(variables_, "$name$_.Clear();\n");
  187. }
  188. void RepeatedMessageFieldGenerator::
  189. GenerateMergingCode(io::Printer* printer) const {
  190. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  191. }
  192. void RepeatedMessageFieldGenerator::
  193. GenerateSwappingCode(io::Printer* printer) const {
  194. printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
  195. }
  196. void RepeatedMessageFieldGenerator::
  197. GenerateConstructorCode(io::Printer* printer) const {
  198. // Not needed for repeated fields.
  199. }
  200. void RepeatedMessageFieldGenerator::
  201. GenerateMergeFromCodedStream(io::Printer* printer) const {
  202. if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
  203. printer->Print(variables_,
  204. "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n"
  205. " input, add_$name$()));\n");
  206. } else {
  207. printer->Print(variables_,
  208. "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n"
  209. " $number$, input, add_$name$()));\n");
  210. }
  211. }
  212. void RepeatedMessageFieldGenerator::
  213. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  214. printer->Print(variables_,
  215. "for (int i = 0; i < this->$name$_size(); i++) {\n"
  216. " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
  217. " $number$, this->$name$(i), output);\n"
  218. "}\n");
  219. }
  220. void RepeatedMessageFieldGenerator::
  221. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  222. printer->Print(variables_,
  223. "for (int i = 0; i < this->$name$_size(); i++) {\n"
  224. " target = ::google::protobuf::internal::WireFormatLite::\n"
  225. " Write$declared_type$NoVirtualToArray(\n"
  226. " $number$, this->$name$(i), target);\n"
  227. "}\n");
  228. }
  229. void RepeatedMessageFieldGenerator::
  230. GenerateByteSize(io::Printer* printer) const {
  231. printer->Print(variables_,
  232. "total_size += $tag_size$ * this->$name$_size();\n"
  233. "for (int i = 0; i < this->$name$_size(); i++) {\n"
  234. " total_size +=\n"
  235. " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n"
  236. " this->$name$(i));\n"
  237. "}\n");
  238. }
  239. } // namespace cpp
  240. } // namespace compiler
  241. } // namespace protobuf
  242. } // namespace google