Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/CustomSerialization.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.2 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.Runtime.Serialization;
39
40/*
41 * This entire source file is not supported on the Silverlight platform
42 */
43#if !SILVERLIGHT
44namespace Google.ProtocolBuffers
45{
46    /*
47     * Specialized handing of *all* message types.  Messages are serialized into a byte[] and stored
48     * into the SerializationInfo, and are then reconstituted by an IObjectReference class after
49     * deserialization.  IDeserializationCallback is supported on both the Builder and Message.
50     */
51    [Serializable]
52    partial class AbstractMessageLite<TMessage, TBuilder> : ISerializable
53    {
54        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
55        {
56            info.SetType(typeof(SerializationSurrogate));
57            info.AddValue("message", ToByteArray());
58            info.AddValue("initialized", IsInitialized);
59        }
60
61        [Serializable]
62        private sealed class SerializationSurrogate : IObjectReference, ISerializable
63        {
64            static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
65            private readonly byte[] _message;
66            private readonly bool _initialized;
67
68            private SerializationSurrogate(SerializationInfo info, StreamingContext context)
69            {
70                _message = (byte[])info.GetValue("message", typeof(byte[]));
71                _initialized = info.GetBoolean("initialized");
72            }
73
74            object IObjectReference.GetRealObject(StreamingContext context)
75            {
76                ExtensionRegistry registry = context.Context as ExtensionRegistry;
77                TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
78                builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
79
80                IDeserializationCallback callback = builder as IDeserializationCallback;
81                if(callback != null)
82                {
83                    callback.OnDeserialization(context);
84                }
85
86                TMessage message = _initialized ? builder.Build() : builder.BuildPartial();
87                callback = message as IDeserializationCallback;
88                if (callback != null)
89                {
90                    callback.OnDeserialization(context);
91                }
92
93                return message;
94            }
95
96            void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
97            {
98                info.AddValue("message", _message);
99            }
100        }
101    }
102
103    [Serializable]
104    partial class AbstractBuilderLite<TMessage, TBuilder> : ISerializable
105    {
106        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
107        {
108            info.SetType(typeof(SerializationSurrogate));
109            info.AddValue("message", Clone().BuildPartial().ToByteArray());
110        }
111
112        [Serializable]
113        private sealed class SerializationSurrogate : IObjectReference, ISerializable
114        {
115            static readonly TBuilder TemplateInstance = (TBuilder)Activator.CreateInstance(typeof(TBuilder));
116            private readonly byte[] _message;
117
118            private SerializationSurrogate(SerializationInfo info, StreamingContext context)
119            {
120                _message = (byte[])info.GetValue("message", typeof(byte[]));
121            }
122
123            object IObjectReference.GetRealObject(StreamingContext context)
124            {
125                ExtensionRegistry registry = context.Context as ExtensionRegistry;
126                TBuilder builder = TemplateInstance.DefaultInstanceForType.CreateBuilderForType();
127                builder.MergeFrom(_message, registry ?? ExtensionRegistry.Empty);
128
129                IDeserializationCallback callback = builder as IDeserializationCallback;
130                if(callback != null)
131                {
132                    callback.OnDeserialization(context);
133                }
134
135                return builder;
136            }
137
138            void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
139            {
140                info.AddValue("message", _message);
141            }
142        }
143    }
144
145    /*
146     * Spread some attribute love around, keeping this all here so we don't use conditional compliation
147     * in every one of these classes.  If we introduce a new platform that also does not support this
148     * we can control it all from this source file.
149     */
150
151    [Serializable]
152    partial class GeneratedMessageLite<TMessage, TBuilder> { }
153
154    [Serializable]
155    partial class ExtendableMessageLite<TMessage, TBuilder> { }
156
157    [Serializable]
158    partial class AbstractMessage<TMessage, TBuilder> { }
159
160    [Serializable]
161    partial class GeneratedMessage<TMessage, TBuilder> { }
162
163    [Serializable]
164    partial class ExtendableMessage<TMessage, TBuilder> { }
165
166    [Serializable]
167    partial class GeneratedBuilderLite<TMessage, TBuilder> { }
168
169    [Serializable]
170    partial class ExtendableBuilderLite<TMessage, TBuilder> { }
171
172    [Serializable]
173    partial class AbstractBuilder<TMessage, TBuilder> { }
174
175    [Serializable]
176    partial class GeneratedBuilder<TMessage, TBuilder> { }
177
178    [Serializable]
179    partial class ExtendableBuilder<TMessage, TBuilder> { }
180
181    [Serializable]
182    partial class DynamicMessage
183    {
184        [Serializable]
185        partial class Builder { }
186    }
187}
188#endif
Note: See TracBrowser for help on using the repository browser.