csharp_generator.cc
Go to the documentation of this file.
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 #include <sstream>
32 
39 
45 
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace csharp {
50 
51 void GenerateFile(const FileDescriptor* file, io::Printer* printer,
52  const Options* options) {
53  ReflectionClassGenerator reflectionClassGenerator(file, options);
54  reflectionClassGenerator.Generate(printer);
55 }
56 
58  const FileDescriptor* file,
59  const string& parameter,
60  GeneratorContext* generator_context,
61  string* error) const {
62 
63  std::vector<std::pair<string, string> > options;
64  ParseGeneratorParameter(parameter, &options);
65 
66  // We only support proto3 - but we make an exception for descriptor.proto.
67  if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && !IsDescriptorProto(file)) {
68  *error = "C# code generation only supports proto3 syntax";
69  return false;
70  }
71 
72  struct Options cli_options;
73 
74  for (int i = 0; i < options.size(); i++) {
75  if (options[i].first == "file_extension") {
76  cli_options.file_extension = options[i].second;
77  } else if (options[i].first == "base_namespace") {
78  cli_options.base_namespace = options[i].second;
79  cli_options.base_namespace_specified = true;
80  } else if (options[i].first == "internal_access") {
81  cli_options.internal_access = true;
82  } else if (options[i].first == "serializable") {
83  cli_options.serializable = true;
84  } else {
85  *error = "Unknown generator option: " + options[i].first;
86  return false;
87  }
88  }
89 
90  string filename_error = "";
91  std::string filename = GetOutputFile(file,
92  cli_options.file_extension,
93  cli_options.base_namespace_specified,
94  cli_options.base_namespace,
95  &filename_error);
96 
97  if (filename.empty()) {
98  *error = filename_error;
99  return false;
100  }
101  std::unique_ptr<io::ZeroCopyOutputStream> output(
102  generator_context->Open(filename));
103  io::Printer printer(output.get(), '$');
104 
105  GenerateFile(file, &printer, &cli_options);
106 
107  return true;
108 }
109 
110 } // namespace csharp
111 } // namespace compiler
112 } // namespace protobuf
113 } // namespace google
google::protobuf::compiler::csharp::ReflectionClassGenerator
Definition: csharp_reflection_class.h:46
zero_copy_stream.h
google::protobuf::compiler::csharp::GetOutputFile
std::string GetOutputFile(const FileDescriptor *descriptor, const std::string file_extension, const bool generate_directories, const std::string base_namespace, string *error)
Definition: csharp_helpers.cc:371
google::protobuf::compiler::csharp::GenerateFile
void GenerateFile(const FileDescriptor *file, io::Printer *printer, const Options *options)
Definition: csharp_generator.cc:51
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
google::protobuf::compiler::csharp::Options::base_namespace_specified
bool base_namespace_specified
Definition: csharp_options.h:67
google::protobuf::compiler::csharp::Options::internal_access
bool internal_access
Definition: csharp_options.h:70
google::protobuf::compiler::csharp::Generator::Generate
virtual bool Generate(const FileDescriptor *file, const string &parameter, GeneratorContext *generator_context, string *error) const
Definition: csharp_generator.cc:57
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
error
Definition: cJSON.c:88
google::protobuf::FileDescriptor::syntax
Syntax syntax() const
Definition: src/google/protobuf/descriptor.h:2175
google::protobuf::compiler::csharp::Options
Definition: csharp_options.h:42
strutil.h
google::protobuf::compiler::csharp::Options::base_namespace
std::string base_namespace
Definition: csharp_options.h:62
csharp_reflection_class.h
google::protobuf::compiler::csharp::IsDescriptorProto
bool IsDescriptorProto(const FileDescriptor *descriptor)
Definition: csharp_helpers.h:131
printer.h
google::protobuf::compiler::csharp::ReflectionClassGenerator::Generate
void Generate(io::Printer *printer)
Definition: csharp_reflection_class.cc:66
code_generator.h
csharp_generator.h
google::protobuf::FileDescriptor::SYNTAX_PROTO3
@ SYNTAX_PROTO3
Definition: src/google/protobuf/descriptor.h:1394
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::compiler::csharp::Options::serializable
bool serializable
Definition: csharp_options.h:73
csharp_helpers.h
csharp_options.h
csharp_names.h
descriptor.h
google::protobuf::compiler::csharp::Options::file_extension
std::string file_extension
Definition: csharp_options.h:51
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: code_generator.cc:101
first
GLint first
Definition: glcorearb.h:2830
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
descriptor.pb.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49