Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target .NET Standard 2.1, .NET 6, .NET 8 only #8184

Merged
merged 9 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
exclude:
# Clang++15 10.3.0 stdlibc++ doesn't fully support std 23
- cxx: clang++-15
std: 23
std: 23

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -186,34 +186,6 @@ jobs:
shell: bash
run: echo "hashes=$(sha256sum Windows.flatc.binary.zip | base64 -w0)" >> $GITHUB_OUTPUT

build-windows-2017:
name: Build Windows 2017
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: cmake
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build tool version 15 (VS 2017)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=15.0
- name: test
run: Release\flattests.exe

build-windows-2015:
name: Build Windows 2015
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: cmake
run: cmake -G "Visual Studio 14 2015" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_STRICT_MODE=ON .
- name: build tool version 14 (VS 2015)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=14.0
- name: test
run: Release\flattests.exe

build-dotnet-windows:
name: Build .NET Windows
runs-on: windows-2022-64core
Expand All @@ -230,17 +202,17 @@ jobs:
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v3
with:
dotnet-version: '3.1.x'
dotnet-version: '8.0.x'
- name: Build
run: |
cd tests\FlatBuffers.Test
dotnet new sln --force --name FlatBuffers.Core.Test
dotnet sln FlatBuffers.Core.Test.sln add FlatBuffers.Core.Test.csproj
dotnet build -c Release ${{matrix.configuration}} -o out FlatBuffers.Core.Test.sln
dotnet new sln --force --name FlatBuffers.Test
dotnet sln FlatBuffers.Test.sln add FlatBuffers.Test.csproj
dotnet build -c Release ${{matrix.configuration}} -o out FlatBuffers.Test.sln
- name: Run
run: |
cd tests\FlatBuffers.Test
out\FlatBuffers.Core.Test.exe
out\FlatBuffers.Test.exe
build-mac-intel:
permissions:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'

dotnet-version: '8.0.x'
- name: Build
run: |
dotnet build Google.FlatBuffers.csproj -c Release
Expand Down
23 changes: 10 additions & 13 deletions docs/source/CsharpUsage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ FlatBuffers).
## Building the FlatBuffers C# library

The `FlatBuffers.csproj` project contains multitargeting for .NET Standard 2.1,
.NET Standard 2.0, and .NET Framework 4.6 (Unity 2017). Support for .NET
Framework 3.5 (Unity 5) is provided by the `FlatBuffers.net35.csproj` project.
In most cases (including Unity 2018 and newer), .NET Standard 2.0 is
recommended.
.NET 6 and .NET 8.

You can build for a specific framework target when using the cross-platform
[.NET Core SDK](https://dotnet.microsoft.com/download) by adding the `-f`
command line option:

~~~{.sh}
dotnet build -f netstandard2.0 "FlatBuffers.csproj"
dotnet build -f netstandard2.1 "FlatBuffers.csproj"
~~~

The `FlatBuffers.csproj` project also provides support for defining various
Expand Down Expand Up @@ -142,10 +139,10 @@ To use it:
`ByKey` only works if the vector has been sorted, it will
likely not find elements if it hasn't been sorted.

## Buffer verification
## Buffer verification

As mentioned in [C++ Usage](@ref flatbuffers_guide_use_cpp) buffer
accessor functions do not verify buffer offsets at run-time.
accessor functions do not verify buffer offsets at run-time.
If it is necessary, you can optionally use a buffer verifier before you
access the data. This verifier will check all offsets, all sizes of
fields, and null termination of strings to ensure that when a buffer
Expand All @@ -158,17 +155,17 @@ e.g. `Monster.VerifyMonster`. This can be called as shown:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if `ok` is true, the buffer is safe to read.

For a more detailed control of verification `MonsterVerify.Verify`
for `Monster` type can be used:
For a more detailed control of verification `MonsterVerify.Verify`
for `Monster` type can be used:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cs}
# Sequence of calls
FlatBuffers.Verifier verifier = new FlatBuffers.Verifier(buf);
var ok = verifier.VerifyBuffer("MONS", false, MonsterVerify.Verify);
# Or single line call
# Or single line call
var ok = new FlatBuffers.Verifier(bb).setStringCheck(true).\
VerifyBuffer("MONS", false, MonsterVerify.Verify);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if `ok` is true, the buffer is safe to read.

