Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.8/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/SourceGeneratorBase.cs

Last change on this file was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 5.8 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System.Collections.Generic;
38using Google.ProtocolBuffers.DescriptorProtos;
39using Google.ProtocolBuffers.Descriptors;
40
41namespace Google.ProtocolBuffers.ProtoGen
42{
43    internal abstract class SourceGeneratorBase<T> where T : IDescriptor
44    {
45        private readonly T descriptor;
46
47        protected readonly bool OptimizeSpeed;
48        protected readonly bool OptimizeSize;
49        protected readonly bool UseLiteRuntime;
50        protected readonly string RuntimeSuffix;
51
52        protected SourceGeneratorBase(T descriptor)
53        {
54            this.descriptor = descriptor;
55
56            OptimizeSize = descriptor.File.Options.OptimizeFor ==
57                           FileOptions.Types.OptimizeMode.CODE_SIZE;
58            OptimizeSpeed = descriptor.File.Options.OptimizeFor ==
59                            FileOptions.Types.OptimizeMode.SPEED;
60            UseLiteRuntime = descriptor.File.Options.OptimizeFor ==
61                             FileOptions.Types.OptimizeMode.LITE_RUNTIME;
62            //Lite runtime uses OptimizeSpeed code branches
63            OptimizeSpeed |= UseLiteRuntime;
64            RuntimeSuffix = UseLiteRuntime ? "Lite" : "";
65        }
66
67        protected T Descriptor
68        {
69            get { return descriptor; }
70        }
71
72        internal static string GetClassName(IDescriptor descriptor)
73        {
74            return ToCSharpName(descriptor.FullName, descriptor.File);
75        }
76
77        // Groups are hacky:  The name of the field is just the lower-cased name
78        // of the group type.  In C#, though, we would like to retain the original
79        // capitalization of the type name.
80        internal static string GetFieldName(FieldDescriptor descriptor)
81        {
82            if (descriptor.FieldType == FieldType.Group)
83            {
84                return descriptor.MessageType.Name;
85            }
86            else
87            {
88                return descriptor.Name;
89            }
90        }
91
92        internal static string GetFieldConstantName(FieldDescriptor field)
93        {
94            return field.CSharpOptions.PropertyName + "FieldNumber";
95        }
96
97        private static string ToCSharpName(string name, FileDescriptor file)
98        {
99            string result = file.CSharpOptions.Namespace;
100            if (file.CSharpOptions.NestClasses)
101            {
102                if (result != "")
103                {
104                    result += ".";
105                }
106                result += file.CSharpOptions.UmbrellaClassname;
107            }
108            if (result != "")
109            {
110                result += '.';
111            }
112            string classname;
113            if (file.Package == "")
114            {
115                classname = name;
116            }
117            else
118            {
119                // Strip the proto package from full_name since we've replaced it with
120                // the C# namespace.
121                classname = name.Substring(file.Package.Length + 1);
122            }
123            result += classname.Replace(".", ".Types.");
124            return "global::" + result;
125        }
126
127        protected string ClassAccessLevel
128        {
129            get { return descriptor.File.CSharpOptions.PublicClasses ? "public" : "internal"; }
130        }
131
132        protected void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children)
133            where TChild : IDescriptor
134        {
135            // Copy the set of children; makes access easier
136            List<TChild> copy = new List<TChild>(children);
137            if (copy.Count == 0)
138            {
139                return;
140            }
141
142            if (region != null)
143            {
144                writer.WriteLine("#region {0}", region);
145            }
146            foreach (TChild child in children)
147            {
148                SourceGenerators.CreateGenerator(child).Generate(writer);
149            }
150            if (region != null)
151            {
152                writer.WriteLine("#endregion");
153                writer.WriteLine();
154            }
155        }
156    }
157}
Note: See TracBrowser for help on using the repository browser.