示例#1
0
        /// <summary>
        /// Calls the specified try function with the specified parameter and returns an option containing the result.
        /// </summary>
        /// <typeparam name="T1">The type of <paramref name="arg1"/>.</typeparam>
        /// <param name="tryFunction">The try function to call.</param>
        /// <param name="arg1">The first parameter to be passed to the try function.</param>
        /// <returns>
        /// An option containing the result parameter produced by calling <paramref name="tryFunction"/>,
        /// if the operation completed successfully; otherwise, an empty option.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="tryFunction"/> is <see langword="null"/>.
        /// </exception>
        public static Opt <TResult> Call <T1>(TryFunction <T1, TResult> tryFunction, T1 arg1)
        {
            if (tryFunction == null)
            {
                throw new ArgumentNullException(nameof(tryFunction));
            }
            bool hasValue = tryFunction(arg1, out TResult result);

            return(Opt.Create(hasValue, result));
        }
示例#2
0
        /// <summary>
        /// Synchronize write.
        /// </summary>
        /// <typeparam name="T"> Generic type. </typeparam>
        /// <param name="tryFunc"> Functions used for writing. </param>
        /// <param name="value"> An out parameter passed to <paramref name="tryFunc"/>. </param>
        /// <returns> Returns true if <paramref name="tryFunc"/> is a success. </returns>
        protected bool SyncWrite <T>(TryFunction <T> tryFunc, out T value)
        {
            ThrowUtilities.NullArgument(tryFunc, nameof(tryFunc));

            _lock.EnterWriteLock();
            var success = tryFunc(out value);

            _lock.ExitWriteLock();
            return(success);
        }
示例#3
0
        /// <summary>
        /// Calls the specified try function with the specified parameters and returns an option containing the result.
        /// </summary>
        /// <typeparam name="T1">The type of <paramref name="arg1"/>.</typeparam>
        /// <typeparam name="T2">The type of <paramref name="arg2"/>.</typeparam>
        /// <typeparam name="T3">The type of <paramref name="arg3"/>.</typeparam>
        /// <typeparam name="T4">The type of <paramref name="arg4"/>.</typeparam>
        /// <typeparam name="T5">The type of <paramref name="arg5"/>.</typeparam>
        /// <typeparam name="T6">The type of <paramref name="arg6"/>.</typeparam>
        /// <typeparam name="T7">The type of <paramref name="arg7"/>.</typeparam>
        /// <typeparam name="T8">The type of <paramref name="arg8"/>.</typeparam>
        /// <typeparam name="T9">The type of <paramref name="arg9"/>.</typeparam>
        /// <typeparam name="T10">The type of <paramref name="arg10"/>.</typeparam>
        /// <param name="tryFunction">The try function to call.</param>
        /// <param name="arg1">The first parameter to be passed to the try function.</param>
        /// <param name="arg2">The second parameter to be passed to the try function.</param>
        /// <param name="arg3">The third parameter to be passed to the try function.</param>
        /// <param name="arg4">The fourth parameter to be passed to the try function.</param>
        /// <param name="arg5">The fifth parameter to be passed to the try function.</param>
        /// <param name="arg6">The sixth parameter to be passed to the try function.</param>
        /// <param name="arg7">The seventh parameter to be passed to the try function.</param>
        /// <param name="arg8">The eighth parameter to be passed to the try function.</param>
        /// <param name="arg9">The ninth parameter to be passed to the try function.</param>
        /// <param name="arg10">The tenth parameter to be passed to the try function.</param>
        /// <returns>
        /// An option containing the result parameter produced by calling <paramref name="tryFunction"/>,
        /// if the operation completed successfully; otherwise, an empty option.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="tryFunction"/> is <see langword="null"/>.
        /// </exception>
        public static Opt <TResult> Call <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(TryFunction <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> tryFunction, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
        {
            if (tryFunction == null)
            {
                throw new ArgumentNullException(nameof(tryFunction));
            }
            bool hasValue = tryFunction(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, out TResult result);

            return(Opt.Create(hasValue, result));
        }
示例#4
0
        /// <summary>
        /// Calls the specified try function with the specified parameters and returns an option containing the result.
        /// </summary>
        /// <typeparam name="T1">The type of <paramref name="arg1"/>.</typeparam>
        /// <typeparam name="T2">The type of <paramref name="arg2"/>.</typeparam>
        /// <typeparam name="T3">The type of <paramref name="arg3"/>.</typeparam>
        /// <typeparam name="T4">The type of <paramref name="arg4"/>.</typeparam>
        /// <typeparam name="T5">The type of <paramref name="arg5"/>.</typeparam>
        /// <param name="tryFunction">The try function to call.</param>
        /// <param name="arg1">The first parameter to be passed to the try function.</param>
        /// <param name="arg2">The second parameter to be passed to the try function.</param>
        /// <param name="arg3">The third parameter to be passed to the try function.</param>
        /// <param name="arg4">The fourth parameter to be passed to the try function.</param>
        /// <param name="arg5">The fifth parameter to be passed to the try function.</param>
        /// <returns>
        /// An option containing the result parameter produced by calling <paramref name="tryFunction"/>,
        /// if the operation completed successfully; otherwise, an empty option.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="tryFunction"/> is <see langword="null"/>.
        /// </exception>
        public static Opt <TResult> Call <T1, T2, T3, T4, T5>(TryFunction <T1, T2, T3, T4, T5, TResult> tryFunction, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
        {
            if (tryFunction == null)
            {
                throw new ArgumentNullException(nameof(tryFunction));
            }
            bool hasValue = tryFunction(arg1, arg2, arg3, arg4, arg5, out TResult result);

            return(Opt.Create(hasValue, result));
        }
示例#5
0
        public static R GetOrElse <T, R>(TryFunction <T, R> @get, T input, Func <R> @else)
        {
            R result;

            if (@get(input, out result))
            {
                return(result);
            }
            else
            {
                return(@else());
            }
        }
示例#6
0
        static T TryMany <T>(TryFunction <T> run, int tries = 5, int millisecondsTimeout = 1000)
        {
            for (int t = 0; t < tries; t++)
            {
                var result = run();
                if (!EqualityComparer <T> .Default.Equals(result, default(T)))
                {
                    return(result);
                }
                Thread.Sleep(millisecondsTimeout);
            }

            return(default(T));
        }
示例#7
0
        public void ShouldSuccess()
        {
            TryFunction <int> t;
            MockedLogger      logger;
            int result;

            logger = new MockedLogger();
            t      = new TryFunction <int>(logger, 1, "TestUnit", "TestMethod", () => 1234);
            Assert.AreEqual(1234, t.OrThrow("Failure"));
            Assert.AreEqual(0, logger.Logs.Count);
            Assert.AreEqual(true, t.OrAlert(out result, "Failure"));
            Assert.AreEqual(1234, result);
            Assert.AreEqual(0, logger.Logs.Count);
        }