/Utilities/Datatypes/Advanced/DataContainer.cs
C# | 199 lines | 121 code | 19 blank | 59 comment | 10 complexity | 8c8d3dc2a85eadedbb21670875f0c400 MD5 | raw file
1using System; 2using System.IO; 3using NUnit.Framework; 4 5namespace Delta.Utilities.Datatypes.Advanced 6{ 7 /// <summary> 8 /// This class allows to save and load primitive data which can be helpful 9 /// for classes that uses the ISaveLoadBinary interface. 10 /// </summary> 11 public class DataContainer : ISaveLoadBinary 12 { 13 #region Constants 14 /// <summary> 15 /// The current version of the implementation of this control class. 16 /// </summary> 17 private const int VersionNumber = 1; 18 #endregion 19 20 #region ByteData (Public) 21 /// <summary> 22 /// Byte data 23 /// </summary> 24 public byte ByteData; 25 #endregion 26 27 #region BoolData (Public) 28 /// <summary> 29 /// Boolean data 30 /// </summary> 31 public bool BoolData; 32 #endregion 33 34 #region IntData (Public) 35 /// <summary> 36 /// Int data 37 /// </summary> 38 public int IntData; 39 #endregion 40 41 #region FloatData (Public) 42 /// <summary> 43 /// Float data 44 /// </summary> 45 public float FloatData; 46 #endregion 47 48 #region StringData (Public) 49 /// <summary> 50 /// String data 51 /// </summary> 52 public string StringData; 53 #endregion 54 55 #region ISaveLoadBinary Members 56 /// <summary> 57 /// Loads all data of the object again which were previously saved. 58 /// </summary> 59 /// <param name="dataReader">The data of the object which were saved 60 /// before.</param> 61 public void Load(BinaryReader dataReader) 62 { 63 // We currently only support our version, if more versions are added, 64 // we need to do different loading code depending on the version here. 65 int version = dataReader.ReadInt32(); 66 switch (version) 67 { 68 // Version 1 69 case VersionNumber: 70 // Now load all previously saved data 71 ByteData = dataReader.ReadByte(); 72 BoolData = dataReader.ReadBoolean(); 73 IntData = dataReader.ReadInt32(); 74 FloatData = dataReader.ReadSingle(); 75 // Do we have also string data to load ? 76 if (dataReader.ReadBoolean()) 77 { 78 StringData = dataReader.ReadString(); 79 } 80 break; 81 82 default: 83 Log.InvalidVersionWarning(GetType().Name, version, VersionNumber); 84 break; 85 } 86 } 87 88 /// <summary> 89 /// Saves all necessary data of the object to a byte array. 90 /// </summary> 91 public void Save(BinaryWriter dataWriter) 92 { 93 // First we write the current version number of the class data format 94 dataWriter.Write(VersionNumber); 95 96 // and save all the values for that instance then 97 dataWriter.Write(ByteData); 98 dataWriter.Write(BoolData); 99 dataWriter.Write(IntData); 100 dataWriter.Write(FloatData); 101 // Make sure that we always have a valid string for saving 102 bool hasStringData = StringData != null; 103 dataWriter.Write(hasStringData); 104 if (hasStringData) 105 { 106 dataWriter.Write(StringData); 107 } 108 } 109 #endregion 110 111 #region Equals (Public) 112 /// <summary> 113 /// Equals 114 /// </summary> 115 /// <param name="otherObject">Object</param> 116 /// <returns>True if the otherObject is equal.</returns> 117 public override bool Equals(object otherObject) 118 { 119 if (otherObject is DataContainer) 120 { 121 DataContainer otherContainer = (DataContainer)otherObject; 122 123 return otherContainer.BoolData == BoolData && 124 otherContainer.ByteData == ByteData && 125 otherContainer.FloatData == FloatData && 126 otherContainer.IntData == IntData && 127 otherContainer.StringData == StringData; 128 } 129 130 return base.Equals(otherObject); 131 } 132 #endregion 133 134 #region GetHashCode (Public) 135 /// <summary> 136 /// Get hash code for this data container (will just return base hashcode). 137 /// </summary> 138 /// <returns>hash code</returns> 139 public override int GetHashCode() 140 { 141 return base.GetHashCode(); 142 } 143 #endregion 144 145 /// <summary> 146 /// Tests 147 /// </summary> 148 internal class DataContainerTests 149 { 150 #region SaveAndLoad 151 /// <summary> 152 /// Save and load 153 /// </summary> 154 [Test] 155 public void SaveAndLoad() 156 { 157 // Create any custom data 158 DataContainer data = new DataContainer 159 { 160 ByteData = 7, 161 BoolData = true, 162 IntData = 23456, 163 FloatData = 0.12345f, 164 StringData = "Container data", 165 }; 166 // and save it out 167 MemoryStream stream = new MemoryStream(); 168 BinaryWriter writer = new BinaryWriter(stream); 169 data.Save(writer); 170 171 // Next create a second instance 172 DataContainer loadedData = new DataContainer(); 173 // and make a sanity check that it hasn't already the same data 174 Assert.NotEqual(loadedData.ByteData, data.ByteData); 175 Assert.NotEqual(loadedData.BoolData, data.BoolData); 176 Assert.NotEqual(loadedData.IntData, data.IntData); 177 Assert.NotEqual(loadedData.FloatData, data.FloatData); 178 Assert.NotEqual(loadedData.StringData, data.StringData); 179 180 // Now load the saved data of the first instance 181 stream.Seek(0, SeekOrigin.Begin); 182 BinaryReader reader = new BinaryReader(stream); 183 loadedData.Load(reader); 184 185 // and check that all values are the same 186 Assert.Equal(loadedData.ByteData, data.ByteData); 187 Assert.Equal(loadedData.BoolData, data.BoolData); 188 Assert.Equal(loadedData.IntData, data.IntData); 189 Assert.Equal(loadedData.FloatData, data.FloatData); 190 Assert.Equal(loadedData.StringData, data.StringData); 191 192 string typeString = data.GetType().FullName; 193 Type type = Type.GetType(typeString); 194 Assert.Equal(type, typeof(DataContainer)); 195 } 196 #endregion 197 } 198 } 199}