[runtime] Updates comments.
[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 [System.Runtime.InteropServices.ComVisibleAttribute (true)]
43 public abstract class Formatter : IFormatter
44 {
45         protected Formatter ()
46         {
47         }
48         
49         protected ObjectIDGenerator m_idGenerator = new ObjectIDGenerator ();
50         protected Queue m_objectQueue = new Queue ();
51
52         public abstract SerializationBinder Binder {
53                 get;
54                 set;
55         }
56
57         public abstract StreamingContext Context {
58                 get;
59                 set;
60         }
61
62         public abstract ISurrogateSelector SurrogateSelector {
63                 get;
64                 set;
65         }
66
67         public abstract object Deserialize (Stream serializationStream);
68
69         protected virtual object GetNext (out long objID)
70         {
71                 if (m_objectQueue.Count == 0)
72                 {
73                         // set the out field to 0
74                         objID = 0L;
75                         return null;
76                 }
77
78                 Object o = m_objectQueue.Dequeue ();
79                 bool FirstTime;
80                 objID = m_idGenerator.HasId (o, out FirstTime);
81
82                 return o;
83         }
84
85         protected virtual long Schedule (object obj)
86         {
87                 if (obj == null)
88                         return 0L;
89
90                 bool FirstTime;
91                 long ID = m_idGenerator.GetId (obj, out FirstTime);
92                 if (FirstTime)
93                         m_objectQueue.Enqueue (obj);
94
95                 return ID;
96         }
97
98         public abstract void Serialize (Stream serializationStream, object graph);
99          
100         protected abstract void WriteArray (object obj, string name, Type memberType);
101          
102         protected abstract void WriteBoolean (bool val, string name);
103          
104         protected abstract void WriteByte (byte val, string name);
105          
106         protected abstract void WriteChar (char val, string name);
107          
108         protected abstract void WriteDateTime (DateTime val, string name);
109
110         protected abstract void WriteDecimal (Decimal val, string name);
111
112         protected abstract void WriteDouble (double val, string name);
113
114         protected abstract void WriteInt16 (short val, string name);
115
116         protected abstract void WriteInt32 (int val, string name);
117
118         protected abstract void WriteInt64 (long val, string name);
119
120         protected virtual void WriteMember (string memberName, object data)
121         {
122                 if (data == null)
123                         WriteObjectRef (data, memberName, typeof(Object));
124
125                 Type dataType = data.GetType ();
126                 if (dataType.IsArray)
127                         WriteArray (data, memberName, dataType);
128                 else if (dataType == typeof(bool))
129                         WriteBoolean ((bool)data, memberName);
130                 else if (dataType == typeof(byte))
131                         WriteByte ((byte)data, memberName);
132                 else if (dataType == typeof(char))
133                         WriteChar ((char)data, memberName);
134                 else if (dataType == typeof(DateTime))
135                         WriteDateTime ((DateTime)data, memberName);
136                 else if (dataType == typeof(decimal))
137                         WriteDecimal ((decimal)data, memberName);
138                 else if (dataType == typeof(double))
139                         WriteDouble ((double)data, memberName);
140                 else if (dataType == typeof(Int16))
141                         WriteInt16 ((Int16)data, memberName);
142                 else if (dataType == typeof(Int32))
143                         WriteInt32 ((Int32)data, memberName);
144                 else if (dataType == typeof(Int64))
145                         WriteInt64 ((Int64)data, memberName);
146                 else if (dataType == typeof(sbyte))
147                         WriteSByte ((sbyte)data, memberName);
148                 else if (dataType == typeof(float))
149                         WriteSingle ((float)data, memberName);
150                 else if (dataType == typeof(TimeSpan))
151                         WriteTimeSpan ((TimeSpan)data, memberName);
152                 else if (dataType == typeof(UInt16))
153                         WriteUInt16 ((UInt16)data, memberName);
154                 else if (dataType == typeof(UInt32))
155                         WriteUInt32 ((UInt32)data, memberName);
156                 else if (dataType == typeof(UInt64))
157                         WriteUInt64 ((UInt64)data, memberName);
158                 else if (dataType.IsValueType)
159                         WriteValueType (data, memberName, dataType);
160
161                 WriteObjectRef (data, memberName, dataType);
162         }
163
164         protected abstract void WriteObjectRef (object obj, string name, Type memberType);
165
166
167         [CLSCompliant (false)]
168         protected abstract void WriteSByte (sbyte val, string name);
169
170
171         protected abstract void WriteSingle (float val, string name);
172         
173         protected abstract void WriteTimeSpan (TimeSpan val, string name);
174
175         [CLSCompliant (false)]
176         protected abstract void WriteUInt16 (ushort val, string name);
177
178         [CLSCompliant (false)]
179         protected abstract void WriteUInt32 (uint val, string name);
180
181         [CLSCompliant (false)]
182         protected abstract void WriteUInt64 (ulong val, string name);
183
184         protected abstract void WriteValueType (object obj, string name, Type memberType);      
185 }
186 }