Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SerializerExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Runtime.CompilerServices;
7 
8 namespace SiliconStudio.Core.Serialization
9 {
10  using SiliconStudio.Core.Reflection;
11 
12  /// <summary>
13  /// Various useful extension methods on top of SerializationStream for serialization/deserialization of common types.
14  /// </summary>
15  public static class SerializerExtensions
16  {
17  public static T Clone<T>(T obj)
18  {
19  var memoryStream = new MemoryStream();
20  var writer = new BinarySerializationWriter(memoryStream);
21  writer.SerializeExtended(obj, ArchiveMode.Serialize, null);
22  writer.Flush();
23 
24  T result = default(T);
25  memoryStream.Seek(0, SeekOrigin.Begin);
26  var reader = new BinarySerializationReader(memoryStream);
27  reader.SerializeExtended(ref result, ArchiveMode.Deserialize, null);
28  return result;
29  }
30 
31  /// <summary>
32  /// Serializes the specified object.
33  /// </summary>
34  /// <typeparam name="T">The object type to serialize.</typeparam>
35  /// <param name="stream">The stream to serialize to.</param>
36  /// <param name="obj">The object to serialize.</param>
37  /// <param name="mode">The serialization mode.</param>
38  /// <param name="dataSerializer">The data serializer (can be null).</param>
39  [MethodImpl(MethodImplOptions.AggressiveInlining)]
40  public static void SerializeExtended<T>(this SerializationStream stream, T obj, ArchiveMode mode, DataSerializer<T> dataSerializer = null)
41  {
42  MemberReuseSerializer<T>.SerializeExtended(ref obj, mode, stream, dataSerializer);
43  }
44 
45  /// <summary>
46  /// Serializes the specified object.
47  /// </summary>
48  /// <typeparam name="T">The object type to serialize.</typeparam>
49  /// <param name="stream">The stream to serialize to.</param>
50  /// <param name="obj">The object to serialize.</param>
51  /// <param name="mode">The serialization mode.</param>
52  /// <param name="dataSerializer">The data serializer (can be null).</param>
53  [MethodImpl(MethodImplOptions.AggressiveInlining)]
54  public static void SerializeExtended<T>(this SerializationStream stream, ref T obj, ArchiveMode mode, DataSerializer<T> dataSerializer = null)
55  {
56  MemberReuseSerializer<T>.SerializeExtended(ref obj, mode, stream, dataSerializer);
57  }
58 
59  /// <summary>
60  /// Reads the specified object from the stream.
61  /// </summary>
62  /// <typeparam name="T">The type of the object to read.</typeparam>
63  /// <param name="stream">The stream to read the object from.</param>
64  /// <returns>The object that has just been read.</returns>
65  [MethodImpl(MethodImplOptions.AggressiveInlining)]
66  public static T Read<T>(this SerializationStream stream)
67  {
68  T result = default(T);
69  stream.Serialize(ref result, ArchiveMode.Deserialize);
70  return result;
71  }
72  /// <summary>
73  /// Writes the specified object to the stream.
74  /// </summary>
75  /// <typeparam name="T">The type of the object to write.</typeparam>
76  /// <param name="stream">The stream to write the object to.</param>
77  /// <param name="obj">The object to write.</param>
78  [MethodImpl(MethodImplOptions.AggressiveInlining)]
79  public static void Write<T>(this SerializationStream stream, T obj)
80  {
81  Serialize(stream, ref obj, ArchiveMode.Serialize);
82  }
83 
84  /// <summary>
85  /// Serializes the specified object.
86  /// </summary>
87  /// <typeparam name="T"></typeparam>
88  /// <param name="stream">The stream to serialize to.</param>
89  /// <param name="obj">The object to serialize.</param>
90  /// <param name="mode">The serialization mode.</param>
91  [MethodImpl(MethodImplOptions.AggressiveInlining)]
92  public static void Serialize<T>(this SerializationStream stream, ref T obj, ArchiveMode mode)
93  {
94  DataSerializer<T> dataSerializer = stream.Context.SerializerSelector.GetSerializer<T>();
95  if (dataSerializer == null)
96  throw new InvalidOperationException(string.Format("Could not find serializer for type {0}.", typeof(T)));
97 
98  dataSerializer.PreSerialize(ref obj, mode, stream);
99  dataSerializer.Serialize(ref obj, mode, stream);
100  }
101 
102  /// <summary>
103  /// Reads a boolean value from the stream.
104  /// </summary>
105  /// <param name="stream">The stream.</param>
106  /// <returns>A boolean value read from the stream.</returns>
107  [MethodImpl(MethodImplOptions.AggressiveInlining)]
108  public static bool ReadBoolean(this SerializationStream stream)
109  {
110  bool value = false;
111  stream.Serialize(ref value);
112  return value;
113  }
114 
115  /// <summary>
116  /// Reads a 4-byte floating point value from the stream.
117  /// </summary>
118  /// <param name="stream">The stream.</param>
119  /// <returns>A 4-byte floating point value read from the stream.</returns>
120  [MethodImpl(MethodImplOptions.AggressiveInlining)]
121  public static float ReadSingle(this SerializationStream stream)
122  {
123  float value = 0.0f;
124  stream.Serialize(ref value);
125  return value;
126  }
127 
128  /// <summary>
129  /// Reads a 8-byte floating point value from the stream.
130  /// </summary>
131  /// <param name="stream">The stream.</param>
132  /// <returns>A 8-byte floating point value read from the stream.</returns>
133  [MethodImpl(MethodImplOptions.AggressiveInlining)]
134  public static double ReadDouble(this SerializationStream stream)
135  {
136  double value = 0.0;
137  stream.Serialize(ref value);
138  return value;
139  }
140 
141  /// <summary>
142  /// Reads a 2-byte signed integer from the stream.
143  /// </summary>
144  /// <param name="stream">The stream.</param>
145  /// <returns>A 2-byte signed integer read from the stream.</returns>
146  [MethodImpl(MethodImplOptions.AggressiveInlining)]
147  public static short ReadInt16(this SerializationStream stream)
148  {
149  short value = 0;
150  stream.Serialize(ref value);
151  return value;
152  }
153 
154  /// <summary>
155  /// Reads a 4-byte signed integer from the stream.
156  /// </summary>
157  /// <param name="stream">The stream.</param>
158  /// <returns>A 4-byte signed integer read from the stream.</returns>
159  [MethodImpl(MethodImplOptions.AggressiveInlining)]
160  public static int ReadInt32(this SerializationStream stream)
161  {
162  int value = 0;
163  stream.Serialize(ref value);
164  return value;
165  }
166 
167  /// <summary>
168  /// Reads a 8-byte signed integer from the stream.
169  /// </summary>
170  /// <param name="stream">The stream.</param>
171  /// <returns>A 8-byte signed integer read from the stream.</returns>
172  [MethodImpl(MethodImplOptions.AggressiveInlining)]
173  public static long ReadInt64(this SerializationStream stream)
174  {
175  long value = 0;
176  stream.Serialize(ref value);
177  return value;
178  }
179 
180  /// <summary>
181  /// Reads a 2-byte unsigned integer from the stream.
182  /// </summary>
183  /// <param name="stream">The stream.</param>
184  /// <returns>A 2-byte unsigned integer read from the stream.</returns>
185  [MethodImpl(MethodImplOptions.AggressiveInlining)]
186  public static ushort ReadUInt16(this SerializationStream stream)
187  {
188  ushort value = 0;
189  stream.Serialize(ref value);
190  return value;
191  }
192 
193  /// <summary>
194  /// Reads a 4-byte unsigned integer from the stream.
195  /// </summary>
196  /// <param name="stream">The stream.</param>
197  /// <returns>A 4-byte unsigned integer read from the stream.</returns>
198  [MethodImpl(MethodImplOptions.AggressiveInlining)]
199  public static uint ReadUInt32(this SerializationStream stream)
200  {
201  uint value = 0;
202  stream.Serialize(ref value);
203  return value;
204  }
205 
206  /// <summary>
207  /// Reads a 8-byte unsigned integer from the stream.
208  /// </summary>
209  /// <param name="stream">The stream.</param>
210  /// <returns>A 8-byte unsigned integer read from the stream.</returns>
211  [MethodImpl(MethodImplOptions.AggressiveInlining)]
212  public static ulong ReadUInt64(this SerializationStream stream)
213  {
214  ulong value = 0;
215  stream.Serialize(ref value);
216  return value;
217  }
218 
219  /// <summary>
220  /// Reads a string.
221  /// </summary>
222  /// <param name="stream">The stream.</param>
223  /// <returns>A string read from the stream.</returns>
224  [MethodImpl(MethodImplOptions.AggressiveInlining)]
225  public static string ReadString(this SerializationStream stream)
226  {
227  string value = null;
228  stream.Serialize(ref value);
229  return value;
230  }
231 
232  /// <summary>
233  /// Reads a unicode character from the stream.
234  /// </summary>
235  /// <param name="stream">The stream.</param>
236  /// <returns>A unicode character read from the stream.</returns>
237  [MethodImpl(MethodImplOptions.AggressiveInlining)]
238  public static char ReadChar(this SerializationStream stream)
239  {
240  char value = '\0';
241  stream.Serialize(ref value);
242  return value;
243  }
244 
245  /// <summary>
246  /// Reads a unsigned byte integer from the stream.
247  /// </summary>
248  /// <param name="stream">The stream.</param>
249  /// <returns>An unsigned byte read from the stream.</returns>
250  [MethodImpl(MethodImplOptions.AggressiveInlining)]
251  public static byte ReadByte(this SerializationStream stream)
252  {
253  byte value = 0;
254  stream.Serialize(ref value);
255  return value;
256  }
257 
258  /// <summary>
259  /// Reads a signed byte from the stream.
260  /// </summary>
261  /// <param name="stream">The stream.</param>
262  /// <returns>A signed byte read from the stream.</returns>
263  [MethodImpl(MethodImplOptions.AggressiveInlining)]
264  public static sbyte ReadSByte(this SerializationStream stream)
265  {
266  sbyte value = 0;
267  stream.Serialize(ref value);
268  return value;
269  }
270 
271  /// <summary>
272  /// Reads the specified number of bytes.
273  /// </summary>
274  /// <param name="stream">The stream.</param>
275  /// <returns>A byte array containing the data read from the stream.</returns>
276  [MethodImpl(MethodImplOptions.AggressiveInlining)]
277  public static byte[] ReadBytes(this SerializationStream stream, int count)
278  {
279  byte[] value = new byte[count];
280  stream.Serialize(value, 0, count);
281  return value;
282  }
283 
284  /// <summary>
285  /// Writes a boolean value to the specified stream.
286  /// </summary>
287  /// <param name="stream">The stream.</param>
288  /// <param name="value">The boolean value to write.</param>
289  /// <returns>The stream.</returns>
290  [MethodImpl(MethodImplOptions.AggressiveInlining)]
291  public static SerializationStream Write(this SerializationStream stream, bool value)
292  {
293  stream.Serialize(ref value);
294  return stream;
295  }
296 
297  /// <summary>
298  /// Writes a 4-byte floating point value to the specified stream.
299  /// </summary>
300  /// <param name="stream">The stream.</param>
301  /// <param name="value">The 4-byte floating point value to write.</param>
302  /// <returns>The stream.</returns>
303  [MethodImpl(MethodImplOptions.AggressiveInlining)]
304  public static SerializationStream Write(this SerializationStream stream, float value)
305  {
306  stream.Serialize(ref value);
307  return stream;
308  }
309 
310  /// <summary>
311  /// Writes a 8-byte floating point value to the specified stream.
312  /// </summary>
313  /// <param name="stream">The stream.</param>
314  /// <param name="value">The 8-byte floating point value to write.</param>
315  /// <returns>The stream.</returns>
316  [MethodImpl(MethodImplOptions.AggressiveInlining)]
317  public static SerializationStream Write(this SerializationStream stream, double value)
318  {
319  stream.Serialize(ref value);
320  return stream;
321  }
322 
323  /// <summary>
324  /// Writes a 2-byte signed integer to the specified stream.
325  /// </summary>
326  /// <param name="stream">The stream.</param>
327  /// <param name="value">The 2-byte signed integer to write.</param>
328  /// <returns>The stream.</returns>
329  [MethodImpl(MethodImplOptions.AggressiveInlining)]
330  public static SerializationStream Write(this SerializationStream stream, short value)
331  {
332  stream.Serialize(ref value);
333  return stream;
334  }
335 
336  /// <summary>
337  /// Writes a 4-byte signed integer to the specified stream.
338  /// </summary>
339  /// <param name="stream">The stream.</param>
340  /// <param name="value">The 4-byte signed integer to write.</param>
341  /// <returns>The stream.</returns>
342  [MethodImpl(MethodImplOptions.AggressiveInlining)]
343  public static SerializationStream Write(this SerializationStream stream, int value)
344  {
345  stream.Serialize(ref value);
346  return stream;
347  }
348 
349  /// <summary>
350  /// Writes a 8-byte signed integer to the specified stream.
351  /// </summary>
352  /// <param name="stream">The stream.</param>
353  /// <param name="value">The 8-byte signed integer to write.</param>
354  /// <returns>The stream.</returns>
355  [MethodImpl(MethodImplOptions.AggressiveInlining)]
356  public static SerializationStream Write(this SerializationStream stream, long value)
357  {
358  stream.Serialize(ref value);
359  return stream;
360  }
361 
362  /// <summary>
363  /// Writes a 2-byte unsigned integer to the specified stream.
364  /// </summary>
365  /// <param name="stream">The stream.</param>
366  /// <param name="value">The 2-byte unsigned integer to write.</param>
367  /// <returns>The stream.</returns>
368  [MethodImpl(MethodImplOptions.AggressiveInlining)]
369  public static SerializationStream Write(this SerializationStream stream, ushort value)
370  {
371  stream.Serialize(ref value);
372  return stream;
373  }
374 
375  /// <summary>
376  /// Writes a 4-byte unsigned integer to the specified stream.
377  /// </summary>
378  /// <param name="stream">The stream.</param>
379  /// <param name="value">The 4-byte unsigned integer to write.</param>
380  /// <returns>The stream.</returns>
381  [MethodImpl(MethodImplOptions.AggressiveInlining)]
382  public static SerializationStream Write(this SerializationStream stream, uint value)
383  {
384  stream.Serialize(ref value);
385  return stream;
386  }
387 
388  /// <summary>
389  /// Writes a 8-byte unsigned integer to the specified stream.
390  /// </summary>
391  /// <param name="stream">The stream.</param>
392  /// <param name="value">The 8-byte unsigned integer to write.</param>
393  /// <returns>The stream.</returns>
394  [MethodImpl(MethodImplOptions.AggressiveInlining)]
395  public static SerializationStream Write(this SerializationStream stream, ulong value)
396  {
397  stream.Serialize(ref value);
398  return stream;
399  }
400 
401  /// <summary>
402  /// Writes a string to the specified stream.
403  /// </summary>
404  /// <param name="stream">The stream.</param>
405  /// <param name="value">The string to write.</param>
406  /// <returns>The stream.</returns>
407  [MethodImpl(MethodImplOptions.AggressiveInlining)]
408  public static SerializationStream Write(this SerializationStream stream, string value)
409  {
410  stream.Serialize(ref value);
411  return stream;
412  }
413 
414  /// <summary>
415  /// Writes a unicode character to the specified stream.
416  /// </summary>
417  /// <param name="stream">The stream.</param>
418  /// <param name="value">The unicode character to write.</param>
419  /// <returns>The stream.</returns>
420  [MethodImpl(MethodImplOptions.AggressiveInlining)]
421  public static SerializationStream Write(this SerializationStream stream, char value)
422  {
423  stream.Serialize(ref value);
424  return stream;
425  }
426 
427  /// <summary>
428  /// Writes an unsigned byte to the specified stream.
429  /// </summary>
430  /// <param name="stream">The stream.</param>
431  /// <param name="value">The unsigned byte to write.</param>
432  /// <returns>The stream.</returns>
433  [MethodImpl(MethodImplOptions.AggressiveInlining)]
434  public static SerializationStream Write(this SerializationStream stream, byte value)
435  {
436  stream.Serialize(ref value);
437  return stream;
438  }
439 
440  /// <summary>
441  /// Writes a signed byte to the specified stream.
442  /// </summary>
443  /// <param name="stream">The stream.</param>
444  /// <param name="value">The signed byte to write.</param>
445  /// <returns>The stream.</returns>
446  [MethodImpl(MethodImplOptions.AggressiveInlining)]
447  public static SerializationStream Write(this SerializationStream stream, sbyte value)
448  {
449  stream.Serialize(ref value);
450  return stream;
451  }
452 
453  /// <summary>
454  /// Writes a byte array region to the specified stream.
455  /// </summary>
456  /// <param name="stream">The stream.</param>
457  /// <param name="values">The byte array to write.</param>
458  /// <param name="offset">The starting offset in values to write.</param>
459  /// <param name="count">The number of bytes to write.</param>
460  /// <returns>
461  /// The stream.
462  /// </returns>
463  [MethodImpl(MethodImplOptions.AggressiveInlining)]
464  public static SerializationStream Write(this SerializationStream stream, byte[] values, int offset, int count)
465  {
466  stream.Serialize(values, offset, count);
467  return stream;
468  }
469  }
470 }
static uint ReadUInt32(this SerializationStream stream)
Reads a 4-byte unsigned integer from the stream.
static SerializationStream Write(this SerializationStream stream, int value)
Writes a 4-byte signed integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, byte[] values, int offset, int count)
Writes a byte array region to the specified stream.
static SerializationStream Write(this SerializationStream stream, double value)
Writes a 8-byte floating point value to the specified stream.
static char ReadChar(this SerializationStream stream)
Reads a unicode character from the stream.
static int ReadInt32(this SerializationStream stream)
Reads a 4-byte signed integer from the stream.
static byte[] ReadBytes(this SerializationStream stream, int count)
Reads the specified number of bytes.
Various useful extension methods on top of SerializationStream for serialization/deserialization of c...
static SerializationStream Write(this SerializationStream stream, sbyte value)
Writes a signed byte to the specified stream.
static string ReadString(this SerializationStream stream)
Reads a string.
static SerializationStream Write(this SerializationStream stream, short value)
Writes a 2-byte signed integer to the specified stream.
Implements SerializationStream as a binary writer.
static double ReadDouble(this SerializationStream stream)
Reads a 8-byte floating point value from the stream.
_In_ size_t count
Definition: DirectXTexP.h:174
static float ReadSingle(this SerializationStream stream)
Reads a 4-byte floating point value from the stream.
static byte ReadByte(this SerializationStream stream)
Reads a unsigned byte integer from the stream.
static SerializationStream Write(this SerializationStream stream, long value)
Writes a 8-byte signed integer to the specified stream.
Base class for implementation of SerializationStream.
static ushort ReadUInt16(this SerializationStream stream)
Reads a 2-byte unsigned integer from the stream.
static SerializationStream Write(this SerializationStream stream, byte value)
Writes an unsigned byte to the specified stream.
static SerializationStream Write(this SerializationStream stream, string value)
Writes a string to the specified stream.
Implements SerializationStream as a binary reader.
static SerializationStream Write(this SerializationStream stream, char value)
Writes a unicode character to the specified stream.
static short ReadInt16(this SerializationStream stream)
Reads a 2-byte signed integer from the stream.
static long ReadInt64(this SerializationStream stream)
Reads a 8-byte signed integer from the stream.
static ulong ReadUInt64(this SerializationStream stream)
Reads a 8-byte unsigned integer from the stream.
static bool ReadBoolean(this SerializationStream stream)
Reads a boolean value from the stream.
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
Describes how to serialize and deserialize an object of a given type.
static SerializationStream Write(this SerializationStream stream, bool value)
Writes a boolean value to the specified stream.
static SerializationStream Write(this SerializationStream stream, ushort value)
Writes a 2-byte unsigned integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, ulong value)
Writes a 8-byte unsigned integer to the specified stream.
static SerializationStream Write(this SerializationStream stream, float value)
Writes a 4-byte floating point value to the specified stream.
static sbyte ReadSByte(this SerializationStream stream)
Reads a signed byte from the stream.
static SerializationStream Write(this SerializationStream stream, uint value)
Writes a 4-byte unsigned integer to the specified stream.