//============================================================================ // NCFNumber::SetObject : Set the object. //---------------------------------------------------------------------------- bool NCFNumber::SetObject(CFNumberRef cfObject, bool takeOwnership) { NCFObject theObject(cfObject, takeOwnership); float64_t valueFloat64; float32_t valueFloat32; int64_t valueInt64; bool isValid; // Get the state we need isValid = (cfObject != NULL); SetInt8(0); // Set the object if (isValid) { if (CFNumberGetValue( cfObject, kCFNumberLongLongType, &valueInt64)) SetInt64(valueInt64); else if (CFNumberGetValue(cfObject, kCFNumberFloatType, &valueFloat32)) SetFloat32(valueFloat32); else if (CFNumberGetValue(cfObject, kCFNumberDoubleType, &valueFloat64)) SetFloat64(valueFloat64); else NN_LOG("Unable to convert CFNumber to NNumber"); } return(isValid); }
//============================================================================ // NNumber::NNumber : Constructor. //---------------------------------------------------------------------------- NNumber::NNumber(Float32 theValue) { // Initialise ourselves SetFloat32(theValue); }
//============================================================================ // NNumber::SetValue : Set the value. //---------------------------------------------------------------------------- bool NNumber::SetValue(const NString &theValue) { NRange foundDot, foundE; NIndex thePrecision; int64_t valueInteger; float64_t valueReal; // Parse the value // // Some integers will also pass parsing as floats, however we coerce these // back to integers when possible to allow us to use more tightly packed // types for storage in the future. if (sscanf(theValue.GetUTF8(), "%lf", &valueReal) == 1) { // Get the state we need foundDot = theValue.Find("."); foundE = theValue.Find("e", kNStringNoCase); if (foundDot.IsEmpty() || !foundE.IsEmpty()) thePrecision = kDecimalsFloat64; else thePrecision = theValue.GetSize() - foundDot.GetNext(); // Cast the value if (foundDot.IsEmpty() && foundE.IsEmpty() && valueReal >= kInt64Min && valueReal <= kInt64Max) SetInt64((int64_t) valueReal); else if (thePrecision <= kDecimalsFloat32 && valueReal >= kFloat32Min && valueReal <= kFloat32Max) SetFloat32((float32_t) valueReal); else SetFloat64(valueReal); return(true); } else if (sscanf(theValue.GetUTF8(), "%lld", &valueInteger) == 1 || sscanf(theValue.GetUTF8(), "%llx", &valueInteger) == 1 || sscanf(theValue.GetUTF8(), "0x%llx", &valueInteger) == 1 || sscanf(theValue.GetUTF8(), "0X%llx", &valueInteger) == 1) { SetInt64(valueInteger); return(true); } return(false); }
//============================================================================ // NNumber::SetValue : Set the value. //---------------------------------------------------------------------------- bool NNumber::SetValue(const NVariant &theValue) { UInt8 valueUInt8; UInt16 valueUInt16; UInt32 valueUInt32; UInt64 valueUInt64; SInt8 valueSInt8; SInt16 valueSInt16; SInt32 valueSInt32; SInt64 valueSInt64; Float32 valueFloat32; Float64 valueFloat64; int valueInt; long valueLong; NString valueString; // Set the value // // NVariant treats some unsized types as numeric, to support literal constants. if (theValue.GetValue(*this)) ; // Assigned to this else if (theValue.GetValue(valueUInt8)) SetUInt8(valueUInt8); else if (theValue.GetValue(valueUInt16)) SetUInt16(valueUInt16); else if (theValue.GetValue(valueUInt32)) SetUInt32(valueUInt32); else if (theValue.GetValue(valueUInt64)) SetUInt64(valueUInt64); else if (theValue.GetValue(valueSInt8)) SetSInt8(valueSInt8); else if (theValue.GetValue(valueSInt16)) SetSInt16(valueSInt16); else if (theValue.GetValue(valueSInt32)) SetSInt32(valueSInt32); else if (theValue.GetValue(valueSInt64)) SetSInt64(valueSInt64); else if (theValue.GetValue(valueFloat32)) SetFloat32(valueFloat32); else if (theValue.GetValue(valueFloat64)) SetFloat64(valueFloat64); else if (theValue.GetValue(valueInt)) SetSInt64(valueInt); else if (theValue.GetValue(valueLong)) SetSInt64(valueLong); else if (theValue.GetValue(valueString)) return(SetValue(valueString)); else return(false); return(true); }