Пример #1
0
 public static TTo Convert <TFrom, TTo>(this TFrom?obj, Func <TFrom, TTo> some, Func <TTo> none)
     where TFrom : class
 {
     return(obj != null
                         ? some(obj)
                         : none());
 }
Пример #2
0
 /// <inheritdoc/>
 public override TTo Convert(TFrom?value)
 {
     if (toIsValueType && !value.HasValue)
     {
         throw new ArgumentNullException("value");
     }
     return(BaseConverter.Convert(value.GetValueOrDefault()));
 }
Пример #3
0
        public TTo Map(TFrom?from, IObjectMapperResolver resolver)
        {
            if (from == null)
            {
                return(null);
            }

            return(resolver.GetMapper <TFrom, TTo>().Map(from.Value, resolver));
        }
Пример #4
0
        public TTo Map(TFrom?from, IObjectMapperResolver resolver)
        {
            if (from == null)
            {
                throw new ArgumentNullException("`from` is null but `to` does not allow null.");
            }

            return(resolver.GetMapper <TFrom, TTo>().Map(from.Value, resolver));
        }
Пример #5
0
        public static TTo?ConvertEnum <TFrom, TTo>(TFrom?from)
            where TTo : struct
            where TFrom : struct
        {
            if (from.HasValue)
            {
                return(ConvertEnum <TFrom, TTo>(from.Value));
            }

            return(null);
        }
Пример #6
0
 public static ValueOption <TFrom> op_Implicit <TFrom>(TFrom?value) where TFrom : struct
 {
     if ((!value.HasValue))
     {
         return(ValueOption <TFrom> .None());
     }
     else
     {
         return(ValueOption <TFrom> .Some(value.Value));
     }
 }
Пример #7
0
        internal static TTo?MapNullableEnum <TFrom, TTo>(TFrom?otherEnum)
            where TTo : struct
            where TFrom : struct
        {
            if (otherEnum == null)
            {
                return(null);
            }

            return(UtilitiesInternal.MapEnum <TFrom, TTo>(otherEnum.Value));
        }
Пример #8
0
 /// <inheritdoc/>
 public override TTo?Convert(TFrom?value)
 {
     if (value.HasValue)
     {
         return(BaseConverter.Convert(value.Value));
     }
     else
     {
         return(null);
     }
 }
Пример #9
0
 public static option <TFrom> op_Implicit <TFrom>(TFrom?value) where TFrom : struct
 {
     if (!value.HasValue)
     {
         return(option <TFrom> .None._N_constant_object);
     }
     else
     {
         return(new option <TFrom> .Some(value.Value));
     }
 }
Пример #10
0
 /// <summary>
 /// Method that will be called by <see cref="Convert(object, Type, object, CultureInfo)"/>.
 /// </summary>
 /// <param name="value">Value to be converted from <see cref="TFrom"/> to <see cref="TTo"/>.</param>
 /// <returns>An object of type <see cref="TTo"/>.</returns>
 public abstract TTo?ConvertFrom(TFrom?value);
 protected static IResult <TReturned, NonEmptyString> TryCreateInt(TFrom?value, NonEmptyString field, Func <TFrom, IResult <TReturned, NonEmptyString> > createFunc)
 {
     return(value == null?GetFailResult((NonEmptyString)"{0} can't be null", field) : createFunc(value.Value));
 }
Пример #12
0
        private static ReferenceCountedDisposable <TTo>?TryAddReferenceImpl <TFrom, TTo>(TFrom?target, StrongBox <int> referenceCount,
                                                                                         out CreateResult result)
            where TFrom : class, IDisposable
            where TTo : class, IDisposable
        {
            lock (referenceCount)
            {
                if (referenceCount.Value == 0)
                {
                    // The target is already disposed, and cannot be reused
                    result = CreateResult.Disposed;
                    return(null);
                }

                if (target == null)
                {
                    // The current reference has been disposed, so even though it isn't disposed yet we don't have a
                    // reference to the target
                    result = CreateResult.Disposed;
                    return(null);
                }

                if (!(target is TTo castedTarget))
                {
                    // The target cannot be casted to type TTo
                    result = CreateResult.NotCastable;
                    return(null);
                }

                checked
                {
                    referenceCount.Value++;
                }

                // Must return a new instance, in order for the Dispose operation on each individual instance to
                // be idempotent.
                result = CreateResult.Success;
                return(new ReferenceCountedDisposable <TTo>(castedTarget, referenceCount));
            }
        }