Expand All @@ -181,7 +178,7 @@ Verifier supports options that can be set using appropriate fluent methods:
* SetMaxTables - total amount of tables the verifier may encounter. Default: 64
* SetAlignmentCheck - check content alignment. Default: True
* SetStringCheck - check if strings contain termination '0' character. Default: true


## Text parsing

Expand Down
52 changes: 26 additions & 26 deletions net/FlatBuffers/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
using System.Runtime.InteropServices;
using System.Text;

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
using System.Buffers.Binary;
#endif

#if ENABLE_SPAN_T && !UNSAFE_BYTEBUFFER && !NETSTANDARD2_1
#if ENABLE_SPAN_T && !UNSAFE_BYTEBUFFER
#warning ENABLE_SPAN_T requires UNSAFE_BYTEBUFFER to also be defined
#endif

namespace Google.FlatBuffers
{
public abstract class ByteBufferAllocator
{
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public abstract Span<byte> Span { get; }
public abstract ReadOnlySpan<byte> ReadOnlySpan { get; }
public abstract Memory<byte> Memory { get; }
Expand Down Expand Up @@ -103,7 +103,7 @@ public override void GrowFront(int newSize)
InitBuffer();
}

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public override Span<byte> Span => _buffer;
public override ReadOnlySpan<byte> ReadOnlySpan => _buffer;
public override Memory<byte> Memory => _buffer;
Expand Down Expand Up @@ -237,7 +237,7 @@ public static int ArraySize<T>(ArraySegment<T> x)
return SizeOf<T>() * x.Count;
}

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public static int ArraySize<T>(Span<T> x)
{
return SizeOf<T>() * x.Length;
Expand All @@ -246,7 +246,7 @@ public static int ArraySize<T>(Span<T> x)

// Get a portion of the buffer casted into an array of type T, given
// the buffer position and length.
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public T[] ToArray<T>(int pos, int len)
where T : struct
{
Expand Down Expand Up @@ -274,7 +274,7 @@ public byte[] ToFullArray()
return ToArray<byte>(0, Length);
}

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public ReadOnlyMemory<byte> ToReadOnlyMemory(int pos, int len)
{
return _buffer.ReadOnlyMemory.Slice(pos, len);
Expand Down Expand Up @@ -337,7 +337,7 @@ static public ulong ReverseBytes(ulong input)
((input & 0xFF00000000000000UL) >> 56));
}

#if !UNSAFE_BYTEBUFFER && (!ENABLE_SPAN_T || !NETSTANDARD2_1)
#if !UNSAFE_BYTEBUFFER && !ENABLE_SPAN_T
// Helper functions for the safe (but slower) version.
protected void WriteLittleEndian(int offset, int count, ulong data)
{
Expand Down Expand Up @@ -377,7 +377,7 @@ protected ulong ReadLittleEndian(int offset, int count)
}
return r;
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
#elif ENABLE_SPAN_T
protected void WriteLittleEndian(int offset, int count, ulong data)
{
if (BitConverter.IsLittleEndian)
Expand Down Expand Up @@ -427,7 +427,7 @@ private void AssertOffsetAndLength(int offset, int length)
#endif
}

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER

public void PutSbyte(int offset, sbyte value)
{
Expand Down Expand Up @@ -487,7 +487,7 @@ public unsafe void PutStringUTF8(int offset, string value)
}
}
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
#elif ENABLE_SPAN_T
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
Expand Down Expand Up @@ -652,7 +652,7 @@ public void PutFloat(int offset, float value)
// that contains it.
ConversionUnion union;
union.intValue = 0;
union.floatValue = value;
union.floatValue = value;
WriteLittleEndian(offset, sizeof(float), (ulong)union.intValue);
}

