/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
- using System;
- using System.IO;
- using NUnit.Framework;
-
- namespace Delta.Utilities.Datatypes.Advanced
- {
- /// <summary>
- /// This class allows to save and load primitive data which can be helpful
- /// for classes that uses the ISaveLoadBinary interface.
- /// </summary>
- public class DataContainer : ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// The current version of the implementation of this control class.
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region ByteData (Public)
- /// <summary>
- /// Byte data
- /// </summary>
- public byte ByteData;
- #endregion
-
- #region BoolData (Public)
- /// <summary>
- /// Boolean data
- /// </summary>
- public bool BoolData;
- #endregion
-
- #region IntData (Public)
- /// <summary>
- /// Int data
- /// </summary>
- public int IntData;
- #endregion
-
- #region FloatData (Public)
- /// <summary>
- /// Float data
- /// </summary>
- public float FloatData;
- #endregion
-
- #region StringData (Public)
- /// <summary>
- /// String data
- /// </summary>
- public string StringData;
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Loads all data of the object again which were previously saved.
- /// </summary>
- /// <param name="dataReader">The data of the object which were saved
- /// before.</param>
- public void Load(BinaryReader dataReader)
- {
- // We currently only support our version, if more versions are added,
- // we need to do different loading code depending on the version here.
- int version = dataReader.ReadInt32();
- switch (version)
- {
- // Version 1
- case VersionNumber:
- // Now load all previously saved data
- ByteData = dataReader.ReadByte();
- BoolData = dataReader.ReadBoolean();
- IntData = dataReader.ReadInt32();
- FloatData = dataReader.ReadSingle();
- // Do we have also string data to load ?
- if (dataReader.ReadBoolean())
- {
- StringData = dataReader.ReadString();
- }
- break;
-
- default:
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- break;
- }
- }
-
- /// <summary>
- /// Saves all necessary data of the object to a byte array.
- /// </summary>
- public void Save(BinaryWriter dataWriter)
- {
- // First we write the current version number of the class data format
- dataWriter.Write(VersionNumber);
-
- // and save all the values for that instance then
- dataWriter.Write(ByteData);
- dataWriter.Write(BoolData);
- dataWriter.Write(IntData);
- dataWriter.Write(FloatData);
- // Make sure that we always have a valid string for saving
- bool hasStringData = StringData != null;
- dataWriter.Write(hasStringData);
- if (hasStringData)
- {
- dataWriter.Write(StringData);
- }
- }
- #endregion
-
- #region Equals (Public)
- /// <summary>
- /// Equals
- /// </summary>
- /// <param name="otherObject">Object</param>
- /// <returns>True if the otherObject is equal.</returns>
- public override bool Equals(object otherObject)
- {
- if (otherObject is DataContainer)
- {
- DataContainer otherContainer = (DataContainer)otherObject;
-
- return otherContainer.BoolData == BoolData &&
- otherContainer.ByteData == ByteData &&
- otherContainer.FloatData == FloatData &&
- otherContainer.IntData == IntData &&
- otherContainer.StringData == StringData;
- }
-
- return base.Equals(otherObject);
- }
- #endregion
-
- #region GetHashCode (Public)
- /// <summary>
- /// Get hash code for this data container (will just return base hashcode).
- /// </summary>
- /// <returns>hash code</returns>
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
- #endregion
-
- /// <summary>
- /// Tests
- /// </summary>
- internal class DataContainerTests
- {
- #region SaveAndLoad
- /// <summary>
- /// Save and load
- /// </summary>
- [Test]
- public void SaveAndLoad()
- {
- // Create any custom data
- DataContainer data = new DataContainer
- {
- ByteData = 7,
- BoolData = true,
- IntData = 23456,
- FloatData = 0.12345f,
- StringData = "Container data",
- };
- // and save it out
- MemoryStream stream = new MemoryStream();
- BinaryWriter writer = new BinaryWriter(stream);
- data.Save(writer);
-
- // Next create a second instance
- DataContainer loadedData = new DataContainer();
- // and make a sanity check that it hasn't already the same data
- Assert.NotEqual(loadedData.ByteData, data.ByteData);
- Assert.NotEqual(loadedData.BoolData, data.BoolData);
- Assert.NotEqual(loadedData.IntData, data.IntData);
- Assert.NotEqual(loadedData.FloatData, data.FloatData);
- Assert.NotEqual(loadedData.StringData, data.StringData);
-
- // Now load the saved data of the first instance
- stream.Seek(0, SeekOrigin.Begin);
- BinaryReader reader = new BinaryReader(stream);
- loadedData.Load(reader);
-
- // and check that all values are the same
- Assert.Equal(loadedData.ByteData, data.ByteData);
- Assert.Equal(loadedData.BoolData, data.BoolData);
- Assert.Equal(loadedData.IntData, data.IntData);
- Assert.Equal(loadedData.FloatData, data.FloatData);
- Assert.Equal(loadedData.StringData, data.StringData);
-
- string typeString = data.GetType().FullName;
- Type type = Type.GetType(typeString);
- Assert.Equal(type, typeof(DataContainer));
- }
- #endregion
- }
- }
- }