Playing with nullable.

This commit is contained in:
Eugene Wang
2023-03-31 22:20:43 -04:00
parent 64e631bd2a
commit 299e74621b
3 changed files with 99 additions and 98 deletions

View File

@@ -19,9 +19,9 @@
<Version>$(PkgVersion)</Version> <Version>$(PkgVersion)</Version>
<FileVersion>$(PkgVersion)</FileVersion> <FileVersion>$(PkgVersion)</FileVersion>
<LangVersion>7.1</LangVersion> <LangVersion>11</LangVersion>
<!--don't warn missing xml docs and naming conventions--> <!--don't warn missing xml docs, naming conventions, and suppressions-->
<NoWarn>1591,IDE1006</NoWarn> <NoWarn>1591,IDE1006,IDE0079</NoWarn>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'"> <PropertyGroup Condition="'$(Configuration)'=='Release'">

View File

@@ -3,11 +3,12 @@
<PropertyGroup> <PropertyGroup>
<PackageId>NTwain</PackageId> <PackageId>NTwain</PackageId>
<Description>Library containing the TWAIN API for dotnet.</Description> <Description>Library containing the TWAIN API for dotnet.</Description>
<TargetFrameworks>net462;netcoreapp3.1;net5.0;net6.0;net7.0;netstandard2.0</TargetFrameworks> <TargetFrameworks>net462;net6.0;net7.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>11</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" /> <PackageReference Include="System.Drawing.Common" Version="6.0.0" />

View File

