Пример #1
0
//============================================================================
//		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);
}
Пример #2
0
/*************************************
 * operator =
 *************************************/
SHVDataVariant& SHVDataVariantImpl::operator=(const SHVDataVariant& val)
{
	switch (val.GetDataType())
	{
		case SHVDataVariant::TypeInt:
			SetInt(val.AsInt());
			break;
		case SHVDataVariant::TypeInt64:
			SetInt64(val.AsInt64());
			break;
		case SHVDataVariant::TypeBool:
			SetBool(val.AsBool());
			break;
		case SHVDataVariant::TypeDouble:
			SetDouble(val.AsDouble());
			break;
		case SHVDataVariant::TypeString:
			SetString(val.AsString());
			break;
		case SHVDataVariant::TypeTime:
			SetTime(val.AsTime());
			break;
	}
	return *this;
}
Пример #3
0
//============================================================================
//		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);
}
Пример #4
0
//============================================================================
//		NNumber::NNumber : Constructor.
//----------------------------------------------------------------------------
NNumber::NNumber(int64_t theValue)
{


	// Initialise ourselves
	SetInt64(theValue);
}
Пример #5
0
void CMdbPara::SetNumber(register uint32 nFieldNo, register void* pVal, register uint32 nOperator)
{
	switch(GetType(nFieldNo))
	{
	case MDB_INT8_FIELD:
		SetInt8(nFieldNo, *(int8*)pVal, nOperator);
		break;
	case MDB_INT16_FIELD:
		SetInt16(nFieldNo, *(int16*)pVal, nOperator);
		break;
	case MDB_INT32_FIELD:
		SetInt32(nFieldNo, *(int32*)pVal, nOperator);
		break;
	case MDB_INT64_FIELD:
		SetInt64(nFieldNo, *(int64*)pVal, nOperator);
		break;
	case MDB_UINT8_FIELD:
		SetUInt8(nFieldNo, *(uint8*)pVal, nOperator);
		break;
	case MDB_UINT16_FIELD:
		SetUInt16(nFieldNo, *(uint16*)pVal, nOperator);
		break;
	case MDB_UINT32_FIELD:
		SetUInt32(nFieldNo, *(uint32*)pVal, nOperator);
		break;
	case MDB_UINT64_FIELD:
		SetUInt64(nFieldNo, *(uint64*)pVal, nOperator);
		break;
	case MDB_FLOAT_FIELD:
		SetFloat(nFieldNo, *(float*)pVal, nOperator);
		break;
	case MDB_DOUBLE_FIELD:
		SetDouble(nFieldNo, *(double*)pVal, nOperator);
		break;
	case MDB_DATE_FIELD:
		SetDate(nFieldNo, *(CDate*)pVal, nOperator);
		break;
	case MDB_TIME_FIELD:
		SetTime(nFieldNo, *(CTime*)pVal, nOperator);
		break;
	case MDB_DATETIME_FIELD:
		SetDateTime(nFieldNo, *(CDateTime*)pVal, nOperator);
		break;
	}
}
Пример #6
0
bool Pb2Json::Json2Message(const Json& json, ProtobufMsg& message, bool str2enum) {
    auto descriptor = message.GetDescriptor();
    auto reflection = message.GetReflection();
    if (nullptr == descriptor || nullptr == reflection) return false;

    auto count = descriptor->field_count();
    for (auto i = 0; i < count; ++i) {
        const auto field = descriptor->field(i);
        if (nullptr == field) continue;

        auto& value = json[field->name()];
        if (value.is_null()) continue;

        if (field->is_repeated()) {
            if (!value.is_array()) {
                return false;
            } else {
                Json2RepeatedMessage(value, message, field, reflection, str2enum);
                continue;
            }
        }

        switch (field->type()) {
            case ProtobufFieldDescriptor::TYPE_BOOL: {
                if (value.is_boolean())
                    reflection->SetBool(&message, field, value.get<bool>());
                else if (value.is_number_integer())
                    reflection->SetBool(&message, field, value.get<uint32_t>() != 0);
                else if (value.is_string()) {
                    if (value.get<std::string>() == "true")
                        reflection->SetBool(&message, field, true);
                    else if (value.get<std::string>() == "false")
                        reflection->SetBool(&message, field, false);
                }
            } break;

            case ProtobufFieldDescriptor::TYPE_ENUM: {
                auto const* pedesc = field->enum_type();
                const ::google::protobuf::EnumValueDescriptor* pevdesc = nullptr;

                if (str2enum) {
                    pevdesc = pedesc->FindValueByName(value.get<std::string>());
                } else {
                    pevdesc = pedesc->FindValueByNumber(value.get<int>());
                }

                if (nullptr != pevdesc) {
                    reflection->SetEnum(&message, field, pevdesc);
                }
            } break;

            case ProtobufFieldDescriptor::TYPE_INT32:
            case ProtobufFieldDescriptor::TYPE_SINT32:
            case ProtobufFieldDescriptor::TYPE_SFIXED32: {
                if (value.is_number()) reflection->SetInt32(&message, field, value.get<int32_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_UINT32:
            case ProtobufFieldDescriptor::TYPE_FIXED32: {
                if (value.is_number()) reflection->SetUInt32(&message, field, value.get<uint32_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_INT64:
            case ProtobufFieldDescriptor::TYPE_SINT64:
            case ProtobufFieldDescriptor::TYPE_SFIXED64: {
                if (value.is_number()) reflection->SetInt64(&message, field, value.get<int64_t>());
            } break;
            case ProtobufFieldDescriptor::TYPE_UINT64:
            case ProtobufFieldDescriptor::TYPE_FIXED64: {
                if (value.is_number()) reflection->SetUInt64(&message, field, value.get<uint64_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_FLOAT: {
                if (value.is_number()) reflection->SetFloat(&message, field, value.get<float>());
            } break;

            case ProtobufFieldDescriptor::TYPE_DOUBLE: {
                if (value.is_number()) reflection->SetDouble(&message, field, value.get<double>());
            } break;

            case ProtobufFieldDescriptor::TYPE_STRING:
            case ProtobufFieldDescriptor::TYPE_BYTES: {
                if (value.is_string()) reflection->SetString(&message, field, value.get<std::string>());
            } break;

            case ProtobufFieldDescriptor::TYPE_MESSAGE: {
                if (value.is_object()) Json2Message(value, *reflection->MutableMessage(&message, field));
            } break;

            default:
                break;
        }
    }
    return true;
}
Пример #7
0
//============================================================================
//		NNumber::SetValue : Set the value.
//----------------------------------------------------------------------------
bool NNumber::SetValue(const NVariant &theValue)
{	uint8_t					valueUInt8;
	uint16_t				valueUInt16;
	uint32_t				valueUInt32;
	uint64_t				valueUInt64;
	int8_t					valueSInt8;
	int16_t					valueSInt16;
	int32_t					valueSInt32;
	int64_t					valueSInt64;
	float32_t				valueFloat32;
	float64_t				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))
		SetInt8(valueSInt8);
	
	else if (theValue.GetValue(valueSInt16))
		SetInt16(valueSInt16);
	
	else if (theValue.GetValue(valueSInt32))
		SetInt32(valueSInt32);
	
	else if (theValue.GetValue(valueSInt64))
		SetInt64(valueSInt64);
	
	else if (theValue.GetValue(valueFloat32))
		SetFloat32(valueFloat32);
	
	else if (theValue.GetValue(valueFloat64))
		SetFloat64(valueFloat64);
	
	else if (theValue.GetValue(valueInt))
		SetInt64(valueInt);
	
	else if (theValue.GetValue(valueLong))
		SetInt64(valueLong);
	
	else if (theValue.GetValue(valueString))
		return(SetValue(valueString));

	else
		return(false);

	return(true);
}