├── .github ├── FUNDING.yml └── workflows │ ├── codecov.yml │ └── dotnet.yml ├── .gitignore ├── .yamato └── Build.yml ├── LICENSE.md ├── ProjectStatus.md ├── README.md ├── benchmarks ├── 4.2.3 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ └── Benchmark.FBBench.FBSerializeBench-report-github.md ├── 5.0.0 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ └── Benchmark.FBBench.FBSerializeBench-report-github.md ├── 5.7.1 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ └── Benchmark.FBBench.FBSerializeBench-report-github.md ├── 6.0.0 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ ├── Benchmark.FBBench.FBSerializeBench-report-github.md │ ├── Benchmark.FBBench.FBSharedStringBench-report-github.md │ ├── Benchmark.FBBench.OthersDeserializeBench-report-github.md │ └── Benchmark.SerializationContextBenchmark-report-github.md ├── 6.3.3 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ ├── Benchmark.FBBench.FBDeserializeBench-report.csv │ ├── Benchmark.FBBench.FBDeserializeBench-report.html │ ├── Benchmark.FBBench.FBSerializeBench-report-github.md │ ├── Benchmark.FBBench.FBSerializeBench-report.csv │ ├── Benchmark.FBBench.FBSerializeBench-report.html │ ├── Benchmark.FBBench.FBSharedStringBench-report-github.md │ ├── Benchmark.FBBench.FBSharedStringBench-report.csv │ ├── Benchmark.FBBench.FBSharedStringBench-report.html │ ├── Benchmark.FBBench.WriteThroughBench-report-github.md │ ├── Benchmark.FBBench.WriteThroughBench-report.csv │ └── Benchmark.FBBench.WriteThroughBench-report.html ├── 7.0.0 │ ├── Benchmark.FBBench.FBDeserializeBench-report-github.md │ ├── Benchmark.FBBench.FBDeserializeBench-report.csv │ ├── Benchmark.FBBench.FBDeserializeBench-report.html │ ├── Benchmark.FBBench.FBSerializeBench-report-github.md │ ├── Benchmark.FBBench.FBSerializeBench-report.csv │ ├── Benchmark.FBBench.FBSerializeBench-report.html │ ├── Benchmark.FBBench.FBSharedStringBench-report-github.md │ ├── Benchmark.FBBench.FBSharedStringBench-report.csv │ ├── Benchmark.FBBench.FBSharedStringBench-report.html │ ├── Benchmark.FBBench.OthersDeserializeBench-report-github.md │ ├── Benchmark.FBBench.OthersDeserializeBench-report.csv │ ├── Benchmark.FBBench.OthersDeserializeBench-report.html │ ├── Benchmark.FBBench.WriteThroughBench-report-github.md │ ├── Benchmark.FBBench.WriteThroughBench-report.csv │ ├── Benchmark.FBBench.WriteThroughBench-report.html │ ├── Benchmark.SerializationContextBenchmark-report-github.md │ ├── Benchmark.SerializationContextBenchmark-report.csv │ └── Benchmark.SerializationContextBenchmark-report.html └── README.md ├── codecov.yml ├── global.json ├── misc ├── public.snk └── strongname.snk ├── samples ├── Assert.cs ├── Example00-Basics │ ├── Basics.cs │ └── Basics.fbs ├── Example01-DeserializationModes │ ├── DeserializationModes.cs │ └── DeserializationModes.fbs ├── Example02-Vectors │ ├── Vectors.fbs │ └── VectorsSample.cs ├── Example03-IOOptions │ ├── IOOperationsExample.cs │ └── IOOptionsExample.fbs ├── Example04-gRPC │ ├── GrpcExample.cs │ └── GrpcExample.fbs ├── Example05-CopyConstructors │ ├── CopyConstructorsExample.cs │ └── CopyConstructorsExample.fbs ├── Example06-Includes │ ├── A.fbs │ ├── IncludesExample.cs │ ├── IncludesExample.fbs │ ├── Shared │ │ └── C.fbs │ └── Subdirectory │ │ └── B.fbs ├── Example07-SortedVectors │ ├── SortedVectors.fbs │ └── SortedVectorsExample.cs ├── Example08-IndexedVectors │ ├── IndexedVectorExample.cs │ └── IndexedVectors.fbs ├── Example09-Unions │ ├── Unions.fbs │ └── UnionsExample.cs ├── Example10-SharedStrings │ ├── SharedStrings.fbs │ └── SharedStringsExample.cs ├── Example11-StructVectors │ ├── StructVectors.cs │ └── StructVectors.fbs ├── Example12-WriteThrough │ ├── WriteThrough.cs │ └── WriteThrough.fbs ├── Example13-ValueStructs │ ├── ValueStructs.cs │ └── ValueStructs.fbs ├── Example14-UnsafeOptions │ ├── UnsafeOptions.cs │ └── UnsafeOptions.fbs ├── GlobalUsings.cs ├── IFlatSharpSample.cs ├── Program.cs ├── Samples.csproj └── Samples.sln └── src ├── Benchmarks ├── Benchmark.6.0.0 │ └── Benchmark.6.0.0.csproj ├── Benchmark │ ├── Benchmark.csproj │ ├── FBBench │ │ ├── FBBenchCore.cs │ │ ├── FBDeserializeBench.cs │ │ ├── FBSerializeBench.cs │ │ ├── FBSharedStringsBench.cs │ │ ├── Google.Flatbuffers.fbs │ │ ├── Google.Flatbuffers_generated.cs │ │ ├── OthersDeserializeBench.cs │ │ └── WriteThroughBench.cs │ ├── Internal │ │ └── SerializationContextBenchmark.cs │ ├── Program.cs │ └── codegen.bat ├── ExperimentalBenchmark │ ├── Benchmark.fbs │ ├── ExperimentalBenchmark.csproj │ ├── Program.cs │ ├── buildClr.cmd │ └── jitDump.cmd ├── MicroBench.6.3.3 │ └── Microbench.6.3.3.csproj ├── MicroBench.Current │ ├── Constants.cs │ ├── Microbench.Current.csproj │ ├── ParseBenchmarks.cs │ ├── Program.cs │ ├── SerializeBenchmarks.cs │ ├── SortedVectorBenchmarks.cs │ └── VTableBenchmarks.cs └── Microbench.fbs ├── FlatSharp.Compiler ├── AssemblyInfo.cs ├── CloneMethodsGenerator.cs ├── CodeWriter.cs ├── CodeWritingPass.cs ├── CompileContext.cs ├── CompilerOptions.cs ├── DefaultConstructorKind.cs ├── ErrorContext.cs ├── FlatSharp.Compiler.csproj ├── FlatSharp.Compiler.nuspec ├── FlatSharp.Compiler.targets ├── FlatSharpCompiler.cs ├── GlobalUsings.cs ├── Helpers.cs ├── InvalidFbsFileException.cs ├── MetadataKeys.cs ├── PreprocessorHelper.cs ├── RpcStreamingType.cs ├── Schema │ ├── AdvancedFeatures.cs │ ├── BaseType.cs │ ├── EnumVal.cs │ ├── Field.cs │ ├── FlatBufferEnum.cs │ ├── FlatBufferObject.cs │ ├── FlatBufferType.cs │ ├── INamedSchemaElement.cs │ ├── KeyValue.cs │ ├── RpcCall.cs │ ├── RpcService.cs │ ├── Schema.cs │ └── SchemaFile.cs ├── SchemaModel │ ├── AttributeValidationResult.cs │ ├── BaseReferenceTypeSchemaModel.cs │ ├── BaseSchemaModel.cs │ ├── EnumSchemaModel.cs │ ├── FlatBufferSchemaElementType.cs │ ├── FlatSharpAttributeValidator.cs │ ├── FlatSharpAttributes.cs │ ├── IFlatSharpAttributes.cs │ ├── MutableFlatSharpAttributes.cs │ ├── PropertyFieldModel.cs │ ├── ReferenceStructSchemaModel.cs │ ├── ReferenceUnionSchemaModel.cs │ ├── RootModel.cs │ ├── RpcCallSchemaModel.cs │ ├── RpcServiceSchemaModel.cs │ ├── StructVectorPropertyFieldModel.cs │ ├── TableSchemaModel.cs │ ├── ValueStructSchemaModel.cs │ └── ValueUnionSchemaModel.cs ├── SchemaMutators │ ├── ExternalTypeSchemaMutator.cs │ ├── FieldNameNormalizerMutator.cs │ └── ISchemaMutator.cs ├── SetterKind.cs ├── VectorType.cs └── _._ ├── FlatSharp.Runtime ├── Attributes │ ├── ExternalAttribute.cs │ ├── FlatBufferEnumAttribute.cs │ ├── FlatBufferItemAttribute.cs │ ├── FlatBufferMetadataAttribute.cs │ ├── FlatBufferStructAttribute.cs │ └── FlatBufferTableAttribute.cs ├── BufferTooSmallException.cs ├── FlatBufferDeserializationContext.cs ├── FlatBufferSerializationOption.cs ├── FlatSharp.Runtime.csproj ├── FlatSharpInternal.cs ├── GeneratedSerializerWrapper.cs ├── GlobalUsings.cs ├── IFlatBufferDeserializedObject.cs ├── IFlatBufferDeserializedVector.cs ├── IFlatBufferSerializable.cs ├── IGeneratedSerializer.cs ├── IO │ ├── ArrayInputBuffer.cs │ ├── ArraySegmentInputBuffer.cs │ ├── IInputBuffer.cs │ ├── ISharedStringWriter.cs │ ├── ISpanWriter.cs │ ├── InputBufferExtensions.cs │ ├── MemoryInputBuffer.cs │ ├── ReadOnlyMemoryInputBuffer.cs │ ├── ScalarSpanReader.cs │ ├── SharedStringWriter.cs │ ├── SpanWriter.cs │ └── SpanWriterExtensions.cs ├── IObjectPool.cs ├── ISerializer.cs ├── ISerializerExtensions.cs ├── ISpanComparer.cs ├── NotMutableException.cs ├── ObjectPool.cs ├── Polyfills.cs ├── Properties │ └── AssemblyInfo.cs ├── SerializationContext.cs ├── SerializationHelpers.cs ├── SerializerSettings.cs ├── SortedVectorHelpers.cs ├── SortedVectorHelpersInternal.cs ├── SpanComparers.cs ├── SpanComparers.tt ├── StringSpanComparer.cs ├── TableFieldContext.cs ├── UnionTypes.cs ├── UnionTypes.tt ├── VTables │ ├── IVTable.cs │ ├── VTable4.cs │ ├── VTable8.cs │ └── VTableGeneric.cs ├── VectorCloneHelpers.cs └── Vectors │ ├── FlatBufferIndexedVector.cs │ ├── FlatBufferProgressiveIndexedVector.cs │ ├── FlatBufferProgressiveVector.cs │ ├── FlatBufferVectorBase.cs │ ├── GreedyIndexedVector.cs │ ├── IIndexedVector.cs │ ├── ISortableTable.cs │ ├── IVectorItemAccessor.cs │ ├── ImmutableList.cs │ ├── IndexedVector.cs │ └── PoolableList.cs ├── FlatSharp.UnityPolyfills ├── FlatSharp.UnityPolyfills.csproj └── NativeArray.cs ├── FlatSharp.sln ├── FlatSharp ├── FlatBufferMetadataAttributeExtensions.cs ├── FlatBufferSerializer.cs ├── FlatBufferSerializerOptions.cs ├── FlatBufferVectorHelpers.cs ├── FlatSharp.csproj ├── FlatSharpCompilationException.cs ├── GlobalUsings.cs ├── Properties │ └── AssemblyInfo.cs ├── Serialization │ ├── AccessModifier.cs │ ├── CSharpHelpers.cs │ ├── CloneCodeGenContext.cs │ ├── CodeGeneratedMethod.cs │ ├── DefaultMethodNameResolver.cs │ ├── DeserializeClassDefinition.cs │ ├── GetMaxSizeCodeGenContext.cs │ ├── IMethodNameResolver.cs │ ├── ParserCodeGenContext.cs │ ├── RoslynSerializerGenerator.cs │ └── SerializationCodeGenContext.cs ├── TypeModel │ ├── EnumTypeModel.cs │ ├── FlatBufferSchemaType.cs │ ├── FlatSharpTypeModelProvider.cs │ ├── ITypeModel.cs │ ├── ITypeModelExtensions.cs │ ├── ITypeModelProvider.cs │ ├── InvalidFlatBufferDefinitionException.cs │ ├── ItemMemberModel.cs │ ├── NullableTypeModel.cs │ ├── PhysicalLayoutElement.cs │ ├── RuntimeTypeModel.cs │ ├── ScalarTypeModel.cs │ ├── ScalarTypeModelPartials.cs │ ├── ScalarTypeModels.cs │ ├── ScalarTypeModels.tt │ ├── StringTypeModel.cs │ ├── StructMemberModel.cs │ ├── StructTypeModel.cs │ ├── TableMemberModel.cs │ ├── TableTypeModel.cs │ ├── TypeModelContainer.cs │ ├── TypeModelContainerExtensions.cs │ ├── UnionTypeModel.cs │ ├── UnityTypeModelProvider.cs │ ├── ValueStructTypeModel.cs │ ├── Vectors │ │ ├── BaseVectorTypeModel.cs │ │ ├── IndexedVectorTypeModel.cs │ │ ├── ListVectorTypeModel.cs │ │ ├── MemoryVectorTypeModel.cs │ │ └── UnityNativeArrayVectorTypeModel.cs │ └── VectorsOfUnion │ │ ├── BaseVectorOfUnionTypeModel.cs │ │ └── ListVectorOfUnionTypeModel.cs ├── UnionTypes.cs └── UnionTypes.tt ├── Google.FlatBuffers ├── ByteBuffer.cs ├── ByteBufferUtil.cs ├── FlatBufferBuilder.cs ├── FlatBufferConstants.cs ├── Google.FlatBuffers.csproj ├── IFlatbufferObject.cs ├── Offset.cs ├── Struct.cs └── Table.cs ├── Tests ├── FlatSharpCompilerTests │ ├── AccessModifierTests.cs │ ├── AdvancedFeaturesTests.cs │ ├── ApacheArrowTest.cs │ ├── CSharpKeywordTests.cs │ ├── CompilerTestHelpers.cs │ ├── CopyConstructorTests.cs │ ├── DefaultCtorTests.cs │ ├── DepthLimitTests.cs │ ├── EnumTests.cs │ ├── ExternalTests.cs │ ├── FieldNameNormalizationTests.cs │ ├── FileIdentifierTests.cs │ ├── FlatSharpCompilerTests.csproj │ ├── FloatSpecialDefaultTests.cs │ ├── ForceWriteTests.cs │ ├── FullTests.cs │ ├── GlobalUsings.cs │ ├── Grpc │ │ └── GrpcTests.cs │ ├── IncludeTests.cs │ ├── InvalidAttributeTests.cs │ ├── MetadataHelpers.cs │ ├── MetadataTests.cs │ ├── ModuleInitializer.cs │ ├── NullableAnnotationTests.cs │ ├── OptionalScalarTests.cs │ ├── PrecompiledSerializerTests.cs │ ├── RealExamples.cs │ ├── RequiredTests.cs │ ├── ScalarSizeTests.cs │ ├── SharedStringCompilerTests.cs │ ├── SortedVectors.cs │ ├── StructTests.cs │ ├── StructVectorTests.cs │ ├── UnionTests.cs │ ├── ValueStructTests.cs │ ├── VectorOfUnionTests.cs │ ├── VirtualMemberTests.cs │ └── WriteThroughTests.cs ├── FlatSharpEndToEndTests │ ├── ClassLib │ │ ├── FlatBufferSerializerNonGenericTests.cs │ │ ├── FlatBufferSerializerNonGenericTests.fbs │ │ ├── FlatBufferVectorOfUnionTests.cs │ │ ├── FlatBufferVectorOfUnionTests.fbs │ │ ├── FlatBufferVectorTests.cs │ │ ├── FlatBufferVectorTests.fbs │ │ ├── ImmutableListTests.cs │ │ ├── IndexedVectorTests.cs │ │ ├── IndexedVectorTests.fbs │ │ ├── NonScalarVectorTests.cs │ │ ├── NonScalarVectorTests.fbs │ │ ├── PoolableListTests.cs │ │ ├── SerializerConfigurationTests.cs │ │ ├── SerializerConfigurationTests.fbs │ │ ├── SpanComparerTests.cs │ │ └── VTableTests.cs │ ├── CopyConstructors │ │ ├── CopyConstructorTests.cs │ │ └── CopyConstructorTests.fbs │ ├── DepthLimit │ │ ├── DepthLimit.fbs │ │ └── DepthLimitTests.cs │ ├── DeserializationOptionClassData.cs │ ├── Documentation │ │ ├── Documentation.fbs │ │ └── DocumentationTestCases.cs │ ├── FileIdentifier │ │ ├── FileId.fbs │ │ └── FileIdentifierTests.cs │ ├── FlatSharpEndToEndTests.csproj │ ├── GlobalUsings.cs │ ├── Grpc │ │ ├── EchoServiceTestCases.cs │ │ └── Grpc.fbs │ ├── Helpers.cs │ ├── IO │ │ ├── InputBufferTests.cs │ │ └── InputBufferTests.fbs │ ├── Oracle │ │ ├── AlignmentTests.cs │ │ ├── FlatSharp.fbs │ │ ├── Flatc │ │ │ └── FlatSharpEndToEndTests │ │ │ │ └── Oracle │ │ │ │ └── Flatc │ │ │ │ ├── AlignmentTestInner.cs │ │ │ │ ├── AlignmentTestOuter.cs │ │ │ │ ├── AlignmentTestOuterHoder.cs │ │ │ │ ├── BasicTypes.cs │ │ │ │ ├── FiveByteStruct.cs │ │ │ │ ├── FiveByteStructTable.cs │ │ │ │ ├── InnerStruct.cs │ │ │ │ ├── LinkedListNode.cs │ │ │ │ ├── Location.cs │ │ │ │ ├── LocationHolder.cs │ │ │ │ ├── MyEnum.cs │ │ │ │ ├── NestedStructs.cs │ │ │ │ ├── OuterStruct.cs │ │ │ │ ├── SortedVectorDoubleTable.cs │ │ │ │ ├── SortedVectorInt32Table.cs │ │ │ │ ├── SortedVectorStringTable.cs │ │ │ │ ├── SortedVectorTest.cs │ │ │ │ ├── StructVectors.cs │ │ │ │ ├── StructVectorsTable.cs │ │ │ │ ├── Union.cs │ │ │ │ ├── UnionTable.cs │ │ │ │ ├── UnionVectorTable.cs │ │ │ │ ├── VectorOfUnionTable.cs │ │ │ │ └── Vectors.cs │ │ ├── OracleDeserializeTests.cs │ │ └── OracleSerializeTests.cs │ ├── PartialMethods │ │ ├── PartialMethodsTests.cs │ │ └── PartialMethodsTests.fbs │ ├── RawData │ │ ├── RawData.fbs │ │ ├── RawDataSharedStringTests.cs │ │ ├── RawDataStringTests.cs │ │ └── RawDataTableTests.cs │ ├── Required │ │ ├── Required.cs │ │ └── Required.fbs │ ├── SetterTests │ │ ├── AccessModifiers.cs │ │ └── AccessModifiers.fbs │ ├── TableMembers │ │ ├── DeserailizationOptionsTests.cs │ │ ├── TableMembers.fbs │ │ └── TableMembersTests.cs │ ├── Unions │ │ ├── Unions.fbs │ │ └── UnionsTestCases.cs │ ├── Utf8StringComparer.cs │ ├── ValueStructs │ │ ├── ValueStructTests.cs │ │ └── ValueStructs.fbs │ └── Vectors │ │ ├── Sorted │ │ ├── IndexedVectorTests.cs │ │ ├── SortedVectorTests.cs │ │ └── SortedVectors.fbs │ │ └── Standard │ │ ├── StandardVectorTests.cs │ │ └── StandardVectors.fbs ├── FlatSharpPoolableEndToEndTests │ ├── FlatSharpPoolableEndToEndTests.csproj │ ├── GlobalUsings.cs │ ├── PoolingTests.cs │ ├── PoolingTests.fbs │ └── TestObjectPool.cs ├── FlatSharpTests │ ├── ClassLib │ │ └── TypeModelTests.cs │ ├── FlatSharpTests.csproj │ ├── GlobalUsings.cs │ ├── ModuleInitializer.cs │ ├── SerializationTests │ │ ├── ScalarVectorTests.cs │ │ ├── TableSerializationTests.cs │ │ ├── UnionTests.cs │ │ ├── ValueStructTests.cs │ │ ├── VectorDeserializationTests.cs │ │ ├── VectorSerializationTests.cs │ │ └── WriteThroughTests.cs │ ├── Tests.bfbs │ ├── Tests.fbs │ ├── Util │ │ ├── ContextHelpers.cs │ │ ├── ExtensionMethods.cs │ │ └── Utf8StringComparer.cs │ ├── regenerateOracle.cmd │ ├── stryker-flatsharp.cmd │ └── stryker-runtime.cmd ├── Flatc.fbs ├── coverlet.runsettings └── regenerateOracle.cmd ├── common.props ├── ext └── flatc │ ├── LICENSE.txt │ ├── flatc.version.txt │ ├── linux │ └── flatc │ ├── macos_arm │ └── flatc │ ├── macos_intel │ └── flatc │ └── windows │ └── flatc.exe ├── netfx.props └── runbenchmarks.cmd /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [jamescourtney] 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | **/.vs 6 | **/obj/* 7 | **/bin/* 8 | **/*.csproj.user 9 | **/*.fbs.generated.cs 10 | **/*.DotSettings* 11 | **/.coverage/** 12 | /src/Grpc/NativeGrpc/Debug 13 | /src/Debug 14 | /src/Grpc/NativeGrpc/NativeGrpc.vcxproj.user 15 | **/coverage.net5.0.xml 16 | /src/Tests/Coverage/coverage.xml 17 | /src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.xml 18 | /src/Tests/FlatSharpEndToEndTests/FlatSharp.generated.cs 19 | /src/Benchmarks/ExperimentalBenchmark/FlatSharp.generated.cs 20 | **/.idea 21 | -------------------------------------------------------------------------------- /.yamato/Build.yml: -------------------------------------------------------------------------------- 1 | build: 2 | name: Build 3 | agent: 4 | type: Unity::VM 5 | image: burst/burst-base:1.0.8-1195360 6 | flavor: b1.xlarge 7 | commands: 8 | - gsudo choco install dotnet-7.0-sdk 9 | - dotnet build --configuration Release src/FlatSharp.Compiler/FlatSharp.Compiler.csproj -f net7.0 10 | - dotnet build --configuration Release src/FlatSharp.sln 11 | - dotnet test --configuration Release src/FlatSharp.sln 12 | timeout: 30 13 | artifacts: 14 | compiler_artifacts: 15 | paths: 16 | - src/FlatSharp/bin/Release/**/* 17 | - src/FlatSharp.Compiler/bin/Release/**/* 18 | -------------------------------------------------------------------------------- /benchmarks/4.2.3/Benchmark.FBBench.FBSerializeBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1237 (20H2/October2020Update) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=5.0.201 6 | [Host] : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 7 | ShortRun : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 5.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | VectorLength | Mean | Error | StdDev | P25 | P95 | Gen 0 | Allocated | 14 | |------------------------------------------ |------------- |-----------:|---------:|----------:|-----------:|-----------:|-------:|----------:| 15 | | FlatSharp_GetMaxSize | 30 | 214.4 ns | 1.10 ns | 1.75 ns | 213.9 ns | 218.9 ns | - | - | 16 | | FlatSharp_Serialize | 30 | 1,823.8 ns | 87.36 ns | 138.56 ns | 1,729.5 ns | 2,133.9 ns | - | 24 B | 17 | | FlatSharp_Serialize_NonVirtual | 30 | 1,433.9 ns | 19.57 ns | 31.60 ns | 1,405.9 ns | 1,478.8 ns | - | 24 B | 18 | | FlatSharp_Serialize_StringVector_Sorted | 30 | 1,829.3 ns | 20.97 ns | 32.66 ns | 1,796.6 ns | 1,882.2 ns | 0.0057 | 112 B | 19 | | FlatSharp_Serialize_StringVector_Unsorted | 30 | 911.1 ns | 5.74 ns | 8.77 ns | 903.5 ns | 926.6 ns | 0.0010 | 24 B | 20 | | FlatSharp_Serialize_IntVector_Sorted | 30 | 1,255.5 ns | 16.96 ns | 26.90 ns | 1,237.0 ns | 1,287.3 ns | 0.0057 | 112 B | 21 | | FlatSharp_Serialize_IntVector_Unsorted | 30 | 428.4 ns | 1.54 ns | 2.45 ns | 427.2 ns | 435.3 ns | 0.0014 | 24 B | 22 | | FlatSharp_Serialize_ValueTableVector | 30 | 6,581.1 ns | 83.97 ns | 137.96 ns | 6,445.4 ns | 6,852.2 ns | - | 24 B | 23 | -------------------------------------------------------------------------------- /benchmarks/5.0.0/Benchmark.FBBench.FBSerializeBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1237 (20H2/October2020Update) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=5.0.201 6 | [Host] : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 7 | ShortRun : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 5.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | VectorLength | Mean | Error | StdDev | Median | P25 | P95 | Gen 0 | Allocated | 14 | |------------------------------------------ |------------- |-----------:|----------:|----------:|-----------:|-----------:|-----------:|-------:|----------:| 15 | | FlatSharp_GetMaxSize | 30 | 222.3 ns | 2.19 ns | 3.47 ns | 223.5 ns | 223.0 ns | 224.8 ns | - | - | 16 | | FlatSharp_Serialize | 30 | 2,019.3 ns | 51.65 ns | 83.40 ns | 2,017.1 ns | 1,943.0 ns | 2,137.4 ns | - | 24 B | 17 | | FlatSharp_Serialize_NonVirtual | 30 | 1,266.9 ns | 28.81 ns | 45.70 ns | 1,244.9 ns | 1,237.8 ns | 1,368.4 ns | - | 24 B | 18 | | FlatSharp_Serialize_StringVector_Sorted | 30 | 1,805.3 ns | 23.95 ns | 37.28 ns | 1,803.1 ns | 1,771.8 ns | 1,857.3 ns | 0.0057 | 112 B | 19 | | FlatSharp_Serialize_StringVector_Unsorted | 30 | 918.0 ns | 3.68 ns | 5.63 ns | 918.2 ns | 914.7 ns | 927.7 ns | 0.0010 | 24 B | 20 | | FlatSharp_Serialize_IntVector_Sorted | 30 | 1,299.5 ns | 26.31 ns | 42.48 ns | 1,309.1 ns | 1,273.6 ns | 1,375.7 ns | 0.0057 | 112 B | 21 | | FlatSharp_Serialize_IntVector_Unsorted | 30 | 442.9 ns | 26.45 ns | 41.95 ns | 427.1 ns | 420.2 ns | 540.2 ns | 0.0010 | 24 B | 22 | | FlatSharp_Serialize_ValueTableVector | 30 | 6,362.9 ns | 101.40 ns | 157.87 ns | 6,312.3 ns | 6,289.2 ns | 6,771.1 ns | - | 24 B | 23 | -------------------------------------------------------------------------------- /benchmarks/6.0.0/Benchmark.FBBench.FBSharedStringBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1237 (20H2/October2020Update) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=5.0.201 6 | [Host] : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 7 | ShortRun : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 5.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | CacheSize | VectorLength | Mean | Error | StdDev | P25 | P95 | Allocated | 14 | |----------------------------------------------- |---------- |------------- |---------:|---------:|---------:|---------:|---------:|----------:| 15 | | **Serialize_RandomStringVector_WithRegularString** | **127** | **1000** | **16.13 μs** | **0.056 μs** | **0.086 μs** | **16.04 μs** | **16.24 μs** | **-** | 16 | | Serialize_RandomStringVector_WithSharing | 127 | 1000 | 48.29 μs | 0.115 μs | 0.182 μs | 48.18 μs | 48.57 μs | - | 17 | | Serialize_NonRandomStringVector_WithSharing | 127 | 1000 | 47.22 μs | 0.135 μs | 0.214 μs | 47.11 μs | 47.58 μs | - | 18 | | **Serialize_RandomStringVector_WithRegularString** | **1024** | **1000** | **15.95 μs** | **0.071 μs** | **0.112 μs** | **15.89 μs** | **16.10 μs** | **-** | 19 | | Serialize_RandomStringVector_WithSharing | 1024 | 1000 | 47.48 μs | 0.195 μs | 0.310 μs | 47.30 μs | 47.95 μs | - | 20 | | Serialize_NonRandomStringVector_WithSharing | 1024 | 1000 | 45.57 μs | 0.265 μs | 0.412 μs | 45.24 μs | 46.40 μs | - | 21 | -------------------------------------------------------------------------------- /benchmarks/6.3.3/Benchmark.FBBench.FBSerializeBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=7.0.100 6 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 7 | ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 7.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | VectorLength | Mean | Error | StdDev | P25 | P95 | Gen0 | Allocated | 14 | |------------------------------------------ |------------- |-----------:|---------:|---------:|-----------:|-----------:|-------:|----------:| 15 | | FlatSharp_GetMaxSize | 30 | 138.8 ns | 1.62 ns | 2.57 ns | 137.8 ns | 143.7 ns | - | - | 16 | | FlatSharp_Serialize | 30 | 1,219.5 ns | 11.01 ns | 16.81 ns | 1,206.5 ns | 1,240.6 ns | - | - | 17 | | FlatSharp_Serialize_ValueStructs | 30 | 1,237.3 ns | 4.99 ns | 7.62 ns | 1,230.6 ns | 1,246.7 ns | - | - | 18 | | FlatSharp_Serialize_StringVector_Sorted | 30 | 1,760.1 ns | 33.91 ns | 55.71 ns | 1,732.9 ns | 1,860.9 ns | 0.0038 | 88 B | 19 | | FlatSharp_Serialize_StringVector_Unsorted | 30 | 827.8 ns | 6.18 ns | 9.63 ns | 824.0 ns | 846.9 ns | - | - | 20 | | FlatSharp_Serialize_IntVector_Sorted | 30 | 977.2 ns | 16.61 ns | 26.35 ns | 949.8 ns | 1,010.2 ns | 0.0048 | 88 B | 21 | | FlatSharp_Serialize_IntVector_Unsorted | 30 | 395.3 ns | 1.88 ns | 3.04 ns | 393.9 ns | 400.7 ns | - | - | 22 | | FlatSharp_Serialize_ValueTableVector | 30 | 796.4 ns | 15.92 ns | 25.25 ns | 782.9 ns | 853.8 ns | - | - | 23 | -------------------------------------------------------------------------------- /benchmarks/6.3.3/Benchmark.FBBench.FBSharedStringBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=7.0.100 6 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 7 | ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 7.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | CacheSize | VectorLength | Mean | Error | StdDev | P25 | P95 | Allocated | 14 | |----------------------------------------------- |---------- |------------- |---------:|---------:|---------:|---------:|---------:|----------:| 15 | | **Serialize_RandomStringVector_WithRegularString** | **127** | **1000** | **14.54 μs** | **0.093 μs** | **0.147 μs** | **14.46 μs** | **14.84 μs** | **-** | 16 | | Serialize_RandomStringVector_WithSharing | 127 | 1000 | 49.60 μs | 0.218 μs | 0.346 μs | 49.39 μs | 50.52 μs | - | 17 | | Serialize_NonRandomStringVector_WithSharing | 127 | 1000 | 48.84 μs | 0.248 μs | 0.401 μs | 48.57 μs | 49.54 μs | - | 18 | | **Serialize_RandomStringVector_WithRegularString** | **1024** | **1000** | **14.48 μs** | **0.069 μs** | **0.105 μs** | **14.47 μs** | **14.59 μs** | **-** | 19 | | Serialize_RandomStringVector_WithSharing | 1024 | 1000 | 47.59 μs | 0.380 μs | 0.568 μs | 47.07 μs | 48.64 μs | - | 20 | | Serialize_NonRandomStringVector_WithSharing | 1024 | 1000 | 45.42 μs | 0.301 μs | 0.487 μs | 44.95 μs | 46.08 μs | - | 21 | -------------------------------------------------------------------------------- /benchmarks/6.3.3/Benchmark.FBBench.WriteThroughBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=7.0.100 6 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 7 | ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 7.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | Mean | Error | StdDev | Median | P25 | P95 | Gen0 | Allocated | 14 | |----------------- |-----------:|--------:|---------:|-----------:|-----------:|-----------:|-------:|----------:| 15 | | RefStructSmall | 174.3 ns | 0.70 ns | 1.09 ns | 174.4 ns | 174.1 ns | 175.6 ns | 0.0086 | 144 B | 16 | | RefStructLarge | 1,141.0 ns | 6.62 ns | 10.30 ns | 1,147.1 ns | 1,129.8 ns | 1,153.1 ns | 0.0095 | 176 B | 17 | | ValueStructSmall | 221.5 ns | 0.47 ns | 0.76 ns | 221.4 ns | 221.0 ns | 223.2 ns | 0.0062 | 104 B | 18 | | ValueStructLarge | 1,220.0 ns | 6.21 ns | 9.67 ns | 1,213.9 ns | 1,211.0 ns | 1,232.1 ns | 0.0057 | 104 B | 19 | -------------------------------------------------------------------------------- /benchmarks/6.3.3/Benchmark.FBBench.WriteThroughBench-report.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Benchmark.FBBench.WriteThroughBench-20221116-201005 6 | 7 | 13 | 14 | 15 |

