PageRenderTime 31ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/Utilities/Datatypes/Advanced/DataContainer.cs

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