Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/GeneratedMessage.cs @ 11211

Last change on this file since 11211 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.1 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 System.Collections;
39using System.Collections.Generic;
40using System.IO;
41using System.Xml;
42using Google.ProtocolBuffers.Collections;
43using Google.ProtocolBuffers.Descriptors;
44using Google.ProtocolBuffers.FieldAccess;
45
46namespace Google.ProtocolBuffers
47{
48    /// <summary>
49    /// All generated protocol message classes extend this class. It implements
50    /// most of the IMessage interface using reflection. Users
51    /// can ignore this class as an implementation detail.
52    /// </summary>
53    public abstract partial class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
54        where TMessage : GeneratedMessage<TMessage, TBuilder>
55        where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
56    {
57        private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
58
59        /// <summary>
60        /// Returns the message as a TMessage.
61        /// </summary>
62        protected abstract TMessage ThisMessage { get; }
63
64        internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder
65        {
66            get { return InternalFieldAccessors; }
67        }
68
69        protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
70
71        public override MessageDescriptor DescriptorForType
72        {
73            get { return InternalFieldAccessors.Descriptor; }
74        }
75
76        internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap()
77        {
78            // Use a SortedList so we'll end up serializing fields in order
79            var ret = new SortedList<FieldDescriptor, object>();
80            MessageDescriptor descriptor = DescriptorForType;
81            foreach (FieldDescriptor field in descriptor.Fields)
82            {
83                IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
84                if (field.IsRepeated)
85                {
86                    if (accessor.GetRepeatedCount(ThisMessage) != 0)
87                    {
88                        ret[field] = accessor.GetValue(ThisMessage);
89                    }
90                }
91                else if (HasField(field))
92                {
93                    ret[field] = accessor.GetValue(ThisMessage);
94                }
95            }
96            return ret;
97        }
98
99        public override bool IsInitialized
100        {
101            get
102            {
103                foreach (FieldDescriptor field in DescriptorForType.Fields)
104                {
105                    // Check that all required fields are present.
106                    if (field.IsRequired && !HasField(field))
107                    {
108                        return false;
109                    }
110                    // Check that embedded messages are initialized.
111                    // This code is similar to that in AbstractMessage, but we don't
112                    // fetch all the field values - just the ones we need to.
113                    if (field.MappedType == MappedType.Message)
114                    {
115                        if (field.IsRepeated)
116                        {
117                            // We know it's an IList<T>, but not the exact type - so
118                            // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
119                            foreach (IMessageLite element in (IEnumerable) this[field])
120                            {
121                                if (!element.IsInitialized)
122                                {
123                                    return false;
124                                }
125                            }
126                        }
127                        else
128                        {
129                            if (HasField(field) && !((IMessageLite) this[field]).IsInitialized)
130                            {
131                                return false;
132                            }
133                        }
134                    }
135                }
136                return true;
137            }
138        }
139
140        public override IDictionary<FieldDescriptor, object> AllFields
141        {
142            get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
143        }
144
145        public override bool HasField(FieldDescriptor field)
146        {
147            return InternalFieldAccessors[field].Has(ThisMessage);
148        }
149
150        public override int GetRepeatedFieldCount(FieldDescriptor field)
151        {
152            return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
153        }
154
155        public override object this[FieldDescriptor field, int index]
156        {
157            get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
158        }
159
160        public override object this[FieldDescriptor field]
161        {
162            get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
163        }
164
165        public override UnknownFieldSet UnknownFields
166        {
167            get { return unknownFields; }
168        }
169
170        /// <summary>
171        /// Replaces the set of unknown fields for this message. This should
172        /// only be used before a message is built, by the builder. (In the
173        /// Java code it is private, but the builder is nested so has access
174        /// to it.)
175        /// </summary>
176        internal void SetUnknownFields(UnknownFieldSet fieldSet)
177        {
178            unknownFields = fieldSet;
179        }
180    }
181}
Note: See TracBrowser for help on using the repository browser.