Beispiel #1
0
        /// <summary>
        /// Function to write an array of value types to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static void WriteArrayValues(MemoryStream stream)
        {
            stream.Position = 0;

            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var expected = new SomeTestData[3];

                for (int i = 1; i < 4; ++i)
                {
                    expected[i - 1] = new SomeTestData
                    {
                        Value1 = i,
                        Value2 = System.Math.PI * i,
                        Value3 = (short)(i & 2)
                    };
                }

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading an array of value types to a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.WriteRange(expected);

                stream.Position = 0;

                var actual = new SomeTestData[4];
                reader.ReadRange(actual, 1);

                for (int i = 1; i < 4; ++i)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"int32 Value1 = {expected[i - 1].Value1}: {actual[i].Value1 == expected[i - 1].Value1}");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"double Value2 = {expected[i - 1].Value2:0.00000}: {actual[i].Value2.EqualsEpsilon(expected[i - 1].Value2)}");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write($"[{i - 1}] ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine($"int16 Value3 = {expected[i - 1].Value3}: {actual[i].Value3 == expected[i - 1].Value3}");
                }

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Function to write the contents pointed at by an unsafe pointer to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static unsafe void WritePointer(MemoryStream stream)
        {
            stream.Position = 0;
            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var data = new SomeTestData
                {
                    Value1 = 1,
                    Value2 = 2.1,
                    Value3 = 3
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading pointer to memory into a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.Write(&data, Unsafe.SizeOf <SomeTestData>());

                stream.Position = 0;

                SomeTestData readData = default;
                reader.Read(&readData, Unsafe.SizeOf <SomeTestData>());

                Console.WriteLine($"int32 Value1 = 1: {readData.Value1 == 1}");
                Console.WriteLine($"double Value2 = 2.1: {readData.Value2.EqualsEpsilon(2.1)}");
                Console.WriteLine($"int16 Value3 = 3: {readData.Value3 == 3}");

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Function to write the contents of a value type to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static void WriteByRefValueType(MemoryStream stream)
        {
            stream.Position = 0;
            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var data = new SomeTestData
                {
                    Value1 = 1234,
                    Value2 = 3.1459,
                    Value3 = 42
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading a value type to a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.WriteValue(ref data);

                stream.Position = 0;

                reader.ReadValue(out SomeTestData readData);

                Console.WriteLine($"int32 Value1 = 1234: {readData.Value1 == 1234}");
                Console.WriteLine($"double Value2 = 3.1459: {readData.Value2.EqualsEpsilon(3.1459)}");
                Console.WriteLine($"int16 Value3 = 42: {readData.Value3 == 42}");

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }