PageRenderTime 41ms CodeModel.GetById 17ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 453 lines | 359 code | 42 blank | 52 comment | 27 complexity | c878dab069b1dd4f16f88cd8419eedcc 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
 31// Author: kenton@google.com (Kenton Varda)
 32//  Based on original Protocol Buffers design by
 33//  Sanjay Ghemawat, Jeff Dean, and others.
 34
 35#include <google/protobuf/compiler/cpp/cpp_string_field.h>
 36#include <google/protobuf/compiler/cpp/cpp_helpers.h>
 37#include <google/protobuf/io/printer.h>
 38#include <google/protobuf/descriptor.pb.h>
 39#include <google/protobuf/stubs/strutil.h>
 40
 41namespace google {
 42namespace protobuf {
 43namespace compiler {
 44namespace cpp {
 45
 46namespace {
 47
 48void SetStringVariables(const FieldDescriptor* descriptor,
 49                        map<string, string>* variables) {
 50  SetCommonFieldVariables(descriptor, variables);
 51  (*variables)["default"] = DefaultValue(descriptor);
 52  (*variables)["default_variable"] = descriptor->default_value_string().empty()
 53      ? "::google::protobuf::internal::kEmptyString"
 54      : "_default_" + FieldName(descriptor) + "_";
 55  (*variables)["pointer_type"] =
 56      descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
 57}
 58
 59}  // namespace
 60
 61// ===================================================================
 62
 63StringFieldGenerator::
 64StringFieldGenerator(const FieldDescriptor* descriptor)
 65  : descriptor_(descriptor) {
 66  SetStringVariables(descriptor, &variables_);
 67}
 68
 69StringFieldGenerator::~StringFieldGenerator() {}
 70
 71void StringFieldGenerator::
 72GeneratePrivateMembers(io::Printer* printer) const {
 73  printer->Print(variables_, "::std::string* $name$_;\n");
 74  if (!descriptor_->default_value_string().empty()) {
 75    printer->Print(variables_, "static const ::std::string $default_variable$;\n");
 76  }
 77}
 78
 79void StringFieldGenerator::
 80GenerateAccessorDeclarations(io::Printer* printer) const {
 81  // If we're using StringFieldGenerator for a field with a ctype, it's
 82  // because that ctype isn't actually implemented.  In particular, this is
 83  // true of ctype=CORD and ctype=STRING_PIECE in the open source release.
 84  // We aren't releasing Cord because it has too many Google-specific
 85  // dependencies and we aren't releasing StringPiece because it's hardly
 86  // useful outside of Google and because it would get confusing to have
 87  // multiple instances of the StringPiece class in different libraries (PCRE
 88  // already includes it for their C++ bindings, which came from Google).
 89  //
 90  // In any case, we make all the accessors private while still actually
 91  // using a string to represent the field internally.  This way, we can
 92  // guarantee that if we do ever implement the ctype, it won't break any
 93  // existing users who might be -- for whatever reason -- already using .proto
 94  // files that applied the ctype.  The field can still be accessed via the
 95  // reflection interface since the reflection interface is independent of
 96  // the string's underlying representation.
 97  if (descriptor_->options().ctype() != FieldOptions::STRING) {
 98    printer->Outdent();
 99    printer->Print(
100      " private:\n"
101      "  // Hidden due to unknown ctype option.\n");
102    printer->Indent();
103  }
104
105  printer->Print(variables_,
106    "inline const ::std::string& $name$() const$deprecation$;\n"
107    "inline void set_$name$(const ::std::string& value)$deprecation$;\n"
108    "inline void set_$name$(const char* value)$deprecation$;\n"
109    "inline void set_$name$(const $pointer_type$* value, size_t size)"
110                 "$deprecation$;\n"
111    "inline ::std::string* mutable_$name$()$deprecation$;\n"
112    "inline ::std::string* release_$name$()$deprecation$;\n");
113
114  if (descriptor_->options().ctype() != FieldOptions::STRING) {
115    printer->Outdent();
116    printer->Print(" public:\n");
117    printer->Indent();
118  }
119}
120
121void StringFieldGenerator::
122GenerateInlineAccessorDefinitions(io::Printer* printer) const {
123  printer->Print(variables_,
124    "inline const ::std::string& $classname$::$name$() const {\n"
125    "  return *$name$_;\n"
126    "}\n"
127    "inline void $classname$::set_$name$(const ::std::string& value) {\n"
128    "  set_has_$name$();\n"
129    "  if ($name$_ == &$default_variable$) {\n"
130    "    $name$_ = new ::std::string;\n"
131    "  }\n"
132    "  $name$_->assign(value);\n"
133    "}\n"
134    "inline void $classname$::set_$name$(const char* value) {\n"
135    "  set_has_$name$();\n"
136    "  if ($name$_ == &$default_variable$) {\n"
137    "    $name$_ = new ::std::string;\n"
138    "  }\n"
139    "  $name$_->assign(value);\n"
140    "}\n"
141    "inline "
142    "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n"
143    "  set_has_$name$();\n"
144    "  if ($name$_ == &$default_variable$) {\n"
145    "    $name$_ = new ::std::string;\n"
146    "  }\n"
147    "  $name$_->assign(reinterpret_cast<const char*>(value), size);\n"
148    "}\n"
149    "inline ::std::string* $classname$::mutable_$name$() {\n"
150    "  set_has_$name$();\n"
151    "  if ($name$_ == &$default_variable$) {\n");
152  if (descriptor_->default_value_string().empty()) {
153    printer->Print(variables_,
154      "    $name$_ = new ::std::string;\n");
155  } else {
156    printer->Print(variables_,
157      "    $name$_ = new ::std::string($default_variable$);\n");
158  }
159  printer->Print(variables_,
160    "  }\n"
161    "  return $name$_;\n"
162    "}\n"
163    "inline ::std::string* $classname$::release_$name$() {\n"
164    "  clear_has_$name$();\n"
165    "  if ($name$_ == &$default_variable$) {\n"
166    "    return NULL;\n"
167    "  } else {\n"
168    "    ::std::string* temp = $name$_;\n"
169    "    $name$_ = const_cast< ::std::string*>(&$default_variable$);\n"
170    "    return temp;\n"
171    "  }\n"
172    "}\n");
173}
174
175void StringFieldGenerator::
176GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
177  if (!descriptor_->default_value_string().empty()) {
178    printer->Print(variables_,
179      "const ::std::string $classname$::$default_variable$($default$);\n");
180  }
181}
182
183void StringFieldGenerator::
184GenerateClearingCode(io::Printer* printer) const {
185  if (descriptor_->default_value_string().empty()) {
186    printer->Print(variables_,
187      "if ($name$_ != &$default_variable$) {\n"
188      "  $name$_->clear();\n"
189      "}\n");
190  } else {
191    printer->Print(variables_,
192      "if ($name$_ != &$default_variable$) {\n"
193      "  $name$_->assign($default_variable$);\n"
194      "}\n");
195  }
196}
197
198void StringFieldGenerator::
199GenerateMergingCode(io::Printer* printer) const {
200  printer->Print(variables_, "set_$name$(from.$name$());\n");
201}
202
203void StringFieldGenerator::
204GenerateSwappingCode(io::Printer* printer) const {
205  printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
206}
207
208void StringFieldGenerator::
209GenerateConstructorCode(io::Printer* printer) const {
210  printer->Print(variables_,
211    "$name$_ = const_cast< ::std::string*>(&$default_variable$);\n");
212}
213
214void StringFieldGenerator::
215GenerateDestructorCode(io::Printer* printer) const {
216  printer->Print(variables_,
217    "if ($name$_ != &$default_variable$) {\n"
218    "  delete $name$_;\n"
219    "}\n");
220}
221
222void StringFieldGenerator::
223GenerateMergeFromCodedStream(io::Printer* printer) const {
224  printer->Print(variables_,
225    "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
226    "      input, this->mutable_$name$()));\n");
227  if (HasUtf8Verification(descriptor_->file()) &&
228      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
229    printer->Print(variables_,
230      "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
231      "  this->$name$().data(), this->$name$().length(),\n"
232      "  ::google::protobuf::internal::WireFormat::PARSE);\n");
233  }
234}
235
236void StringFieldGenerator::
237GenerateSerializeWithCachedSizes(io::Printer* printer) const {
238  if (HasUtf8Verification(descriptor_->file()) &&
239      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
240    printer->Print(variables_,
241      "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
242      "  this->$name$().data(), this->$name$().length(),\n"
243      "  ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
244  }
245  printer->Print(variables_,
246    "::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
247    "  $number$, this->$name$(), output);\n");
248}
249
250void StringFieldGenerator::
251GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
252  if (HasUtf8Verification(descriptor_->file()) &&
253      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
254    printer->Print(variables_,
255      "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
256      "  this->$name$().data(), this->$name$().length(),\n"
257      "  ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
258  }
259  printer->Print(variables_,
260    "target =\n"
261    "  ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray(\n"
262    "    $number$, this->$name$(), target);\n");
263}
264
265void StringFieldGenerator::
266GenerateByteSize(io::Printer* printer) const {
267  printer->Print(variables_,
268    "total_size += $tag_size$ +\n"
269    "  ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
270    "    this->$name$());\n");
271}
272
273// ===================================================================
274
275RepeatedStringFieldGenerator::
276RepeatedStringFieldGenerator(const FieldDescriptor* descriptor)
277  : descriptor_(descriptor) {
278  SetStringVariables(descriptor, &variables_);
279}
280
281RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
282
283void RepeatedStringFieldGenerator::
284GeneratePrivateMembers(io::Printer* printer) const {
285  printer->Print(variables_,
286    "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n");
287}
288
289void RepeatedStringFieldGenerator::
290GenerateAccessorDeclarations(io::Printer* printer) const {
291  // See comment above about unknown ctypes.
292  if (descriptor_->options().ctype() != FieldOptions::STRING) {
293    printer->Outdent();
294    printer->Print(
295      " private:\n"
296      "  // Hidden due to unknown ctype option.\n");
297    printer->Indent();
298  }
299
300  printer->Print(variables_,
301    "inline const ::std::string& $name$(int index) const$deprecation$;\n"
302    "inline ::std::string* mutable_$name$(int index)$deprecation$;\n"
303    "inline void set_$name$(int index, const ::std::string& value)$deprecation$;\n"
304    "inline void set_$name$(int index, const char* value)$deprecation$;\n"
305    "inline "
306    "void set_$name$(int index, const $pointer_type$* value, size_t size)"
307                 "$deprecation$;\n"
308    "inline ::std::string* add_$name$()$deprecation$;\n"
309    "inline void add_$name$(const ::std::string& value)$deprecation$;\n"
310    "inline void add_$name$(const char* value)$deprecation$;\n"
311    "inline void add_$name$(const $pointer_type$* value, size_t size)"
312                 "$deprecation$;\n");
313
314  printer->Print(variables_,
315    "inline const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() const"
316                 "$deprecation$;\n"
317    "inline ::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$()"
318                 "$deprecation$;\n");
319
320  if (descriptor_->options().ctype() != FieldOptions::STRING) {
321    printer->Outdent();
322    printer->Print(" public:\n");
323    printer->Indent();
324  }
325}
326
327void RepeatedStringFieldGenerator::
328GenerateInlineAccessorDefinitions(io::Printer* printer) const {
329  printer->Print(variables_,
330    "inline const ::std::string& $classname$::$name$(int index) const {\n"
331    "  return $name$_.Get(index);\n"
332    "}\n"
333    "inline ::std::string* $classname$::mutable_$name$(int index) {\n"
334    "  return $name$_.Mutable(index);\n"
335    "}\n"
336    "inline void $classname$::set_$name$(int index, const ::std::string& value) {\n"
337    "  $name$_.Mutable(index)->assign(value);\n"
338    "}\n"
339    "inline void $classname$::set_$name$(int index, const char* value) {\n"
340    "  $name$_.Mutable(index)->assign(value);\n"
341    "}\n"
342    "inline void "
343    "$classname$::set_$name$"
344    "(int index, const $pointer_type$* value, size_t size) {\n"
345    "  $name$_.Mutable(index)->assign(\n"
346    "    reinterpret_cast<const char*>(value), size);\n"
347    "}\n"
348    "inline ::std::string* $classname$::add_$name$() {\n"
349    "  return $name$_.Add();\n"
350    "}\n"
351    "inline void $classname$::add_$name$(const ::std::string& value) {\n"
352    "  $name$_.Add()->assign(value);\n"
353    "}\n"
354    "inline void $classname$::add_$name$(const char* value) {\n"
355    "  $name$_.Add()->assign(value);\n"
356    "}\n"
357    "inline void "
358    "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n"
359    "  $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n"
360    "}\n");
361  printer->Print(variables_,
362    "inline const ::google::protobuf::RepeatedPtrField< ::std::string>&\n"
363    "$classname$::$name$() const {\n"
364    "  return $name$_;\n"
365    "}\n"
366    "inline ::google::protobuf::RepeatedPtrField< ::std::string>*\n"
367    "$classname$::mutable_$name$() {\n"
368    "  return &$name$_;\n"
369    "}\n");
370}
371
372void RepeatedStringFieldGenerator::
373GenerateClearingCode(io::Printer* printer) const {
374  printer->Print(variables_, "$name$_.Clear();\n");
375}
376
377void RepeatedStringFieldGenerator::
378GenerateMergingCode(io::Printer* printer) const {
379  printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
380}
381
382void RepeatedStringFieldGenerator::
383GenerateSwappingCode(io::Printer* printer) const {
384  printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
385}
386
387void RepeatedStringFieldGenerator::
388GenerateConstructorCode(io::Printer* printer) const {
389  // Not needed for repeated fields.
390}
391
392void RepeatedStringFieldGenerator::
393GenerateMergeFromCodedStream(io::Printer* printer) const {
394  printer->Print(variables_,
395    "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
396    "      input, this->add_$name$()));\n");
397  if (HasUtf8Verification(descriptor_->file()) &&
398      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
399    printer->Print(variables_,
400      "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
401      "  this->$name$(0).data(), this->$name$(0).length(),\n"
402      "  ::google::protobuf::internal::WireFormat::PARSE);\n");
403  }
404}
405
406void RepeatedStringFieldGenerator::
407GenerateSerializeWithCachedSizes(io::Printer* printer) const {
408  printer->Print(variables_,
409    "for (int i = 0; i < this->$name$_size(); i++) {\n");
410  if (HasUtf8Verification(descriptor_->file()) &&
411      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
412    printer->Print(variables_,
413      "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
414      "  this->$name$(i).data(), this->$name$(i).length(),\n"
415      "  ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
416  }
417  printer->Print(variables_,
418    "  ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
419    "    $number$, this->$name$(i), output);\n"
420    "}\n");
421}
422
423void RepeatedStringFieldGenerator::
424GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
425  printer->Print(variables_,
426    "for (int i = 0; i < this->$name$_size(); i++) {\n");
427  if (HasUtf8Verification(descriptor_->file()) &&
428      descriptor_->type() == FieldDescriptor::TYPE_STRING) {
429    printer->Print(variables_,
430      "  ::google::protobuf::internal::WireFormat::VerifyUTF8String(\n"
431      "    this->$name$(i).data(), this->$name$(i).length(),\n"
432      "    ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
433  }
434  printer->Print(variables_,
435    "  target = ::google::protobuf::internal::WireFormatLite::\n"
436    "    Write$declared_type$ToArray($number$, this->$name$(i), target);\n"
437    "}\n");
438}
439
440void RepeatedStringFieldGenerator::
441GenerateByteSize(io::Printer* printer) const {
442  printer->Print(variables_,
443    "total_size += $tag_size$ * this->$name$_size();\n"
444    "for (int i = 0; i < this->$name$_size(); i++) {\n"
445    "  total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
446    "    this->$name$(i));\n"
447    "}\n");
448}
449
450}  // namespace cpp
451}  // namespace compiler
452}  // namespace protobuf
453}  // namespace google