Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Reference.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;
5 
6 using SiliconStudio.Core;
7 
8 namespace SiliconStudio.Quantum.References
9 {
10  internal static class Reference
11  {
12  private static int creatingReference;
13 
14  /// <summary>
15  /// A constant value used as index of a reference that is not in a collection.
16  /// </summary>
17  internal static readonly object NotInCollection = new object();
18 
19  internal static IReference CreateReference(object objectValue, Type objectType, object index)
20  {
21  if (objectValue != null && !objectType.IsInstanceOfType(objectValue)) throw new ArgumentException(@"objectValue type does not match objectType", "objectValue");
22 
23  ++creatingReference;
24 
25  IReference reference;
26  var enumerableValue = objectValue as IEnumerable;
27 
28  if (enumerableValue != null && index == NotInCollection)
29  {
30  reference = new ReferenceEnumerable(enumerableValue, objectType, index);
31  }
32  else
33  {
34  reference = new ObjectReference(objectValue, objectType, index);
35  }
36 
37  --creatingReference;
38 
39  return reference;
40  }
41 
42  internal static Type GetReferenceType(object objectValue, object index)
43  {
44  return objectValue is IEnumerable && index == NotInCollection ? typeof(ReferenceEnumerable) : typeof(ObjectReference);
45  }
46 
47  internal static void CheckReferenceCreationSafeGuard()
48  {
49  if (creatingReference == 0) throw new InvalidOperationException("A reference can only be constructed with the method Reference.CreateReference");
50  }
51  }
52 }
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.