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