Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/GeneratedMessage.cs @ 4095

Last change on this file since 4095 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.1 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections;
37using System.Collections.Generic;
38using Google.ProtocolBuffers.Collections;
39using Google.ProtocolBuffers.Descriptors;
40using Google.ProtocolBuffers.FieldAccess;
41
42namespace Google.ProtocolBuffers {
43
44  /// <summary>
45  /// All generated protocol message classes extend this class. It implements
46  /// most of the IMessage interface using reflection. Users
47  /// can ignore this class as an implementation detail.
48  /// </summary>
49  public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
50    where TMessage : GeneratedMessage<TMessage, TBuilder>
51    where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
52
53    private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
54
55    /// <summary>
56    /// Returns the message as a TMessage.
57    /// </summary>
58    protected abstract TMessage ThisMessage { get; }
59
60    internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder {
61      get { return InternalFieldAccessors; }
62    }
63
64    protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
65
66    public override MessageDescriptor DescriptorForType {
67      get { return InternalFieldAccessors.Descriptor; }
68    }
69
70    internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() {
71
72      // Use a SortedList so we'll end up serializing fields in order
73      var ret = new SortedList<FieldDescriptor, object>();
74      MessageDescriptor descriptor = DescriptorForType;
75      foreach (FieldDescriptor field in descriptor.Fields) {
76        IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
77        if (field.IsRepeated) {
78          if (accessor.GetRepeatedCount(ThisMessage) != 0) {
79            ret[field] = accessor.GetValue(ThisMessage);
80          }
81        } else if (HasField(field)) {
82          ret[field] = accessor.GetValue(ThisMessage);
83        }
84      }
85      return ret;
86    }
87
88    public override bool IsInitialized {
89      get {
90        foreach (FieldDescriptor field in DescriptorForType.Fields) {
91          // Check that all required fields are present.
92          if (field.IsRequired && !HasField(field)) {
93            return false;
94          }
95          // Check that embedded messages are initialized.
96          // This code is similar to that in AbstractMessage, but we don't
97          // fetch all the field values - just the ones we need to.
98          if (field.MappedType == MappedType.Message) {
99            if (field.IsRepeated) {
100              // We know it's an IList<T>, but not the exact type - so
101              // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
102              foreach (IMessage element in (IEnumerable)this[field]) {
103                if (!element.IsInitialized) {
104                  return false;
105                }
106              }
107            } else {
108              if (HasField(field) && !((IMessage)this[field]).IsInitialized) {
109                return false;
110              }
111            }
112          }
113        }
114        return true;
115      }
116    }
117
118    public override IDictionary<FieldDescriptor, object> AllFields {
119      get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
120    }
121
122    public override bool HasField(FieldDescriptor field) {
123      return InternalFieldAccessors[field].Has(ThisMessage);
124    }
125
126    public override int GetRepeatedFieldCount(FieldDescriptor field) {
127      return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
128    }
129
130    public override object this[FieldDescriptor field, int index] {
131      get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
132    }
133
134    public override object this[FieldDescriptor field] {
135      get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
136    }
137
138    public override UnknownFieldSet UnknownFields {
139      get { return unknownFields; }
140    }
141
142    /// <summary>
143    /// Replaces the set of unknown fields for this message. This should
144    /// only be used before a message is built, by the builder. (In the
145    /// Java code it is private, but the builder is nested so has access
146    /// to it.)
147    /// </summary>
148    internal void SetUnknownFields(UnknownFieldSet fieldSet) {
149      unknownFields = fieldSet;
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.