kOS.Safe.Encapsulation.ScalarValue.Create C# (CSharp) Method

Create() public static method

public static Create ( object value ) : ScalarValue
value object
return ScalarValue
        public static ScalarValue Create(object value)
        {
            Type compareType = typeof(ScalarValue);
            if (compareType.IsInstanceOfType(value))
            {
                return (ScalarValue)value;
            }
            if (value is float)
                value = Convert.ToDouble(value);
            if (value is double)
            {
                bool inBounds = int.MinValue < (double)value && (double)value < int.MaxValue;
                if (inBounds && !double.IsNaN((double)value))
                {
                    // Convert the double to an int, and check and see if they are still equal.
                    // if so, treat the double as if it was an int.
                    int intPart = Convert.ToInt32(value);

                    // ReSharper disable once CompareOfFloatsByEqualityOperator
                    if ((double)value == intPart)
                    {
                        return new ScalarIntValue(intPart);
                    }
                }
                return new ScalarDoubleValue((double)value);
            }

            if (value is int)
            {
                return new ScalarIntValue((int)value);
            }

            throw new KOSException(string.Format("Failed to set scalar value.  Passed type {0}, expected Double or Int", value.GetType().Name));
        }

Usage Example

Example #1
0
        /// <summary>
        /// Attempt to convert the given object into a kOS encapsulation type (something
        /// derived from kOS.Safe.Encapsulation.Structure), returning that instead.
        /// This never throws exception or complains in any way if the conversion cannot happen.
        /// Insted in that case it just silently ignores the request and returns the original object
        /// reference unchanged.  Thus it is safe to call it "just in case", even in places where it won't
        /// always be necessary, or have an effect at all.  You should use in anywhere you need to
        /// ensure that a value a user's script might see on the stack or in a script variable is properly
        /// wrapped in a kOS Structure, and not just a raw primitive like int or double.
        /// </summary>
        /// <param name="value">value to convert</param>
        /// <returns>new converted value, or original value if conversion couldn't happen or was unnecesary</returns>
        public static object FromPrimitive(object value)
        {
            if (value == null)
            {
                return(value); // If a null exists, let it pass through so it will bomb elsewhere, not here in FromPrimitive() where the exception message would be obtuse.
            }
            if (value is Structure)
            {
                return(value); // Conversion is unnecessary - it's already a Structure.
            }
            var convert = value as IConvertible;

            if (convert == null)
            {
                return(value); // Conversion isn't even theoretically possible.
            }
            TypeCode code = convert.GetTypeCode();

            switch (code)
            {
            case TypeCode.Boolean:
                return(new BooleanValue(Convert.ToBoolean(convert)));

            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Single:
                return(ScalarValue.Create(Convert.ToDouble(convert)));

            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return(ScalarValue.Create(Convert.ToInt32(convert)));

            case TypeCode.String:
                return(new StringValue(Convert.ToString(convert, CultureInfo.CurrentCulture)));

            default:
                break;
            }
            return(value); // Conversion is one this method didn't implement.
        }