Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/ExtensionGenerator.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: 7.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;
38using Google.ProtocolBuffers.Descriptors;
39
40namespace Google.ProtocolBuffers.ProtoGen
41{
42    internal class ExtensionGenerator : FieldGeneratorBase, ISourceGenerator
43    {
44        private readonly string extends;
45        private readonly string scope;
46        private readonly string type;
47        private readonly string name;
48
49        internal ExtensionGenerator(FieldDescriptor descriptor)
50            : base(descriptor, 0)
51        {
52            if (Descriptor.ExtensionScope != null)
53            {
54                scope = GetClassName(Descriptor.ExtensionScope);
55            }
56            else
57            {
58                scope = DescriptorUtil.GetFullUmbrellaClassName(Descriptor.File);
59            }
60            switch (Descriptor.MappedType)
61            {
62                case MappedType.Message:
63                    type = GetClassName(Descriptor.MessageType);
64                    break;
65                case MappedType.Enum:
66                    type = GetClassName(Descriptor.EnumType);
67                    break;
68                default:
69                    type = DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
70                    break;
71            }
72            extends = GetClassName(Descriptor.ContainingType);
73            name = Descriptor.CSharpOptions.PropertyName;
74        }
75
76        public void Generate(TextGenerator writer)
77        {
78            if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(Descriptor).StartsWith("_"))
79            {
80                writer.WriteLine("[global::System.CLSCompliant(false)]");
81            }
82
83            writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber);
84
85            if (UseLiteRuntime)
86            {
87                if (Descriptor.MappedType == MappedType.Message && Descriptor.MessageType.Options.MessageSetWireFormat)
88                {
89                    throw new ArgumentException(
90                        "option message_set_wire_format = true; is not supported in Lite runtime extensions.");
91                }
92                if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
93                {
94                    writer.WriteLine("[global::System.CLSCompliant(false)]");
95                }
96                writer.WriteLine("{0} static pb::{4}<{1}, {2}> {3};", ClassAccessLevel, extends, type, name,
97                                 Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite");
98            }
99            else if (Descriptor.IsRepeated)
100            {
101                if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
102                {
103                    writer.WriteLine("[global::System.CLSCompliant(false)]");
104                }
105                writer.WriteLine("{0} static pb::GeneratedExtensionBase<scg::IList<{1}>> {2};", ClassAccessLevel, type,
106                                 name);
107            }
108            else
109            {
110                if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
111                {
112                    writer.WriteLine("[global::System.CLSCompliant(false)]");
113                }
114                writer.WriteLine("{0} static pb::GeneratedExtensionBase<{1}> {2};", ClassAccessLevel, type, name);
115            }
116        }
117
118        internal void GenerateStaticVariableInitializers(TextGenerator writer)
119        {
120            if (UseLiteRuntime)
121            {
122                writer.WriteLine("{0}.{1} = ", scope, name);
123                writer.Indent();
124                writer.WriteLine("new pb::{0}<{1}, {2}>(",
125                                 Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite",
126                                 extends, type);
127                writer.Indent();
128                writer.WriteLine("\"{0}\",", Descriptor.FullName);
129                writer.WriteLine("{0}.DefaultInstance,", extends);
130                if (!Descriptor.IsRepeated)
131                {
132                    writer.WriteLine("{0},",
133                                     Descriptor.HasDefaultValue
134                                         ? DefaultValue
135                                         : IsNullableType ? "null" : "default(" + type + ")");
136                }
137                writer.WriteLine("{0},",
138                                 (Descriptor.MappedType == MappedType.Message) ? type + ".DefaultInstance" : "null");
139                writer.WriteLine("{0},",
140                                 (Descriptor.MappedType == MappedType.Enum) ? "new EnumLiteMap<" + type + ">()" : "null");
141                writer.WriteLine("{0}.{1}FieldNumber,", scope, name);
142                writer.Write("pbd::FieldType.{0}", Descriptor.FieldType);
143                if (Descriptor.IsRepeated)
144                {
145                    writer.WriteLine(",");
146                    writer.Write(Descriptor.IsPacked ? "true" : "false");
147                }
148                writer.Outdent();
149                writer.WriteLine(");");
150                writer.Outdent();
151            }
152            else if (Descriptor.IsRepeated)
153            {
154                writer.WriteLine(
155                    "{0}.{1} = pb::GeneratedRepeatExtension<{2}>.CreateInstance({0}.Descriptor.Extensions[{3}]);", scope,
156                    name, type, Descriptor.Index);
157            }
158            else
159            {
160                writer.WriteLine(
161                    "{0}.{1} = pb::GeneratedSingleExtension<{2}>.CreateInstance({0}.Descriptor.Extensions[{3}]);", scope,
162                    name, type, Descriptor.Index);
163            }
164        }
165
166        internal void GenerateExtensionRegistrationCode(TextGenerator writer)
167        {
168            writer.WriteLine("registry.Add({0}.{1});", scope, name);
169        }
170
171        public override void WriteHash(TextGenerator writer)
172        {
173        }
174
175        public override void WriteEquals(TextGenerator writer)
176        {
177        }
178
179        public override void WriteToString(TextGenerator writer)
180        {
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.