Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DataSerializer.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.Runtime.CompilerServices;
5 using SiliconStudio.Core.Storage;
6 
7 namespace SiliconStudio.Core.Serialization
8 {
9  /// <summary>
10  /// Describes how to serialize and deserialize an object without knowing its type.
11  /// Used as a common base class for all data serializers.
12  /// </summary>
13  public abstract class DataSerializer
14  {
15  // Binary format version, needs to be bumped in case of big changes in serialization formats (i.e. primitive types).
16  public const int BinaryFormatVersion = 1;
17 
19 
20  /// <inheritdoc/>
21  public abstract Type SerializationType { get; }
22 
23  /// <inheritdoc/>
24  public abstract bool IsBlittable { get; }
25 
26  /// <inheritdoc/>
27  public abstract void Serialize(ref object obj, ArchiveMode mode, SerializationStream stream);
28 
29  /// <inheritdoc/>
30  public abstract void PreSerialize(ref object obj, ArchiveMode mode, SerializationStream stream);
31  }
32 
33  /// <summary>
34  /// Describes how to serialize and deserialize an object of a given type.
35  /// </summary>
36  /// <typeparam name="T">The type of object to serialize or deserialize.</typeparam>
37  public abstract class DataSerializer<T> : DataSerializer
38  {
39  /// <inheritdoc/>
40  public override Type SerializationType { get { return typeof(T); } }
41 
42  /// <inheritdoc/>
43  public override bool IsBlittable { get { return false; } }
44 
45  /// <inheritdoc/>
46  public override void Serialize(ref object obj, ArchiveMode mode, SerializationStream stream)
47  {
48  var objT = (obj == null ? default(T) : (T)obj);
49  Serialize(ref objT, mode, stream);
50  obj = objT;
51  }
52 
53  /// <inheritdoc/>
54  [MethodImpl(MethodImplOptions.AggressiveInlining)]
55  public void Serialize(T obj, SerializationStream stream)
56  {
57  Serialize(ref obj, ArchiveMode.Serialize, stream);
58  }
59 
60  /// <inheritdoc/>
61  public override void PreSerialize(ref object obj, ArchiveMode mode, SerializationStream stream)
62  {
63  var objT = (obj == null ? default(T) : (T)obj);
64  PreSerialize(ref objT, mode, stream);
65  obj = objT;
66  }
67 
68  /// <inheritdoc/>
69  public virtual void PreSerialize(ref T obj, ArchiveMode mode, SerializationStream stream)
70  {
71  }
72 
73  /// <inheritdoc/>
74  public abstract void Serialize(ref T obj, ArchiveMode mode, SerializationStream stream);
75  }
76 }
void Serialize(T obj, SerializationStream stream)
virtual void PreSerialize(ref T obj, ArchiveMode mode, SerializationStream stream)
override void PreSerialize(ref object obj, ArchiveMode mode, SerializationStream stream)
Base class for implementation of SerializationStream.
override void Serialize(ref object obj, ArchiveMode mode, SerializationStream stream)
A hash to uniquely identify data.
Definition: ObjectId.cs:13
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8