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

* Target .NET Standard 2.1, .NET 6, .NET 8 only

* Remove mono usage

* Fix bat name ref

* Up deps

* Up deps

* Reinstate build-windows

* Fix name

---------

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Björn Harrtell
2023-12-19 23:43:55 +01:00
committed by GitHub
parent e5fc3b16d8
commit 129ef422e8
15 changed files with 109 additions and 449 deletions

View File

@@ -43,11 +43,11 @@ using System.Runtime.CompilerServices;
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
@@ -55,7 +55,7 @@ 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; }
@@ -103,7 +103,7 @@ namespace Google.FlatBuffers
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;
@@ -237,7 +237,7 @@ namespace Google.FlatBuffers
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;
@@ -246,7 +246,7 @@ namespace Google.FlatBuffers
// 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
{
@@ -274,7 +274,7 @@ namespace Google.FlatBuffers
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);
@@ -337,7 +337,7 @@ namespace Google.FlatBuffers
((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)
{
@@ -377,7 +377,7 @@ namespace Google.FlatBuffers
}
return r;
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
#elif ENABLE_SPAN_T
protected void WriteLittleEndian(int offset, int count, ulong data)
{
if (BitConverter.IsLittleEndian)
@@ -427,7 +427,7 @@ namespace Google.FlatBuffers
#endif
}
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public void PutSbyte(int offset, sbyte value)
{
@@ -487,7 +487,7 @@ namespace Google.FlatBuffers
}
}
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
#elif ENABLE_SPAN_T
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
@@ -652,7 +652,7 @@ namespace Google.FlatBuffers
// that contains it.
ConversionUnion union;
union.intValue = 0;
union.floatValue = value;
union.floatValue = value;
WriteLittleEndian(offset, sizeof(float), (ulong)union.intValue);
}
@@ -664,7 +664,7 @@ namespace Google.FlatBuffers
#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));
@@ -698,7 +698,7 @@ namespace Google.FlatBuffers
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));
@@ -765,7 +765,7 @@ namespace Google.FlatBuffers
#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
@@ -885,16 +885,16 @@ namespace Google.FlatBuffers
}
/// <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
@@ -921,7 +921,7 @@ namespace Google.FlatBuffers
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;
@@ -942,17 +942,17 @@ namespace Google.FlatBuffers
}
/// <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
@@ -980,7 +980,7 @@ namespace Google.FlatBuffers
// 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));
}
@@ -1001,7 +1001,7 @@ namespace Google.FlatBuffers
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
{

View File

@@ -235,7 +235,7 @@ namespace Google.FlatBuffers
_space = _bb.Put<T>(_space, ptr, sizeInBytes);
}
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
/// <summary>
/// Puts a span of type T into this builder at the
/// current offset
@@ -399,7 +399,7 @@ namespace Google.FlatBuffers
Put<T>(ptr, sizeInBytes);
}
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
/// <summary>
/// Add a span of type T to the buffer (aligns the data and grows if necessary).
/// </summary>
@@ -533,10 +533,10 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable boolean value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddBool(int o, bool? x) { if (x.HasValue) { AddBool(x.Value); Slot(o); } }
/// <summary>
/// Adds a SByte to the Table at index `o` in its vtable using the value `x` and default `d`
/// </summary>
@@ -551,7 +551,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable sbyte value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddSbyte(int o, sbyte? x) { if (x.HasValue) { AddSbyte(x.Value); Slot(o); } }
/// <summary>
@@ -568,7 +568,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable byte value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddByte(int o, byte? x) { if (x.HasValue) { AddByte(x.Value); Slot(o); } }
/// <summary>
@@ -585,7 +585,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable int16 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddShort(int o, short? x) { if (x.HasValue) { AddShort(x.Value); Slot(o); } }
/// <summary>
@@ -602,7 +602,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable uint16 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddUshort(int o, ushort? x) { if (x.HasValue) { AddUshort(x.Value); Slot(o); } }
/// <summary>
@@ -619,7 +619,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable int32 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddInt(int o, int? x) { if (x.HasValue) { AddInt(x.Value); Slot(o); } }
/// <summary>
@@ -636,7 +636,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable uint32 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddUint(int o, uint? x) { if (x.HasValue) { AddUint(x.Value); Slot(o); } }
/// <summary>
@@ -653,7 +653,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable int64 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddLong(int o, long? x) { if (x.HasValue) { AddLong(x.Value); Slot(o); } }
/// <summary>
@@ -670,7 +670,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable int64 value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddUlong(int o, ulong? x) { if (x.HasValue) { AddUlong(x.Value); Slot(o); } }
/// <summary>
@@ -687,7 +687,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable single value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddFloat(int o, float? x) { if (x.HasValue) { AddFloat(x.Value); Slot(o); } }
/// <summary>
@@ -704,7 +704,7 @@ namespace Google.FlatBuffers
/// </summary>
/// <param name="o">The index into the vtable</param>
/// <param name="x">The nullable double value to put into the buffer. If it doesn't have a value
/// it will skip writing to the buffer.</param>
/// it will skip writing to the buffer.</param>
public void AddDouble(int o, double? x) { if (x.HasValue) { AddDouble(x.Value); Slot(o); } }
/// <summary>
@@ -739,7 +739,7 @@ namespace Google.FlatBuffers
}
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
/// <summary>
/// Creates a string in the buffer from a Span containing
/// a UTF8 string.

View File

@@ -1,57 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{28C00774-1E73-4A75-AD8F-844CD21A064D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FlatBuffers</RootNamespace>
<AssemblyName>FlatBuffers</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\net35</OutputPath>
<IntermediateOutputPath>obj\Debug\net35</IntermediateOutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\net35</OutputPath>
<IntermediateOutputPath>obj\Release\net35</IntermediateOutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ByteBuffer.cs" />
<Compile Include="ByteBufferUtil.cs" />
<Compile Include="FlatBufferBuilder.cs" />
<Compile Include="FlatBufferConstants.cs" />
<Compile Include="FlatBufferVerify.cs" />
<Compile Include="IFlatbufferObject.cs" />
<Compile Include="Offset.cs" />
<Compile Include="Struct.cs" />
<Compile Include="Table.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net46</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Description>A cross-platform memory efficient serialization library</Description>
<PackageVersion>23.5.26</PackageVersion>
<Authors>Google LLC</Authors>
@@ -30,12 +30,8 @@
<DefineConstants>$(DefineConstants);ENABLE_SPAN_T</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="('$(ENABLE_SPAN_T)' == 'true') And (('$(TargetFramework)' == 'netstandard2.0') Or ('$(TargetFramework)' == 'net46'))">
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>

View File

@@ -90,7 +90,7 @@ namespace Google.FlatBuffers
return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length
}
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
// Get the data of a vector whoses offset is stored at "offset" in this object as an
// Spant&lt;byte&gt;. If the vector is not present in the ByteBuffer,
// then an empty span will be returned.