-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Compiler generated union subclasses * More tests
- Loading branch information
1 parent
90055e9
commit 625c779
Showing
14 changed files
with
4,195 additions
and
3,981 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2020 James Courtney | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Samples.Unions; | ||
|
||
enum DogBreed : byte { Golden, Lab, Corgi } | ||
enum CatBreed : byte { Calico, Shorthair, Bengal } | ||
enum FishKind : byte { Coelacanth, Dorado, Piranha } | ||
|
||
// FlatSharp generates a "Pet" class that derives from the "FlatBufferUnion" class. The derived class | ||
// is syntax sugar and just contains some quality-of-life improvements. | ||
// In this example, the "Doggo" label is applied, so all of the union's properties in C# will | ||
// refer to "Doggo". For fish and Cat, the names are left as-is. | ||
union Pet { Doggo:Dog, Cat, Fish } | ||
|
||
// Don't generate a custom type for this union. | ||
union BasicPet (NoCustomType) { Dog, Cat, Fish } | ||
|
||
table Dog { Breed:DogBreed; Name:string; } | ||
table Cat { Breed:CatBreed; Name:string; } | ||
table Fish { Kind:FishKind; Weight:float64; Name:string; } | ||
|
||
table Person | ||
{ | ||
// A person can have one pet. Either a dog, cat, or fish. | ||
Pet:Pet; | ||
|
||
BasicPet:BasicPet; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2020 James Courtney | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using FlatSharp; | ||
using FlatSharp.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace Samples.Unions | ||
{ | ||
/// <summary> | ||
/// This example shows how to use FlatSharp with unions. FlatBuffer unions are discriminated unions, which means exactly one of the fields may be set. | ||
/// FlatSharp can | ||
/// </summary> | ||
public class UnionsExample | ||
{ | ||
public static void Run() | ||
{ | ||
Cat simon = new Cat { Breed = CatBreed.Bengal, Name = "Simon" }; | ||
Dog george = new Dog { Breed = DogBreed.Corgi, Name = "George" }; | ||
Fish brick = new Fish { Kind = FishKind.Coelacanth, Name = "Brick" }; | ||
|
||
Person person = new Person | ||
{ | ||
// The difference between Person.Pet and Person.BasicPet is that Person.Pet is | ||
// a custom convenience class that derives from FlatBufferUnion. In | ||
Pet = new Pet(brick), | ||
BasicPet = new FlatBufferUnion<Dog, Cat, Fish>(brick), | ||
}; | ||
|
||
UsePet(person.Pet); | ||
UseUnion(person.BasicPet); | ||
|
||
UseUnion(person.Pet); // Custom unions derive from FlatBufferUnion. | ||
} | ||
|
||
// These two methods do the same thing. They are coded separately to | ||
// demonstrate the usability you get with FlatSharp's compiled union | ||
// types. | ||
|
||
public static void UsePet(Pet pet) | ||
{ | ||
// Enum members | ||
if (pet.Kind == Pet.ItemKind.Doggo) | ||
{ | ||
Dog dog = pet.Doggo; | ||
dog.Name.ToLowerInvariant(); | ||
} | ||
|
||
string breed = pet.Switch( | ||
caseDefault: () => "unknown", | ||
caseDoggo: d => d.Breed.ToString(), | ||
caseCat: c => c.Breed.ToString(), | ||
caseFish: f => f.Kind.ToString()); | ||
} | ||
|
||
public static void UseUnion(FlatBufferUnion<Dog, Cat, Fish> basicUnion) | ||
{ | ||
if (basicUnion.Discriminator == 1) | ||
{ | ||
Dog dog = basicUnion.Item1; | ||
dog.Name.ToLowerInvariant(); | ||
} | ||
|
||
string breed = basicUnion.Switch( | ||
defaultCase: () => "unknown", | ||
case1: d => d.Breed.ToString(), | ||
case2: c => c.Breed.ToString(), | ||
case3: f => f.Kind.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.