16 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819)
17 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
18 | .NET SDK=7.0.100
19 |   [Host]   : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2
20 |   ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2
21 | 
22 |
Job=ShortRun  AnalyzeLaunchVariance=True  Runtime=.NET 7.0  
23 | IterationCount=5  LaunchCount=7  WarmupCount=3  
24 | 
25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
MethodMeanErrorStdDevMedian P25 P95Gen0Allocated
RefStructSmall174.3 ns0.70 ns1.09 ns174.4 ns174.1 ns175.6 ns0.0086144 B
RefStructLarge1,141.0 ns6.62 ns10.30 ns1,147.1 ns1,129.8 ns1,153.1 ns0.0095176 B
ValueStructSmall221.5 ns0.47 ns0.76 ns221.4 ns221.0 ns223.2 ns0.0062104 B
ValueStructLarge1,220.0 ns6.21 ns9.67 ns1,213.9 ns1,211.0 ns1,232.1 ns0.0057104 B
34 | 35 | 36 | -------------------------------------------------------------------------------- /benchmarks/7.0.0/Benchmark.FBBench.FBSharedStringBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=7.0.100 6 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 7 | ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 7.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | CacheSize | VectorLength | Mean | Error | StdDev | Median | P25 | P95 | Allocated | 14 | |----------------------------------------------- |---------- |------------- |---------:|---------:|---------:|---------:|---------:|---------:|----------:| 15 | | **Serialize_RandomStringVector_WithRegularString** | **127** | **1000** | **14.65 μs** | **0.056 μs** | **0.084 μs** | **14.66 μs** | **14.60 μs** | **14.79 μs** | **-** | 16 | | Serialize_RandomStringVector_WithSharing | 127 | 1000 | 49.45 μs | 0.120 μs | 0.190 μs | 49.37 μs | 49.33 μs | 49.86 μs | - | 17 | | Serialize_NonRandomStringVector_WithSharing | 127 | 1000 | 48.45 μs | 0.134 μs | 0.212 μs | 48.46 μs | 48.22 μs | 48.75 μs | - | 18 | | **Serialize_RandomStringVector_WithRegularString** | **1024** | **1000** | **14.48 μs** | **0.087 μs** | **0.133 μs** | **14.41 μs** | **14.38 μs** | **14.72 μs** | **-** | 19 | | Serialize_RandomStringVector_WithSharing | 1024 | 1000 | 47.17 μs | 0.335 μs | 0.521 μs | 47.05 μs | 46.74 μs | 48.10 μs | - | 20 | | Serialize_NonRandomStringVector_WithSharing | 1024 | 1000 | 45.30 μs | 0.282 μs | 0.447 μs | 45.46 μs | 45.01 μs | 45.84 μs | - | 21 | -------------------------------------------------------------------------------- /benchmarks/7.0.0/Benchmark.FBBench.WriteThroughBench-report-github.md: -------------------------------------------------------------------------------- 1 | ``` ini 2 | 3 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819) 4 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores 5 | .NET SDK=7.0.100 6 | [Host] : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 7 | ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2 8 | 9 | Job=ShortRun AnalyzeLaunchVariance=True Runtime=.NET 7.0 10 | IterationCount=5 LaunchCount=7 WarmupCount=3 11 | 12 | ``` 13 | | Method | Mean | Error | StdDev | P25 | P95 | Gen0 | Allocated | 14 | |----------------- |-----------:|--------:|---------:|-----------:|-----------:|-------:|----------:| 15 | | RefStructSmall | 180.3 ns | 1.69 ns | 2.58 ns | 178.3 ns | 184.9 ns | 0.0086 | 144 B | 16 | | RefStructLarge | 1,156.9 ns | 6.27 ns | 9.76 ns | 1,145.5 ns | 1,167.2 ns | 0.0095 | 176 B | 17 | | ValueStructSmall | 201.7 ns | 9.58 ns | 15.47 ns | 193.6 ns | 239.1 ns | 0.0062 | 104 B | 18 | | ValueStructLarge | 1,227.7 ns | 7.77 ns | 12.32 ns | 1,217.2 ns | 1,246.2 ns | 0.0057 | 104 B | 19 | -------------------------------------------------------------------------------- /benchmarks/7.0.0/Benchmark.FBBench.WriteThroughBench-report.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Benchmark.FBBench.WriteThroughBench-20221116-183542 6 | 7 | 13 | 14 | 15 |

