Beispiel #1
0
        public static Double TryObjectToDouble(object obj, out ConversionStrictness strictness)
        {
            string s;

            if (obj is double)
            {
                strictness = ConversionStrictness.ImplExactMatch; return((double)obj);
            }
            if (obj is int)
            {
                strictness = ConversionStrictness.ImplExactMatch; return((double)(int)obj);
            }
            if ((s = PhpVariable.AsString(obj)) != null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(Convert.StringToDouble(s));
            }
            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplDomainChange; return((bool)obj ? 1.0 : 0.0);
            }
            if (obj is long)
            {
                strictness = ConversionStrictness.ImplExactMatch; return((double)(long)obj);
            }

            strictness = ConversionStrictness.Failed;
            return(0.0);
        }
Beispiel #2
0
        public static T TryObjectToDelegate <T>(object obj, out ConversionStrictness strictness)
            where T : class
        {
            T      result   = null;
            object bare_obj = PhpVariable.Unwrap(obj);

            if (bare_obj == null || (result = bare_obj as T) != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            // try to convert the object to PhpCallback
            PhpCallback callback = Convert.ObjectToCallback(obj, true);

            if (callback != null && callback.Bind(true))
            {
                // generate a conversion stub
                result = EventClass <T> .GetStub(
                    callback.TargetInstance,
                    callback.TargetRoutine,
                    callback.IsBoundToCaller?callback.RoutineName : null);

                if (result != null)
                {
                    strictness = ConversionStrictness.ImplExactMatch;
                    return(result);
                }
            }

            strictness = ConversionStrictness.Failed;
            return(default(T));
        }
Beispiel #3
0
        public static DateTime TryObjectToDateTime(object obj, out ConversionStrictness strictness)
        {
            // try wrapped DateTime:
            var exactMatch = obj as Reflection.ClrValue <DateTime>;

            if (exactMatch != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(exactMatch.realValue);
            }

            // try obj -> String -> DateTime
            string str = TryObjectToString(obj, out strictness);

            if (strictness != ConversionStrictness.Failed)
            {
                DateTime result;
#if !SILVERLIGHT
                if (DateTime.TryParse(str, out result))
#else
                // TODO: Any way to optimize this?
                result = default(DateTime);
                bool success = true;
                try { result = DateTime.Parse(str); } catch { success = false; }
                if (success)
#endif
                { strictness = ConversionStrictness.ImplDomainChange; return(result); }
            }

            strictness = ConversionStrictness.Failed;
            return(new DateTime());
        }
Beispiel #4
0
        public static Int16 TryObjectToInt16(object obj, out ConversionStrictness strictness)
        {
            int result = TryObjectToInt32(obj, out strictness);

            if (result < Int16.MinValue || result > Int16.MaxValue)
            {
                strictness = ConversionStrictness.Failed;
            }
            return(unchecked ((Int16)result));
        }
Beispiel #5
0
        public static UInt32 TryObjectToUInt32(object obj, out ConversionStrictness strictness)
        {
            long result = TryObjectToInt64(obj, out strictness);

            if (result < UInt32.MinValue || result > UInt32.MaxValue)
            {
                strictness = ConversionStrictness.Failed;
            }
            return(unchecked ((UInt32)result));
        }
Beispiel #6
0
        public static SByte TryObjectToInt8(object obj, out ConversionStrictness strictness)
        {
            int result = TryObjectToInt32(obj, out strictness);

            if (result < SByte.MinValue || result > SByte.MaxValue)
            {
                strictness = ConversionStrictness.Failed;
            }
            return(unchecked ((SByte)result));
        }
Beispiel #7
0
        public static Single TryObjectToSingle(object obj, out ConversionStrictness strictness)
        {
            double result = TryObjectToDouble(obj, out strictness);

            strictness = (ConversionStrictness)Math.Min((byte)ConversionStrictness.ImplPercisionLost, (byte)strictness);
            if (result < Single.MinValue && result > Single.MaxValue)
            {
                strictness = ConversionStrictness.Failed;
            }
            return(unchecked ((Single)result));
        }
Beispiel #8
0
        public static Char TryObjectToChar(object obj, out ConversionStrictness strictness)
        {
            string result = TryObjectToString(obj, out strictness);

            if (result != null && result.Length == 1)
            {
                strictness = ConversionStrictness.ImplExactMatch; return(result[0]);
            }

            strictness = ConversionStrictness.Failed;
            return('\0');
        }
Beispiel #9
0
 public static T TryObjectToStruct <T>(object obj, out ConversionStrictness strictness)
     where T : struct
 {
     obj = PhpVariable.Unwrap(obj);
     if (obj is T)
     {
         strictness = ConversionStrictness.ImplExactMatch;
         return((T)obj);
     }
     else
     {
         strictness = ConversionStrictness.Failed;
         return(default(T));
     }
 }
Beispiel #10
0
        public static String TryObjectToString(object obj, out ConversionStrictness strictness)
        {
            string          s;
            PhpReference    pr;
            IPhpConvertible conv;

            if ((s = PhpVariable.AsString(obj)) != null)
            {
                strictness = ConversionStrictness.ImplExactMatch; return(s);
            }
            if ((pr = obj as PhpReference) != null)
            {
                return(TryObjectToString(pr.Value, out strictness));
            }
            if (obj == null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(null);
            }
            if (obj is int)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(obj.ToString());
            }
            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(((bool)obj) ? "1" : String.Empty);
            }
            if (obj is double)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(Convert.DoubleToString((double)obj));
            }
            if (obj is long)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(obj.ToString());
            }

            // explicit conversion
            if ((conv = obj as IPhpConvertible) != null)
            {
                bool   success;
                string ret = conv.ToString(false, out success);
                strictness = success?ConversionStrictness.Failed:ConversionStrictness.Explicit;
                return(ret);
            }

            strictness = ConversionStrictness.Failed;
            return(String.Empty);
        }
