forked from jamescourtney/FlatSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyConstructorsExample.cs
99 lines (90 loc) · 3.9 KB
/
CopyConstructorsExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2020 James Courtney
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Samples.CopyConstructorsExample
{
using FlatSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// This sample shows the implementation and usage of a simple FlatBuffers GRPC service.
/// </summary>
public class CopyConstructorsExample
{
public static void Run()
{
FooBarContainer container = new FooBarContainer
{
fruit = Fruit.Pears,
initialized = true,
location = "location",
list = new List<FooBar>
{
new FooBar
{
name = "name",
postfix = 1,
rating = 3,
sibling = new Bar
{
ratio = 3.14f,
size = ushort.MaxValue,
time = int.MinValue,
// Nested structs are not intended to have null values,
// but it is possible due to FlatSharp modeling
// them as reference types. However, null structs
// do not cause a problem when serializing or parsing.
parent = null!,
}
}
},
};
// Simple use case: make a deep copy of an object you're using.
var copy = new FooBarContainer(container);
Debug.Assert(!object.ReferenceEquals(copy.list, container.list), "A new list is created");
for (int i = 0; i < container.list.Count; ++i)
{
var originalItem = container.list[i];
Debug.Assert(copy.list is not null);
var copyItem = copy.list[i];
Debug.Assert(!object.ReferenceEquals(copyItem, originalItem));
}
// Now let's look at how this can be useful when operating on deserialized objects.
var serializer = new FlatBufferSerializer(FlatBufferDeserializationOption.Lazy);
byte[] data = new byte[1024];
serializer.Serialize(container, data);
var deserialized = serializer.Parse<FooBarContainer>(data);
// Take a deserialized item and "upcast" it back to the original type.
// This performs a full traversal of the object and allows the underlying buffer to be reused.
Debug.Assert(deserialized.GetType() != container.GetType(), "The deserialized type is a subclass of the FooBarContainer type");
copy = new FooBarContainer(deserialized);
Debug.Assert(copy.GetType() == container.GetType(), "By using the copy constructor, we can get an instance of the original type.");
// Next: Some deserialization modes, such as Lazy, don't permit mutation of the object.
// Using the copy constructor can convert this to an object that we can mutate!
try
{
// will throw
deserialized.fruit = Fruit.Apples;
Debug.Assert(false);
}
catch
{
}
// Modifying the copy is just fine, though.
copy.fruit = Fruit.Apples;
}
}
}