@@ -139,89 +139,89 @@ namespace TWAINWorkingGroup
public string ResponseJson; public string ResponseJson;
} }
/// <summary> ///// <summary>
/// A more dotnet-friendly representation of <see cref="TW_ENUMERATION"/>. ///// A more dotnet-friendly representation of <see cref="TW_ENUMERATION"/>.
/// </summary> ///// </summary>
/// <typeparam name="TValue"></typeparam> ///// <typeparam name="TValue"></typeparam>
public class Enumeration<TValue> where TValue : struct //public class Enumeration<TValue> where TValue : struct
{ //{
public int CurrentIndex; // public int CurrentIndex;
public int DefaultIndex; // public int DefaultIndex;
public TValue[] Items; // public TValue[] Items;
} //}
/// <summary> ///// <summary>
/// A more dotnet-friendly representation of <see cref="TW_RANGE"/>. ///// A more dotnet-friendly representation of <see cref="TW_RANGE"/>.
/// </summary> ///// </summary>
/// <typeparam name="TValue"></typeparam> ///// <typeparam name="TValue"></typeparam>
public partial class Range<TValue> : IEnumerable<TValue> where TValue : struct //public partial class Range<TValue> : IEnumerable<TValue> where TValue : struct
{ //{
public TValue MinValue; // public TValue MinValue;
public TValue MaxValue; // public TValue MaxValue;
public TValue StepSize; // public TValue StepSize;
public TValue DefaultValue; // public TValue DefaultValue;
public TValue CurrentValue; // public TValue CurrentValue;
IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator() // IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator()
{ // {
if (!(MinValue is IConvertible)) // if (!(MinValue is IConvertible))
throw new NotSupportedException($"The value type {typeof(TValue).Name} is not supported for range enumeration."); // throw new NotSupportedException($"The value type {typeof(TValue).Name} is not supported for range enumeration.");
return new DynamicEnumerator(MinValue, MaxValue, StepSize); // return new DynamicEnumerator(MinValue, MaxValue, StepSize);
} // }
IEnumerator IEnumerable.GetEnumerator() // IEnumerator IEnumerable.GetEnumerator()
{ // {
return ((IEnumerable<TValue>)this).GetEnumerator(); // return ((IEnumerable<TValue>)this).GetEnumerator();
} // }
// dynamic is a cheap hack to sidestep the compiler restrictions if I know TValue is numeric // // dynamic is a cheap hack to sidestep the compiler restrictions if I know TValue is numeric
class DynamicEnumerator : IEnumerator<TValue> // class DynamicEnumerator : IEnumerator<TValue>
{ // {
private readonly TValue _min; // private readonly TValue _min;
private readonly TValue _max; // private readonly TValue _max;
private readonly TValue _step; // private readonly TValue _step;
private TValue _cur; // private TValue _cur;
bool started = false; // bool started = false;
public DynamicEnumerator(TValue min, TValue max, TValue step) // public DynamicEnumerator(TValue min, TValue max, TValue step)
{ // {
_min = min; // _min = min;
_max = max; // _max = max;
_step = step; // _step = step;
_cur = min; // _cur = min;
} // }
public TValue Current => _cur; // public TValue Current => _cur;
object IEnumerator.Current => this.Current; // object IEnumerator.Current => this.Current;
public void Dispose() { } // public void Dispose() { }
public bool MoveNext() // public bool MoveNext()
{ // {
if (!started) // if (!started)
{ // {
started = true; // started = true;
return true; // return true;
} // }
var next = _cur + (dynamic)_step; // var next = _cur + (dynamic)_step;
if (next == _cur || next < _min || next > _max) return false; // if (next == _cur || next < _min || next > _max) return false;
_cur = next; // _cur = next;
return true; // return true;
} // }
public void Reset() // public void Reset()
{ // {
_cur = _min; // _cur = _min;
started = false; // started = false;
} // }
} // }
} //}
partial struct TW_FIX32 : IEquatable<TW_FIX32>, IConvertible partial struct TW_FIX32 : IEquatable<TW_FIX32>, IConvertible
{ {
@@ -262,7 +262,7 @@ namespace TWAINWorkingGroup
{ {
return Whole == other.Whole && Frac == other.Frac; return Whole == other.Whole && Frac == other.Frac;
} }
public override bool Equals(object obj) public override bool Equals(object? obj)
{ {
if (obj is TW_FIX32 other) if (obj is TW_FIX32 other)
{ {
@@ -283,82 +283,82 @@ namespace TWAINWorkingGroup
return TypeCode.Single; return TypeCode.Single;
} }
bool IConvertible.ToBoolean(IFormatProvider provider) bool IConvertible.ToBoolean(IFormatProvider? provider)
{ {
return this != 0; return this != 0;
} }
byte IConvertible.ToByte(IFormatProvider provider) byte IConvertible.ToByte(IFormatProvider? provider)
{ {
return Convert.ToByte((float)this); return Convert.ToByte((float)this);
} }
char IConvertible.ToChar(IFormatProvider provider) char IConvertible.ToChar(IFormatProvider? provider)
{ {
return Convert.ToChar((float)this); return Convert.ToChar((float)this);
} }
DateTime IConvertible.ToDateTime(IFormatProvider provider) DateTime IConvertible.ToDateTime(IFormatProvider? provider)
{ {
return Convert.ToDateTime((float)this); return Convert.ToDateTime((float)this);
} }
decimal IConvertible.ToDecimal(IFormatProvider provider) decimal IConvertible.ToDecimal(IFormatProvider? provider)
{ {
return Convert.ToDecimal((float)this); return Convert.ToDecimal((float)this);
} }
double IConvertible.ToDouble(IFormatProvider provider) double IConvertible.ToDouble(IFormatProvider? provider)
{ {
return Convert.ToDouble((float)this); return Convert.ToDouble((float)this);
} }
short IConvertible.ToInt16(IFormatProvider provider) short IConvertible.ToInt16(IFormatProvider? provider)
{ {
return Convert.ToInt16((float)this); return Convert.ToInt16((float)this);
} }
int IConvertible.ToInt32(IFormatProvider provider) int IConvertible.ToInt32(IFormatProvider? provider)
{ {
return Convert.ToInt32((float)this); return Convert.ToInt32((float)this);
} }
long IConvertible.ToInt64(IFormatProvider provider) long IConvertible.ToInt64(IFormatProvider? provider)
{ {
return Convert.ToInt64((float)this); return Convert.ToInt64((float)this);
} }
sbyte IConvertible.ToSByte(IFormatProvider provider) sbyte IConvertible.ToSByte(IFormatProvider? provider)
{ {
return Convert.ToSByte((float)this); return Convert.ToSByte((float)this);
} }
float IConvertible.ToSingle(IFormatProvider provider) float IConvertible.ToSingle(IFormatProvider? provider)
{ {
return Convert.ToSingle((float)this); return Convert.ToSingle((float)this);
} }
string IConvertible.ToString(IFormatProvider provider) string IConvertible.ToString(IFormatProvider? provider)
{ {
return this.ToString(); return this.ToString();
} }
object IConvertible.ToType(Type conversionType, IFormatProvider provider) object IConvertible.ToType(Type conversionType, IFormatProvider? provider)
{ {
return Convert.ChangeType((float)this, conversionType, CultureInfo.InvariantCulture); return Convert.ChangeType((float)this, conversionType, CultureInfo.InvariantCulture);
} }
ushort IConvertible.ToUInt16(IFormatProvider provider) ushort IConvertible.ToUInt16(IFormatProvider? provider)
{ {
return Convert.ToUInt16((float)this); return Convert.ToUInt16((float)this);
} }
uint IConvertible.ToUInt32(IFormatProvider provider) uint IConvertible.ToUInt32(IFormatProvider? provider)
{ {
return Convert.ToUInt32((float)this); return Convert.ToUInt32((float)this);
} }
ulong IConvertible.ToUInt64(IFormatProvider provider) ulong IConvertible.ToUInt64(IFormatProvider? provider)
{ {
return Convert.ToUInt64((float)this); return Convert.ToUInt64((float)this);
} }
@@ -366,10 +366,10 @@ namespace TWAINWorkingGroup
#endregion #endregion
public static implicit operator float(TW_FIX32 value) => value.ToFloat(); public static implicit operator float(TW_FIX32 value) => value.ToFloat();
public static implicit operator TW_FIX32(float value) => new TW_FIX32(value); public static implicit operator TW_FIX32(float value) => new(value);
public static implicit operator double(TW_FIX32 value) => value.ToDouble(); public static implicit operator double(TW_FIX32 value) => value.ToDouble();
public static implicit operator TW_FIX32(double value) => new TW_FIX32((float)value); public static implicit operator TW_FIX32(double value) => new((float)value);
public static bool operator ==(TW_FIX32 value1, TW_FIX32 value2) => value1.Equals(value2); public static bool operator ==(TW_FIX32 value1, TW_FIX32 value2) => value1.Equals(value2);
public static bool operator !=(TW_FIX32 value1, TW_FIX32 value2) => !value1.Equals(value2); public static bool operator !=(TW_FIX32 value1, TW_FIX32 value2) => !value1.Equals(value2);
@@ -412,7 +412,7 @@ namespace TWAINWorkingGroup
Right == other.Right && Bottom == other.Bottom; Right == other.Right && Bottom == other.Bottom;
} }
public override bool Equals(object obj) public override bool Equals(object? obj)
{ {
if (obj is TW_FRAME other) if (obj is TW_FRAME other)
{ {
@@ -453,7 +453,7 @@ namespace TWAINWorkingGroup
} }
public static implicit operator string(TW_STR32 value) => value.ToString(); public static implicit operator string(TW_STR32 value) => value.ToString();
public static explicit operator TW_STR32(string value) => new TW_STR32(value); public static explicit operator TW_STR32(string value) => new(value);
} }
@@ -472,7 +472,7 @@ namespace TWAINWorkingGroup
} }
public static implicit operator string(TW_STR64 value) => value.ToString(); public static implicit operator string(TW_STR64 value) => value.ToString();
public static explicit operator TW_STR64(string value) => new TW_STR64(value); public static explicit operator TW_STR64(string value) => new(value);
} }
partial struct TW_STR128 partial struct TW_STR128
@@ -490,7 +490,7 @@ namespace TWAINWorkingGroup
} }
public static implicit operator string(TW_STR128 value) => value.ToString(); public static implicit operator string(TW_STR128 value) => value.ToString();
public static explicit operator TW_STR128(string value) => new TW_STR128(value); public static explicit operator TW_STR128(string value) => new(value);
} }
partial struct TW_STR255 partial struct TW_STR255
@@ -508,7 +508,7 @@ namespace TWAINWorkingGroup
} }
public static implicit operator string(TW_STR255 value) => value.ToString(); public static implicit operator string(TW_STR255 value) => value.ToString();
public static explicit operator TW_STR255(string value) => new TW_STR255(value); public static explicit operator TW_STR255(string value) => new(value);
} }
partial struct TW_IDENTITY partial struct TW_IDENTITY