csharp_helpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
  35. #include <string>
  36. #include <google/protobuf/stubs/port.h>
  37. #include <google/protobuf/descriptor.pb.h>
  38. #include <google/protobuf/descriptor.h>
  39. #include <google/protobuf/compiler/code_generator.h>
  40. #include <google/protobuf/io/printer.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace compiler {
  44. namespace csharp {
  45. struct Options;
  46. class FieldGeneratorBase;
  47. // TODO: start using this enum.
  48. enum CSharpType {
  49. CSHARPTYPE_INT32 = 1,
  50. CSHARPTYPE_INT64 = 2,
  51. CSHARPTYPE_UINT32 = 3,
  52. CSHARPTYPE_UINT64 = 4,
  53. CSHARPTYPE_FLOAT = 5,
  54. CSHARPTYPE_DOUBLE = 6,
  55. CSHARPTYPE_BOOL = 7,
  56. CSHARPTYPE_STRING = 8,
  57. CSHARPTYPE_BYTESTRING = 9,
  58. CSHARPTYPE_MESSAGE = 10,
  59. CSHARPTYPE_ENUM = 11,
  60. MAX_CSHARPTYPE = 11
  61. };
  62. // Converts field type to corresponding C# type.
  63. CSharpType GetCSharpType(FieldDescriptor::Type type);
  64. std::string StripDotProto(const std::string& proto_file);
  65. // Gets unqualified name of the reflection class
  66. std::string GetReflectionClassUnqualifiedName(const FileDescriptor* descriptor);
  67. std::string GetClassName(const EnumDescriptor* descriptor);
  68. std::string GetFieldName(const FieldDescriptor* descriptor);
  69. std::string GetFieldConstantName(const FieldDescriptor* field);
  70. std::string GetPropertyName(const FieldDescriptor* descriptor);
  71. int GetFixedSize(FieldDescriptor::Type type);
  72. std::string UnderscoresToCamelCase(const std::string& input,
  73. bool cap_next_letter,
  74. bool preserve_period);
  75. inline std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter) {
  76. return UnderscoresToCamelCase(input, cap_next_letter, false);
  77. }
  78. std::string UnderscoresToPascalCase(const std::string& input);
  79. // Note that we wouldn't normally want to export this (we're not expecting
  80. // it to be used outside libprotoc itself) but this exposes it for testing.
  81. std::string LIBPROTOBUF_EXPORT GetEnumValueName(const std::string& enum_name, const std::string& enum_value_name);
  82. // TODO(jtattermusch): perhaps we could move this to strutil
  83. std::string StringToBase64(const std::string& input);
  84. std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
  85. FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
  86. int fieldOrdinal,
  87. const Options* options);
  88. // Determines whether the given message is a map entry message,
  89. // i.e. one implicitly created by protoc due to a map<key, value> field.
  90. inline bool IsMapEntryMessage(const Descriptor* descriptor) {
  91. return descriptor->options().map_entry();
  92. }
  93. // Determines whether we're generating code for the proto representation of
  94. // descriptors etc, for use in the runtime. This is the only type which is
  95. // allowed to use proto2 syntax, and it generates internal classes.
  96. inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
  97. return descriptor->name() == "google/protobuf/descriptor.proto";
  98. }
  99. // Determines whether the given message is an options message within descriptor.proto.
  100. inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
  101. if (!IsDescriptorProto(descriptor->file())) {
  102. return false;
  103. }
  104. const string name = descriptor->full_name();
  105. return name == "google.protobuf.FileOptions" ||
  106. name == "google.protobuf.MessageOptions" ||
  107. name == "google.protobuf.FieldOptions" ||
  108. name == "google.protobuf.OneofOptions" ||
  109. name == "google.protobuf.EnumOptions" ||
  110. name == "google.protobuf.EnumValueOptions" ||
  111. name == "google.protobuf.ServiceOptions" ||
  112. name == "google.protobuf.MethodOptions";
  113. }
  114. inline bool IsWrapperType(const FieldDescriptor* descriptor) {
  115. return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
  116. descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
  117. }
  118. } // namespace csharp
  119. } // namespace compiler
  120. } // namespace protobuf
  121. } // namespace google
  122. #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__