/src/google/protobuf/compiler/c/c_primitive_field.cc

http://protobuf-c.googlecode.com/ · C++ · 168 lines · 127 code · 16 blank · 25 comment · 7 complexity · fd66f7ae9790e712af527e88e174c0d2 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Author: kenton@google.com (Kenton Varda)
  17. // Based on original Protocol Buffers design by
  18. // Sanjay Ghemawat, Jeff Dean, and others.
  19. // Modified to implement C code by Dave Benson.
  20. #include <google/protobuf/compiler/c/c_primitive_field.h>
  21. #include <google/protobuf/compiler/c/c_helpers.h>
  22. #include <google/protobuf/io/printer.h>
  23. #include <google/protobuf/wire_format.h>
  24. namespace google {
  25. namespace protobuf {
  26. namespace compiler {
  27. namespace c {
  28. PrimitiveFieldGenerator::
  29. PrimitiveFieldGenerator(const FieldDescriptor* descriptor)
  30. : FieldGenerator(descriptor) {
  31. }
  32. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  33. void PrimitiveFieldGenerator::GenerateStructMembers(io::Printer* printer) const
  34. {
  35. string c_type;
  36. map<string, string> vars;
  37. switch (descriptor_->type()) {
  38. case FieldDescriptor::TYPE_SINT32 :
  39. case FieldDescriptor::TYPE_SFIXED32:
  40. case FieldDescriptor::TYPE_INT32 : c_type = "int32_t"; break;
  41. case FieldDescriptor::TYPE_SINT64 :
  42. case FieldDescriptor::TYPE_SFIXED64:
  43. case FieldDescriptor::TYPE_INT64 : c_type = "int64_t"; break;
  44. case FieldDescriptor::TYPE_UINT32 :
  45. case FieldDescriptor::TYPE_FIXED32 : c_type = "uint32_t"; break;
  46. case FieldDescriptor::TYPE_UINT64 :
  47. case FieldDescriptor::TYPE_FIXED64 : c_type = "uint64_t"; break;
  48. case FieldDescriptor::TYPE_FLOAT : c_type = "float"; break;
  49. case FieldDescriptor::TYPE_DOUBLE : c_type = "double"; break;
  50. case FieldDescriptor::TYPE_BOOL : c_type = "protobuf_c_boolean"; break;
  51. case FieldDescriptor::TYPE_ENUM :
  52. case FieldDescriptor::TYPE_STRING :
  53. case FieldDescriptor::TYPE_BYTES :
  54. case FieldDescriptor::TYPE_GROUP :
  55. case FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
  56. // No default because we want the compiler to complain if any new
  57. // types are added.
  58. }
  59. vars["c_type"] = c_type;
  60. vars["name"] = FieldName(descriptor_);
  61. vars["deprecated"] = FieldDeprecated(descriptor_);
  62. switch (descriptor_->label()) {
  63. case FieldDescriptor::LABEL_REQUIRED:
  64. printer->Print(vars, "$c_type$ $name$$deprecated$;\n");
  65. break;
  66. case FieldDescriptor::LABEL_OPTIONAL:
  67. printer->Print(vars, "protobuf_c_boolean has_$name$$deprecated$;\n");
  68. printer->Print(vars, "$c_type$ $name$$deprecated$;\n");
  69. break;
  70. case FieldDescriptor::LABEL_REPEATED:
  71. printer->Print(vars, "size_t n_$name$$deprecated$;\n");
  72. printer->Print(vars, "$c_type$ *$name$$deprecated$;\n");
  73. break;
  74. }
  75. }
  76. string PrimitiveFieldGenerator::GetDefaultValue() const
  77. {
  78. /* XXX: SimpleItoa seems woefully inadequate for anything but int32,
  79. * but that's what protobuf uses. */
  80. switch (descriptor_->cpp_type()) {
  81. case FieldDescriptor::CPPTYPE_INT32:
  82. return SimpleItoa(descriptor_->default_value_int32());
  83. case FieldDescriptor::CPPTYPE_INT64:
  84. return SimpleItoa(descriptor_->default_value_int64());
  85. case FieldDescriptor::CPPTYPE_UINT32:
  86. return SimpleItoa(descriptor_->default_value_uint32());
  87. case FieldDescriptor::CPPTYPE_UINT64:
  88. return SimpleItoa(descriptor_->default_value_uint64());
  89. case FieldDescriptor::CPPTYPE_FLOAT:
  90. return SimpleFtoa(descriptor_->default_value_float());
  91. case FieldDescriptor::CPPTYPE_DOUBLE:
  92. return SimpleDtoa(descriptor_->default_value_double());
  93. case FieldDescriptor::CPPTYPE_BOOL:
  94. return descriptor_->default_value_bool() ? "1" : "0";
  95. default:
  96. GOOGLE_LOG(DFATAL) << "unexpected CPPTYPE in c_primitive_field";
  97. return "UNEXPECTED_CPPTYPE";
  98. }
  99. }
  100. void PrimitiveFieldGenerator::GenerateStaticInit(io::Printer* printer) const
  101. {
  102. map<string, string> vars;
  103. if (descriptor_->has_default_value()) {
  104. vars["default_value"] = GetDefaultValue();
  105. } else {
  106. vars["default_value"] = "0";
  107. }
  108. switch (descriptor_->label()) {
  109. case FieldDescriptor::LABEL_REQUIRED:
  110. printer->Print(vars, "$default_value$");
  111. break;
  112. case FieldDescriptor::LABEL_OPTIONAL:
  113. printer->Print(vars, "0,$default_value$");
  114. break;
  115. case FieldDescriptor::LABEL_REPEATED:
  116. printer->Print("0,NULL");
  117. break;
  118. }
  119. }
  120. void PrimitiveFieldGenerator::GenerateDescriptorInitializer(io::Printer* printer) const
  121. {
  122. string c_type_macro;
  123. switch (descriptor_->type()) {
  124. #define WRITE_CASE(shortname) case FieldDescriptor::TYPE_##shortname: c_type_macro = #shortname; break;
  125. WRITE_CASE(INT32)
  126. WRITE_CASE(SINT32)
  127. WRITE_CASE(UINT32)
  128. WRITE_CASE(SFIXED32)
  129. WRITE_CASE(FIXED32)
  130. WRITE_CASE(INT64)
  131. WRITE_CASE(SINT64)
  132. WRITE_CASE(UINT64)
  133. WRITE_CASE(FIXED64)
  134. WRITE_CASE(SFIXED64)
  135. WRITE_CASE(FLOAT)
  136. WRITE_CASE(DOUBLE)
  137. WRITE_CASE(BOOL)
  138. #undef WRITE_CASE
  139. case FieldDescriptor::TYPE_ENUM :
  140. case FieldDescriptor::TYPE_STRING :
  141. case FieldDescriptor::TYPE_BYTES :
  142. case FieldDescriptor::TYPE_GROUP :
  143. case FieldDescriptor::TYPE_MESSAGE : GOOGLE_LOG(FATAL) << "not a primitive type"; break;
  144. // No default because we want the compiler to complain if any new
  145. // types are added.
  146. }
  147. GenerateDescriptorInitializerGeneric(printer, true, c_type_macro, "NULL");
  148. }
  149. } // namespace c
  150. } // namespace compiler
  151. } // namespace protobuf
  152. } // namespace google