This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / Formatter.cs
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 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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.
32 //
33
34 using System;
35 using System.Collections;
36 using System.IO;
37
38 namespace System.Runtime.Serialization
39 {
40 [CLSCompliant (false)]
41 [Serializable]
42 public abstract class Formatter : IFormatter
43 {
44         protected Formatter ()
45         {
46         }
47         
48         protected ObjectIDGenerator m_idGenerator;
49         protected Queue m_objectQueue;
50
51         public abstract SerializationBinder Binder {
52                 get;
53                 set;
54         }
55
56         public abstract StreamingContext Context {
57                 get;
58                 set;
59         }
60
61         public abstract ISurrogateSelector SurrogateSelector {
62                 get;
63                 set;
64         }
65
66         public abstract object Deserialize (Stream serializationStream);
67
68         protected virtual object GetNext (out long objID)
69         {
70                 if (m_objectQueue.Count == 0)
71                 {
72                         // set the out field to 0
73                         objID = 0L;
74                         return null;
75                 }
76
77                 Object o = m_objectQueue.Dequeue ();
78                 bool FirstTime;
79                 objID = m_idGenerator.HasId (o, out FirstTime);
80
81                 return o;
82         }
83
84         protected virtual long Schedule (object obj)
85         {
86                 if (obj == null)
87                         return 0L;
88
89                 bool FirstTime;
90                 long ID = m_idGenerator.GetId (obj, out FirstTime);
91                 if (FirstTime)
92                         m_objectQueue.Enqueue (obj);
93
94                 return ID;
95         }
96
97         public abstract void Serialize (Stream serializationStream, object graph);
98          
99         protected abstract void WriteArray (object obj, string name, Type memberType);
100          
101         protected abstract void WriteBoolean (bool val, string name);
102          
103         protected abstract void WriteByte (byte val, string name);
104          
105         protected abstract void WriteChar (char val, string name);
106          
107         protected abstract void WriteDateTime (DateTime val, string name);
108
109         protected abstract void WriteDecimal (Decimal val, string name);
110
111         protected abstract void WriteDouble (double val, string name);
112
113         protected abstract void WriteInt16 (short val, string name);
114
115         protected abstract void WriteInt32 (int val, string name);
116
117         protected abstract void WriteInt64 (long val, string name);
118
119         protected virtual void WriteMember (string memberName, object data)
120         {
121                 if (data == null)
122                         WriteObjectRef (data, memberName, typeof(Object));
123
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);
159
160                 WriteObjectRef (data, memberName, dataType);
161         }
162
163         protected abstract void WriteObjectRef (object obj, string name, Type memberType);
164
165
166         [CLSCompliant (false)]
167         protected abstract void WriteSByte (sbyte val, string name);
168
169
170         protected abstract void WriteSingle (float val, string name);
171         
172         protected abstract void WriteTimeSpan (TimeSpan val, string name);
173
174         [CLSCompliant (false)]
175         protected abstract void WriteUInt16 (ushort val, string name);
176
177         [CLSCompliant (false)]
178         protected abstract void WriteUInt32 (uint val, string name);
179
180         [CLSCompliant (false)]
181         protected abstract void WriteUInt64 (ulong val, string name);
182
183         protected abstract void WriteValueType (object obj, string name, Type memberType);      
184 }
185 }