16 | BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.819)
17 | AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
18 | .NET SDK=7.0.100
19 |   [Host]   : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2
20 |   ShortRun : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2
21 | 
22 |
Job=ShortRun  AnalyzeLaunchVariance=True  Runtime=.NET 7.0  
23 | IterationCount=5  LaunchCount=7  WarmupCount=3  
24 | 
25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
MethodMeanErrorStdDev P25 P95Gen0Allocated
RefStructSmall180.3 ns1.69 ns2.58 ns178.3 ns184.9 ns0.0086144 B
RefStructLarge1,156.9 ns6.27 ns9.76 ns1,145.5 ns1,167.2 ns0.0095176 B
ValueStructSmall201.7 ns9.58 ns15.47 ns193.6 ns239.1 ns0.0062104 B
ValueStructLarge1,227.7 ns7.77 ns12.32 ns1,217.2 ns1,246.2 ns0.0057104 B
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 20 | { 21 | public IEnumerator GetEnumerator() 22 | { 23 | yield return new object[] { FlatBufferDeserializationOption.Lazy }; 24 | yield return new object[] { FlatBufferDeserializationOption.Progressive }; 25 | yield return new object[] { FlatBufferDeserializationOption.Greedy }; 26 | yield return new object[] { FlatBufferDeserializationOption.GreedyMutable }; 27 | } 28 | 29 | IEnumerator IEnumerable.GetEnumerator() 30 | { 31 | return this.GetEnumerator(); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Documentation/Documentation.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.Documentation; 9 | 10 | enum UndocumentedEnum : ubyte 11 | { 12 | A, 13 | B 14 | } 15 | 16 | /// CommentTest:73c67946-5c52-4643-baa9-a1a0c0d758d7 17 | enum TestEnum : uint 18 | { 19 | /// CommentTest:282048b4-2817-4312-93ec-8c21e8d42f8d 20 | A, 21 | Uncommented 22 | } 23 | 24 | /// CommentTest:d54eef2c-f1d0-497f-874f-eeb0e47588b6 25 | union TestUnion 26 | { 27 | RefStruct, 28 | ValueStruct, 29 | Table 30 | } 31 | 32 | union UndocumentedUnion 33 | { 34 | RefStruct, 35 | ValueStruct, 36 | Table 37 | } 38 | 39 | /// CommentTest:eb9a0f9a-6f4e-4839-be67-e719b411a274 40 | table Table (fs_serializer) 41 | { 42 | /// CommentTest:41b60a53-10a9-4f4a-9d36-3bf73fb95392 43 | Property : int; 44 | 45 | /// <><> 46 | EscapeTest : int; 47 | 48 | Uncommented : int; 49 | } 50 | 51 | 52 | table UndocumentedTable 53 | { 54 | } 55 | 56 | /// CommentTest:cb1b9369-c724-4106-b02b-2afe4984cf89 57 | struct RefStruct 58 | { 59 | /// CommentTest:95571f1b-c255-4191-a489-2b06ddcae495 60 | Property : int; 61 | 62 | /// CommentTest:93efd448-fa30-4ddb-9d3b-56ca3f10b403 63 | Vector : [ int : 1 ]; 64 | 65 | UncommentedProperty : int; 66 | UncommentedVector : [ int : 1 ]; 67 | } 68 | 69 | struct UncommentedRefStruct 70 | { 71 | Property : int; 72 | } 73 | 74 | /// CommentTest:7a379625-7511-48f9-8b57-34b41c073da8 75 | struct ValueStruct (fs_valueStruct) 76 | { 77 | /// CommentTest:62d5d69b-b3a0-4446-9abe-d0b314555a4b 78 | Property : int; 79 | 80 | /// CommentTest:30add22e-345d-46e2-a1a2-346043cfd444 81 | Vector : [ int : 1 ]; 82 | 83 | UncommentedProperty : int; 84 | UncommentedVector : [ int : 1 ]; 85 | } 86 | 87 | /// CommentTest:d9232e86-035e-4b86-bc77-a3cb04575ba1 88 | rpc_service Service (fs_rpcInterface) 89 | { 90 | /// CommentTest:932b099d-1cff-4533-81f3-b0eef65e21f3 91 | MethodCall(Table) : Table; 92 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/FileIdentifier/FileId.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.FileIdentifiers; 9 | 10 | table NoId (fs_serializer) 11 | { 12 | } 13 | table HasId (fs_serializer) 14 | { 15 | } 16 | 17 | file_identifier "abcd"; 18 | root_type HasId; 19 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/FileIdentifier/FileIdentifierTests.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 System.IO; 18 | 19 | namespace FlatSharpEndToEndTests.FileIdentifiers; 20 | 21 | public class FileIdentifierTests 22 | { 23 | private static byte[] EmptyTableWithId = 24 | { 25 | 8, 0, 0, 0, // uoffset to the start of the table. 26 | 97, 98, 99, 100, // abcd file id 27 | 252, 255, 255, 255, // soffset_t to the vtable 28 | 4, 0, // vtable size 29 | 4, 0, // table length 30 | }; 31 | 32 | private static byte[] EmptyTableWithoutId = 33 | { 34 | 4, 0, 0, 0, // uoffset to the start of the table. 35 | 252, 255, 255, 255, // soffset_t to the vtable 36 | 4, 0, // vtable size 37 | 4, 0, // table length 38 | }; 39 | 40 | [Fact] 41 | public void FileIdentifier_Serialized() 42 | { 43 | byte[] buffer = new HasId().AllocateAndSerialize(); 44 | Assert.True(EmptyTableWithId.AsSpan().SequenceEqual(buffer)); 45 | } 46 | 47 | [Fact] 48 | public void NoFileIdentifier_Serialized() 49 | { 50 | byte[] buffer = new NoId().AllocateAndSerialize(); 51 | Assert.True(EmptyTableWithoutId.AsSpan().SequenceEqual(buffer)); 52 | } 53 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/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 Xunit; 32 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Grpc/Grpc.fbs: -------------------------------------------------------------------------------- 1 |  2 | attribute "fs_serializer"; 3 | attribute "fs_sharedString"; 4 | attribute "fs_rpcInterface"; 5 | 6 | namespace Other.Namespace.Foobar; 7 | table Blah (fs_serializer) { Value : int; } 8 | 9 | namespace FlatSharpEndToEndTests.GrpcTests; 10 | 11 | table StringMessage (fs_serializer) { 12 | Value : string; 13 | } 14 | 15 | table MultiStringMessage (fs_serializer:"Progressive") { 16 | Value : [string] (fs_sharedString, required); 17 | } 18 | 19 | rpc_service EchoService (fs_rpcInterface) { 20 | EchoUnary(StringMessage) : StringMessage; 21 | EchoClientStreaming(StringMessage) : MultiStringMessage (streaming:"client"); 22 | EchoServerStreaming(MultiStringMessage) : StringMessage (streaming:"server"); 23 | EchoDuplexStreaming(StringMessage) : StringMessage (streaming:"duplex"); 24 | 25 | NsTest(Other.Namespace.Foobar.Blah) : StringMessage; 26 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/IO/InputBufferTests.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.IO.InputBufferTests; 10 | 11 | table PrimitiveTypesTable (fs_serializer) 12 | { 13 | A : bool; 14 | B : ubyte; 15 | C : byte; 16 | D : ushort; 17 | E : short; 18 | F : uint; 19 | G : int; 20 | H : ulong; 21 | I : long; 22 | J : float; 23 | K : double; 24 | L : string; 25 | M : string (fs_sharedString); 26 | } 27 | 28 | table ReadOnlyMemoryTable (fs_serializer) 29 | { 30 | Memory : [ ubyte ] (fs_vector:"ReadOnlyMemory"); 31 | } 32 | 33 | table MemoryTable (fs_serializer) 34 | { 35 | Memory : [ ubyte ]; 36 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Oracle/Flatc/FlatSharpEndToEndTests/Oracle/Flatc/FiveByteStruct.cs: -------------------------------------------------------------------------------- 1 | // 2 | // automatically generated by the FlatBuffers compiler, do not modify 3 | // 4 | 5 | namespace FlatSharpEndToEndTests.Oracle.Flatc 6 | { 7 | 8 | using global::System; 9 | using global::System.Collections.Generic; 10 | using global::Google.FlatBuffers; 11 | 12 | public struct FiveByteStruct : IFlatbufferObject 13 | { 14 | private Struct __p; 15 | public ByteBuffer ByteBuffer { get { return __p.bb; } } 16 | public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); } 17 | public FiveByteStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } 18 | 19 | public int Int { get { return __p.bb.GetInt(__p.bb_pos + 0); } } 20 | public byte Byte { get { return __p.bb.Get(__p.bb_pos + 4); } } 21 | 22 | public static Offset CreateFiveByteStruct(FlatBufferBuilder builder, int Int, byte Byte) { 23 | builder.Prep(4, 8); 24 | builder.Pad(3); 25 | builder.PutByte(Byte); 26 | builder.PutInt(Int); 27 | return new Offset(builder.Offset); 28 | } 29 | public FiveByteStructT UnPack() { 30 | var _o = new FiveByteStructT(); 31 | this.UnPackTo(_o); 32 | return _o; 33 | } 34 | public void UnPackTo(FiveByteStructT _o) { 35 | _o.Int = this.Int; 36 | _o.Byte = this.Byte; 37 | } 38 | public static Offset Pack(FlatBufferBuilder builder, FiveByteStructT _o) { 39 | if (_o == null) return default(Offset); 40 | return CreateFiveByteStruct( 41 | builder, 42 | _o.Int, 43 | _o.Byte); 44 | } 45 | } 46 | 47 | public class FiveByteStructT 48 | { 49 | public int Int { get; set; } 50 | public byte Byte { get; set; } 51 | 52 | public FiveByteStructT() { 53 | this.Int = 0; 54 | this.Byte = 0; 55 | } 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Oracle/Flatc/FlatSharpEndToEndTests/Oracle/Flatc/InnerStruct.cs: -------------------------------------------------------------------------------- 1 | // 2 | // automatically generated by the FlatBuffers compiler, do not modify 3 | // 4 | 5 | namespace FlatSharpEndToEndTests.Oracle.Flatc 6 | { 7 | 8 | using global::System; 9 | using global::System.Collections.Generic; 10 | using global::Google.FlatBuffers; 11 | 12 | public struct InnerStruct : IFlatbufferObject 13 | { 14 | private Struct __p; 15 | public ByteBuffer ByteBuffer { get { return __p.bb; } } 16 | public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); } 17 | public InnerStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } 18 | 19 | public int A { get { return __p.bb.GetInt(__p.bb_pos + 0); } } 20 | 21 | public static Offset CreateInnerStruct(FlatBufferBuilder builder, int A) { 22 | builder.Prep(4, 4); 23 | builder.PutInt(A); 24 | return new Offset(builder.Offset); 25 | } 26 | public InnerStructT UnPack() { 27 | var _o = new InnerStructT(); 28 | this.UnPackTo(_o); 29 | return _o; 30 | } 31 | public void UnPackTo(InnerStructT _o) { 32 | _o.A = this.A; 33 | } 34 | public static Offset Pack(FlatBufferBuilder builder, InnerStructT _o) { 35 | if (_o == null) return default(Offset); 36 | return CreateInnerStruct( 37 | builder, 38 | _o.A); 39 | } 40 | } 41 | 42 | public class InnerStructT 43 | { 44 | public int A { get; set; } 45 | 46 | public InnerStructT() { 47 | this.A = 0; 48 | } 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Oracle/Flatc/FlatSharpEndToEndTests/Oracle/Flatc/Location.cs: -------------------------------------------------------------------------------- 1 | // 2 | // automatically generated by the FlatBuffers compiler, do not modify 3 | // 4 | 5 | namespace FlatSharpEndToEndTests.Oracle.Flatc 6 | { 7 | 8 | using global::System; 9 | using global::System.Collections.Generic; 10 | using global::Google.FlatBuffers; 11 | 12 | public struct Location : IFlatbufferObject 13 | { 14 | private Struct __p; 15 | public ByteBuffer ByteBuffer { get { return __p.bb; } } 16 | public void __init(int _i, ByteBuffer _bb) { __p = new Struct(_i, _bb); } 17 | public Location __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } 18 | 19 | public float X { get { return __p.bb.GetFloat(__p.bb_pos + 0); } } 20 | public float Y { get { return __p.bb.GetFloat(__p.bb_pos + 4); } } 21 | public float Z { get { return __p.bb.GetFloat(__p.bb_pos + 8); } } 22 | 23 | public static Offset CreateLocation(FlatBufferBuilder builder, float X, float Y, float Z) { 24 | builder.Prep(4, 12); 25 | builder.PutFloat(Z); 26 | builder.PutFloat(Y); 27 | builder.PutFloat(X); 28 | return new Offset(builder.Offset); 29 | } 30 | public LocationT UnPack() { 31 | var _o = new LocationT(); 32 | this.UnPackTo(_o); 33 | return _o; 34 | } 35 | public void UnPackTo(LocationT _o) { 36 | _o.X = this.X; 37 | _o.Y = this.Y; 38 | _o.Z = this.Z; 39 | } 40 | public static Offset Pack(FlatBufferBuilder builder, LocationT _o) { 41 | if (_o == null) return default(Offset); 42 | return CreateLocation( 43 | builder, 44 | _o.X, 45 | _o.Y, 46 | _o.Z); 47 | } 48 | } 49 | 50 | public class LocationT 51 | { 52 | public float X { get; set; } 53 | public float Y { get; set; } 54 | public float Z { get; set; } 55 | 56 | public LocationT() { 57 | this.X = 0.0f; 58 | this.Y = 0.0f; 59 | this.Z = 0.0f; 60 | } 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Oracle/Flatc/FlatSharpEndToEndTests/Oracle/Flatc/MyEnum.cs: -------------------------------------------------------------------------------- 1 | // 2 | // automatically generated by the FlatBuffers compiler, do not modify 3 | // 4 | 5 | namespace FlatSharpEndToEndTests.Oracle.Flatc 6 | { 7 | 8 | public enum MyEnum : sbyte 9 | { 10 | A = 0, 11 | B = 1, 12 | }; 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Oracle/Flatc/FlatSharpEndToEndTests/Oracle/Flatc/Union.cs: -------------------------------------------------------------------------------- 1 | // 2 | // automatically generated by the FlatBuffers compiler, do not modify 3 | // 4 | 5 | namespace FlatSharpEndToEndTests.Oracle.Flatc 6 | { 7 | 8 | public enum Union : byte 9 | { 10 | NONE = 0, 11 | BasicTypes = 1, 12 | Location = 2, 13 | stringValue = 3, 14 | }; 15 | 16 | public class UnionUnion { 17 | public Union Type { get; set; } 18 | public object Value { get; set; } 19 | 20 | public UnionUnion() { 21 | this.Type = Union.NONE; 22 | this.Value = null; 23 | } 24 | 25 | public T As() where T : class { return this.Value as T; } 26 | public FlatSharpEndToEndTests.Oracle.Flatc.BasicTypesT AsBasicTypes() { return this.As(); } 27 | public static UnionUnion FromBasicTypes(FlatSharpEndToEndTests.Oracle.Flatc.BasicTypesT _basictypes) { return new UnionUnion{ Type = Union.BasicTypes, Value = _basictypes }; } 28 | public FlatSharpEndToEndTests.Oracle.Flatc.LocationT AsLocation() { return this.As(); } 29 | public static UnionUnion FromLocation(FlatSharpEndToEndTests.Oracle.Flatc.LocationT _location) { return new UnionUnion{ Type = Union.Location, Value = _location }; } 30 | public string AsstringValue() { return this.As(); } 31 | public static UnionUnion FromstringValue(string _stringvalue) { return new UnionUnion{ Type = Union.stringValue, Value = _stringvalue }; } 32 | 33 | public static int Pack(Google.FlatBuffers.FlatBufferBuilder builder, UnionUnion _o) { 34 | switch (_o.Type) { 35 | default: return 0; 36 | case Union.BasicTypes: return FlatSharpEndToEndTests.Oracle.Flatc.BasicTypes.Pack(builder, _o.AsBasicTypes()).Value; 37 | case Union.Location: return FlatSharpEndToEndTests.Oracle.Flatc.Location.Pack(builder, _o.AsLocation()).Value; 38 | case Union.stringValue: return builder.CreateString(_o.AsstringValue()).Value; 39 | } 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/PartialMethods/PartialMethodsTests.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.PartialMethods; 10 | 11 | struct Struct 12 | { 13 | a : int; 14 | } 15 | 16 | table Table (fs_serializer) 17 | { 18 | s : Struct; 19 | } 20 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Required/Required.fbs: -------------------------------------------------------------------------------- 1 |  2 | attribute "fs_serializer"; 3 | attribute "fs_valueStruct"; 4 | 5 | namespace FlatSharpEndToEndTests.Required; 6 | 7 | struct RefStruct 8 | { 9 | A : int; 10 | } 11 | 12 | struct ValueStruct (fs_valueStruct) 13 | { 14 | A : int; 15 | } 16 | 17 | table RequiredTable (fs_serializer) 18 | { 19 | A : string (required); 20 | B : [ string ] (required); 21 | C : NonRequiredTable (required); 22 | D : [ ubyte ] (required); 23 | E : RefStruct (required); 24 | F : ValueStruct (required); 25 | } 26 | 27 | table NonRequiredTable (fs_serializer) 28 | { 29 | A : string; 30 | B : [ string ]; 31 | C : NonRequiredTable; 32 | D : [ ubyte ]; 33 | E : RefStruct; 34 | F : ValueStruct; 35 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/SetterTests/AccessModifiers.fbs: -------------------------------------------------------------------------------- 1 |  2 | attribute "fs_serializer"; 3 | attribute "fs_setter"; 4 | 5 | namespace FlatSharpEndToEndTests.AccessModifiers; 6 | 7 | table Table (fs_serializer) 8 | { 9 | public_implicit : int; 10 | none : int (fs_setter:"None"); 11 | 12 | public : int (fs_setter:"Public"); 13 | protected_internal : int (fs_setter:"ProtectedInternal"); 14 | protected : int (fs_setter:"Protected"); 15 | 16 | public_init : int (fs_setter:"PublicInit"); 17 | protected_init : int (fs_setter:"ProtectedInit"); 18 | protected_internal_init : int (fs_setter:"ProtectedInternalInit"); 19 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Unions/Unions.fbs: -------------------------------------------------------------------------------- 1 |  2 | attribute "fs_serializer"; 3 | attribute "fs_sharedString"; 4 | attribute "fs_rpcInterface"; 5 | attribute "fs_valueStruct"; 6 | attribute "fs_unsafeUnion"; 7 | attribute "fs_unsafeExternal"; 8 | 9 | namespace FlatSharpEndToEndTests.Unions; 10 | 11 | struct A { V : uint; } 12 | struct B { V : uint; } 13 | struct C { V : uint; } 14 | struct D { V : uint; } 15 | 16 | union MyUnion { A, B, C, D } 17 | 18 | struct ValueStructInt (fs_valueStruct) { Value : int; } 19 | struct ValueStructVec3 (fs_valueStruct) { X : float32; Y : float32; Z : float32; } 20 | struct ValueStructExtCorrectSize (fs_valueStruct, fs_unsafeExternal:"System.Numerics.Vector3") { Temp : [ float32 : 3 ]; } 21 | struct ValueStructExtWrongSize (fs_valueStruct, fs_unsafeExternal:"System.Numerics.Vector") { Temp : [ ubyte : 4 ]; } 22 | 23 | union UnsafeUnion (fs_unsafeUnion) { ValueStructInt, ValueStructVec3, ExtCorrect : ValueStructExtCorrectSize, ExtWrong : ValueStructExtWrongSize } 24 | 25 | table Container (fs_serializer) { 26 | Value : [MyUnion]; 27 | Unsafe : UnsafeUnion; 28 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Utf8StringComparer.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 FlatSharp.Internal; 18 | 19 | namespace FlatSharpEndToEndTests; 20 | 21 | public class Utf8StringComparer : IComparer 22 | { 23 | public int Compare(string x, string y) 24 | { 25 | return StringSpanComparer.Instance.Compare(x != null, SerializationHelpers.Encoding.GetBytes(x), y != null, SerializationHelpers.Encoding.GetBytes(y)); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpEndToEndTests/Vectors/Standard/StandardVectors.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_sortedVector"; 8 | 9 | namespace FlatSharpEndToEndTests.Vectors.Standard; 10 | 11 | table StandardVectorTable (fs_serializer) 12 | { 13 | implicit_string_list : [ string ]; 14 | explicit_string_list : [ string ] (fs_vector:"IList"); 15 | read_only_string_list : [ string ] (fs_vector:"IReadOnlyList"); 16 | 17 | implicit_memory : [ ubyte ]; 18 | explicit_memory : [ ubyte ] (fs_vector:"Memory"); 19 | read_only_memory : [ ubyte ] (fs_vector:"ReadOnlyMemory"); 20 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpPoolableEndToEndTests/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 Xunit; 32 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpPoolableEndToEndTests/PoolingTests.fbs: -------------------------------------------------------------------------------- 1 |  2 | attribute "fs_serializer"; 3 | attribute "fs_unsafeStructVector"; 4 | attribute "fs_valueStruct"; 5 | attribute "fs_writeThrough"; 6 | attribute "fs_vector"; 7 | 8 | namespace FlatSharpEndToEndTests.PoolingTests; 9 | 10 | struct RefStruct { x : int; } 11 | struct ValueStruct (fs_valueStruct) { x : int; } 12 | table InnerTable { x : int; } 13 | 14 | union InnerUnion { RefStruct, ValueStruct, InnerTable } 15 | 16 | table KeyValue 17 | { 18 | key : int (key); 19 | value : int; 20 | } 21 | 22 | table Root (fs_serializer:"Lazy") 23 | { 24 | ref_struct : RefStruct; 25 | value_struct : ValueStruct; 26 | inner_table : InnerTable; 27 | 28 | vector_of_ref_struct : [ RefStruct ]; 29 | vector_of_value_struct : [ ValueStruct ]; 30 | vector_of_table : [ InnerTable ]; 31 | 32 | vector_of_union : [ InnerUnion ]; 33 | 34 | indexed_vector : [ KeyValue ] (fs_vector:"IIndexedVector"); 35 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/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.Reflection; 27 | global using System.Runtime.CompilerServices; 28 | global using FlatSharp; 29 | global using FlatSharp.Attributes; 30 | global using FlatSharp.Internal; 31 | global using Xunit; 32 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/ModuleInitializer.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 FlatSharpTests 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 | } 29 | } 30 | } 31 | 32 | #if !NET5_0_OR_GREATER 33 | namespace System.Runtime.CompilerServices 34 | { 35 | [AttributeUsage(AttributeTargets.Method)] 36 | public class ModuleInitializerAttribute : Attribute 37 | { 38 | } 39 | } 40 | #endif 41 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/Tests.bfbs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/src/Tests/FlatSharpTests/Tests.bfbs -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/Util/ContextHelpers.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.CodeGen; 18 | using FlatSharp.TypeModel; 19 | 20 | namespace FlatSharpTests; 21 | 22 | public static class ContextHelpers 23 | { 24 | public static ParserCodeGenContext CreateParserContext(FlatBufferSerializerOptions? options = null) 25 | { 26 | return new ParserCodeGenContext( 27 | "a", 28 | "b", 29 | "e", 30 | "c", 31 | false, 32 | "d", 33 | new DefaultMethodNameResolver(), 34 | options ?? new FlatBufferSerializerOptions(), 35 | TypeModelContainer.CreateDefault(), 36 | new Dictionary>()); 37 | } 38 | 39 | public static SerializationCodeGenContext CreateSerializeContext(FlatBufferSerializerOptions? options = null) 40 | { 41 | return new SerializationCodeGenContext( 42 | "a", 43 | "b", 44 | "c", 45 | "d", 46 | "e", 47 | "f", 48 | false, 49 | new DefaultMethodNameResolver(), 50 | TypeModelContainer.CreateDefault(), 51 | options ?? new FlatBufferSerializerOptions(), 52 | new Dictionary>()); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/Util/ExtensionMethods.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; 18 | 19 | internal static class Extensions 20 | { 21 | internal static string? GetCSharp(this ISerializer serializer) where T : class 22 | { 23 | return ((GeneratedSerializerWrapper)serializer).CSharp; 24 | } 25 | 26 | internal static string? GetCSharp(this ISerializer serializer) 27 | { 28 | var prop = typeof(GeneratedSerializerWrapper<>) 29 | .MakeGenericType(serializer.RootType) 30 | .GetProperty("CSharp"); 31 | 32 | return (string?)prop.GetValue(serializer); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/Util/Utf8StringComparer.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; 18 | 19 | public class Utf8StringComparer : IComparer 20 | { 21 | public int Compare(string x, string y) 22 | { 23 | return StringSpanComparer.Instance.Compare(x != null, SerializationHelpers.Encoding.GetBytes(x), y != null, SerializationHelpers.Encoding.GetBytes(y)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/regenerateOracle.cmd: -------------------------------------------------------------------------------- 1 | 2 | rmdir /S /Q .\Generated\FlatSharpTests 3 | ..\..\ext\flatc\windows\flatc.exe --csharp --gen-object-api -o .\OracleTests\Generated\ .\Tests.fbs 4 | ..\..\ext\flatc\windows\flatc.exe -b -o . .\Tests.fbs -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/stryker-flatsharp.cmd: -------------------------------------------------------------------------------- 1 | dotnet stryker --excluded-mutations "['checked', 'string']" --project-file=FlatSharp.csproj -------------------------------------------------------------------------------- /src/Tests/FlatSharpTests/stryker-runtime.cmd: -------------------------------------------------------------------------------- 1 | dotnet stryker --mutate "['!*UnionTypes.cs']" --excluded-mutations "['checked', 'string']" --project-file=FlatSharp.Runtime.csproj -------------------------------------------------------------------------------- /src/Tests/coverlet.runsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | opencover 8 | true 9 | false 10 | true 11 | false 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Tests/regenerateOracle.cmd: -------------------------------------------------------------------------------- 1 | 2 | rmdir /S /Q .\FlatSharpEndToEndTests\Oracle\Flatc\ 3 | ..\ext\flatc\windows\flatc.exe --csharp --gen-object-api -o .\FlatSharpEndToEndTests\Oracle\Flatc\ .\Flatc.fbs -------------------------------------------------------------------------------- /src/common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | false 5 | $(MSBuildThisFileDirectory)\..\misc\strongname.snk 6 | 11.0 7 | true 8 | 9 | 10 | 11 | $(DefineConstants);SIGNED_BUILD 12 | 13 | 14 | 15 | true 16 | 7.1.0 17 | 7.1.0 18 | $(Version) 19 | James Courtney 20 | FlatSharp is a fast, idiomatic implementation of the FlatBuffer binary format. 21 | 2023 22 | https://github.com/jamescourtney/FlatSharp/ 23 | flatbuffers serialization flatbuffer flatsharp 24 | Release notes at https://github.com/jamescourtney/FlatSharp/releases 25 | true 26 | Apache-2.0 27 | version=$(PackageVersion);releaseNotes=$(PackageReleaseNotes) 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/ext/flatc/flatc.version.txt: -------------------------------------------------------------------------------- 1 | Flatc binaries are copyright Google Inc, and are distributed in accordance with the Apache 2 License, which is included adjacent to this file. 2 | 3 | The executables included are sourced from the following release of FlatBuffers: 4 | https://github.com/google/flatbuffers/releases/tag/v22.10.26 5 | 6 | You are encouraged to verify the hashes of the files included in this package with those from the linked CI run. -------------------------------------------------------------------------------- /src/ext/flatc/linux/flatc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/src/ext/flatc/linux/flatc -------------------------------------------------------------------------------- /src/ext/flatc/macos_arm/flatc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/src/ext/flatc/macos_arm/flatc -------------------------------------------------------------------------------- /src/ext/flatc/macos_intel/flatc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/src/ext/flatc/macos_intel/flatc -------------------------------------------------------------------------------- /src/ext/flatc/windows/flatc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Unity-Technologies/FlatSharp/8eb62e8f4c5c76b1a404bed1e1775bd720dc77f7/src/ext/flatc/windows/flatc.exe -------------------------------------------------------------------------------- /src/runbenchmarks.cmd: -------------------------------------------------------------------------------- 1 | dotnet build -c release 2 | 3 | pushd Benchmarks\Benchmark\bin\release\net7.0 4 | Benchmark.exe 5 | popd 6 | 7 | pushd Benchmarks\Benchmark.6.0.0\bin\release\net7.0 8 | Benchmark.6.0.0.exe 9 | popd 10 | 11 | 12 | --------------------------------------------------------------------------------