wasSharp – Diff between revs 27 and 39

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 27 Rev 39
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.IO; 9 using System.IO;
10 using System.Xml; 10 using System.Xml;
11 using System.Xml.Schema; 11 using System.Xml.Schema;
12 using System.Xml.Serialization; 12 using System.Xml.Serialization;
13   13  
14 namespace wasSharp.Collections.Generic 14 namespace wasSharp.Collections.Generic
15 { 15 {
16 /// <summary> 16 /// <summary>
17 /// A serializable dictionary class. 17 /// A serializable dictionary class.
18 /// </summary> 18 /// </summary>
19 /// <typeparam name="TKey">the key</typeparam> 19 /// <typeparam name="TKey">the key</typeparam>
20 /// <typeparam name="TValue">the value</typeparam> 20 /// <typeparam name="TValue">the value</typeparam>
21 public class SerializableDictionary<TKey, TValue> 21 public class SerializableDictionary<TKey, TValue>
22 : Dictionary<TKey, TValue>, IXmlSerializable 22 : Dictionary<TKey, TValue>, IXmlSerializable
23 { 23 {
24 public XmlSchema GetSchema() 24 public XmlSchema GetSchema()
25 { 25 {
26 return null; 26 return null;
27 } 27 }
28   28  
29 public void ReadXml(XmlReader reader) 29 public void ReadXml(XmlReader reader)
30 { 30 {
31 if (reader.IsEmptyElement) 31 if (reader.IsEmptyElement)
32 { 32 {
33 return; 33 return;
34 } 34 }
35   35  
36 reader.ReadStartElement(); 36 reader.ReadStartElement();
37 while (!reader.NodeType.Equals(XmlNodeType.EndElement)) 37 while (!reader.NodeType.Equals(XmlNodeType.EndElement))
38 { 38 {
39 reader.ReadStartElement(ItemNodeName); 39 reader.ReadStartElement(ItemNodeName);
40   40  
41 reader.ReadStartElement(KeyNodeName); 41 reader.ReadStartElement(KeyNodeName);
42 var key = (TKey)KeySerializer.Deserialize(reader); 42 var key = (TKey)KeySerializer.Deserialize(reader);
43 reader.ReadEndElement(); 43 reader.ReadEndElement();
44   44  
45 reader.ReadStartElement(ValueNodeName); 45 reader.ReadStartElement(ValueNodeName);
46 var value = (TValue)ValueSerializer.Deserialize(reader); 46 var value = (TValue)ValueSerializer.Deserialize(reader);
47 reader.ReadEndElement(); 47 reader.ReadEndElement();
48   48  
49 Add(key, value); 49 Add(key, value);
50   50  
51 reader.ReadEndElement(); 51 reader.ReadEndElement();
52 reader.MoveToContent(); 52 reader.MoveToContent();
53 } 53 }
54 reader.ReadEndElement(); 54 reader.ReadEndElement();
55 } 55 }
56   56  
57 public void WriteXml(XmlWriter writer) 57 public void WriteXml(XmlWriter writer)
58 { 58 {
59 foreach (var key in Keys) 59 foreach (var key in Keys)
60 { 60 {
61 writer.WriteStartElement(ItemNodeName); 61 writer.WriteStartElement(ItemNodeName);
62   62  
63 writer.WriteStartElement(KeyNodeName); 63 writer.WriteStartElement(KeyNodeName);
64 KeySerializer.Serialize(writer, key); 64 KeySerializer.Serialize(writer, key);
65 writer.WriteEndElement(); 65 writer.WriteEndElement();
66   66  
67 writer.WriteStartElement(ValueNodeName); 67 writer.WriteStartElement(ValueNodeName);
68 ValueSerializer.Serialize(writer, this[key]); 68 ValueSerializer.Serialize(writer, this[key]);
69 writer.WriteEndElement(); 69 writer.WriteEndElement();
70   70  
71 writer.WriteEndElement(); 71 writer.WriteEndElement();
72 } 72 }
73 } 73 }
74   -  
75 /// <summary> -  
76 /// Deep-clones the serializable dictionary. -  
77 /// </summary> -  
78 /// <returns>a deep clone of the original dictionary</returns> -  
79 public SerializableDictionary<TKey, TValue> Clone() -  
80 { -  
81 SerializableDictionary<TKey, TValue> clone; -  
82 try -  
83 { -  
84 using (var memoryStream = new MemoryStream()) -  
85 { -  
86 new XmlSerializer( -  
87 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName)) -  
88 .Serialize(memoryStream, this); -  
89 memoryStream.Position = 0; -  
90 clone = (SerializableDictionary<TKey, TValue>) -  
91 new XmlSerializer( -  
92 typeof(SerializableDictionary<TKey, TValue>), new XmlRootAttribute(DictionaryNodeName)) -  
93 .Deserialize(memoryStream); -  
94 } -  
95 } -  
96 /* cloning failed so return an empty dictionary */ -  
97 catch (Exception) -  
98 { -  
99 clone = new SerializableDictionary<TKey, TValue>(); -  
100 } -  
101 return clone; -  
102 } -  
103   74  
104 #region Constants 75 #region Constants
105   76  
106 public string DictionaryNodeName { get; set; } = "Dictionary"; 77 public readonly string DictionaryNodeName = @"Dictionary";
107 public string ItemNodeName { get; set; } = "Item"; 78 public readonly string ItemNodeName = @"Item";
108 public string KeyNodeName { get; set; } = "Key"; 79 public readonly string KeyNodeName = @"Key";
109 public string ValueNodeName { get; set; } = "Value"; 80 public readonly string ValueNodeName = @"Value";
110   81  
111 #endregion Constants 82 #endregion Constants
112   83  
113 #region Constructors 84 #region Constructors
114   85  
115 public SerializableDictionary() 86 public SerializableDictionary()
116 { 87 {
117 } 88 }
118   89  
119 public SerializableDictionary(IDictionary<TKey, TValue> dictionary) 90 public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
120 : base(dictionary) 91 : base(dictionary)
121 { 92 {
122 } 93 }
123   94  
124 public SerializableDictionary(IEqualityComparer<TKey> comparer) 95 public SerializableDictionary(IEqualityComparer<TKey> comparer)
125 : base(comparer) 96 : base(comparer)
126 { 97 {
127 } 98 }
128   99  
129 public SerializableDictionary(int capacity) 100 public SerializableDictionary(int capacity)
130 : base(capacity) 101 : base(capacity)
131 { 102 {
132 } 103 }
133   104  
134 public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) 105 public SerializableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
135 : base(dictionary, comparer) 106 : base(dictionary, comparer)
136 { 107 {
137 } 108 }
138   109  
139 public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer) 110 public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
140 : base(capacity, comparer) 111 : base(capacity, comparer)
141 { 112 {
142 } 113 }
143   114  
144 #endregion Constructors 115 #endregion Constructors
145   116  
146 #region Private Properties 117 #region Private Properties
147   118  
148 protected XmlSerializer ValueSerializer 119 protected XmlSerializer ValueSerializer
149 => valueSerializer ?? (valueSerializer = new XmlSerializer(typeof(TValue))); 120 => valueSerializer ?? (valueSerializer = new XmlSerializer(typeof(TValue)));
150   121  
151 private XmlSerializer KeySerializer => keySerializer ?? (keySerializer = new XmlSerializer(typeof(TKey))); 122 private XmlSerializer KeySerializer => keySerializer ?? (keySerializer = new XmlSerializer(typeof(TKey)));
152   123  
153 #endregion Private Properties 124 #endregion Private Properties
154   125  
155 #region Private Members 126 #region Private Members
156   127  
157 private XmlSerializer keySerializer; 128 private XmlSerializer keySerializer;
158 private XmlSerializer valueSerializer; 129 private XmlSerializer valueSerializer;
159   130  
160 #endregion Private Members 131 #endregion Private Members
161 } 132 }
162 } 133 }
163   134