Beispiel #11
0
        public static Boolean TryObjectToBoolean(object obj, out ConversionStrictness strictness)
        {
            string    s;
            PhpBytes  b;
            PhpString ps;

            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplExactMatch; return((bool)obj);
            }
            if (obj is int)
            {
                strictness = ConversionStrictness.ImplDomainChange; return((int)obj != 0);
            }
            if (obj is double)
            {
                strictness = ConversionStrictness.ImplDomainChange; return((double)obj != 0.0);
            }
            if (obj is long)
            {
                strictness = ConversionStrictness.ImplDomainChange; return((long)obj != 0);
            }

            // we have to check PHP string types separately from the rest of IPhpConvertibles here
            // as only these strings are "naturally" convertible to boolean:
            if ((s = obj as string) != null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(Convert.StringToBoolean(s));
            }
            if ((b = obj as PhpBytes) != null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(b.ToBoolean());
            }
            if ((ps = obj as PhpString) != null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(ps.ToBoolean());
            }

            // explicit conversion
            // if ((conv = obj as IPhpConvertible) != null) { strictness = ConversionStrictness.Explicit; return conv.ToBoolean(); }

            strictness = ConversionStrictness.Failed;
            return(false);
        }
Beispiel #12
0
        public static T TryObjectToClass <T>(object obj, out ConversionStrictness strictness)
            where T : class
        {
            if (obj == null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(null);
            }

            T result = null;

            if ((result = PhpVariable.Unwrap(obj) as T) != null && (!(result is IPhpVariable) || result is PhpObject || result is PhpArray))
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            strictness = ConversionStrictness.Failed;
            return(default(T));
        }
