internal static TOut?SafeGet <TIn, TOut>(this TIn?input, Func <TIn, TOut?> converter)
            where TIn : struct
            where TOut : struct

        {
            return(input.HasValue ? converter(input.Value) : default(TOut?));
        }
Пример #2
0
        public static TOut SelectOrDefault <TIn, TOut>(this TIn?value, Func <TIn, TOut> resultIfNotNull, Func <TOut> resultIfNull = null)
            where TIn : struct
        {
            if (value.HasValue)
            {
                return(resultIfNotNull(value.Value));
            }

            return(resultIfNull != null?resultIfNull() : default(TOut));
        }
Пример #3
0
        public static void TryConsume <TIn>(this TIn?instance, [NotNull] Action <TIn> action) where TIn : struct
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (instance.HasValue)
            {
                action(instance.Value);
            }
        }
Пример #4
0
 public static TOut?Adapt <TIn, TOut>(this IAdapter <TIn, TOut> adapter, TIn?input)
     where TIn : struct
     where TOut : struct
 {
     if (adapter == null)
     {
         throw new ArgumentNullException(nameof(adapter));
     }
     return(input.HasValue
         ? adapter.Adapt(input.Value)
         : new TOut?());
 }
Пример #5
0
        public static TOut?TryTransform <TIn, TOut>(this TIn?instance, [NotNull] Func <TIn, TOut> transform, TOut?elseValue = null)
            where TIn : struct
            where TOut : struct
        {
            if (transform == null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            if (instance == null)
            {
                return(elseValue);
            }

            return(transform(instance.Value));
        }
Пример #6
0
 private static byte[] Pack <TIn, TOut>(TIn?inVal, TOut?outVal) where TIn : struct where TOut : struct
 {
     using (var ms = new MemoryStream())
         using (var wtr = new BinaryWriter(ms))
         {
             wtr.Write(inVal.HasValue ? Marshal.SizeOf(typeof(TIn)) : 0);
             wtr.Write(outVal.HasValue ? Marshal.SizeOf(typeof(TOut)) : 0);
             if (inVal.HasValue)
             {
                 wtr.Write(inVal.Value);
             }
             if (outVal.HasValue)
             {
                 wtr.Write(outVal.Value);
             }
             return(ms.ToArray());
         }
 }
 internal static string ToParameterValue <TIn>(this TIn?input, Func <TIn, string> convert, string defaultValue = null)
     where TIn : struct
 {
     return(input.HasValue ? convert(input.Value) : defaultValue);
 }
Пример #8
0
 /// <summary>
 /// Asynchronously maps input nullable value to an output <see cref="Maybe{T}" />.
 /// </summary>
 /// <typeparam name="TIn">Type of input nullable.</typeparam>
 /// <typeparam name="TOut">Type of output <see cref="Maybe{T}" />.</typeparam>
 /// <param name="self">Input nullable value to map.</param>
 /// <param name="f">Asynchronous mapping.</param>
 /// <returns>A task of the output <see cref="Maybe{T}" />.</returns>
 public static async Task <Maybe <TOut> > SelectAsync <TIn, TOut>(this TIn?self, Func <TIn, Task <TOut> > f)
     where TIn : struct
     where TOut : class =>
 self.HasValue ? await f(self.Value).ConfigureAwait(false) : Maybe <TOut> .None;
 public static TOut?Select <TIn, TOut>(this TIn?nullable, Func <TIn, TOut> selector)
     where TIn : struct
     where TOut : struct
 {
     return(nullable.HasValue ? selector(nullable.Value) : (TOut?)null);
 }
Пример #10
0
        /// <summary>
        /// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
        /// </summary>
        /// <typeparam name="TIn">The type of the <paramref name="inVal"/>.</typeparam>
        /// <typeparam name="TOut">The type of the return value.</typeparam>
        /// <param name="hDevice">
        /// A handle to the device on which the operation is to be performed. The device is typically a volume, directory, file, or stream.
        /// To retrieve a device handle, use the CreateFile function. For more information, see Remarks.
        /// </param>
        /// <param name="ioControlCode">
        /// The control code for the operation. This value identifies the specific operation to be performed and the type of device on which
        /// to perform it.
        /// </param>
        /// <param name="inVal">
        /// The input value required to perform the operation. The type of this data depends on the value of the <paramref
        /// name="ioControlCode"/> parameter.
        /// </param>
        /// <returns>An asynchronous result containing the populated data supplied by <typeparamref name="TOut"/>.</returns>
        /// <remarks>
        /// <para>
        /// To retrieve a handle to the device, you must call the CreateFile function with either the name of a device or the name of the
        /// driver associated with a device. To specify a device name, use the following format:
        /// </para>
        /// <para>\\.\DeviceName</para>
        /// <para>
        /// DeviceIoControl can accept a handle to a specific device. For example, to open a handle to the logical drive
        /// A: with CreateFile, specify \\.\a:. Alternatively, you can use the names \\.\PhysicalDrive0, \\.\PhysicalDrive1, and so on, to
        /// open handles to the physical drives on a system.
        /// </para>
        /// <para>
        /// You should specify the FILE_SHARE_READ and FILE_SHARE_WRITE access flags when calling CreateFile to open a handle to a device
        /// driver. However, when you open a communications resource, such as a serial port, you must specify exclusive access. Use the other
        /// CreateFile parameters as follows when opening a device handle:
        /// </para>
        /// <list type="bullet">
        /// <item>
        /// <description>The fdwCreate parameter must specify OPEN_EXISTING.</description>
        /// </item>
        /// <item>
        /// <description>The hTemplateFile parameter must be NULL.</description>
        /// </item>
        /// <item>
        /// <description>
        /// The fdwAttrsAndFlags parameter can specify FILE_FLAG_OVERLAPPED to indicate that the returned handle can be used in overlapped
        /// (asynchronous) I/O operations.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        public static Task <TOut?> DeviceIoControlAsync <TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn?inVal) where TIn : struct where TOut : struct
        {
            TOut?outValue = default(TOut);
            var  buf      = Pack(inVal, outValue);

            return(Task.Factory.FromAsync(BeginDeviceIoControl <TIn, TOut>, EndDeviceIoControl <TIn, TOut>, hDevice, ioControlCode, buf, null));
        }
Пример #11
0
 public static TOut?Bind <TIn, TOut>(this TIn?m, Func <TIn, TOut> map)
     where TIn  : struct
     where TOut : struct
 {
     return(m.HasValue ? map(m.Value) : default(TOut?));
 }
Пример #12
0
 public static Option <TOut> ToOption <TIn, TOut>(this TIn?value, Func <TIn, TOut> map) where TIn : struct
 {
     return(value.HasValue
         ? Option.Some(map(value.Value))
         : Option.None());
 }
 [DebuggerStepThrough]         //Can get rid of this when upgraded to c# 6.0. It has Null Reference opertaor as part of the language
 internal static TReturn IfNotNull <TIn, TReturn>(this TIn?value, Func <TIn?, TReturn> then, TReturn @else = default(TReturn))
     where TIn : struct
 => value.HasValue ? then(value) : @else;
Пример #14
0
        public static IAsyncResult BeginDeviceIoControl <TIn, TOut>(SafeFileHandle hDevice, uint dwIoControlCode, TIn?inVal, TOut?outVal, AsyncCallback userCallback) where TIn : struct where TOut : struct
        {
            var buffer = Pack(inVal, outVal);

            return(BeginDeviceIoControl <TIn, TOut>(hDevice, dwIoControlCode, buffer, userCallback, null));
        }
Пример #15
0
        public static IResult <Maybe <TResult>, NonEmptyString> ToMaybe <TResult, TIn>(this TIn?value, NonEmptyString field, Func <TIn?, NonEmptyString, IResult <TResult, NonEmptyString> > creatorFunc)
            where TResult : class
            where TIn : struct
        {
            if (value == null)
            {
                return(((Maybe <TResult>)null).GetOkMessage());
            }

            var result = creatorFunc(value, field);

            return(result.IsFailure ? result.Error.GetFailResult <Maybe <TResult> >() : ((Maybe <TResult>)result.Value).GetOkMessage());
        }
Пример #16
0
        /// <summary>
        /// Sends a control code directly to a specified device driver, causing the corresponding device to perform the corresponding operation.
        /// </summary>
        /// <typeparam name="TIn">The type of the <paramref name="inVal"/>.</typeparam>
        /// <typeparam name="TOut">The type of the <paramref name="outVal"/>.</typeparam>
        /// <param name="hDevice">A handle to the device on which the operation is to be performed. The device is typically a volume, directory, file, or stream. To retrieve a device
        /// handle, use the CreateFile function. For more information, see Remarks.</param>
        /// <param name="ioControlCode">The control code for the operation. This value identifies the specific operation to be performed and the type of device on which to perform it.</param>
        /// <param name="inVal">The input value required to perform the operation. The type of this data depends on the value of the <paramref name="ioControlCode"/> parameter.</param>
        /// <param name="outVal">The output value that is to receive the data returned by the operation. The type of this data depends on the value of the dwIoControlCode parameter.</param>
        /// <returns>An asynchronous result containing the populated data supplied by <paramref name="outVal"/>.</returns>
        /// <remarks>
        /// <para>
        /// To retrieve a handle to the device, you must call the CreateFile function with either the name of a device or the name of the driver associated with
        /// a device. To specify a device name, use the following format:
        /// </para>
        /// <para>\\.\DeviceName</para>
        /// <para>
        /// DeviceIoControl can accept a handle to a specific device. For example, to open a handle to the logical drive
        /// A: with CreateFile, specify \\.\a:. Alternatively, you can use the names \\.\PhysicalDrive0, \\.\PhysicalDrive1, and so on, to open handles to the
        /// physical drives on a system.
        /// </para>
        /// <para>
        /// You should specify the FILE_SHARE_READ and FILE_SHARE_WRITE access flags when calling CreateFile to open a handle to a device driver. However, when
        /// you open a communications resource, such as a serial port, you must specify exclusive access. Use the other CreateFile parameters as follows when
        /// opening a device handle:
        /// </para>
        /// <list type="bullet">
        ///   <item>
        ///     <description>The fdwCreate parameter must specify OPEN_EXISTING.</description>
        ///   </item>
        ///   <item>
        ///     <description>The hTemplateFile parameter must be NULL.</description>
        ///   </item>
        ///   <item>
        ///     <description>The fdwAttrsAndFlags parameter can specify FILE_FLAG_OVERLAPPED to indicate that the returned handle can be used in overlapped (asynchronous) I/O operations.</description>
        ///   </item>
        /// </list>
        /// </remarks>
        public static Task <TOut?> DeviceIoControlAsync <TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn?inVal, TOut?outVal) where TIn : struct where TOut : struct
        {
            var buf = Pack(inVal, outVal);

            return(new TaskFactory().FromAsync(BeginDeviceIoControl <TIn, TOut>, EndDeviceIoControl <TIn, TOut>, hDevice, ioControlCode, buf, null));
        }
