Beispiel #1
0
        public void Sample()
        {
            //It is impossible to inherit a struct in other struct
            //but we can implement interfaces

            //STRUCTS ARE VALUE TYPES
            //CLASSES ARE REFERENCE TYPES


            SampleStruct firstStruct; //This is a "group" of variables. All the variables contained on SampleStruct may be accessed

            //IMPORTANT: AUTOMATICALLY INITIALIZES THE DEFAULT VALUES FOR THE DECLARED TYPES

            firstStruct.a = 1;
            firstStruct.b = 1;
            firstStruct.c = 1;
            firstStruct.d = 1;


            //ANOTHER WAY TO USE
            SampleStruct secondStruct;

            secondStruct = new SampleStruct()
            {
                a = 1, b = 2, c = 3, d = 4
            };

            //THIRD WAY
            var otherStruct = new OtherStruct(1, 2);
        }
Beispiel #2
0
        public void TestManualMarshalStringToPtr()
        {
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            try
            {
                string      testString  = "hello, my name is Test!";
                StringClass stringClass = new StringClass
                {
                    SimpleString = testString
                };

                OtherStruct otherStruct = new OtherStruct
                {
                    OtherInt    = 42,
                    OtherString = "blablabla"
                };

                StringStructure stringStructure = new StringStructure
                {
                    OtherField = otherStruct
                };

                Assert.Throws <ArgumentNullException>(() =>
                                                      MarshalUtils.AllStringsToPtrs <StringClass, StringStructure>(
                                                          null,
                                                          ref stringStructure,
                                                          allocatedPointers));

                Assert.Throws <ArgumentNullException>(() =>
                                                      MarshalUtils.AllStringsToPtrs(
                                                          stringClass,
                                                          ref stringStructure,
                                                          null));

                MarshalUtils.AllStringsToPtrs(
                    stringClass,
                    ref stringStructure,
                    allocatedPointers);

                string restoredString = MarshalUtils.PtrToString(stringStructure.SimpleString);
                Assert.AreEqual(stringClass.SimpleString, restoredString);
                Assert.AreEqual(1, allocatedPointers.Count);
                Assert.AreEqual(stringStructure.OtherField.OtherInt, otherStruct.OtherInt);
                Assert.AreEqual(stringStructure.OtherField.OtherString, otherStruct.OtherString);
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
            }
        }
Beispiel #3
0
        public void TestManualMarshalPtrToString()
        {
            IntPtr pTestString = IntPtr.Zero;

            try
            {
                string testString = "hello, my name is Test!";
                pTestString = MarshalUtils.StringToPtr(testString);
                StringStructure stringStructure = new StringStructure
                {
                    SimpleString = pTestString
                };

                OtherStruct otherStruct = new OtherStruct
                {
                    OtherInt    = 42,
                    OtherString = "blablabla"
                };

                StringClass stringClass = new StringClass
                {
                    OtherField = otherStruct
                };

                Assert.Throws <ArgumentNullException>(() =>
                                                      MarshalUtils.AllPtrsToStrings <StringStructure, StringClass>(
                                                          stringStructure,
                                                          null));
                MarshalUtils.AllPtrsToStrings(stringStructure, stringClass);
                Assert.NotNull(stringClass.SimpleString);
                Assert.AreEqual(stringClass.SimpleString, testString);
                Assert.AreEqual(stringClass.OtherField.OtherInt, otherStruct.OtherInt);
                Assert.AreEqual(stringClass.OtherField.OtherString, otherStruct.OtherString);
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(pTestString);
            }
        }
            static void Test(FlatBufferDeserializationOption option)
            {
                var table = new Table <WriteThroughStruct>
                {
                    Struct = new WriteThroughStruct
                    {
                        Value = new OtherStruct {
                            Prop1 = 10, Prop2 = 10
                        },
                        ValueStruct = new() { Value = 3, }
                    }
                };

                FlatBufferSerializer serializer = new FlatBufferSerializer(option);

                byte[] buffer = new byte[1024];
                serializer.Serialize(table, buffer);

                // parse
                var parsed1 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                // mutate
                Assert.Equal(10, parsed1.Struct.Value.Prop1);
                Assert.Equal(10, parsed1.Struct.Value.Prop2);
                Assert.Equal(3, parsed1.Struct.ValueStruct.Value);
                parsed1.Struct.Value = new OtherStruct {
                    Prop1 = 300, Prop2 = 300
                };
                parsed1.Struct.ValueStruct = new() { Value = -1 };
                Assert.Equal(300, parsed1.Struct.Value.Prop1);
                Assert.Equal(300, parsed1.Struct.Value.Prop2);
                Assert.Equal(-1, parsed1.Struct.ValueStruct.Value);

                // verify, set to null
                var parsed2 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                Assert.Equal(300, parsed2.Struct.Value.Prop1);
                Assert.Equal(300, parsed2.Struct.Value.Prop2);
                Assert.Equal(-1, parsed2.Struct.ValueStruct.Value);
                parsed2.Struct.Value = null !;

                if (option == FlatBufferDeserializationOption.Progressive)
                {
                    // we are null temporarily until we re-parse.
                    Assert.Null(parsed2.Struct.Value);
                }
                else if (option == FlatBufferDeserializationOption.Lazy)
                {
                    // lazy write through clears it out.
                    Assert.Equal(0, parsed2.Struct.Value.Prop1);
                    Assert.Equal(0, parsed2.Struct.Value.Prop2);
                }
                else
                {
                    Assert.False(true);
                }

                // verify, set to null
                var parsed3 = serializer.Parse <Table <WriteThroughStruct> >(buffer);

                Assert.Equal(0, parsed3.Struct.Value.Prop1);
                Assert.Equal(0, parsed3.Struct.Value.Prop2);
            }

            Test(FlatBufferDeserializationOption.Progressive);
            Test(FlatBufferDeserializationOption.Lazy);
        }