Expand All @@ -664,7 +664,7 @@ public void PutDouble(int offset, double value)

#endif // UNSAFE_BYTEBUFFER

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public sbyte GetSbyte(int index)
{
AssertOffsetAndLength(index, sizeof(sbyte));
Expand Down Expand Up @@ -698,7 +698,7 @@ public unsafe string GetStringUTF8(int startPos, int len)
return Encoding.UTF8.GetString(buffer, len);
}
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
#elif ENABLE_SPAN_T
public string GetStringUTF8(int startPos, int len)
{
return Encoding.UTF8.GetString(_buffer.Span.Slice(startPos, len));
Expand Down Expand Up @@ -765,7 +765,7 @@ public unsafe ulong GetUlong(int offset)
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt64LittleEndian(span);
#else
#else
fixed (byte* ptr = _buffer.Buffer)
{
return BitConverter.IsLittleEndian
Expand Down Expand Up @@ -885,16 +885,16 @@ public int Put<T>(int offset, T[] x)
}

/// <summary>
/// Copies an array segment of type T into this buffer, ending at the
/// given offset into this buffer. The starting offset is calculated
/// Copies an array segment of type T into this buffer, ending at the
/// given offset into this buffer. The starting offset is calculated
/// based on the count of the array segment and is the value returned.
/// </summary>
/// <typeparam name="T">The type of the input data (must be a struct)
/// </typeparam>
/// <param name="offset">The offset into this buffer where the copy
/// <param name="offset">The offset into this buffer where the copy
/// will end</param>
/// <param name="x">The array segment to copy data from</param>
/// <returns>The 'start' location of this buffer now, after the copy
/// <returns>The 'start' location of this buffer now, after the copy
/// completed</returns>
public int Put<T>(int offset, ArraySegment<T> x)
where T : struct
Expand All @@ -921,7 +921,7 @@ public int Put<T>(int offset, ArraySegment<T> x)
offset -= numBytes;
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
MemoryMarshal.Cast<T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
#else
var srcOffset = ByteBuffer.SizeOf<T>() * x.Offset;
Expand All @@ -942,17 +942,17 @@ public int Put<T>(int offset, ArraySegment<T> x)
}

/// <summary>
/// Copies an array segment of type T into this buffer, ending at the
/// given offset into this buffer. The starting offset is calculated
/// Copies an array segment of type T into this buffer, ending at the
/// given offset into this buffer. The starting offset is calculated
/// based on the count of the array segment and is the value returned.
/// </summary>
/// <typeparam name="T">The type of the input data (must be a struct)
/// </typeparam>
/// <param name="offset">The offset into this buffer where the copy
/// <param name="offset">The offset into this buffer where the copy
/// will end</param>
/// <param name="ptr">The pointer to copy data from</param>
/// <param name="sizeInBytes">The number of bytes to copy</param>
/// <returns>The 'start' location of this buffer now, after the copy
/// <returns>The 'start' location of this buffer now, after the copy
/// completed</returns>
public int Put<T>(int offset, IntPtr ptr, int sizeInBytes)
where T : struct
Expand Down Expand Up @@ -980,7 +980,7 @@ public int Put<T>(int offset, IntPtr ptr, int sizeInBytes)
// if we are LE, just do a block copy
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
unsafe
{
{
var span = new Span<byte>(ptr.ToPointer(), sizeInBytes);
span.CopyTo(_buffer.Span.Slice(offset, sizeInBytes));
}
Expand All @@ -1001,7 +1001,7 @@ public int Put<T>(int offset, IntPtr ptr, int sizeInBytes)
return offset;
}

#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public int Put<T>(int offset, Span<T> x)
where T : struct
{
Expand Down
Loading
Loading