示例#1
0
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
         options.HasFlag(ETypeCreationOptions.NonZero))
     {
         if (template == null)
         {
             return(Unsigned.One);
         }
         else
         {
             var unsigned = (Unsigned)template;
             return(Unsigned.FromUInt(1, unsigned.Size));
         }
     }
     else
     {
         if (template == null)
         {
             return(Unsigned.Zero);
         }
         else
         {
             var unsigned = (Unsigned)template;
             return(Unsigned.FromUInt(0, unsigned.Size));
         }
     }
 }
示例#2
0
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
         options.HasFlag(ETypeCreationOptions.NonZero))
     {
         if (template == null)
         {
             return Signed.One;
         }
         else
         {
             var signed = (Signed)template;
             return Signed.FromInt(1, signed.Size);
         }
     }
     else
     {
         if (template == null)
         {
             return Signed.Zero;
         }
         else
         {
             var signed = (Signed)template;
             return Signed.FromInt(0, signed.Size);
         }
     }
 }
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
         options.HasFlag(ETypeCreationOptions.NonZero))
         return (ushort)1;
     else
         return (ushort)0;
 }
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral))
     {
         return(1.0);
     }
     else
     {
         return(0.0);
     }
 }
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
         options.HasFlag(ETypeCreationOptions.NonZero))
     {
         return((ushort)1);
     }
     else
     {
         return((ushort)0);
     }
 }
示例#6
0
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral))
     {
         if (template == null)
         {
             return UFix.One;
         }
         else
         {
             var ufix = (UFix)template;
             return UFix.FromDouble(1.0, ufix.Format.IntWidth, ufix.Format.FracWidth);
         }
     }
     else if (options.HasFlag(ETypeCreationOptions.NonZero))
     {
         if (template == null)
         {
             return UFix.One;
         }
         else
         {
             var ufix = (UFix)template;
             return UFix.FromUnsigned(Unsigned.FromUInt(1, ufix.Format.TotalWidth), ufix.Format.FracWidth);
         }
     }
     else
     {
         if (template == null)
         {
             return UFix.Zero;
         }
         else
         {
             var ufix = (UFix)template;
             return UFix.FromDouble(0.0, ufix.Format.IntWidth, ufix.Format.FracWidth);
         }
     }
 }
示例#7
0
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral))
     {
         if (template == null)
         {
             return(SFix.One);
         }
         else
         {
             var sfix = (SFix)template;
             return(SFix.FromDouble(1.0, sfix.Format.IntWidth, sfix.Format.FracWidth));
         }
     }
     else if (options.HasFlag(ETypeCreationOptions.NonZero))
     {
         if (template == null)
         {
             return(SFix.One);
         }
         else
         {
             var sfix = (SFix)template;
             return(SFix.FromSigned(Signed.FromInt(1, sfix.Format.TotalWidth), sfix.Format.FracWidth));
         }
     }
     else
     {
         if (template == null)
         {
             return(SFix.Zero);
         }
         else
         {
             var sfix = (SFix)template;
             return(SFix.FromDouble(0.0, sfix.Format.IntWidth, sfix.Format.FracWidth));
         }
     }
 }
示例#8
0
        /// <summary>
        /// Creates a sample instance of the described type.
        /// </summary>
        /// <param name="options">creation options</param>
        public object GetSampleInstance(ETypeCreationOptions options = ETypeCreationOptions.AnyObject)
        {
            Contract.Requires(
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral)) &&
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)) &&
                !(options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)));

            if (IsArtificial)
            {
                throw new InvalidOperationException("Cannot construct a sample instance from an artificial type");
            }

            if (options.HasFlag(ETypeCreationOptions.AdditiveNeutral) ||
                options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
                options.HasFlag(ETypeCreationOptions.NonZero))
            {
                var atf = CILType.GetCustomOrInjectedAttribute <IAlgebraicTypeFactory>();
                return(atf.CreateInstance(options, _sample));
            }

            if (_sample != null)
            {
                return(_sample);
            }

            if (options.HasFlag(ETypeCreationOptions.ReturnNullIfUnavailable))
            {
                return(null);
            }

            if (!options.HasFlag(ETypeCreationOptions.ForceCreation) && (IsConstrained || !IsStatic))
            {
                throw new InvalidOperationException("Cannot construct sample instance from constrained incomplete or non-static type");
            }

            object inst = Activator.CreateInstance(CILType);

            return(inst);
        }
示例#9
0
        /// <summary>
        /// Creates a sample instance of the described type.
        /// </summary>
        /// <param name="options">creation options</param>
        public object GetSampleInstance(ETypeCreationOptions options = ETypeCreationOptions.AnyObject)
        {
            Contract.Requires(
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral)) &&
                !(options.HasFlag(ETypeCreationOptions.AdditiveNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)) &&
                !(options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) && options.HasFlag(ETypeCreationOptions.NonZero)));

            if (IsArtificial)
                throw new InvalidOperationException("Cannot construct a sample instance from an artificial type");

            if (options.HasFlag(ETypeCreationOptions.AdditiveNeutral) ||
                options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral) ||
                options.HasFlag(ETypeCreationOptions.NonZero))
            {
                var atf = CILType.GetCustomOrInjectedAttribute<IAlgebraicTypeFactory>();
                return atf.CreateInstance(options, _sample);
            }

            if (_sample != null)
                return _sample;

            if (options.HasFlag(ETypeCreationOptions.ReturnNullIfUnavailable))
                return null;

            if (!options.HasFlag(ETypeCreationOptions.ForceCreation) && (IsConstrained || !IsStatic))
                throw new InvalidOperationException("Cannot construct sample instance from constrained incomplete or non-static type");

            object inst = Activator.CreateInstance(CILType);
            return inst;
        }
示例#10
0
 /// <summary>
 /// Creates a value.
 /// </summary>
 /// <param name="options">creation options</param>
 /// <param name="template">exemplary value of another instance</param>
 /// <returns>a value according to the creation options</returns>
 public abstract object CreateInstance(ETypeCreationOptions options, object template);
 public override object CreateInstance(ETypeCreationOptions options, object template)
 {
     if (options.HasFlag(ETypeCreationOptions.MultiplicativeNeutral))
         return 1.0;
     else
         return 0.0;
 }
示例#12
0
 /// <summary>
 /// Creates a value.
 /// </summary>
 /// <param name="options">creation options</param>
 /// <param name="template">exemplary value of another instance</param>
 /// <returns>a value according to the creation options</returns>
 public abstract object CreateInstance(ETypeCreationOptions options, object template);