Пример #17
0
        private static unsafe Task <TOut?> ExplicitDeviceIoControlAsync <TIn, TOut>(HFILE hDevice, uint ioControlCode, TIn?inVal, TOut?outVal) where TIn : struct where TOut : struct
        {
#pragma warning disable CS0618 // Type or member is obsolete
            ThreadPool.BindHandle((IntPtr)hDevice);
#pragma warning restore CS0618 // Type or member is obsolete
            var tcs              = new TaskCompletionSource <TOut?>();
            var buffer           = Pack(inVal, outVal);
            var nativeOverlapped = new Overlapped().Pack((code, bytes, overlap) =>
            {
                try
                {
                    switch (code)
                    {
                    case Win32Error.ERROR_SUCCESS:
                        outVal = Unpack <TIn, TOut>(buffer).Item2;
                        tcs.TrySetResult(outVal);
                        break;

                    case Win32Error.ERROR_OPERATION_ABORTED:
                        tcs.TrySetCanceled();
                        break;

                    default:
                        tcs.TrySetException(new Win32Exception((int)code));
                        break;
                    }
                }
                finally
                {
                    Overlapped.Unpack(overlap);
                    Overlapped.Free(overlap);
                }
            }, buffer);

            var unpack = true;
            try
            {
                var inSz = Marshal.SizeOf(typeof(TIn));
                fixed(byte *pIn = buffer, pOut = &buffer[inSz])
                {
                    uint bRet;
                    var  ret = DeviceIoControl(hDevice, ioControlCode, pIn, (uint)inSz, pOut, (uint)(buffer.Length - inSz), out bRet,
                                               nativeOverlapped);

                    if (ret)
                    {
                        outVal = Unpack <TIn, TOut>(buffer).Item2;
                        tcs.SetResult(outVal);
                        return(tcs.Task);
                    }
                }

                var lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != Win32Error.ERROR_IO_PENDING && lastWin32Error != Win32Error.ERROR_SUCCESS)
                {
                    throw new Win32Exception(lastWin32Error);
                }
                unpack = false;
                return(tcs.Task);
            }
            finally
            {
                if (unpack)
                {
                    Overlapped.Unpack(nativeOverlapped);
                    Overlapped.Free(nativeOverlapped);
                }
            }
        }
Пример #18
0
 public static TOut IfNotNull <TIn, TOut>(this TIn?value, Func <TIn, TOut> innerProperty, TOut otherwise)
     where TIn : struct
 {
     return(value.HasValue ? innerProperty(value.Value) : otherwise);
 }
Пример #19
0
 /// <inheritdoc />
 public abstract void Execute(object?executor, TIn?data);
Пример #20
0
 public static TOut?IfNotNull <TIn, TOut>(this TIn?value, Func <TIn, TOut?> innerProperty)
     where TIn : struct
     where TOut : struct
 {
     return(value.HasValue ? innerProperty(value.Value) : null);
 }