34 |
35 |
36 |
--------------------------------------------------------------------------------
/benchmarks/README.md:
--------------------------------------------------------------------------------
1 | This folder contains the output of the FlatSharp benchmarks. These are "living" benchmarks, so old versions of FlatSharp are run against new Benchmarks that are defined later, when possible.
2 | These folders contain the outputs of a specific version of FlatSharp's benchmark results. So, the ```3.0.0``` folder contains the raw data for the benchmark run that used
3 | FlatSharp version 3.0.0.
--------------------------------------------------------------------------------
/codecov.yml:
--------------------------------------------------------------------------------
1 | codecov:
2 | require_ci_to_pass: yes
3 |
4 | ignore:
5 | - "src/Tests/**/*"
6 | - "src/Benchmarks/**/*"
7 | - "src/Google.FlatBuffers/**/*"
8 | - "src/Google.FlatBuffers/*"
9 |
10 | coverage:
11 | precision: 2
12 | round: down
13 | range: "70...100"
14 |
15 | parsers:
16 | gcov:
17 | branch_detection:
18 | conditional: yes
19 | loop: yes
20 | method: no
21 | macro: no
22 |
23 | comment:
24 | layout: "reach,diff,flags,files,footer"
25 | behavior: default
26 | require_changes: no
27 |
28 |
--------------------------------------------------------------------------------
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "7.0.0",
4 | "rollForward": "minor"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/misc/public.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/misc/public.snk
--------------------------------------------------------------------------------
/misc/strongname.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/misc/strongname.snk
--------------------------------------------------------------------------------
/samples/Assert.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Diagnostics.CodeAnalysis;
18 |
19 | namespace Samples;
20 |
21 | // Simple assertions
22 | public static class Assert
23 | {
24 | public static void True([DoesNotReturnIf(false)] bool condition, string message)
25 | {
26 | if (!condition)
27 | {
28 | throw new Exception(message);
29 | }
30 | }
31 |
32 | public static void SameObject(object? left, object? right, string message)
33 | {
34 | Assert.True(object.ReferenceEquals(left, right), message);
35 | }
36 |
37 | public static void NotSameObject(object? left, object? right, string message)
38 | {
39 | Assert.True(!object.ReferenceEquals(left, right), message);
40 | }
41 |
42 | public static void Fail(string message)
43 | {
44 | throw new Exception(message);
45 | }
46 |
47 | public static void Throws(Action callback) where TException : Exception
48 | {
49 | try
50 | {
51 | callback();
52 | Assert.Fail("Expected exception of type: " + typeof(TException).FullName);
53 | }
54 | catch (TException)
55 | {
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/samples/Example01-DeserializationModes/DeserializationModes.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | // Declare FlatSharp attributes.
18 |
19 | attribute "fs_vector";
20 | attribute "fs_serializer";
21 | attribute "fs_valueStruct";
22 |
23 | namespace Samples.DeserializationModes;
24 |
25 | // tell FlatSharp that you want a lazy serializer generated for this type.
26 | // If you don't specify, FlatSharp will generate a GreedyMutable serializer by default
27 | // though this can be changed at runtime using ISerializer.WithSettings or by passing a mode to ISerializer.Serialize.
28 |
29 | table Person (fs_serializer:"Lazy")
30 | {
31 | name : string;
32 | favorite_fruits : [ Fruit ];
33 | }
34 |
35 | table Fruit
36 | {
37 | name : string;
38 | reason : string;
39 | }
--------------------------------------------------------------------------------
/samples/Example02-Vectors/Vectors.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | // Declare FlatSharp attributes.
18 |
19 | attribute "fs_vector";
20 | attribute "fs_serializer";
21 |
22 | namespace Samples.Vectors;
23 |
24 | // Vectors (or, Lists in C# terminology) are indexed-based collections
25 | // of items sequentially laid out in the underlying buffer.
26 | // Vectors can be:
27 | // - primitives
28 | // - strings
29 | // - enums
30 | // - tables
31 | // - structs
32 | // - unions
33 |
34 | // There are a few Vector topics that get their own samples that aren't covered here:
35 | // Indexed Vectors (key/value lookups)
36 | // Sorted Vectors and Binary Search
37 | // Write Through Vectors (update the underlying buffer in-place)
38 |
39 | struct SimpleStruct { A : int; B : float; }
40 | table SimpleTable { A : int; InnerVector : [ string ]; }
41 |
42 | union SimpleUnion { SimpleStruct, SimpleTable }
43 |
44 | table LotsOfLists (fs_serializer)
45 | {
46 | // Vectors are IList by default. You can make this explicity with fs_vector:"IList"
47 | list_vector_of_table : [ SimpleTable ];
48 |
49 | // They can also be IReadOnlyList.
50 | read_only_list_vector_of_struct : [ SimpleStruct ] (fs_vector:"IReadOnlyList");
51 |
52 | // Or even unions!
53 | list_vector_of_union : [ SimpleUnion ];
54 |
55 | // Or even Memory
56 | vector_of_ubyte : [ ubyte ];
57 |
58 | // And finally ReadOnlyMemory
59 | read_only_vector_of_ubyte : [ ubyte ] (fs_vector:"ReadOnlyMemory");
60 | }
--------------------------------------------------------------------------------
/samples/Example03-IOOptions/IOOptionsExample.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | attribute "fs_serializer";
18 | attribute "fs_vector";
19 |
20 | namespace Samples.IOOptionsExample;
21 |
22 | enum Gender : ubyte { Male, Female }
23 | enum DogBreed : ubyte { GoldenRetriever, GermanShepard, BostonTerrier }
24 | enum CatBreed : ubyte { Persian, Calico, GrumpyCat }
25 | union FavoritePet { Dog, Cat }
26 |
27 | table Dog {
28 | Breed:DogBreed;
29 | Vitals:AnimalVitals;
30 | }
31 |
32 | table Cat {
33 | Breed:CatBreed;
34 | Vitals:AnimalVitals;
35 | }
36 |
37 | table AnimalVitals {
38 | Gender:Gender;
39 | Age:short;
40 | Name:string;
41 | }
42 |
43 | table Person (fs_serializer) {
44 | Name:string;
45 | Age:int;
46 |
47 | FavoritePet:FavoritePet;
48 | Cats:[Cat];
49 | Dogs:[Dog];
50 |
51 | Signature:[ubyte] (fs_vector:"ReadOnlyMemory");
52 | }
--------------------------------------------------------------------------------
/samples/Example04-gRPC/GrpcExample.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | attribute "fs_serializer";
18 | attribute "fs_rpcInterface";
19 | attribute "fs_sharedString";
20 |
21 | namespace Samples.GrpcExample;
22 |
23 | table SingleMessage (fs_serializer:"Lazy")
24 | {
25 | Message : string (required);
26 | }
27 |
28 | table MultiMessage (fs_serializer:"Lazy")
29 | {
30 | Message : [ string ] (fs_sharedString, required);
31 | }
32 |
33 | // Specify the fs_rpcInterface attribute to generate an interface for this service.
34 | rpc_service EchoService (fs_rpcInterface)
35 | {
36 | EchoUnary (SingleMessage) : SingleMessage;
37 | EchoClientStreaming (SingleMessage) : MultiMessage (streaming : "client");
38 | EchoServerStreaming (MultiMessage) : SingleMessage (streaming : "server");
39 | EchoDuplex (SingleMessage) : SingleMessage (streaming : "duplex");
40 | }
--------------------------------------------------------------------------------
/samples/Example05-CopyConstructors/CopyConstructorsExample.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | attribute "fs_serializer";
18 |
19 | namespace Samples.CopyConstructorsExample;
20 |
21 | enum Fruit : byte { Apples, Pears, Bananas }
22 |
23 | struct Foo {
24 | id:ulong;
25 | count:short;
26 | prefix:byte;
27 | length:uint;
28 | }
29 |
30 | struct Bar {
31 | parent:Foo;
32 | time:int;
33 | ratio:float;
34 | size:ushort;
35 | }
36 |
37 | table FooBar {
38 | sibling:Bar;
39 | name:string;
40 | rating:double;
41 | postfix:ubyte;
42 | }
43 |
44 | table FooBarContainer (fs_serializer:"Lazy") {
45 | list:[FooBar];
46 | initialized:bool;
47 | fruit:Fruit;
48 | location:string;
49 | }
50 |
--------------------------------------------------------------------------------
/samples/Example06-Includes/A.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | include "Subdirectory/B.fbs";
18 | include "Shared/C.fbs";
19 |
20 | namespace Samples.IncludesExample;
21 |
22 | table TableFromA {
23 | NestedTableB:TableFromB;
24 | NestedTableC:TableFromC;
25 | NestedEnum:EnumFromB;
26 | NestedStruct1:StructFromA;
27 | NestedStruct2:StructFromB;
28 | }
29 |
30 | struct StructFromA {
31 | NestedStruct:StructFromB;
32 | Value:int;
33 | }
--------------------------------------------------------------------------------
/samples/Example06-Includes/IncludesExample.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.IncludesExample;
18 |
19 | ///
20 | /// This example shows how different flatbuffer files can include each other. In this example,
21 | /// IncludesExample.fbs references A, which references B, which is in a subdirectory.
22 | /// We can create a complete serializer package and gRPC service just from the declarations in IncludesExample.cs.
23 | /// There is no code sample here -- just FBS files.
24 | ///
25 | public class IncludesExample : IFlatSharpSample
26 | {
27 | public bool HasConsoleOutput => false;
28 |
29 | public void Run()
30 | {
31 | // No code here. Take a look at the FBS file!
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/samples/Example06-Includes/IncludesExample.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | include "A.fbs";
18 |
19 | attribute "fs_serializer";
20 |
21 | namespace Samples.IncludesExample;
22 |
23 | table Request (fs_serializer) {
24 | Table:TableFromA;
25 | }
26 |
27 | rpc_service Service
28 | {
29 | // Single input, single output.
30 | SingleOperation(Request):Request;
31 | }
--------------------------------------------------------------------------------
/samples/Example06-Includes/Shared/C.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.IncludesExample;
18 |
19 | table TableFromC {
20 | String:string;
21 | }
22 |
--------------------------------------------------------------------------------
/samples/Example06-Includes/Subdirectory/B.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | include "Shared/C.fbs";
18 |
19 | namespace Samples.IncludesExample;
20 |
21 | enum EnumFromB : ubyte { One, Two, Three }
22 |
23 | table TableFromB {
24 | NestedTableC:TableFromC;
25 | String:string;
26 | }
27 |
28 | struct StructFromB {
29 | IntValue:int;
30 | LongValue:int64;
31 | }
--------------------------------------------------------------------------------
/samples/Example07-SortedVectors/SortedVectors.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.SortedVectors;
18 |
19 | attribute "fs_serializer";
20 | attribute "fs_sortedVector";
21 |
22 | table UserList (fs_serializer) {
23 | users:[User] (fs_sortedVector); // Declare the vector to be sorted.
24 | }
25 |
26 | table User {
27 | first_name:string;
28 | last_name:string;
29 | SSN:string (key, required); // Use the SSN field as the key to sort the user table by. Best practice is for keys to be required.
30 | }
--------------------------------------------------------------------------------
/samples/Example08-IndexedVectors/IndexedVectors.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.IndexedVectors;
18 |
19 | attribute "fs_defaultCtor";
20 | attribute "fs_vector";
21 | attribute "fs_setter";
22 | attribute "fs_serializer";
23 |
24 | table IndexedVectorTable (fs_serializer:"Progressive")
25 | {
26 | Users : [ User ] (fs_vector:"IIndexedVector");
27 | }
28 |
29 | // If using something before .NET 7, consider using fs_defaultCtor:"None" on the table
30 | // so you can define a constructor that forces Id to be defined.
31 | table User
32 | {
33 | id : string (key, required, fs_setter:"PublicInit");
34 | first_name : string;
35 | last_name : string;
36 | }
37 |
--------------------------------------------------------------------------------
/samples/Example09-Unions/Unions.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | attribute "fs_serializer";
18 |
19 | namespace Samples.Unions;
20 |
21 | enum DogBreed : byte { Golden, Lab, Corgi }
22 | enum CatBreed : byte { Calico, Shorthair, Bengal }
23 | enum FishKind : byte { Coelacanth, Dorado, Piranha, Puffer }
24 |
25 | // FlatSharp generates a "Pet" union.
26 | // In this example, the "Doggo" label is applied, so all of the union's properties in C# will
27 | // refer to "Doggo". For fish and Cat, the names are left as-is.
28 | // If you want to include strings in your union, you need to indicate a label for the string (this is a flatc quirk).
29 | union Pet { Doggo : Dog, Cat, Fish, someString : string }
30 |
31 | table Dog { Breed:DogBreed; Name:string; }
32 | table Cat { Breed:CatBreed; Name:string; }
33 | table Fish { Kind:FishKind; Weight:float64; Name:string; }
34 |
35 | table Person
36 | {
37 | // A person can have one pet. Either a dog, cat, or fish.
38 | Pet : Pet;
39 |
40 | // We can also store unions in a vector.
41 | PreviousPets : [ Pet ];
42 | }
--------------------------------------------------------------------------------
/samples/Example10-SharedStrings/SharedStrings.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.SharedStrings;
18 |
19 | attribute "fs_serializer";
20 | attribute "fs_sharedString";
21 |
22 | table Database (fs_serializer) {
23 | Rows : [ Row ];
24 | }
25 |
26 | table Row {
27 | Values : [ Column ];
28 | }
29 |
30 | table Column {
31 | // the shared string metadata is necessary to enable shared strings. Specifying it in the schema
32 | // allows you to choose which strings should be shared and which should not, rather than a global
33 | // setting one way or the other. This metadata also works with vectors of strings, shown below.
34 | ColumnName : string (fs_sharedString);
35 | Value : string;
36 | Vector : [ string ] (fs_sharedString);
37 | }
--------------------------------------------------------------------------------
/samples/Example11-StructVectors/StructVectors.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.StructVectors;
18 |
19 | attribute "fs_serializer";
20 | attribute "fs_nonVirtual";
21 | attribute "fs_valueStruct";
22 | attribute "fs_unsafeStructVector";
23 |
24 | table Transaction (fs_serializer) {
25 | amount : double;
26 | sender : string;
27 | receiver : string;
28 |
29 | hash_reference : Sha256_Reference (required);
30 | hash_value : Sha256_Value (required);
31 | hash_fast_value : Sha256_FastValue (required);
32 | }
33 |
34 | // A reference (class-based) struct representing a SHA256 value.
35 | struct Sha256_Reference
36 | {
37 | value : [ubyte:32] (fs_nonVirtual);
38 | }
39 |
40 | // A value (struct-based) struct representing the same.
41 | struct Sha256_Value (fs_valueStruct)
42 | {
43 | value : [ ubyte : 32 ];
44 | }
45 |
46 | // A value (struct-based) struct using unsafe code for fast access to the value array.
47 | // This option generates code that is not safe, but is considerably faster than the alternative.
48 | struct Sha256_FastValue (fs_valueStruct)
49 | {
50 | Value : [ ubyte : 32 ] (fs_unsafeStructVector);
51 | }
--------------------------------------------------------------------------------
/samples/Example13-ValueStructs/ValueStructs.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.ValueStructs;
18 |
19 | attribute "fs_serializer";
20 | attribute "fs_valueStruct";
21 |
22 | table Path (fs_serializer:"Lazy")
23 | {
24 | points : [ Point ];
25 | }
26 |
27 | // Beginning in FlatSharp version 5.7.0, FlatBuffer Structs can be declared as value types.
28 | struct Point (fs_valueStruct)
29 | {
30 | x : float;
31 | y : float;
32 | z : float;
33 | }
34 |
--------------------------------------------------------------------------------
/samples/Example14-UnsafeOptions/UnsafeOptions.fbs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples.UnsafeOptions;
18 |
19 | attribute "fs_serializer";
20 | attribute "fs_valueStruct";
21 | attribute "fs_unsafeExternal";
22 | attribute "fs_unsafeStructVector";
23 | attribute "fs_unsafeUnion";
24 |
25 | // Declare some structs
26 |
27 | // A 16 byte struct. Could be a GUID.
28 | struct SixteenBytes (fs_valueStruct)
29 | {
30 | // 16 bytes accessed in an unsafe manner. Much faster than the safe alternative.
31 | data : [ ubyte : 16 ] (fs_unsafeStructVector);
32 | }
33 |
34 | struct FourBytes (fs_valueStruct)
35 | {
36 | a : int;
37 | }
38 |
39 | // Will be emitted as System.Numerics.Vector3. There are runtime checks
40 | // to ensure that the size of Vector3 is the same as the size of this struct.
41 | struct Vec3 (fs_valueStruct, fs_unsafeExternal:"System.Numerics.Vector3")
42 | {
43 | data : [ float : 3 ];
44 | }
45 |
46 | struct TwentyBytes (fs_valueStruct)
47 | {
48 | sixteen : SixteenBytes;
49 | four : FourBytes;
50 | }
51 |
52 | union UnsafeUnion (fs_unsafeUnion)
53 | {
54 | SixteenBytes,
55 | FourBytes,
56 | Vec3,
57 | TwentyBytes
58 | }
59 |
60 | table RootTable (fs_serializer)
61 | {
62 | union : [ UnsafeUnion ];
63 | }
--------------------------------------------------------------------------------
/samples/GlobalUsings.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | global using System;
18 | global using System.Collections.Generic;
19 | global using System.Diagnostics;
20 | global using System.Linq;
21 | global using FlatSharp;
22 | global using FlatSharp.Attributes;
23 |
--------------------------------------------------------------------------------
/samples/IFlatSharpSample.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Samples;
18 |
19 | public interface IFlatSharpSample
20 | {
21 | public bool HasConsoleOutput => true;
22 |
23 | void Run();
24 | }
--------------------------------------------------------------------------------
/samples/Samples.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0
5 | exe
6 | 11.0
7 | enable
8 |
9 |
10 | true
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | Example06-Includes
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | all
36 | runtime; build; native; contentfiles; analyzers; buildtransitive
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/samples/Samples.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29709.97
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples", "Samples.csproj", "{EB6D16ED-6FB0-4205-9FCF-B971A7C2D516}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {EB6D16ED-6FB0-4205-9FCF-B971A7C2D516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {EB6D16ED-6FB0-4205-9FCF-B971A7C2D516}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {EB6D16ED-6FB0-4205-9FCF-B971A7C2D516}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {EB6D16ED-6FB0-4205-9FCF-B971A7C2D516}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {C7FFE098-183C-484B-A284-176A31AF9BAD}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/src/Benchmarks/Benchmark.6.0.0/Benchmark.6.0.0.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Exe
6 | net7.0
7 | false
8 | false
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | %(RecursiveDir)%(Filename)%(Extension)
24 |
25 |
26 | %(RecursiveDir)%(Filename)%(Extension)
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/Benchmarks/Benchmark/Benchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Exe
6 | net7.0
7 | false
8 | false
9 | $(DefineContants);CURRENT_VERSION_ONLY;FLATSHARP_7_0_0_OR_GREATER;RUN_COMPARISON_BENCHMARKS
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Benchmarks/Benchmark/FBBench/FBDeserializeBench.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Benchmark.FBBench
18 | {
19 | using BenchmarkDotNet.Attributes;
20 | using FlatSharp;
21 | using System;
22 |
23 | public class FBDeserializeBench : FBBenchCore
24 | {
25 | [Params(1, 5)]
26 | public override int TraversalCount { get; set; }
27 |
28 | [Params(
29 | FlatBufferDeserializationOption.Lazy,
30 | FlatBufferDeserializationOption.Progressive,
31 | FlatBufferDeserializationOption.Greedy,
32 | FlatBufferDeserializationOption.GreedyMutable
33 | )]
34 |
35 | public override FlatBufferDeserializationOption DeserializeOption { get; set; }
36 |
37 | [Benchmark]
38 | public override void FlatSharp_ParseAndTraverse() => base.FlatSharp_ParseAndTraverse();
39 |
40 | [Benchmark]
41 | public override void FlatSharp_ParseAndTraversePartial() => base.FlatSharp_ParseAndTraversePartial();
42 |
43 | [Benchmark]
44 | public override void FlatSharp_ParseAndTraverse_ValueStructs() => base.FlatSharp_ParseAndTraverse_ValueStructs();
45 |
46 | [Benchmark]
47 | public override void FlatSharp_ParseAndTraversePartial_ValueStructs() => base.FlatSharp_ParseAndTraversePartial_ValueStructs();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Benchmarks/Benchmark/FBBench/Google.Flatbuffers.fbs:
--------------------------------------------------------------------------------
1 | // Copyright 2015 Google Inc. All rights reserved.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 |
16 | // trying to represent a typical mix of datatypes:
17 | // 1 array of 3 elements, each element: 1 string, 3 nested objects, 9 scalars
18 | // root element has the array, additional string and a short
19 |
20 | namespace Benchmark.FBBench.Google;
21 |
22 | struct Foo {
23 | id:ulong;
24 | count:short;
25 | prefix:byte;
26 | length:uint;
27 | }
28 |
29 | struct Bar {
30 | parent:Foo;
31 | time:int;
32 | ratio:float;
33 | size:ushort;
34 | }
35 |
36 | table FooBar {
37 | sibling:Bar;
38 | name:string;
39 | rating:double;
40 | postfix:ubyte;
41 | }
42 |
43 | table FooBarContainer {
44 | list:[FooBar]; // 3 copies of the above
45 | initialized:bool;
46 | fruit:short;
47 | location:string;
48 | }
49 |
50 | table SortedVectorContainer {
51 | StringVector:[SortedVectorStringKey];
52 | IntVector:[SortedVectorIntKey];
53 | }
54 |
55 | table SortedVectorStringKey { Key:string (key); }
56 | table SortedVectorIntKey { Key:int (key); }
--------------------------------------------------------------------------------
/src/Benchmarks/Benchmark/codegen.bat:
--------------------------------------------------------------------------------
1 | ..\..\ext\flatc\windows\flatc.exe --gen-onefile --csharp --gen-object-api -o FBBench\ FBBench\Google.Flatbuffers.fbs
--------------------------------------------------------------------------------
/src/Benchmarks/ExperimentalBenchmark/Benchmark.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_nonVirtual";
4 | attribute "fs_valueStruct";
5 | attribute "fs_unsafeStructVector";
6 | attribute "fs_nonVirtual";
7 | attribute "fs_sortedVector";
8 | attribute "fs_unsafeExternal";
9 | attribute "fs_memoryMarshal";
10 | attribute "fs_vector";
11 |
12 | namespace BenchmarkCore;
13 |
14 | struct ExternalVector3(fs_unsafeExternal:"System.Numerics.Vector3", fs_valueStruct)
15 | {
16 | X : float32;
17 | Y : float32;
18 | Z : float32;
19 | }
20 |
21 | struct ExternalVectorAvx2(fs_unsafeExternal:"System.Numerics.Vector", fs_valueStruct)
22 | {
23 | Data : [ float32 : 4 ];
24 | }
25 |
26 | table Item
27 | {
28 | Key : string (key, required);
29 | Value : int;
30 | Vec3 : ExternalVector3;
31 | AVX : ExternalVectorAvx2;
32 | }
33 |
34 | table Outer (fs_serializer:"Lazy")
35 | {
36 | Items : [ Item ] (fs_vector:"IIndexedVector");
37 | }
--------------------------------------------------------------------------------
/src/Benchmarks/ExperimentalBenchmark/ExperimentalBenchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Exe
6 | net7.0
7 | false
8 | false
9 |
10 | false
11 | false
12 | $([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\..\..\FlatSharp.Compiler\bin\$(Configuration)\net7.0\FlatSharp.Compiler.dll'))
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | all
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/Benchmarks/ExperimentalBenchmark/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using BenchmarkDotNet.Attributes;
18 | using BenchmarkDotNet.Configs;
19 | using BenchmarkDotNet.Diagnosers;
20 | using BenchmarkDotNet.Environments;
21 | using BenchmarkDotNet.Jobs;
22 | using BenchmarkDotNet.Running;
23 | using FlatSharp;
24 | using FlatSharp.Internal;
25 | using System;
26 | using System.Collections.Generic;
27 | using System.Numerics;
28 | using System.Runtime.CompilerServices;
29 |
30 | namespace BenchmarkCore
31 | {
32 | public class Program
33 | {
34 | public static void Main(string[] args)
35 | {
36 | IndexedVector values = new();
37 | for (int i = 0; i < 10000; ++i)
38 | {
39 | string key = Guid.NewGuid().ToString();
40 | Item item = new Item() { Key = key, Value = i, Vec3 = new Vector3(i), AVX = new Vector(i) };
41 | values.Add(item);
42 | }
43 |
44 | Outer outer = new Outer { Items = values };
45 |
46 | byte[] buffer = new byte[Outer.Serializer.GetMaxSize(outer)];
47 | Outer.Serializer.Write(buffer, outer);
48 |
49 | var parsed = Outer.Serializer.Parse(buffer);
50 |
51 | int sum = 0;
52 | foreach (var kvp in values)
53 | {
54 | sum += parsed.Items[kvp.Key].Value;
55 | }
56 |
57 | Console.WriteLine(sum);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/Benchmarks/ExperimentalBenchmark/buildClr.cmd:
--------------------------------------------------------------------------------
1 | pushd c:\source\runtime
2 |
3 | build.cmd -c Release
4 | build.cmd -c Debug
5 |
6 | popd
--------------------------------------------------------------------------------
/src/Benchmarks/ExperimentalBenchmark/jitDump.cmd:
--------------------------------------------------------------------------------
1 |
2 | set COMPlus_JitDisasm=*
3 | set COMPlus_JitDiffableDasm=1
4 | dotnet publish -c Release
5 | robocopy /e c:\source\runtime\artifacts\bin\coreclr\Windows_NT.x64.Release .\bin\Release\net5.0\win-x64\publish
6 | copy /y c:\source\runtime\artifacts\bin\coreclr\Windows_NT.x64.Debug\clrjit.dll .\bin\Release\net5.0\win-x64\publish
7 |
8 | dotnet .\bin\Release\net5.0\win-x64\publish\ExperimentalBenchmark.dll > jit.txt
9 |
--------------------------------------------------------------------------------
/src/Benchmarks/MicroBench.6.3.3/Microbench.6.3.3.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Exe
6 | net7.0
7 | false
8 | false
9 | enable
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | all
18 | runtime; build; native; contentfiles; analyzers; buildtransitive
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/Benchmarks/MicroBench.Current/Microbench.Current.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Exe
6 | net7.0
7 | false
8 | false
9 | enable
10 | $(DefineConstants);PUBLIC_IVTABLE
11 | true
12 | $([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\..\..\FlatSharp.Compiler\bin\$(Configuration)\net7.0\FlatSharp.Compiler.dll'))
13 | false
14 | false
15 |
16 |
17 |
18 | $(DefineConstants);POOLABLE
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/Benchmarks/Microbench.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_nonVirtual";
4 | attribute "fs_valueStruct";
5 | attribute "fs_unsafeStructVector";
6 | attribute "fs_nonVirtual";
7 | attribute "fs_sortedVector";
8 | attribute "fs_writeThrough";
9 | attribute "fs_vector";
10 | attribute "fs_unsafeUnion";
11 |
12 | namespace Microbench;
13 |
14 | // Tests reading and writing a string.
15 | table StringTable (fs_serializer:"Progressive") {
16 | SingleString : string;
17 | Vector : [string];
18 | }
19 |
20 | table PrimitivesTable (fs_serializer:"Progressive") {
21 | Bool : bool;
22 | Byte : ubyte;
23 | SByte : byte;
24 | Short : short;
25 | UShort : ushort;
26 | Int : int;
27 | UInt : uint;
28 | Long : long;
29 | ULong : ulong;
30 | Double : double;
31 | Float : float;
32 | }
33 |
34 | struct RefStruct (fs_writeThrough) { Value : int; }
35 | struct ValueStruct (fs_valueStruct) { Value : int; }
36 |
37 | table StructsTable (fs_serializer:"Progressive")
38 | {
39 | SingleRef : RefStruct;
40 | SingleValue : ValueStruct (fs_writeThrough, required);
41 | VecRef : [RefStruct];
42 | VecValue : [ValueStruct] (fs_writeThrough);
43 | }
44 |
45 | table StringKey { Key : string (key); }
46 | table IntKey { Key : int (key); }
47 |
48 | table SortedTable (fs_serializer:"Progressive")
49 | {
50 | Strings : [StringKey] (fs_vector:"IIndexedVector");
51 | Ints : [IntKey] (fs_vector:"IIndexedVector");
52 | }
53 |
54 |
55 | struct ValueStructA (fs_valueStruct) { x : int; }
56 | struct ValueStructB (fs_valueStruct) { y : long; }
57 | struct ValueStructC (fs_valueStruct) { a : ValueStructA; b : ValueStructB; }
58 |
59 | union UnsafeUnion (fs_unsafeUnion) { ValueStructA, ValueStructB, ValueStructC }
60 | union SafeUnion { ValueStructA, ValueStructB, ValueStructC }
61 | union MixedUnion { ValueStructA, ValueStructB, ValueStructC, Something : string }
62 |
63 | table UnionTable (fs_serializer:"Progressive")
64 | {
65 | Unsafe : [ UnsafeUnion ];
66 | Safe : [ SafeUnion ];
67 | Mixed : [ MixedUnion ];
68 | }
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/CodeWritingPass.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | ///
20 | /// Enumerates the values for code write pass. FlatSharp compiler proceeds in several passes, getting
21 | /// closer to the final output each time. This multi-phase approach is taken to allow the compiler
22 | /// to only have a minimal understanding of the type system and the relationship between types. Instead,
23 | /// it uses reflection on previous invocations to fine-tune its approach.
24 | ///
25 | public enum CodeWritingPass
26 | {
27 | ///
28 | /// Basic definitions of types and properties are written. Output code is reflectable but not functional.
29 | ///
30 | Initialization = 1,
31 |
32 | ///
33 | /// Consumes the assembly from the initialization pass and adds the full details of property definitions. Output code has fully-defined FlatSharp
34 | /// data contracts.
35 | ///
36 | PropertyModeling = 2,
37 |
38 | ///
39 | /// Serializers are generated and included in the output.
40 | ///
41 | SerializerAndRpcGeneration = 3,
42 |
43 | LastPass = SerializerAndRpcGeneration,
44 | }
45 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/CompileContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.TypeModel;
18 |
19 | namespace FlatSharp.Compiler;
20 |
21 | ///
22 | /// Compilation context
23 | ///
24 | public record CompileContext
25 | {
26 | ///
27 | /// The current compilation pass.
28 | ///
29 | public CodeWritingPass CompilePass { get; init; }
30 |
31 | ///
32 | /// The assembly from the previous step, if any.
33 | ///
34 | public Assembly? PreviousAssembly { get; init; }
35 |
36 | ///
37 | /// The input hash.
38 | ///
39 | public string InputHash { get; init; } = string.Empty;
40 |
41 | ///
42 | /// The fully qualified name of a static method to deep-clone an item.
43 | ///
44 | public string? FullyQualifiedCloneMethodName { get; set; }
45 |
46 | ///
47 | /// The command line options.
48 | ///
49 | public CompilerOptions Options { get; init; } = new CompilerOptions();
50 |
51 | ///
52 | /// Resolves type models.
53 | ///
54 | public TypeModelContainer TypeModelContainer { get; init; } = TypeModelContainer.CreateDefault();
55 | }
56 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/DefaultConstructorKind.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | ///
20 | /// Defines styles for generating default constructors.
21 | ///
22 | public enum DefaultConstructorKind
23 | {
24 | ///
25 | /// A public default constructor.
26 | ///
27 | Public = 0,
28 |
29 | ///
30 | /// A public default constructor with the 'Obsolete' attribute applied.
31 | ///
32 | PublicObsolete = 1,
33 |
34 | ///
35 | /// No default constructor.
36 | ///
37 | None = 2,
38 | }
39 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/ErrorContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Linq;
18 | using System.Threading;
19 |
20 | namespace FlatSharp.Compiler;
21 |
22 | internal class ErrorContext
23 | {
24 | private static readonly ThreadLocal ThreadLocalContext = new ThreadLocal(() => new ErrorContext());
25 |
26 | public static ErrorContext Current => ThreadLocalContext.Value!;
27 |
28 | private readonly LinkedList<(string scope, int? lineNumber, string? context)> contextStack = new LinkedList<(string, int?, string?)>();
29 | private readonly List errors = new List();
30 |
31 | private ErrorContext()
32 | {
33 | }
34 |
35 | public IEnumerable Errors => this.errors;
36 |
37 | public void ThrowIfHasErrors()
38 | {
39 | if (this.Errors.Any())
40 | {
41 | throw new InvalidFbsFileException(this.Errors);
42 | }
43 | }
44 |
45 | public void Clear()
46 | {
47 | this.errors.Clear();
48 | }
49 |
50 | public void RegisterError(string message)
51 | {
52 | this.errors.Add(message);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/FlatSharp.Compiler.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | FlatSharp.Compiler
5 | $version$
6 | $releaseNotes$
7 | James Courtney
8 | James Courtney
9 | Compile time tools for integrating FlatSharp with FBS schemas.
10 | flatbuffers serialization flatbuffer flatsharp fbs
11 | https://github.com/jamescourtney/FlatSharp/
12 | true
13 | Apache-2.0
14 | true
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/GlobalUsings.cs:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright 2021 James Courtney
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | global using System;
19 | global using System.Collections;
20 | global using System.Collections.Generic;
21 | global using System.ComponentModel;
22 | global using System.Diagnostics;
23 | global using System.Diagnostics.CodeAnalysis;
24 | global using System.Reflection;
25 | global using System.Runtime.CompilerServices;
26 | global using FlatSharp.Internal;
27 |
28 | #if NETSTANDARD2_0
29 | global using FlatSharp.Polyfills;
30 | #endif
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Helpers.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | public static class Helpers
20 | {
21 | public static (string ns, string typeName) ParseName(string name)
22 | {
23 | name = name.Trim();
24 |
25 | int lastIndex = name.LastIndexOf('.');
26 | if (lastIndex == -1)
27 | {
28 | ErrorContext.Current.RegisterError($"Type '{name}' not within a namespace.");
29 | return ("TempNs", name);
30 | }
31 |
32 | return (name.Substring(0, lastIndex), name.Substring(lastIndex + 1));
33 | }
34 |
35 | public static string Visibility(CompileContext context)
36 | {
37 | if (context.Options.InternalTypes)
38 | {
39 | return "internal";
40 | }
41 |
42 | return "public";
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/InvalidFbsFileException.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Linq;
18 |
19 | namespace FlatSharp.Compiler;
20 |
21 | ///
22 | /// Thrown when FlatSharp.Compiler encounters an error in an FBS file.
23 | ///
24 | public class InvalidFbsFileException : Exception
25 | {
26 | public InvalidFbsFileException(IEnumerable errors) : base("Errors in FBS schema: \r\n" + string.Join("\r\n", errors))
27 | {
28 | this.Errors = errors.ToArray();
29 | }
30 |
31 | public InvalidFbsFileException(string error) : this(new[] { error })
32 | {
33 | }
34 |
35 | public string[] Errors { get; }
36 | }
37 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/RpcStreamingType.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | public enum RpcStreamingType
20 | {
21 | Unary = 0,
22 | None = 0,
23 |
24 | Client = 1,
25 | Server = 2,
26 |
27 | Bidirectional = 3,
28 | Bidi = 3,
29 | Duplex = 3,
30 | }
31 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/AdvancedFeatures.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | /*
22 | /// New schema language features that are not supported by old code generators.
23 | enum AdvancedFeatures : ulong (bit_flags) {
24 | AdvancedArrayFeatures,
25 | AdvancedUnionFeatures,
26 | OptionalScalars,
27 | DefaultVectorsAndStrings,
28 | }
29 | */
30 | [FlatBufferEnum(typeof(ulong))]
31 | [Flags]
32 | public enum AdvancedFeatures : ulong
33 | {
34 | None = 0,
35 |
36 | AdvancedArrayFeatures = 1,
37 | AdvancedUnionFeatures = 2,
38 | OptionalScalars = 4,
39 | DefaultVectorsAndStrings = 8,
40 |
41 | All = AdvancedArrayFeatures
42 | | AdvancedUnionFeatures
43 | | OptionalScalars
44 | | DefaultVectorsAndStrings,
45 | }
46 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/EnumVal.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | /*
22 | table EnumVal {
23 | name:string (required);
24 | value:long (key);
25 | object:Object (deprecated);
26 | union_type:Type;
27 | documentation:[string];
28 | }
29 | */
30 |
31 | [FlatBufferTable]
32 | public class EnumVal : ISortableTable
33 | {
34 | static EnumVal() => SortedVectorHelpers.RegisterKeyLookup(ev => ev.Value, 1);
35 |
36 | [FlatBufferItem(0, Required = true)]
37 | public virtual string Key { get; set; } = string.Empty;
38 |
39 | [FlatBufferItem(1, Key = true)]
40 | public virtual long Value { get; set; }
41 |
42 | [FlatBufferItem(2, Deprecated = true)]
43 | public virtual bool Object { get; set; }
44 |
45 | [FlatBufferItem(3)]
46 | public virtual FlatBufferType? UnionType { get; set; }
47 |
48 | [FlatBufferItem(4)]
49 | public virtual IList? Documentation { get; set; }
50 | }
51 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/INamedSchemaElement.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler.Schema;
18 |
19 | ///
20 | /// A schema element with a name.
21 | ///
22 | public interface INamedSchemaElement
23 | {
24 | string Name { get; set; }
25 |
26 | string? OriginalName { get; set; }
27 |
28 | IIndexedVector? Attributes { get; set; }
29 | }
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/KeyValue.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | [FlatBufferTable]
22 | public class KeyValue : ISortableTable
23 | {
24 | static KeyValue()
25 | {
26 | SortedVectorHelpers.RegisterKeyLookup(kv => kv.Key, 0);
27 | }
28 |
29 | [FlatBufferItem(0, Required = true, Key = true)]
30 | public virtual string Key { get; set; } = string.Empty;
31 |
32 | [FlatBufferItem(1)]
33 | public virtual string? Value { get; set; }
34 | }
35 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/RpcCall.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | /*
22 | table RPCCall {
23 | name:string (required, key);
24 | request:Object (required); // must be a table (not a struct)
25 | response:Object (required); // must be a table (not a struct)
26 | attributes:[KeyValue];
27 | documentation:[string];
28 | }
29 | */
30 | [FlatBufferTable]
31 | public class RpcCall
32 | {
33 | [FlatBufferItem(0, Required = true, Key = true)]
34 | public virtual string Name { get; set; } = string.Empty;
35 |
36 | // Must be a table.
37 | [FlatBufferItem(1, Required = true)]
38 | public virtual FlatBufferObject Request { get; set; } = new();
39 |
40 | // Must be a table.
41 | [FlatBufferItem(2, Required = true)]
42 | public virtual FlatBufferObject Response { get; set; } = new();
43 |
44 | [FlatBufferItem(3)]
45 | public virtual IIndexedVector? Attributes { get; set; }
46 |
47 | [FlatBufferItem(4)]
48 | public virtual IList? Documentation { get; set; }
49 | }
50 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/RpcService.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | /*
22 | table Service {
23 | name:string (required, key);
24 | calls:[RPCCall];
25 | attributes:[KeyValue];
26 | documentation:[string];
27 | /// File that this Service is declared in.
28 | declaration_file: string;
29 | }
30 | */
31 | [FlatBufferTable]
32 | public class RpcService
33 | {
34 | [FlatBufferItem(0, Required = true, Key = true)]
35 | public virtual string Name { get; set; } = string.Empty;
36 |
37 | // Must be a table.
38 | [FlatBufferItem(1)]
39 | public virtual IList? Calls { get; set; }
40 |
41 | [FlatBufferItem(2)]
42 | public virtual IIndexedVector? Attributes { get; set; }
43 |
44 | [FlatBufferItem(3)]
45 | public virtual IList? Documentation { get; set; }
46 |
47 | [FlatBufferItem(4, Required = true)]
48 | public virtual string DeclaringFile { get; set; } = string.Empty;
49 | }
50 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/Schema/SchemaFile.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 |
19 | namespace FlatSharp.Compiler.Schema;
20 |
21 | /*
22 | /// File specific information.
23 | /// Symbols declared within a file may be recovered by iterating over all
24 | /// symbols and examining the `declaration_file` field.
25 | table SchemaFile {
26 | /// Filename, relative to project root.
27 | filename:string (required, key);
28 | /// Names of included files, relative to project root.
29 | included_filenames:[string];
30 | }
31 |
32 | */
33 | [FlatBufferTable]
34 | public class SchemaFile : ISortableTable
35 | {
36 | static SchemaFile() => SortedVectorHelpers.RegisterKeyLookup(x => x.FileName, 0);
37 |
38 | [FlatBufferItem(0, Required = true, Key = true)]
39 | public virtual string FileName { get; set; } = string.Empty;
40 |
41 | // Must be a table.
42 | [FlatBufferItem(1)]
43 | public virtual IList? IncludedFileNames { get; set; }
44 | }
45 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/SchemaModel/FlatBufferSchemaElementType.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler.SchemaModel;
18 |
19 | public enum FlatBufferSchemaElementType
20 | {
21 | Unknown = 0,
22 | Table = 1,
23 | ReferenceStruct = 2,
24 | ValueStruct = 3,
25 | Enum = 4,
26 | Union = 5,
27 | RpcService = 6,
28 | TableField = 7,
29 | StructField = 8,
30 | ValueStructField = 9,
31 | StructVector = 10,
32 | ValueStructVector = 11,
33 | RpcCall = 12,
34 | PoolableUnion = 13,
35 | }
36 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/SchemaModel/IFlatSharpAttributes.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 | namespace FlatSharp.Compiler.SchemaModel;
19 |
20 | public interface IFlatSharpAttributes
21 | {
22 | DefaultConstructorKind? DefaultCtorKind { get; }
23 |
24 | FlatBufferDeserializationOption? DeserializationOption { get; }
25 |
26 | bool? ForceWrite { get; }
27 |
28 | MemoryMarshalBehavior? MemoryMarshalBehavior { get; }
29 |
30 | bool? PreserveFieldName { get; }
31 |
32 | bool? RpcInterface { get; }
33 |
34 | SetterKind? SetterKind { get; }
35 |
36 | bool? SharedString { get; }
37 |
38 | bool? SortedVector { get; }
39 |
40 | bool? UnsafeStructVector { get; }
41 |
42 | bool? ValueStruct { get; }
43 |
44 | VectorType? VectorKind { get; }
45 |
46 | bool? WriteThrough { get; }
47 |
48 | RpcStreamingType? StreamingType { get; }
49 |
50 | string? ExternalTypeName { get; }
51 |
52 | bool? UnsafeUnion { get; }
53 | }
54 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/SchemaMutators/ISchemaMutator.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Compiler;
18 | using FlatSharp.Compiler.Schema;
19 |
20 | ///
21 | /// Mutates a FlatBuffer FBS Schema prior to the flatsharp compiler running
22 | ///
23 | public interface ISchemaMutator
24 | {
25 | ///
26 | /// Mutates the given schema.
27 | ///
28 | /// The compiler options
29 | /// A list of delegates to invoke on the generated C#.
30 | /// The schema.
31 | void Mutate(Schema schema, CompilerOptions options, List> postProcessors);
32 | }
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/SetterKind.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | ///
20 | /// Defines styles for generating setters.
21 | ///
22 | public enum SetterKind
23 | {
24 | ///
25 | /// A public setter.
26 | ///
27 | Public = 0,
28 |
29 | ///
30 | /// A protected setter.
31 | ///
32 | Protected = 1,
33 |
34 | ///
35 | /// A protected internal setter.
36 | ///
37 | ProtectedInternal = 2,
38 |
39 | ///
40 | /// A public init-only setter.
41 | ///
42 | PublicInit = 3,
43 |
44 | ///
45 | /// A projected init-only setter.
46 | ///
47 | ProtectedInit = 4,
48 |
49 | ///
50 | /// A protected internal init-only setter.
51 | ///
52 | ProtectedInternalInit = 5,
53 |
54 | ///
55 | /// A private setter.
56 | ///
57 | Private = 6,
58 |
59 | ///
60 | /// No setter.
61 | ///
62 | None = 7,
63 | }
64 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/VectorType.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Compiler;
18 |
19 | ///
20 | /// Enumerates supported vector types.
21 | ///
22 | public enum VectorType
23 | {
24 | IList,
25 | IReadOnlyList,
26 | Memory,
27 | ReadOnlyMemory,
28 | IIndexedVector,
29 | UnityNativeArray
30 | }
31 |
--------------------------------------------------------------------------------
/src/FlatSharp.Compiler/_._:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Attributes/ExternalAttribute.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// Marks a class, struct, or enum as "external", a type that is defined in another location and not generated by FlatSharp. This is a "transient" attribute only used
21 | /// with the FlatSharp compiler. If you see this in generated code, something has gone wrong!
22 | ///
23 | [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
24 | public class ExternalDefinitionAttribute : Attribute
25 | {
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Attributes/FlatBufferEnumAttribute.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Attributes;
18 |
19 | ///
20 | /// Marks an enum as being elible for FlatSharp serialization.
21 | ///
22 | ///
23 | /// Usage of enums comes with two main caveats: the enum may only be extended (ie, values cannot be recycled), and
24 | /// the underlying type may not be changed.
25 | ///
26 | [AttributeUsage(AttributeTargets.Enum, AllowMultiple = false, Inherited = true)]
27 | public class FlatBufferEnumAttribute : Attribute
28 | {
29 | ///
30 | /// Initializes a new instance of the FlatBufferEnumAttribute class.
31 | ///
32 | /// The declared underlying type of the enum. This must match the enum's actual underlying type.
33 | public FlatBufferEnumAttribute(Type underlyingType)
34 | {
35 | this.DeclaredUnderlyingType = underlyingType;
36 | }
37 |
38 | ///
39 | /// The declared underlying type. This is a saftey check to prevent unintended binary breaks.
40 | ///
41 | public Type DeclaredUnderlyingType { get; }
42 | }
43 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Attributes/FlatBufferMetadataAttribute.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Linq;
18 |
19 | namespace FlatSharp.Attributes;
20 |
21 | ///
22 | /// Describes kinds of metadata.
23 | ///
24 | public enum FlatBufferMetadataKind
25 | {
26 | ///
27 | /// A custom get/set accessor for a flatbuffer field.
28 | ///
29 | Accessor = 1,
30 | }
31 |
32 | ///
33 | /// Defines a member of a FlatBuffer struct or table.
34 | ///
35 | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
36 | public class FlatBufferMetadataAttribute : Attribute
37 | {
38 | ///
39 | /// Initializes a new FlatBufferMetadataAttribute.
40 | ///
41 | public FlatBufferMetadataAttribute(FlatBufferMetadataKind kind, string value)
42 | {
43 | this.Kind = kind;
44 | this.Value = value;
45 | }
46 |
47 | ///
48 | /// The value.
49 | ///
50 | public string Value { get; }
51 |
52 | ///
53 | /// The kind of metadata.
54 | ///
55 | public FlatBufferMetadataKind Kind { get; }
56 | }
57 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Attributes/FlatBufferTableAttribute.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Attributes;
18 |
19 | ///
20 | /// Marks a class as being a FlatBuffer table.
21 | ///
22 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
23 | public class FlatBufferTableAttribute : Attribute
24 | {
25 | ///
26 | /// Specifies the file identifier for serialized tables. Must be precisely 4 ASCII characters.
27 | ///
28 | public string? FileIdentifier { get; set; }
29 | }
30 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/BufferTooSmallException.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// Raised in serialization when the provided buffer was too small.
21 | ///
22 | public sealed class BufferTooSmallException : Exception
23 | {
24 | public BufferTooSmallException() : base($"The provided buffer was too small to hold the serialized data.")
25 | {
26 | }
27 |
28 | ///
29 | /// The maximum amount of size needed for this message.
30 | ///
31 | public int SizeNeeded { get; internal set; }
32 | }
33 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/FlatBufferDeserializationContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// A context that FlatSharp-deserialized classes will pass to their parent
21 | /// object on construction, if the parent object defines a constructor that accepts this object.
22 | ///
23 | public struct FlatBufferDeserializationContext
24 | {
25 | ///
26 | /// Initializes a new FlatSharpConstructorContext with the given deserialization option.
27 | ///
28 | public FlatBufferDeserializationContext(
29 | FlatBufferDeserializationOption deserializationOption)
30 | {
31 | this.DeserializationOption = deserializationOption;
32 | }
33 |
34 | ///
35 | /// The deserialization options used to create the current subclass.
36 | ///
37 | public FlatBufferDeserializationOption DeserializationOption { get; }
38 | }
39 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/FlatBufferSerializationOption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// Defines FlatSharp deserialization options.
21 | ///
22 | public enum FlatBufferDeserializationOption
23 | {
24 | ///
25 | /// Full Lazy parsing. Deserialized objects are immutable.
26 | ///
27 | Lazy = 0,
28 |
29 | ///
30 | /// Each object in the deserialized payload is read from the underlying buffer at most once. Reads are done on demand and cached. The returned object is immutable.
31 | ///
32 | Progressive = 1,
33 |
34 | ///
35 | /// The entire object graph is traversed and the deserialized objects do not reference the input buffer. Deserialized objects are immutable.
36 | ///
37 | Greedy = 2,
38 |
39 | ///
40 | /// The entire object graph is traversed and the deserialized objects do not reference the input buffer. Deserialized objects are mutable.
41 | ///
42 | GreedyMutable = 3,
43 |
44 | ///
45 | /// Default options.
46 | ///
47 | Default = GreedyMutable,
48 | }
49 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/FlatSharp.Runtime.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | netstandard2.0;netstandard2.1;net6.0;net7.0
6 | FlatSharp.Runtime
7 | FlatSharp.Runtime
8 | FlatSharp.Runtime is the runtime component of FlatSharp that supports code emitted by the FlatSharp.Compiler package.
9 | annotations
10 | embedded
11 | true
12 | CS1591
13 |
14 |
15 |
16 | enable
17 |
18 |
19 |
20 |
21 | SpanComparers.cs
22 | TextTemplatingFileGenerator
23 |
24 |
25 | TextTemplatingFileGenerator
26 | UnionTypes.cs
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 | True
37 | True
38 | SpanComparers.tt
39 |
40 |
41 | True
42 | True
43 | UnionTypes.tt
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/GlobalUsings.cs:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright 2021 James Courtney
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | global using System;
19 | global using System.Collections;
20 | global using System.Collections.Generic;
21 | global using System.ComponentModel;
22 | global using System.Diagnostics;
23 | global using System.Diagnostics.CodeAnalysis;
24 | global using System.Runtime.CompilerServices;
25 |
26 | global using FlatSharp.Internal;
27 |
28 | #if NETSTANDARD2_0
29 | global using FlatSharp.Polyfills;
30 | #endif
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/IFlatBufferDeserializedObject.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// An interface applied to objects deserialized by FlatSharp. FlatSharp implements this
21 | /// interface on deserialized objects. It should not be implemented externally.
22 | ///
23 | public interface IFlatBufferDeserializedObject
24 | {
25 | ///
26 | /// The actual type of the table or struct. This is generally the base class.
27 | ///
28 | Type TableOrStructType { get; }
29 |
30 | ///
31 | /// The context of the deserialized object.
32 | ///
33 | FlatBufferDeserializationContext DeserializationContext { get; }
34 |
35 | ///
36 | /// Gets the input buffer instance used to lazily read this object.
37 | /// This buffer will not have a value when the derserialization mode
38 | /// is or
39 | /// .
40 | ///
41 | IInputBuffer? InputBuffer { get; }
42 |
43 | ///
44 | /// Indcates that this deserialized object is immutable or has semantics where all changes
45 | /// are written back to the underlying buffer. This can allow serialize operations to be
46 | /// implemented as memcopy instead of a full serialize flow.
47 | ///
48 | bool CanSerializeWithMemoryCopy { get; }
49 | }
50 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/IFlatBufferDeserializedVector.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// An interface implemented on buffer-backed Flatbuffer vectors. This interface is internal to FlatSharp and exposes some
21 | /// functionality to assist with binary searching.
22 | ///
23 | internal interface IFlatBufferDeserializedVector
24 | {
25 | ///
26 | /// Gets the input buffer.
27 | ///
28 | IInputBuffer InputBuffer { get; }
29 |
30 | ///
31 | /// Gets the raw item size of each element in the vector.
32 | ///
33 | int ItemSize { get; }
34 |
35 | ///
36 | /// The number of items.
37 | ///
38 | int Count { get; }
39 |
40 | ///
41 | /// Returns the absolute position in the Input Buffer of the given index in the vector.
42 | ///
43 | int OffsetOf(int index);
44 |
45 | ///
46 | /// Gets the item at the given index.
47 | ///
48 | object ItemAt(int index);
49 | }
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/ISpanComparer.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// An analog of IComparer for Spans. The implementation performs comparison consistent with the type the span represents.
21 | ///
22 | public interface ISpanComparer
23 | {
24 | ///
25 | /// Compares the two spans.
26 | ///
27 | int Compare(bool leftExists, ReadOnlySpan left, bool rightExists, ReadOnlySpan right);
28 | }
29 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/NotMutableException.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// An exception raised from a flatbuffer-deserialized object when the given property cannot be mutated.
21 | ///
22 | public class NotMutableException : Exception
23 | {
24 | public NotMutableException()
25 | {
26 | }
27 |
28 | public NotMutableException(string message) : base(message)
29 | {
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/TableFieldContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// A context containing information about the table field.
21 | ///
22 | public sealed class TableFieldContext
23 | {
24 | public TableFieldContext(string fullName, bool sharedString, bool writeThrough)
25 | {
26 | this.FullName = fullName;
27 | this.SharedString = sharedString;
28 | this.WriteThrough = writeThrough;
29 | }
30 |
31 | ///
32 | /// For debug purposes. Contains the full name of the associated table property.
33 | ///
34 | public readonly string FullName;
35 |
36 | ///
37 | /// Indicates if this context enables shared strings.
38 | ///
39 | public readonly bool SharedString;
40 |
41 | ///
42 | /// Indicates if this field is flagged as writethrough-enabled.
43 | ///
44 | public readonly bool WriteThrough;
45 |
46 | public override bool Equals(object? obj)
47 | {
48 | if (obj is TableFieldContext ctx)
49 | {
50 | return this.FullName == ctx.FullName
51 | && this.SharedString == ctx.SharedString
52 | && this.WriteThrough == ctx.WriteThrough;
53 | }
54 |
55 | return false;
56 | }
57 |
58 | public override int GetHashCode()
59 | {
60 | return this.FullName.GetHashCode();
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/VTables/IVTable.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | public interface IVTable
20 | {
21 | public int MaxSupportedIndex { get; }
22 |
23 | int OffsetOf(TInputBuffer buffer, int index)
24 | where TInputBuffer : IInputBuffer;
25 | }
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/VTables/VTableGeneric.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// Represents a vtable for an arbitrary table.
21 | ///
22 | public struct VTableGeneric : IVTable
23 | {
24 | private int offset;
25 | private nuint count;
26 |
27 | public int MaxSupportedIndex => 255;
28 |
29 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
30 | public static void Create(TInputBuffer buffer, int offset, out VTableGeneric item)
31 | where TInputBuffer : IInputBuffer
32 | {
33 | checked
34 | {
35 | item = new VTableGeneric();
36 |
37 | buffer.InitializeVTable(offset, out item.offset, out item.count, out _);
38 | item.offset += 2 * sizeof(ushort); // skip past vtable length and table length
39 | }
40 | }
41 |
42 | public int OffsetOf(TInputBuffer buffer, int index)
43 | where TInputBuffer : IInputBuffer
44 | {
45 | if ((uint)index >= this.count)
46 | {
47 | return 0;
48 | }
49 |
50 | return buffer.ReadUShort(this.offset + (index << 1));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Vectors/ISortableTable.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// A table that may be sorted in a vector.
21 | ///
22 | public interface ISortableTable where TKey : notnull
23 | {
24 | // Left empty. This interface exists simply to help satisfy generic constraints and does
25 | // not currently have any requirements.
26 | }
27 |
--------------------------------------------------------------------------------
/src/FlatSharp.Runtime/Vectors/IVectorItemAccessor.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.Internal;
18 |
19 | ///
20 | /// Small interface for reading and writing to/from vectors. Implementations should
21 | /// prefer using a struct as that will enable devirtualization.
22 | ///
23 | public interface IVectorItemAccessor
24 | {
25 | int Count { get; }
26 |
27 | int ItemSize { get; }
28 |
29 | void ParseItem(int index, TInputBuffer buffer, short remainingDepth, TableFieldContext context, out TItem item);
30 |
31 | void WriteThrough(int index, TItem value, TInputBuffer inputBuffer, TableFieldContext context);
32 |
33 | int OffsetOf(int index);
34 | }
35 |
--------------------------------------------------------------------------------
/src/FlatSharp.UnityPolyfills/FlatSharp.UnityPolyfills.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | netstandard2.0
6 | FlatSharp.UnityPolyfills
7 | FlatSharp.UnityPolyfills is a utility assembly to expose the Unity API needed for FlatSharp support of UnityEngine.Collections.NativeArray.
8 | annotations
9 | $(DefineContants);FLATSHARP_UNITY_POLYFILLS
10 | FlatSharp.UnityPolyfills
11 | true
12 |
13 |
14 |
15 | enable
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/FlatSharp/FlatBufferMetadataAttributeExtensions.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Attributes;
18 | using System.Linq;
19 |
20 | namespace FlatSharp;
21 |
22 | public static class FlatBufferMetadataAttributeExtensions
23 | {
24 | public static string? GetFlatBufferMetadataOrNull(
25 | this MemberInfo memberInfo,
26 | FlatBufferMetadataKind kind)
27 | {
28 | return memberInfo
29 | .GetCustomAttributes()
30 | .Where(x => x.Kind == kind)
31 | .FirstOrDefault()?
32 | .Value;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/FlatSharp/FlatSharpCompilationException.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp;
18 |
19 | ///
20 | /// An exception thrown when Roslyn fails to compile the generated C# code.
21 | ///
22 | public sealed class FlatSharpCompilationException : Exception
23 | {
24 | public FlatSharpCompilationException(string[] compilerErrors, string cSharp)
25 | : base("FlatSharp failed to generate proper C# for your schema.")
26 | {
27 | this.CompilerErrors = compilerErrors;
28 | this.CSharp = cSharp;
29 | }
30 |
31 | public override string Message
32 | {
33 | get
34 | {
35 | return $"{base.Message}\r\n\r\n{string.Join("\r\n", this.CompilerErrors)}\r\n";
36 | }
37 | }
38 |
39 | ///
40 | /// The list of individual errors from the C# compiler.
41 | ///
42 | public string[] CompilerErrors { get; }
43 |
44 | ///
45 | /// The generated C# that failed to compile.
46 | ///
47 | public string CSharp { get; }
48 | }
49 |
--------------------------------------------------------------------------------
/src/FlatSharp/GlobalUsings.cs:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright 2021 James Courtney
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | global using System;
19 | global using System.Collections;
20 | global using System.Collections.Generic;
21 | global using System.ComponentModel;
22 | global using System.Diagnostics;
23 | global using System.Diagnostics.CodeAnalysis;
24 | global using System.Reflection;
25 | global using System.Runtime.CompilerServices;
26 |
27 | global using FlatSharp.CodeGen;
28 | global using FlatSharp.Internal;
29 |
30 | #if NETSTANDARD2_0
31 | global using FlatSharp.Polyfills;
32 | #endif
--------------------------------------------------------------------------------
/src/FlatSharp/Serialization/AccessModifier.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.CodeGen;
18 |
19 | ///
20 | /// Defines access modifiers
21 | ///
22 | public enum AccessModifier
23 | {
24 | ///
25 | /// A public setter.
26 | ///
27 | Public = 0,
28 |
29 | ///
30 | /// A protected internal setter.
31 | ///
32 | ProtectedInternal = 1,
33 |
34 | ///
35 | /// A protected setter.
36 | ///
37 | Protected = 2,
38 |
39 | ///
40 | /// A private access modifier.
41 | ///
42 | Private = 3,
43 | }
44 |
--------------------------------------------------------------------------------
/src/FlatSharp/Serialization/CloneCodeGenContext.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.CodeGen;
18 |
19 | ///
20 | /// Code gen context for serialization methods.
21 | ///
22 | public class CloneCodeGenContext
23 | {
24 | public CloneCodeGenContext(string itemVariableName, IReadOnlyDictionary methodNameMap)
25 | {
26 | this.ItemVariableName = itemVariableName;
27 | this.MethodNameMap = methodNameMap;
28 | }
29 |
30 | ///
31 | /// The variable name of the current value to serialize.
32 | ///
33 | public string ItemVariableName { get; private set; }
34 |
35 | ///
36 | /// A mapping of type to clone method name for that type.
37 | ///
38 | public IReadOnlyDictionary MethodNameMap { get; private set; }
39 | }
40 |
--------------------------------------------------------------------------------
/src/FlatSharp/Serialization/CodeGeneratedMethod.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.CodeGen;
18 |
19 | ///
20 | /// Defines the result of code generating a method.
21 | ///
22 | public record CodeGeneratedMethod
23 | {
24 | public CodeGeneratedMethod(string methodBody)
25 | {
26 | this.MethodBody = methodBody;
27 | }
28 |
29 | ///
30 | /// The body of the method.
31 | ///
32 | public string MethodBody { get; init; }
33 |
34 | ///
35 | /// A class definition.
36 | ///
37 | public string? ClassDefinition { get; init; }
38 |
39 | ///
40 | /// Indicates if the method should be marked with aggressive inlining.
41 | ///
42 | public bool IsMethodInline { get; init; }
43 |
44 | public string GetMethodImplAttribute()
45 | {
46 | if (this.IsMethodInline)
47 | {
48 | string inlining = "System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining";
49 | return $"[{typeof(MethodImplAttribute).GetGlobalCompilableTypeName()}({inlining})]";
50 | }
51 | else
52 | {
53 | return string.Empty;
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/FlatSharp/Serialization/IMethodNameResolver.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.TypeModel;
18 |
19 | namespace FlatSharp.CodeGen;
20 |
21 | ///
22 | /// Maps type models to method and class names. Used in serializer generation.
23 | ///
24 | public interface IMethodNameResolver
25 | {
26 | (string @namespace, string name) ResolveGeneratedSerializerClassName(ITypeModel type);
27 |
28 | (string @namespace, string name) ResolveHelperClassName(ITypeModel type);
29 |
30 | (string @namespace, string className, string methodName) ResolveGetMaxSize(ITypeModel type);
31 |
32 | (string @namespace, string className, string methodName) ResolveParse(FlatBufferDeserializationOption option, ITypeModel type);
33 |
34 | (string @namespace, string className, string methodName) ResolveSerialize(ITypeModel type);
35 | }
36 |
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/FlatBufferSchemaType.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.TypeModel;
18 |
19 | ///
20 | /// Defines flat buffer schema type elements.
21 | ///
22 | public enum FlatBufferSchemaType
23 | {
24 | ///
25 | /// A flat buffer table.
26 | ///
27 | Table = 1,
28 |
29 | ///
30 | /// A flat buffer struct.
31 | ///
32 | Struct = 2,
33 |
34 | ///
35 | /// A vector.
36 | ///
37 | Vector = 3,
38 |
39 | ///
40 | /// A scalar.
41 | ///
42 | Scalar = 4,
43 |
44 | ///
45 | /// A string.
46 | ///
47 | String = 5,
48 |
49 | ///
50 | /// A union.
51 | ///
52 | Union = 6,
53 | }
54 |
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/ITypeModelProvider.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.TypeModel;
18 |
19 | ///
20 | /// Provides a type model to FlatSharp.
21 | ///
22 | public interface ITypeModelProvider
23 | {
24 | ///
25 | /// Creates a type model for the given type.
26 | ///
27 | bool TryCreateTypeModel(
28 | TypeModelContainer container,
29 | Type type,
30 | [NotNullWhen(true)] out ITypeModel? typeModel);
31 | }
32 |
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/InvalidFlatBufferDefinitionException.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.TypeModel;
18 |
19 | ///
20 | /// Thrown when a type was not valid for FlatBuffers.
21 | ///
22 | public class InvalidFlatBufferDefinitionException : Exception
23 | {
24 | public InvalidFlatBufferDefinitionException(string message) : base(message)
25 | {
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/PhysicalLayoutElement.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.TypeModel;
18 |
19 | ///
20 | /// An element of the physical layout of a Type Model. Types are serialized according to this layout, which represents the inline size
21 | /// when serializing the item. For tables, this is simply a tuple of (size: sizeof(uint), alignment: sizeof(uint)) since
22 | /// tables are written by reference instead of by value, but for structs
23 | /// the layout depends on the inner members. This layout is widely used when serializing vectors, tables, structs and others.
24 | ///
25 | public class PhysicalLayoutElement
26 | {
27 | public PhysicalLayoutElement(int size, int alignment)
28 | {
29 | this.InlineSize = size;
30 | this.Alignment = alignment;
31 | }
32 |
33 | public int InlineSize { get; }
34 |
35 | public int Alignment { get; }
36 | }
37 |
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/TypeModelContainerExtensions.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 Unity Technologies
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharp.TypeModel;
18 |
19 | internal static class TypeModelContainerExtensions
20 | {
21 | public static TypeModelContainer WithUnitySupport(this TypeModelContainer container, bool enableUnitySupport)
22 | {
23 | if (enableUnitySupport)
24 | container.RegisterProvider(new UnityTypeModelProvider());
25 | return container;
26 | }
27 | }
--------------------------------------------------------------------------------
/src/FlatSharp/TypeModel/UnityTypeModelProvider.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 Unity Technologies
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.TypeModel.Vectors;
18 |
19 | namespace FlatSharp.TypeModel;
20 |
21 | public class UnityTypeModelProvider : ITypeModelProvider
22 | {
23 | public bool TryCreateTypeModel(TypeModelContainer container, Type type, [NotNullWhen(true)] out ITypeModel? typeModel)
24 | {
25 | if (type.IsGenericType)
26 | {
27 | var genericDef = type.GetGenericTypeDefinition();
28 | if (genericDef.Namespace == "Unity.Collections" && genericDef.Name == "NativeArray`1")
29 | {
30 | typeModel = new UnityNativeArrayVectorTypeModel(type, container);
31 | return true;
32 | }
33 | }
34 |
35 | typeModel = null;
36 | return false;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/ByteBufferUtil.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System;
18 |
19 | namespace Google.FlatBuffers
20 | {
21 | ///
22 | /// Class that collects utility functions around `ByteBuffer`.
23 | ///
24 | public class ByteBufferUtil
25 | {
26 | // Extract the size prefix from a `ByteBuffer`.
27 | public static int GetSizePrefix(ByteBuffer bb) {
28 | return bb.GetInt(bb.Position);
29 | }
30 |
31 | // Create a duplicate of a size-prefixed `ByteBuffer` that has its position
32 | // advanced just past the size prefix.
33 | public static ByteBuffer RemoveSizePrefix(ByteBuffer bb) {
34 | ByteBuffer s = bb.Duplicate();
35 | s.Position += FlatBufferConstants.SizePrefixLength;
36 | return s;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/FlatBufferConstants.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System;
18 | using System.Collections.Generic;
19 | using System.Linq;
20 | using System.Text;
21 |
22 | namespace Google.FlatBuffers
23 | {
24 | public static class FlatBufferConstants
25 | {
26 | public const int FileIdentifierLength = 4;
27 | public const int SizePrefixLength = 4;
28 | /** A version identifier to force a compile error if someone
29 | accidentally tries to build generated code with a runtime of
30 | two mismatched version. Versions need to always match, as
31 | the runtime and generated code are modified in sync.
32 | Changes to the C# implementation need to be sure to change
33 | the version here and in the code generator on every possible
34 | incompatible change */
35 | public static void FLATBUFFERS_22_10_26() {}
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/Google.FlatBuffers.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | netstandard2.0
6 | true
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/IFlatbufferObject.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Google.FlatBuffers
18 | {
19 | ///
20 | /// This is the base for both structs and tables.
21 | ///
22 | public interface IFlatbufferObject
23 | {
24 | void __init(int _i, ByteBuffer _bb);
25 |
26 | ByteBuffer ByteBuffer { get; }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/Offset.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Google.FlatBuffers
18 | {
19 | ///
20 | /// Offset class for typesafe assignments.
21 | ///
22 | public struct Offset where T : struct
23 | {
24 | public int Value;
25 | public Offset(int value)
26 | {
27 | Value = value;
28 | }
29 | }
30 |
31 | public struct StringOffset
32 | {
33 | public int Value;
34 | public StringOffset(int value)
35 | {
36 | Value = value;
37 | }
38 | }
39 |
40 | public struct VectorOffset
41 | {
42 | public int Value;
43 | public VectorOffset(int value)
44 | {
45 | Value = value;
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Google.FlatBuffers/Struct.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Google Inc. All rights reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace Google.FlatBuffers
18 | {
19 | ///
20 | /// All structs in the generated code derive from this class, and add their own accessors.
21 | ///
22 | public struct Struct
23 | {
24 | public int bb_pos { get; private set; }
25 | public ByteBuffer bb { get; private set; }
26 |
27 | // Re-init the internal state with an external buffer {@code ByteBuffer} and an offset within.
28 | public Struct(int _i, ByteBuffer _bb) : this()
29 | {
30 | bb = _bb;
31 | bb_pos = _i;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/AdvancedFeaturesTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.Compiler.SchemaModel;
18 |
19 | namespace FlatSharpTests.Compiler;
20 |
21 | public class AdvancedFeaturesTests
22 | {
23 | [Fact]
24 | public void Unsupported_AdvancedFeature_Throws()
25 | {
26 | Assert.Throws(
27 | () => new RootModel(FlatSharp.Compiler.Schema.AdvancedFeatures.DefaultVectorsAndStrings));
28 |
29 | Assert.Throws(
30 | () => new RootModel(FlatSharp.Compiler.Schema.AdvancedFeatures.DefaultVectorsAndStrings | FlatSharp.Compiler.Schema.AdvancedFeatures.OptionalScalars));
31 |
32 | new RootModel(FlatSharp.Compiler.Schema.AdvancedFeatures.OptionalScalars);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/CSharpKeywordTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler;
18 |
19 | public class CSharpKeywordTests
20 | {
21 | [Fact]
22 | public void ClassKeyword()
23 | {
24 | string fbs = $"namespace Foo.Bar; enum class : ubyte (bit_flags) {{ Red, Blue, Green, Yellow }}";
25 | var ex = Assert.Throws(
26 | () => FlatSharpCompiler.CompileAndLoadAssembly(fbs, new()));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/CompilerTestHelpers.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using FlatSharp.TypeModel;
18 |
19 | namespace FlatSharpTests.Compiler;
20 |
21 | public static class CompilerTestHelpers
22 | {
23 | public static readonly FlatBufferSerializer CompilerTestSerializer = new FlatBufferSerializer(
24 | new FlatBufferSerializerOptions { EnableAppDomainInterceptOnAssemblyLoad = true }, TypeModelContainer.CreateDefault().WithUnitySupport(true));
25 | }
26 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/FlatSharpCompilerTests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | net6.0;net7.0
6 | false
7 | x64
8 | FlatSharpCompilerTests
9 | FlatSharpTests
10 | annotations
11 | false
12 |
13 |
14 |
15 |
16 | all
17 | runtime; build; native; contentfiles; analyzers; buildtransitive
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | all
28 | runtime; build; native; contentfiles; analyzers; buildtransitive
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/FloatSpecialDefaultTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler;
18 |
19 | public class FloatSpecialDefaultTests
20 | {
21 | ///
22 | /// Tests that we can compile a schema using special float constants.
23 | ///
24 | [Fact]
25 | public void TestFloatSpecialDefaultValues()
26 | {
27 | string schema = $@"
28 | namespace FloatSpecialDefaultTests;
29 |
30 | table FloatSpecialDefaultTable
31 | {{
32 | FloatNan : float = nan;
33 | FloatNInf : float = -inf;
34 | FloatPInf : float = +inf;
35 | FloatInf : float = inf;
36 | FloatNInfinity : float = -infinity;
37 | FloatPInfinity : float = +infinity;
38 | FloatInfinity : float = infinity;
39 | }}
40 |
41 | table DoubleSpecialDefaultTable
42 | {{
43 | DoubleNan : double = nan;
44 | DoubleNInf : double = -inf;
45 | DoublePInf : double = +inf;
46 | DoubleInf : double = inf;
47 | DoubleNInfinity : double = -infinity;
48 | DoublePInfinity : double = +infinity;
49 | DoubleInfinity : double = infinity;
50 | }}
51 | ";
52 |
53 | FlatSharpCompiler.CompileAndLoadAssembly(schema, new());
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/GlobalUsings.cs:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Copyright 2021 James Courtney
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | global using System;
19 | global using System.Buffers;
20 | global using System.Buffers.Binary;
21 | global using System.Collections;
22 | global using System.Collections.Generic;
23 | global using System.ComponentModel;
24 | global using System.Diagnostics;
25 | global using System.Diagnostics.CodeAnalysis;
26 | global using System.Linq;
27 | global using System.Reflection;
28 | global using System.Runtime.CompilerServices;
29 | global using FlatSharp;
30 | global using FlatSharp.Attributes;
31 | global using FlatSharp.Compiler;
32 | global using FlatSharp.Compiler.Schema;
33 | global using FlatSharp.Compiler.SchemaModel;
34 | global using Xunit;
35 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/Grpc/GrpcTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Threading.Channels;
18 |
19 | namespace FlatSharpTests.Compiler;
20 |
21 | public class GrpcTests
22 | {
23 | [Fact]
24 | public void NoPrecompiledSerializer()
25 | {
26 | string schema = $@"
27 | {MetadataHelpers.AllAttributes}
28 | namespace NoPrecompiledSerializer;
29 |
30 | rpc_service RouteGuide
31 | {{
32 | GetFeature(Point):Point;
33 | }}
34 |
35 | table Point
36 | {{
37 | latitude:int32;
38 | longitude:int32;
39 | }}";
40 |
41 | var ex = Assert.Throws(() => FlatSharpCompiler.CompileAndLoadAssembly(
42 | schema,
43 | new(),
44 | additionalReferences: new[]
45 | {
46 | typeof(Grpc.Core.AsyncClientStreamingCall<,>).Assembly,
47 | typeof(ChannelReader<>).Assembly,
48 | }));
49 |
50 | Assert.Contains(
51 | "RPC call 'NoPrecompiledSerializer.RouteGuide.GetFeature' uses table 'NoPrecompiledSerializer.Point', which does not specify the 'fs_serializer' attribute.",
52 | ex.Message);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/MetadataHelpers.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | using System.Linq;
18 |
19 | namespace FlatSharpTests.Compiler;
20 |
21 | public class MetadataHelpers
22 | {
23 | public static readonly string AllAttributes;
24 |
25 | static MetadataHelpers()
26 | {
27 | List names = new List
28 | {
29 | string.Empty,
30 | MetadataKeys.SerializerKind,
31 | MetadataKeys.SortedVector,
32 | MetadataKeys.SharedString,
33 | MetadataKeys.DefaultConstructorKind,
34 | MetadataKeys.VectorKind,
35 | MetadataKeys.Setter,
36 | MetadataKeys.ValueStruct,
37 | MetadataKeys.UnsafeValueStructVector,
38 | MetadataKeys.MemoryMarshalBehavior,
39 | MetadataKeys.ForceWrite,
40 | MetadataKeys.WriteThrough,
41 | MetadataKeys.RpcInterface,
42 | MetadataKeys.UnsafeExternal,
43 | MetadataKeys.LiteralName,
44 | MetadataKeys.UnsafeUnion,
45 | string.Empty
46 | };
47 |
48 | AllAttributes = string.Join("\r\n", names.Select(x => $"attribute \"{x}\";"));
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/ModuleInitializer.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2022 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler
18 | {
19 | using System.Runtime.CompilerServices;
20 | using FlatSharp.CodeGen;
21 |
22 | public partial class ModuleInitializer
23 | {
24 | [ModuleInitializer]
25 | public static void AssemblyInitialize()
26 | {
27 | RoslynSerializerGenerator.EnableStrictValidation = true;
28 | RoslynSerializerGenerator.AllowUnsafeBlocks = true;
29 | }
30 | }
31 | }
32 |
33 | #if !NET5_0_OR_GREATER
34 | namespace System.Runtime.CompilerServices
35 | {
36 | [AttributeUsage(AttributeTargets.Method)]
37 | public class ModuleInitializerAttribute : Attribute
38 | {
39 | }
40 | }
41 | #endif
42 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/NullableAnnotationTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler;
18 |
19 | public class NullableAnnotationTests
20 | {
21 | [Fact]
22 | public void NullableAnnotations()
23 | {
24 | string schema = $@"
25 | {MetadataHelpers.AllAttributes}
26 | namespace NullableAnnotationTests;
27 |
28 | table Table ({MetadataKeys.SerializerKind}) {{
29 | foo:Foo;
30 | defaultInt:int32 = 3;
31 | str:string;
32 | nullableInt:int32 = null;
33 | arrayVector:[int32] ({MetadataKeys.VectorKind}:""IReadOnlyList"");
34 | memoryVector:[ubyte] ({MetadataKeys.VectorKind}:""Memory"");
35 | roMemoryVector:[ubyte] ({MetadataKeys.VectorKind}:""ReadOnlyMemory"");
36 | listVector:[int32] ({MetadataKeys.VectorKind}:""IList"");
37 | nestedTable:InnerTable;
38 | }}
39 |
40 | table InnerTable {{ str:string; }}
41 |
42 | struct Foo {{
43 | id:ulong;
44 | count:short;
45 | prefix:byte;
46 | length:uint;
47 | }}";
48 |
49 | Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
50 | schema,
51 | new());
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/ScalarSizeTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler;
18 |
19 | public class ScalarSizeTests
20 | {
21 | [Theory]
22 | [InlineData(BaseType.Bool, 1)]
23 | [InlineData(BaseType.Byte, 1)]
24 | [InlineData(BaseType.UByte, 1)]
25 | [InlineData(BaseType.Short, 2)]
26 | [InlineData(BaseType.UShort, 2)]
27 | [InlineData(BaseType.Int, 4)]
28 | [InlineData(BaseType.UInt, 4)]
29 | [InlineData(BaseType.Float, 4)]
30 | [InlineData(BaseType.Long, 8)]
31 | [InlineData(BaseType.ULong, 8)]
32 | [InlineData(BaseType.Double, 8)]
33 | public void SizesCorrect(BaseType type, int expectedSize)
34 | {
35 | Assert.True(type.IsScalar());
36 | Assert.Equal(expectedSize, type.GetScalarSize());
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpCompilerTests/VectorOfUnionTests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpTests.Compiler;
18 |
19 | public class VectorOfUnionTests
20 | {
21 | [Fact]
22 | public void VectorOfUnion_CompilerTests()
23 | {
24 | foreach (var vectorKind in new[] { "IList", "IReadOnlyList" })
25 | {
26 | foreach (FlatBufferDeserializationOption option in Enum.GetValues(typeof(FlatBufferDeserializationOption)))
27 | {
28 | this.RunTest(vectorKind, option);
29 | }
30 | }
31 | }
32 |
33 | private void RunTest(string vectorKind, FlatBufferDeserializationOption option)
34 | {
35 | string schema = $@"
36 | {MetadataHelpers.AllAttributes}
37 | namespace VectorOfUnionTests;
38 |
39 | union Union {{ Foo, Table2 }}
40 |
41 | table Table ({MetadataKeys.SerializerKind}:""{option}"") {{
42 | Vector:[Union] (fs_vector:""{vectorKind}"");
43 | }}
44 |
45 | struct Foo {{
46 | A:int;
47 | B:uint64;
48 | }}
49 | table Table2 {{
50 | A:int;
51 | B:[string];
52 | }}";
53 |
54 | Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
55 | schema,
56 | new());
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/FlatBufferSerializerNonGenericTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.FlatBufferSerializerNonGenericTests;
10 |
11 | table SomeTable (fs_serializer)
12 | {
13 | A : int;
14 | }
15 |
16 | table SomeOtherTable (fs_serializer)
17 | {
18 | A : int;
19 | }
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/FlatBufferVectorOfUnionTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.FlatBufferVectorOfUnionTests;
10 |
11 | union MyUnion { str : string, Struct, Table }
12 |
13 | struct Struct { Value : int; }
14 | table Table { Value : int; }
15 | table TableVector (fs_serializer) { Vector : [ MyUnion ]; }
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/FlatBufferVectorTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.FlatBufferVectorTests;
10 |
11 | table StringVector (fs_serializer) { Vector : [ string ]; }
12 | table IntVector (fs_serializer) { Vector : [ int ]; }
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/IndexedVectorTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.IndexedVectorTests;
10 |
11 | table Container (fs_serializer)
12 | {
13 | StringVector : [ StringKey ] (fs_vector:"IIndexedVector");
14 | IntVector : [ IntKey ] (fs_vector:"IIndexedVector");
15 | }
16 |
17 | table StringKey
18 | {
19 | Key : string (key);
20 | Value : string;
21 | }
22 |
23 | table IntKey
24 | {
25 | Key : int (key);
26 | Value : string;
27 | }
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/NonScalarVectorTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.NonScalarVectorTests;
10 |
11 | struct InnerStruct
12 | {
13 | Value : int;
14 | }
15 |
16 | table InnerTable
17 | {
18 | Value : string;
19 | }
20 |
21 | table Root (fs_serializer)
22 | {
23 | TableVector : [ InnerTable ];
24 | StructVector : [ InnerStruct ];
25 | StringVector : [ string ];
26 | }
27 |
28 | table RootReadOnly (fs_serializer)
29 | {
30 | TableVector : [ InnerTable ] (fs_vector:"IReadOnlyList");
31 | StructVector : [ InnerStruct ] (fs_vector:"IReadOnlyList");
32 | StringVector : [ string ] (fs_vector:"IReadOnlyList");
33 | }
34 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/ClassLib/SerializerConfigurationTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.ClassLib.SerializerConfigurationTests;
10 |
11 | table Root (fs_serializer)
12 | {
13 | StringVector : [ string ] (fs_sharedString);
14 | }
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/CopyConstructors/CopyConstructorTests.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_unsafeStructVector";
4 | attribute "fs_valueStruct";
5 | attribute "fs_writeThrough";
6 | attribute "fs_vector";
7 | attribute "fs_sharedString";
8 |
9 | namespace FlatSharpEndToEndTests.CopyConstructors;
10 |
11 | union Union { OuterTable, InnerTable, OuterStruct, InnerStruct } // Optionally add more tables.
12 |
13 | table OuterTable (fs_serializer) {
14 | A:string (id: 0);
15 |
16 | B:byte (id: 1);
17 | C:ubyte (id: 2);
18 | D:int16 (id: 3);
19 | E:uint16 (id: 4);
20 | F:int32 (id: 5);
21 | G:uint32 (id: 6);
22 | H:int64 (id: 7);
23 | I:uint64 (id: 8);
24 |
25 | IntVector_List:[int] (fs_vector:"IList", id: 9);
26 | IntVector_RoList:[int] (fs_vector:"IReadOnlyList", id: 10);
27 | IntVector_Array:[int] (fs_vector:"IList", id: 11);
28 |
29 | TableVector_List:[InnerTable] (fs_vector:"IList", id: 12);
30 | TableVector_RoList:[InnerTable] (fs_vector:"IReadOnlyList", id: 13);
31 | TableVector_Indexed:[InnerTable] (fs_vector:"IIndexedVector", id: 14);
32 | TableVector_Array:[InnerTable] (fs_vector:"IList", id: 15);
33 |
34 | ByteVector:[ubyte] (fs_vector:"Memory", id: 16);
35 | ByteVector_RO:[ubyte] (fs_vector:"ReadOnlyMemory", id: 17);
36 | UnionVal : Union (id: 19);
37 |
38 | VectorOfUnion : [Union] (id: 21);
39 | VectorOfUnion_RoList : [Union] (id: 23, fs_vector:"IReadOnlyList");
40 | VectorOfUnion_Array : [Union] (id: 25, fs_vector:"IList");
41 | }
42 |
43 | struct InnerStruct {
44 | LongValue:int64;
45 | }
46 |
47 | struct OuterStruct {
48 | Value:int;
49 | InnerStructVal:InnerStruct;
50 | }
51 |
52 | table InnerTable {
53 | Name:string (key);
54 | OuterStructVal:OuterStruct;
55 | }
56 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/DepthLimit/DepthLimit.fbs:
--------------------------------------------------------------------------------
1 |
2 | attribute "fs_serializer";
3 | attribute "fs_rpcInterface";
4 | attribute "fs_unsafeStructVector";
5 | attribute "fs_valueStruct";
6 | attribute "fs_writeThrough";
7 |
8 | namespace FlatSharpEndToEndTests.DepthLimit;
9 |
10 | table LLNode (fs_serializer)
11 | {
12 | Value : int;
13 | Next : LLNode;
14 | }
15 |
--------------------------------------------------------------------------------
/src/Tests/FlatSharpEndToEndTests/DeserializationOptionClassData.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 James Courtney
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | namespace FlatSharpEndToEndTests;
18 |
19 | public class DeserializationOptionClassData : IEnumerable