Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GenericDictionary.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 using System.Collections.Generic;
6 using System.Linq;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Serialization;
9 using SiliconStudio.Core.Serialization.Serializers;
10 
11 namespace SiliconStudio.Paradox.Assets.Materials
12 {
13  /// <summary>
14  /// A custom dictionary to keep track of the order the elements were inserted.
15  /// </summary>
16  [DataSerializer(typeof(GenericDictionary.Serializer))]
17  [DataContract("GenericDictionary")]
18  public class GenericDictionary : IDictionary<string, INodeParameter>
19  {
20  private List<KeyValuePair<string, INodeParameter>> internalDictionary;
21 
23  {
24  internalDictionary = new List<KeyValuePair<string, INodeParameter>>();
25  }
26 
27  //TODO: custom enumerator?
28  public IEnumerator<KeyValuePair<string, INodeParameter>> GetEnumerator()
29  {
30  return internalDictionary.GetEnumerator();
31  }
32 
33  IEnumerator IEnumerable.GetEnumerator()
34  {
35  return GetEnumerator();
36  }
37 
38  public void Add(KeyValuePair<string, INodeParameter> item)
39  {
40  internalDictionary.Add(item);
41  }
42 
43  public void Clear()
44  {
45  internalDictionary.Clear();
46  }
47 
48  public bool Contains(KeyValuePair<string, INodeParameter> item)
49  {
50  return internalDictionary.Contains(item);
51  }
52 
53  public void CopyTo(KeyValuePair<string, INodeParameter>[] array, int arrayIndex)
54  {
55  var copyCount = Math.Min(array.Length - arrayIndex, internalDictionary.Count);
56  for (var i = 0; i < copyCount; ++i)
57  {
58  array[arrayIndex + i] = internalDictionary[i];
59  }
60  }
61 
62  public bool Remove(KeyValuePair<string, INodeParameter> item)
63  {
64  return internalDictionary.Remove(item);
65  }
66 
67  public int Count
68  {
69  get
70  {
71  return internalDictionary.Count;
72  }
73  }
74 
75  public bool IsReadOnly
76  {
77  get
78  {
79  return false;
80  }
81  }
82 
83  public bool ContainsKey(string key)
84  {
85  return internalDictionary.Any(x => x.Key == key);
86  }
87 
88  public void Add(string key, INodeParameter value)
89  {
90  internalDictionary.Add(new KeyValuePair<string, INodeParameter>(key, value));
91  }
92 
93  public bool Remove(string key)
94  {
95  if (ContainsKey(key))
96  {
97  internalDictionary.RemoveAll(x => x.Key == key);
98  return true;
99  }
100  return false;
101  }
102 
103  public bool TryGetValue(string key, out INodeParameter value)
104  {
105  if (ContainsKey(key))
106  {
107  value = this[key];
108  return true;
109  }
110  value = null;
111  return false;
112  }
113 
114  public INodeParameter this[string key]
115  {
116  get
117  {
118  var found = internalDictionary.FirstOrDefault(x => x.Key == key).Value;
119  if (found != null)
120  return found;
121  throw new KeyNotFoundException();
122  }
123  set
124  {
125  var newValue = new KeyValuePair<string, INodeParameter>(key, value);
126  var foundIndex = internalDictionary.FindIndex(x => x.Key == key);
127  if (foundIndex >= 0)
128  internalDictionary[foundIndex] = newValue;
129  else
130  internalDictionary.Add(newValue);
131  }
132  }
133 
135  {
136  get
137  {
138  return internalDictionary.Select(x => x.Key).ToList();
139  }
140  }
141 
142  public ICollection<INodeParameter> Values
143  {
144  get
145  {
146  return internalDictionary.Select(x => x.Value).ToList();
147  }
148  }
149 
150  internal class Serializer : DataSerializer<GenericDictionary>, IDataSerializerInitializer, IDataSerializerGenericInstantiation
151  {
152 
153  private DataSerializer<KeyValuePair<string, INodeParameter>> itemDataSerializer;
154 
155  /// <inheritdoc/>
156  public void Initialize(SerializerSelector serializerSelector)
157  {
158  itemDataSerializer = serializerSelector.GetSerializer<KeyValuePair<string, INodeParameter>>();
159  }
160 
161  public override void PreSerialize(ref GenericDictionary obj, ArchiveMode mode, SerializationStream stream)
162  {
163  if (mode == ArchiveMode.Deserialize)
164  {
165  // TODO: Peek the dictionary size
166  if (obj == null)
167  obj = new GenericDictionary();
168  else
169  obj.Clear();
170  }
171  }
172 
173  /// <inheritdoc/>
174  public override void Serialize(ref GenericDictionary obj, ArchiveMode mode, SerializationStream stream)
175  {
176  if (mode == ArchiveMode.Deserialize)
177  {
178  // Should be null if it was
179  int count = stream.ReadInt32();
180  for (int i = 0; i < count; ++i)
181  {
182  var value = new KeyValuePair<string, INodeParameter>();
183  itemDataSerializer.Serialize(ref value, mode, stream);
184  obj.Add(value.Key, value.Value);
185  }
186  }
187  else if (mode == ArchiveMode.Serialize)
188  {
189  stream.Write(obj.Count);
190  foreach (var item in obj.internalDictionary)
191  {
192  itemDataSerializer.Serialize(item, stream);
193  }
194  }
195  }
196 
197  public void EnumerateGenericInstantiations(SerializerSelector serializerSelector, IList<Type> genericInstantiations)
198  {
199  genericInstantiations.Add(typeof(KeyValuePair<string, INodeParameter>));
200  }
201  }
202  }
203 }
bool Remove(KeyValuePair< string, INodeParameter > item)
A custom dictionary to keep track of the order the elements were inserted.
bool TryGetValue(string key, out INodeParameter value)
SiliconStudio.Paradox.Input.Keys Keys
bool Contains(KeyValuePair< string, INodeParameter > item)
_In_ size_t count
Definition: DirectXTexP.h:174
Base class for implementation of SerializationStream.
IEnumerator< KeyValuePair< string, INodeParameter > > GetEnumerator()
Adds initialization feature to a DataSerializer.
void Add(KeyValuePair< string, INodeParameter > item)
void CopyTo(KeyValuePair< string, INodeParameter >[] array, int arrayIndex)
Serializer context. It holds DataSerializer{T} objects and their factories.
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