PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 258 lines | 183 code | 35 blank | 40 comment | 21 complexity | 532687fbee947fc523f602ac8265ba47 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 <set>
  34. #include <map>
  35. #include <google/protobuf/compiler/cpp/cpp_enum.h>
  36. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  37. #include <google/protobuf/io/printer.h>
  38. #include <google/protobuf/stubs/strutil.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace compiler {
  42. namespace cpp {
  43. EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
  44. const string& dllexport_decl)
  45. : descriptor_(descriptor),
  46. classname_(ClassName(descriptor, false)),
  47. dllexport_decl_(dllexport_decl) {
  48. }
  49. EnumGenerator::~EnumGenerator() {}
  50. void EnumGenerator::GenerateDefinition(io::Printer* printer) {
  51. map<string, string> vars;
  52. vars["classname"] = classname_;
  53. vars["short_name"] = descriptor_->name();
  54. printer->Print(vars, "enum $classname$ {\n");
  55. printer->Indent();
  56. const EnumValueDescriptor* min_value = descriptor_->value(0);
  57. const EnumValueDescriptor* max_value = descriptor_->value(0);
  58. for (int i = 0; i < descriptor_->value_count(); i++) {
  59. vars["name"] = descriptor_->value(i)->name();
  60. vars["number"] = SimpleItoa(descriptor_->value(i)->number());
  61. vars["prefix"] = (descriptor_->containing_type() == NULL) ?
  62. "" : classname_ + "_";
  63. if (i > 0) printer->Print(",\n");
  64. printer->Print(vars, "$prefix$$name$ = $number$");
  65. if (descriptor_->value(i)->number() < min_value->number()) {
  66. min_value = descriptor_->value(i);
  67. }
  68. if (descriptor_->value(i)->number() > max_value->number()) {
  69. max_value = descriptor_->value(i);
  70. }
  71. }
  72. printer->Outdent();
  73. printer->Print("\n};\n");
  74. vars["min_name"] = min_value->name();
  75. vars["max_name"] = max_value->name();
  76. if (dllexport_decl_.empty()) {
  77. vars["dllexport"] = "";
  78. } else {
  79. vars["dllexport"] = dllexport_decl_ + " ";
  80. }
  81. printer->Print(vars,
  82. "$dllexport$bool $classname$_IsValid(int value);\n"
  83. "const $classname$ $prefix$$short_name$_MIN = $prefix$$min_name$;\n"
  84. "const $classname$ $prefix$$short_name$_MAX = $prefix$$max_name$;\n"
  85. "const int $prefix$$short_name$_ARRAYSIZE = $prefix$$short_name$_MAX + 1;\n"
  86. "\n");
  87. if (HasDescriptorMethods(descriptor_->file())) {
  88. printer->Print(vars,
  89. "$dllexport$const ::google::protobuf::EnumDescriptor* $classname$_descriptor();\n");
  90. // The _Name and _Parse methods
  91. printer->Print(vars,
  92. "inline const ::std::string& $classname$_Name($classname$ value) {\n"
  93. " return ::google::protobuf::internal::NameOfEnum(\n"
  94. " $classname$_descriptor(), value);\n"
  95. "}\n");
  96. printer->Print(vars,
  97. "inline bool $classname$_Parse(\n"
  98. " const ::std::string& name, $classname$* value) {\n"
  99. " return ::google::protobuf::internal::ParseNamedEnum<$classname$>(\n"
  100. " $classname$_descriptor(), name, value);\n"
  101. "}\n");
  102. }
  103. }
  104. void EnumGenerator::
  105. GenerateGetEnumDescriptorSpecializations(io::Printer* printer) {
  106. if (HasDescriptorMethods(descriptor_->file())) {
  107. printer->Print(
  108. "template <>\n"
  109. "inline const EnumDescriptor* GetEnumDescriptor< $classname$>() {\n"
  110. " return $classname$_descriptor();\n"
  111. "}\n",
  112. "classname", ClassName(descriptor_, true));
  113. }
  114. }
  115. void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
  116. map<string, string> vars;
  117. vars["nested_name"] = descriptor_->name();
  118. vars["classname"] = classname_;
  119. printer->Print(vars, "typedef $classname$ $nested_name$;\n");
  120. for (int j = 0; j < descriptor_->value_count(); j++) {
  121. vars["tag"] = descriptor_->value(j)->name();
  122. printer->Print(vars,
  123. "static const $nested_name$ $tag$ = $classname$_$tag$;\n");
  124. }
  125. printer->Print(vars,
  126. "static inline bool $nested_name$_IsValid(int value) {\n"
  127. " return $classname$_IsValid(value);\n"
  128. "}\n"
  129. "static const $nested_name$ $nested_name$_MIN =\n"
  130. " $classname$_$nested_name$_MIN;\n"
  131. "static const $nested_name$ $nested_name$_MAX =\n"
  132. " $classname$_$nested_name$_MAX;\n"
  133. "static const int $nested_name$_ARRAYSIZE =\n"
  134. " $classname$_$nested_name$_ARRAYSIZE;\n");
  135. if (HasDescriptorMethods(descriptor_->file())) {
  136. printer->Print(vars,
  137. "static inline const ::google::protobuf::EnumDescriptor*\n"
  138. "$nested_name$_descriptor() {\n"
  139. " return $classname$_descriptor();\n"
  140. "}\n");
  141. printer->Print(vars,
  142. "static inline const ::std::string& $nested_name$_Name($nested_name$ value) {\n"
  143. " return $classname$_Name(value);\n"
  144. "}\n");
  145. printer->Print(vars,
  146. "static inline bool $nested_name$_Parse(const ::std::string& name,\n"
  147. " $nested_name$* value) {\n"
  148. " return $classname$_Parse(name, value);\n"
  149. "}\n");
  150. }
  151. }
  152. void EnumGenerator::GenerateDescriptorInitializer(
  153. io::Printer* printer, int index) {
  154. map<string, string> vars;
  155. vars["classname"] = classname_;
  156. vars["index"] = SimpleItoa(index);
  157. if (descriptor_->containing_type() == NULL) {
  158. printer->Print(vars,
  159. "$classname$_descriptor_ = file->enum_type($index$);\n");
  160. } else {
  161. vars["parent"] = ClassName(descriptor_->containing_type(), false);
  162. printer->Print(vars,
  163. "$classname$_descriptor_ = $parent$_descriptor_->enum_type($index$);\n");
  164. }
  165. }
  166. void EnumGenerator::GenerateMethods(io::Printer* printer) {
  167. map<string, string> vars;
  168. vars["classname"] = classname_;
  169. if (HasDescriptorMethods(descriptor_->file())) {
  170. printer->Print(vars,
  171. "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n"
  172. " protobuf_AssignDescriptorsOnce();\n"
  173. " return $classname$_descriptor_;\n"
  174. "}\n");
  175. }
  176. printer->Print(vars,
  177. "bool $classname$_IsValid(int value) {\n"
  178. " switch(value) {\n");
  179. // Multiple values may have the same number. Make sure we only cover
  180. // each number once by first constructing a set containing all valid
  181. // numbers, then printing a case statement for each element.
  182. set<int> numbers;
  183. for (int j = 0; j < descriptor_->value_count(); j++) {
  184. const EnumValueDescriptor* value = descriptor_->value(j);
  185. numbers.insert(value->number());
  186. }
  187. for (set<int>::iterator iter = numbers.begin();
  188. iter != numbers.end(); ++iter) {
  189. printer->Print(
  190. " case $number$:\n",
  191. "number", SimpleItoa(*iter));
  192. }
  193. printer->Print(vars,
  194. " return true;\n"
  195. " default:\n"
  196. " return false;\n"
  197. " }\n"
  198. "}\n"
  199. "\n");
  200. if (descriptor_->containing_type() != NULL) {
  201. // We need to "define" the static constants which were declared in the
  202. // header, to give the linker a place to put them. Or at least the C++
  203. // standard says we have to. MSVC actually insists tha we do _not_ define
  204. // them again in the .cc file.
  205. printer->Print("#ifndef _MSC_VER\n");
  206. vars["parent"] = ClassName(descriptor_->containing_type(), false);
  207. vars["nested_name"] = descriptor_->name();
  208. for (int i = 0; i < descriptor_->value_count(); i++) {
  209. vars["value"] = descriptor_->value(i)->name();
  210. printer->Print(vars,
  211. "const $classname$ $parent$::$value$;\n");
  212. }
  213. printer->Print(vars,
  214. "const $classname$ $parent$::$nested_name$_MIN;\n"
  215. "const $classname$ $parent$::$nested_name$_MAX;\n"
  216. "const int $parent$::$nested_name$_ARRAYSIZE;\n");
  217. printer->Print("#endif // _MSC_VER\n");
  218. }
  219. }
  220. } // namespace cpp
  221. } // namespace compiler
  222. } // namespace protobuf
  223. } // namespace google