**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Runtime.Serialization / Formatter.cs
blob7dcae1f3b0932bd83b93ca84b64cc0818b1f0236
1 //
2 // System.Runtime.Serialization.Formatter.cs
3 //
4 // Authors:
5 // Duncan Mak (duncan@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.
9 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System;
35 using System.Collections;
36 using System.IO;
38 namespace System.Runtime.Serialization
40 [CLSCompliant (false)]
41 [Serializable]
42 public abstract class Formatter : IFormatter
44 protected Formatter ()
48 protected ObjectIDGenerator m_idGenerator;
49 protected Queue m_objectQueue;
51 public abstract SerializationBinder Binder {
52 get;
53 set;
56 public abstract StreamingContext Context {
57 get;
58 set;
61 public abstract ISurrogateSelector SurrogateSelector {
62 get;
63 set;
66 public abstract object Deserialize (Stream serializationStream);
68 protected virtual object GetNext (out long objID)
70 if (m_objectQueue.Count == 0)
72 // set the out field to 0
73 objID = 0L;
74 return null;
77 Object o = m_objectQueue.Dequeue ();
78 bool FirstTime;
79 objID = m_idGenerator.HasId (o, out FirstTime);
81 return o;
84 protected virtual long Schedule (object obj)
86 if (obj == null)
87 return 0L;
89 bool FirstTime;
90 long ID = m_idGenerator.GetId (obj, out FirstTime);
91 if (FirstTime)
92 m_objectQueue.Enqueue (obj);
94 return ID;
97 public abstract void Serialize (Stream serializationStream, object graph);
99 protected abstract void WriteArray (object obj, string name, Type memberType);
101 protected abstract void WriteBoolean (bool val, string name);
103 protected abstract void WriteByte (byte val, string name);
105 protected abstract void WriteChar (char val, string name);
107 protected abstract void WriteDateTime (DateTime val, string name);
109 protected abstract void WriteDecimal (Decimal val, string name);
111 protected abstract void WriteDouble (double val, string name);
113 protected abstract void WriteInt16 (short val, string name);
115 protected abstract void WriteInt32 (int val, string name);
117 protected abstract void WriteInt64 (long val, string name);
119 protected virtual void WriteMember (string memberName, object data)
121 if (data == null)
122 WriteObjectRef (data, memberName, typeof(Object));
124 Type dataType = data.GetType ();
125 if (dataType.IsArray)
126 WriteArray (data, memberName, dataType);
127 else if (dataType == typeof(bool))
128 WriteBoolean ((bool)data, memberName);
129 else if (dataType == typeof(byte))
130 WriteByte ((byte)data, memberName);
131 else if (dataType == typeof(char))
132 WriteChar ((char)data, memberName);
133 else if (dataType == typeof(DateTime))
134 WriteDateTime ((DateTime)data, memberName);
135 else if (dataType == typeof(decimal))
136 WriteDecimal ((decimal)data, memberName);
137 else if (dataType == typeof(double))
138 WriteDouble ((double)data, memberName);
139 else if (dataType == typeof(Int16))
140 WriteInt16 ((Int16)data, memberName);
141 else if (dataType == typeof(Int32))
142 WriteInt32 ((Int32)data, memberName);
143 else if (dataType == typeof(Int64))
144 WriteInt64 ((Int64)data, memberName);
145 else if (dataType == typeof(sbyte))
146 WriteSByte ((sbyte)data, memberName);
147 else if (dataType == typeof(float))
148 WriteSingle ((float)data, memberName);
149 else if (dataType == typeof(TimeSpan))
150 WriteTimeSpan ((TimeSpan)data, memberName);
151 else if (dataType == typeof(UInt16))
152 WriteUInt16 ((UInt16)data, memberName);
153 else if (dataType == typeof(UInt32))
154 WriteUInt32 ((UInt32)data, memberName);
155 else if (dataType == typeof(UInt64))
156 WriteUInt64 ((UInt64)data, memberName);
157 else if (dataType.IsValueType)
158 WriteValueType (data, memberName, dataType);
160 WriteObjectRef (data, memberName, dataType);
163 protected abstract void WriteObjectRef (object obj, string name, Type memberType);
166 [CLSCompliant (false)]
167 protected abstract void WriteSByte (sbyte val, string name);
170 protected abstract void WriteSingle (float val, string name);
172 protected abstract void WriteTimeSpan (TimeSpan val, string name);
174 [CLSCompliant (false)]
175 protected abstract void WriteUInt16 (ushort val, string name);
177 [CLSCompliant (false)]
178 protected abstract void WriteUInt32 (uint val, string name);
180 [CLSCompliant (false)]
181 protected abstract void WriteUInt64 (ulong val, string name);
183 protected abstract void WriteValueType (object obj, string name, Type memberType);