Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 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 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__ |
| 36 | #define GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__ |
| 37 | |
| 38 | #include <string> |
| 39 | #include <google/protobuf/descriptor.pb.h> |
| 40 | #include <google/protobuf/descriptor.h> |
| 41 | #include <google/protobuf/compiler/code_generator.h> |
| 42 | #include <google/protobuf/io/printer.h> |
| 43 | |
| 44 | namespace google { |
| 45 | namespace protobuf { |
| 46 | namespace compiler { |
| 47 | namespace csharp { |
| 48 | |
| 49 | class FieldGeneratorBase; |
| 50 | |
| 51 | // TODO: start using this enum. |
| 52 | enum CSharpType { |
| 53 | CSHARPTYPE_INT32 = 1, |
| 54 | CSHARPTYPE_INT64 = 2, |
| 55 | CSHARPTYPE_UINT32 = 3, |
| 56 | CSHARPTYPE_UINT64 = 4, |
| 57 | CSHARPTYPE_FLOAT = 5, |
| 58 | CSHARPTYPE_DOUBLE = 6, |
| 59 | CSHARPTYPE_BOOL = 7, |
| 60 | CSHARPTYPE_STRING = 8, |
| 61 | CSHARPTYPE_BYTESTRING = 9, |
| 62 | CSHARPTYPE_MESSAGE = 10, |
| 63 | CSHARPTYPE_ENUM = 11, |
| 64 | MAX_CSHARPTYPE = 11 |
| 65 | }; |
| 66 | |
| 67 | // Converts field type to corresponding C# type. |
| 68 | CSharpType GetCSharpType(FieldDescriptor::Type type); |
| 69 | |
| 70 | std::string StripDotProto(const std::string& proto_file); |
| 71 | |
Jan Tattermusch | 43a2dee | 2015-07-29 20:15:03 -0700 | [diff] [blame] | 72 | // Gets unqualified name of the umbrella class |
| 73 | std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor); |
Jan Tattermusch | cacbedf | 2015-07-10 13:40:34 -0700 | [diff] [blame] | 74 | |
Jan Tattermusch | 43a2dee | 2015-07-29 20:15:03 -0700 | [diff] [blame] | 75 | // Gets name of the nested for umbrella class (just the nested part, |
| 76 | // not including the GetFileNamespace part). |
| 77 | std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor); |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 78 | |
Jon Skeet | 53c399a | 2015-07-20 19:24:31 +0100 | [diff] [blame] | 79 | std::string GetClassName(const Descriptor* descriptor); |
| 80 | |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 81 | std::string GetClassName(const EnumDescriptor* descriptor); |
| 82 | |
| 83 | std::string GetFieldName(const FieldDescriptor* descriptor); |
| 84 | |
| 85 | std::string GetFieldConstantName(const FieldDescriptor* field); |
| 86 | |
| 87 | std::string GetPropertyName(const FieldDescriptor* descriptor); |
| 88 | |
| 89 | int GetFixedSize(FieldDescriptor::Type type); |
| 90 | |
Jon Skeet | 8482b6c | 2015-07-03 13:51:40 +0100 | [diff] [blame] | 91 | std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter, bool preserve_period); |
| 92 | |
| 93 | inline std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter) { |
| 94 | return UnderscoresToCamelCase(input, cap_next_letter, false); |
| 95 | } |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 96 | |
| 97 | std::string UnderscoresToPascalCase(const std::string& input); |
| 98 | |
| 99 | // TODO(jtattermusch): perhaps we could move this to strutil |
| 100 | std::string StringToBase64(const std::string& input); |
| 101 | |
| 102 | std::string FileDescriptorToBase64(const FileDescriptor* descriptor); |
| 103 | |
Jon Skeet | 322ec53 | 2015-06-24 17:55:02 +0100 | [diff] [blame] | 104 | uint FixedMakeTag(const FieldDescriptor* descriptor); |
| 105 | |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 106 | FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); |
| 107 | |
Jon Skeet | 541b442 | 2015-07-15 13:36:56 +0100 | [diff] [blame] | 108 | // Determines whether the given message is a map entry message, i.e. one implicitly created |
| 109 | // by protoc due to a map<key, value> field. |
| 110 | inline bool IsMapEntryMessage(const Descriptor* descriptor) { |
| 111 | return descriptor->options().map_entry(); |
| 112 | } |
| 113 | |
Jon Skeet | 493e3db | 2015-07-01 17:11:17 +0100 | [diff] [blame] | 114 | // Determines whether we're generating code for the proto representation of descriptors etc, |
| 115 | // for use in the runtime. This is the only type which is allowed to use proto2 syntax, |
| 116 | // and it generates internal classes. |
| 117 | inline bool IsDescriptorProto(const FileDescriptor* descriptor) { |
Jon Skeet | ca89a1a | 2015-08-25 14:32:28 +0100 | [diff] [blame^] | 118 | return descriptor->name() == "google/protobuf/descriptor.proto"; |
Jon Skeet | 493e3db | 2015-07-01 17:11:17 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Jon Skeet | b2ac868 | 2015-07-15 13:17:42 +0100 | [diff] [blame] | 121 | inline bool IsWrapperType(const FieldDescriptor* descriptor) { |
| 122 | return descriptor->type() == FieldDescriptor::TYPE_MESSAGE && |
| 123 | descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto"; |
| 124 | } |
| 125 | |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 126 | } // namespace csharp |
| 127 | } // namespace compiler |
| 128 | } // namespace protobuf |
| 129 | } // namespace google |
| 130 | #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__ |