Beispiel #13
0
        public static T[] TryObjectToArray <T>(object obj, out ConversionStrictness strictness)
        {
            T[] result = PhpVariable.Unwrap(obj) as T[];
            if (result != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            // try to convert PhpArray to the desired array
            PhpArray array = obj as PhpArray;

            if (array != null && array.StringCount == 0)
            {
                result = new T[array.MaxIntegerKey + 1];

                strictness = ConversionStrictness.ImplExactMatch;
                for (int i = 0; i < result.Length; i++)
                {
                    object item;
                    if (array.TryGetValue(i, out item))
                    {
                        // try to convert the item
                        ConversionStrictness tmp;
                        result[i] = TryObjectToType <T>(item, out tmp);
                        if (tmp > strictness)
                        {
                            strictness = tmp;
                        }
                        if (strictness == ConversionStrictness.Failed)
                        {
                            return(default(T[]));
                        }
                    }
                }

                return(result);
            }

            strictness = ConversionStrictness.Failed;
            return(default(T[]));
        }
Beispiel #14
0
        public static Decimal TryObjectToDecimal(object obj, out ConversionStrictness strictness)
        {
            int    ival;
            long   lval;
            double dval;

            // ignores the higher precision of decimal:
            decimal ret = 0;

            Convert.NumberInfo ni = Convert.ObjectToNumber(obj, out ival, out lval, out dval);
            switch (ni & Convert.NumberInfo.TypeMask)
            {
            case Convert.NumberInfo.Integer: { strictness = ConversionStrictness.ImplExactMatch; ret = ival; break; }

            case Convert.NumberInfo.LongInteger: { strictness = ConversionStrictness.ImplExactMatch; ret = lval; break; }

            case Convert.NumberInfo.Double: { strictness = ConversionStrictness.ImplPercisionLost; ret = unchecked ((decimal)dval); break; }

            case Convert.NumberInfo.Unconvertible: { strictness = ConversionStrictness.Failed; ret = 0; break; }

            default: Debug.Fail(); throw null;
            }
            if (obj is string)
            {
                strictness = ConversionStrictness.ImplDomainChange;
            }
            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplDomainChange;
            }
            if (obj is IPhpConvertible)
            {
                strictness = ConversionStrictness.Explicit;
            }
            if (obj == null)
            {
                strictness = ConversionStrictness.ImplDomainChange;
            }
            return(ret);
        }
Beispiel #15
0
 public static T TryObjectToType <T>(object obj, out ConversionStrictness strictness)
 {
     return(((TryObjectToTypeDelegate <T>)GetConversionRoutine(typeof(T)))(obj, out strictness));
 }
Beispiel #16
0
        public static UInt64 TryObjectToUInt64(object obj, out ConversionStrictness strictness)
        {
            string s;

            if (obj is int)
            {
                int ival = (int)obj;
                strictness = ival >= 0 ?
                             ConversionStrictness.ImplExactMatch : ConversionStrictness.Failed;
                return(unchecked ((UInt64)ival));
            }

            if (obj is long)
            {
                long lval = (long)obj;
                strictness = lval >= 0 ?
                             ConversionStrictness.ImplExactMatch : ConversionStrictness.Failed;
                return(unchecked ((UInt64)lval));
            }

            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplDomainChange;
                return((ulong)((bool)obj ? 1 : 0));
            }

            if (obj is double)
            {
                double dval = (double)obj;
                strictness = (dval >= UInt64.MinValue && dval <= UInt64.MaxValue) ?
                             ConversionStrictness.ImplPercisionLost : ConversionStrictness.Failed;
                return(unchecked ((UInt64)dval));
            }

            if ((s = PhpVariable.AsString(obj)) != null)
            {
                int    ival;
                double dval;
                long   lval;

                // successfull iff the number encoded in the string fits Int32 or Int64:
                Convert.NumberInfo info = Convert.StringToNumber(s, out ival, out lval, out dval);
                if ((info & Convert.NumberInfo.Integer) != 0)
                {
                    strictness = ConversionStrictness.ImplDomainChange; return(unchecked ((UInt64)ival));
                }
                if ((info & Convert.NumberInfo.LongInteger) != 0)
                {
                    strictness = ConversionStrictness.ImplDomainChange; return(unchecked ((UInt64)lval));
                }

                strictness = (dval >= UInt64.MinValue && dval <= UInt64.MaxValue) ?
                             ConversionStrictness.ImplPercisionLost : ConversionStrictness.Failed;
                return(unchecked ((UInt64)dval));
            }

            // explicit conversion

            /*IPhpConvertible conv;
             * if ((conv = obj as IPhpConvertible) != null)
             * {
             *      int ival;
             *      double dval;
             *      long lval;
             *
             *      Convert.NumberInfo info = conv.ToNumber(out ival, out lval, out dval);
             *      if ((info & Convert.NumberInfo.Integer) != 0)
             *      { strictness = ConversionStrictness.Explicit; return unchecked((UInt64)ival); }
             *      if ((info & Convert.NumberInfo.LongInteger) != 0)
             *      { strictness = ConversionStrictness.Explicit; return unchecked((UInt64)lval); }
             *
             *      strictness = (dval >= UInt64.MinValue && dval <= UInt64.MaxValue) ?
             *              ConversionStrictness.Explicit : ConversionStrictness.Failed;
             *      return unchecked((UInt64)dval);
             * }*/

            strictness = ConversionStrictness.Failed;
            return(0);
        }
