public object ToInteger() { if (base.Value == null) { IntPtr buffer = Marshal.AllocCoTaskMem(CFNumberGetByteSize(typeRef)); bool scs = CFNumberGetValue(typeRef, CFNumberGetType(typeRef), buffer); if (scs != true) { UnityEngine.Debug.LogError("CFNumber IntPtr to Integer failed."); } CFNumberType type = CFNumberGetType(typeRef); switch (type) { case Native.CFNumberType.kCFNumberSInt8Type: return(base.Value = Marshal.ReadInt16(buffer)); case Native.CFNumberType.kCFNumberSInt16Type: return(base.Value = Marshal.ReadInt16(buffer)); case Native.CFNumberType.kCFNumberSInt32Type: return(base.Value = Marshal.ReadInt32(buffer)); case Native.CFNumberType.kCFNumberSInt64Type: return(base.Value = Marshal.ReadInt64(buffer)); default: UnityEngine.Debug.LogError("CFNumber value not recognize type of " + ((Native.CFNumberType)type).ToString()); break; } } return(base.Value); }
unsafe void GetValue(CFNumberType theType, void *valuePtr) { if (!CFNumberGetValue(Handle, theType, valuePtr)) { throw new InvalidCastException($"unable to convert CFNumber to {theType}"); } }
/// <summary> /// Converts an arbitrary CFNumber to a double /// </summary> public static double ConvertCFNumber(TypedPtr <CFNumber> Number) { CFNumberType TypeID = CoreImpl.CFNumberGetType((IntPtr)Number); switch (TypeID) { case CFNumberType.kCFNumberFloat32Type: case CFNumberType.kCFNumberFloatType: { float Result; CoreImpl.CFNumberGetValue((IntPtr)Number, CFNumberType.kCFNumberFloat32Type, out Result); return(Result); } case CFNumberType.kCFNumberFloat64Type: case CFNumberType.kCFNumberDoubleType: case CFNumberType.kCFNumberCGFloatType: { double Result; CoreImpl.CFNumberGetValue((IntPtr)Number, CFNumberType.kCFNumberFloat64Type, out Result); return(Result); } case CFNumberType.kCFNumberSInt8Type: case CFNumberType.kCFNumberSInt16Type: case CFNumberType.kCFNumberSInt32Type: case CFNumberType.kCFNumberCharType: case CFNumberType.kCFNumberShortType: case CFNumberType.kCFNumberIntType: case CFNumberType.kCFNumberLongType: case CFNumberType.kCFNumberCFIndexType: { int Result; CoreImpl.CFNumberGetValue((IntPtr)Number, CFNumberType.kCFNumberIntType, out Result); return(Result); } case CFNumberType.kCFNumberSInt64Type: case CFNumberType.kCFNumberLongLongType: case CFNumberType.kCFNumberNSIntegerType: { Int64 Result; CoreImpl.CFNumberGetValue((IntPtr)Number, CFNumberType.kCFNumberSInt64Type, out Result); return(Result); } default: return(0.0); } }
public static extern IntPtr CFNumberCreate_Int64(IntPtr number, CFNumberType theType, ref long valuePtr);
internal static extern bool CFNumberGetValue_Int(IntPtr number, CFNumberType theType, ref int valuePtr);
public extern static Boolean CFNumberGetValue(IntPtr /*CFNumber*/ Number, CFNumberType TheType, out Int64 Value);
public static extern bool CFNumberGetValue(IntPtr number, CFNumberType theType, out IntPtr valuePtr);
internal static extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, IntPtr valuePtr);
internal unsafe static extern bool CFNumberGetValue (IntPtr number, CFNumberType theType, IntPtr valuePtr);
public static extern Boolean CFNumberGetValue(IntPtr/*CFNumber*/ Number, CFNumberType TheType, out uint Value);
static extern unsafe IntPtr CFNumberCreate(IntPtr allocator, CFNumberType theType, void *valuePtr);
public IntPtr CFNumberCreate(IntPtr allocator, CFNumberType theType, ref Int32 Value) { return CoreFoundation.CFNumberCreate(allocator, theType, ref Value); }
public Boolean CFNumberGetValue(TypedPtr<CFNumber> Number, CFNumberType TheType, out Int64 Value) { return CoreFoundation.CFNumberGetValue((IntPtr)Number, TheType, out Value); }
public extern static Boolean CFNumberGetValue(IntPtr/*CFNumber*/ Number, CFNumberType TheType, out double Value);
private static unsafe extern int CFNumberGetValue(IntPtr handle, CFNumberType type, int *value);
internal static unsafe extern bool CFNumberGetValue(IntPtr number, CFNumberType theType, double* valuePtr);
internal static extern bool CFNumberGetValue_Single(IntPtr number, CFNumberType theType, ref float valuePtr);
private extern static Boolean CFNumberGetValue(IntPtr /*CFNumber*/ Number, CFNumberType TheType, out uint Value);
public static extern IntPtr CFNumberCreate(IntPtr allocator, CFNumberType type, ref int value);
public static extern IntPtr CFNumberCreate(IntPtr intptr_2, CFNumberType enum20_0, IntPtr intptr_3);
static extern unsafe bool CFNumberGetValue(IntPtr number, CFNumberType theType, void *valuePtr);
public static extern IntPtr CFNumberCreate_Int16(IntPtr number, CFNumberType theType, ref short valuePtr);
internal unsafe static extern CFNumberRef CFNumberCreate (CFAllocatorRef allocator,CFNumberType theType,int* valuePtr);
public static extern IntPtr CFNumberCreate_Int32(IntPtr number, CFNumberType theType, ref int valuePtr);
public Boolean CFNumberGetValue(TypedPtr <CFNumber> Number, CFNumberType TheType, out Int64 Value) { return(CoreFoundation.CFNumberGetValue((IntPtr)Number, TheType, out Value)); }
public static extern IntPtr CFNumberCreate_Int64(IntPtr number, CFNumberType theType, ref long valuePtr);
private static bool IsIntType(CFNumberType numType) { return (numType == CFNumberType.kCFNumberSInt8Type || numType == CFNumberType.kCFNumberSInt16Type || numType == CFNumberType.kCFNumberSInt32Type || numType == CFNumberType.kCFNumberSInt64Type || numType == CFNumberType.kCFNumberCharType || numType == CFNumberType.kCFNumberShortType || numType == CFNumberType.kCFNumberIntType || numType == CFNumberType.kCFNumberLongType || numType == CFNumberType.kCFNumberLongLongType); }
public static extern IntPtr CFNumberCreate_Float(IntPtr number, CFNumberType theType, ref float valuePtr);
public static extern IntPtr CFNumberCreate_Int32(IntPtr number, CFNumberType theType, ref int valuePtr);
public static extern IntPtr CFNumberCreate_Double(IntPtr number, CFNumberType theType, ref double valuePtr);
internal static extern bool CFNumberGetValue_Double(IntPtr number, CFNumberType theType, ref double valuePtr);
internal static extern bool CFNumberGetValue_Int(IntPtr number, CFNumberType theType, ref int valuePtr);
internal static extern bool CFNumberGetValue_Long(IntPtr number, CFNumberType theType, ref long valuePtr);
internal static extern bool CFNumberGetValue_Long(IntPtr number, CFNumberType theType, ref long valuePtr);
internal static extern bool CFNumberGetValue(IntPtr number, CFNumberType theType, out double valuePtr);
internal static extern bool CFNumberGetValue_Single(IntPtr number, CFNumberType theType, ref float valuePtr);
public static extern bool CFNumberGetValue(IntPtr number, CFNumberType type, out int value);
private static unsafe partial int CFNumberGetValue(IntPtr handle, CFNumberType type, int *value);
unsafe CFNumber(CFNumberType theType, void *valuePtr) : base(CFNumberCreate(IntPtr.Zero, theType, valuePtr)) { }
public static extern IntPtr CFNumberCreate(IntPtr allocator, CFNumberType type, ref int value);
private static extern IntPtr CFNumberCreate(IntPtr allocator, CFNumberType theType, ref Int32 Value);
internal unsafe static extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, int *valuePtr);
internal unsafe static extern bool CFNumberGetValue(IntPtr number, CFNumberType theType, double *valuePtr);
private static extern Boolean CFNumberGetValue(IntPtr/*CFNumber*/ Number, CFNumberType TheType, out Int64 Value);
public static extern bool CFNumberGetValue(IntPtr number, CFNumberType type, out int value);
private static extern int CFNumberGetValue(IntPtr handle, CFNumberType type, out int value);
internal static extern CFNumberRef CFNumberCreate (CFAllocatorRef allocator,CFNumberType theType,IntPtr valuePtr);
internal static extern bool CFNumberGetValue (IntPtr number, CFNumberType theType, out long valuePtr);
public IntPtr CFNumberCreate(IntPtr allocator, CFNumberType theType, ref Int32 Value) { return(CoreFoundation.CFNumberCreate(allocator, theType, ref Value)); }
public static extern IntPtr CFNumberCreate(IntPtr intptr_2, CFNumberType enum20_0, IntPtr intptr_3);
public extern static IntPtr CFNumberCreate(IntPtr allocator, CFNumberType theType, ref Int32 Value);
public static extern IntPtr CFNumberCreate_Double(IntPtr number, CFNumberType theType, ref double valuePtr);
public static extern IntPtr CFNumberCreate_Float(IntPtr number, CFNumberType theType, ref float valuePtr);
public static extern IntPtr CFNumberCreate_Int16(IntPtr number, CFNumberType theType, ref short valuePtr);
private static unsafe string OSNumberToStringMac(IntPtr n, string s, CultureInfo ci, CFNumberType numType, object o) { #if REMOVE if (string.IsNullOrEmpty(s)) s = "G"; IntPtr cfLocale = IntPtr.Zero; IntPtr cfLocaleName = IntPtr.Zero; if (ci == null) { cfLocale = CFLocaleCopyCurrent(); ci = Utilities.CurrentCulture; } else { cfLocaleName = CFStringCreateWithCharacters(IntPtr.Zero, ci.Name, ci.Name.Length); cfLocale = CFLocaleCreate(IntPtr.Zero, cfLocaleName); } IntPtr cfNumber = CFNumberCreate(IntPtr.Zero, numType, n); IntPtr cfFormat = IntPtr.Zero; int formatLen = 0; if (s.Length > 1) { formatLen = int.Parse(s.Substring(1)); } if (s[0] == 'Y') { int dotLoc = double.Parse(o.ToString()).ToString(CultureInfo.InvariantCulture).Trim('-').IndexOf('.'); if (dotLoc == -1) formatLen = 0; else { string subStr = double.Parse(o.ToString()).ToString(CultureInfo.InvariantCulture).Trim('-').Substring(dotLoc + 1); formatLen = Math.Min(formatLen - dotLoc, subStr.Length); if (s.Length == 1) formatLen = subStr.Length; } } if (s[0] == 'Z') formatLen--; if (formatLen < 0) formatLen = 0; int round = 1; IntPtr cfFormatLen = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&formatLen)); IntPtr cfOne = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&round)); int two = 2; IntPtr cfTwo = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&two)); int three = 3; IntPtr cfThree = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&three)); int zero = 0; IntPtr cfZero = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&zero)); int six = 6; IntPtr cfSix = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&six)); int numberDecimalDigit = ci.NumberFormat.NumberDecimalDigits; IntPtr cfNumberDecimalDigits = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&numberDecimalDigit)); switch (s[0].ToString().ToUpper()) //' switch (s[0].ToString().ToUpper(CultureInfo.InvariantCulture)) { case "P": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterPercentStyle); if (formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); } break; case "D": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterUseGroupingSeparator"), IntPtr.Zero); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfFormatLen); break; case "e": return OSNumberToStringMac(n, s, ci, numType, o).ToLower(); //' return OSNumberToStringMac(n, s, ci, numType, o).ToLower(CultureInfo.InvariantCulture); case "E": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterScientificStyle); if (formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } else { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfSix); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfSix); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } break; case "C": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterCurrencyStyle); if (formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } break; case "F": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterNoStyle); if (formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } else { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfNumberDecimalDigits); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfNumberDecimalDigits); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } break; case "N": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle); if (formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } else { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfNumberDecimalDigits); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfNumberDecimalDigits); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } break; case "G": if (IsIntType(numType)) return OSNumberToStringMac(n, s.ToUpper().Replace('G', 'D'), ci, numType, o); ; //' return OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'D'), ci, numType, o); ; string zReplace = OSNumberToStringMac(n, s.ToUpper().Replace('G', 'Z'), ci, numType, o); string yReplace = OSNumberToStringMac(n, s.ToUpper().Replace('G', 'Y'), ci, numType, o); //' string zReplace = OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'Z'), ci, numType, o); //' string yReplace = OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'Y'), ci, numType, o); if ((numType == CFNumberType.kCFNumberDoubleType) && (yReplace.Trim(ci.NumberFormat.NegativeSign.ToCharArray()).Replace(ci.NumberFormat.NumberDecimalSeparator, "").Trim('0').Length > 15)) return zReplace; if ((numType == CFNumberType.kCFNumberFloat32Type) && (yReplace.Trim(ci.NumberFormat.NegativeSign.ToCharArray()).Replace(ci.NumberFormat.NumberDecimalSeparator, "").Trim('0').Length > 7)) return zReplace; if (zReplace.Length < yReplace.Length) return zReplace; return yReplace; case "X": // TODO throw new NotSupportedException("GlobLocHelper does not currently handle hex formatting."); case "Y": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterUseGroupingSeparator"), IntPtr.Zero); if (s.Length > 1 || formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); } else { int largeNum = (numType == CFNumberType.kCFNumberFloat32Type) ? 7 : 15; IntPtr cfLargeNum = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&largeNum)); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfLargeNum); if (cfLargeNum != IntPtr.Zero) CFRelease(cfLargeNum); } break; case "Z": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterScientificStyle); if (s.Length > 1 || formatLen > 0) { CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne); } else { int largeNum = (numType == CFNumberType.kCFNumberFloat32Type) ? 7 : 15; IntPtr cfLargeNum = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&largeNum)); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfLargeNum); if (cfLargeNum != IntPtr.Zero) CFRelease(cfLargeNum); } break; case "R": cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle); int precision = (numType == CFNumberType.kCFNumberDoubleType) ? 15 : 7; IntPtr cfPrecision = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&precision)); CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfPrecision); if (cfPrecision != IntPtr.Zero) CFRelease(cfPrecision); break; default: cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterNoStyle); break; } IntPtr cfReturn = CFNumberFormatterCreateStringWithNumber(IntPtr.Zero, cfFormat, cfNumber); int newLength = CFStringGetLength(cfReturn); CFRange cfRange = new CFRange(); cfRange.x = 0; cfRange.y = newLength; StringBuilder sb = new StringBuilder(newLength); CFStringGetCharacters(cfReturn, cfRange, sb); string ret = sb.ToString().Substring(0, newLength); if (s[0] == 'e' || s[0] == 'g') ret = ret.Replace('E', 'e'); if (cfOne != IntPtr.Zero) CFRelease(cfOne); if (cfThree != IntPtr.Zero) CFRelease(cfThree); if (cfFormatLen != IntPtr.Zero) CFRelease(cfFormatLen); if (cfReturn != IntPtr.Zero) CFRelease(cfReturn); if (cfLocaleName != IntPtr.Zero) CFRelease(cfLocaleName); if (cfLocale != IntPtr.Zero) CFRelease(cfLocale); if (cfFormat != IntPtr.Zero) CFRelease(cfFormat); if (cfNumber != IntPtr.Zero) CFRelease(cfNumber); if (cfTwo != IntPtr.Zero) CFRelease(cfTwo); if (cfZero != IntPtr.Zero) CFRelease(cfZero); if (cfSix != IntPtr.Zero) CFRelease(cfSix); if (cfNumberDecimalDigits != IntPtr.Zero) CFRelease(cfNumberDecimalDigits); return ret; #else throw new NotImplementedException(); #endif }
public static extern bool CFNumberGetValue(IntPtr number, CFNumberType theType, out IntPtr valuePtr);