Beispiel #17
0
 public static DBNull TryObjectToDBNull(object obj, out ConversionStrictness strictness)
 {
     strictness = ConversionStrictness.ImplDomainChange;
     return(DBNull.Value);
 }
Beispiel #18
0
        public static Int32 TryObjectToInt32(object obj, out ConversionStrictness strictness)
        {
            string s;

            if (obj is int)
            {
                strictness = ConversionStrictness.ImplExactMatch; return((int)obj);
            }
            if (obj is bool)
            {
                strictness = ConversionStrictness.ImplDomainChange; return((bool)obj ? 1 : 0);
            }

            if (obj is long)
            {
                long lval = (long)obj;
                if (lval < Int32.MinValue || lval > Int32.MaxValue)
                {
                    strictness = ConversionStrictness.Failed;
                }
                else
                {
                    strictness = ConversionStrictness.ImplExactMatch;
                }
                return(unchecked ((Int32)lval));
            }

            if (obj is double)
            {
                double dval = (double)obj;
                if (dval < Int32.MinValue || dval > Int32.MaxValue)
                {
                    strictness = ConversionStrictness.Failed;
                }
                else
                {
                    strictness = ConversionStrictness.ImplPercisionLost;
                }
                return(unchecked ((Int32)dval));
            }

            if ((s = PhpVariable.AsString(obj)) != null)
            {
                int    ival;
                double dval;
                long   lval;

                // successfull iff the number encoded in the string fits the Int32:
                Convert.NumberInfo info = Convert.StringToNumber(s, out ival, out lval, out dval);
                if ((info & Convert.NumberInfo.Integer) != 0)
                {
                    strictness = ConversionStrictness.ImplDomainChange; return(ival);
                }                                                                                    // "123 hello world" -> 123 (for example)

                strictness = ConversionStrictness.Failed;
                return(unchecked ((Int32)lval));
            }

            // explicit conversion

            /*IPhpConvertible conv;
             * if ((conv = obj as IPhpConvertible) != null)
             * {
             *      int ival;
             *      double dval;
             *      long lval;
             *
             *      Convert.NumberInfo info = conv.ToNumber(out ival, out lval, out dval);
             *      if ((info & (Convert.NumberInfo.Integer | Convert.NumberInfo.IsNumber)) ==
             *              (Convert.NumberInfo.Integer | Convert.NumberInfo.IsNumber))
             *      {
             *              strictness = ConversionStrictness.Explicit;
             *              return ival;
             *      }
             *
             *      strictness = ConversionStrictness.Failed;
             *      return unchecked((Int32)lval);
             * }*/

            strictness = ConversionStrictness.Failed;
            return(0);
        }