1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

Updated fix to preserve assert behavior by adding a check routine that

is called before trying to add an item.
This commit is contained in:
naiiawah 2011-12-06 22:20:46 -07:00
parent 3303313908
commit 6dc121fda6
3 changed files with 53 additions and 14 deletions

View File

@ -492,7 +492,7 @@ QVariant UAVObjectField::getValue(quint32 index)
return QVariant();
}
bool UAVObjectField::setValue(const QVariant& value, quint32 index)
bool UAVObjectField::checkValue(const QVariant& value, quint32 index)
{
QMutexLocker locker(obj->getMutex());
// Check that index is not out of bounds
@ -504,6 +504,45 @@ bool UAVObjectField::setValue(const QVariant& value, quint32 index)
UAVObject::Metadata mdata = obj->getMetadata();
// Update value if the access mode permits
if ( mdata.gcsAccess == UAVObject::ACCESS_READWRITE )
{
switch (type)
{
case INT8:
case INT16:
case INT32:
case UINT8:
case UINT16:
case UINT32:
case FLOAT32:
case STRING:
return true;
break;
case ENUM:
{
qint8 tmpenum = options.indexOf( value.toString() );
return ((tmpenum < 0) ? false : true);
break;
}
default:
qDebug() << "checkValue: other types" << type;
Q_ASSERT(0); // To catch any programming errors where we tried to test invalid values
break;
}
}
}
void UAVObjectField::setValue(const QVariant& value, quint32 index)
{
QMutexLocker locker(obj->getMutex());
// Check that index is not out of bounds
if ( index >= numElements )
{
return;
}
// Get metadata
UAVObject::Metadata mdata = obj->getMetadata();
// Update value if the access mode permits
if ( mdata.gcsAccess == UAVObject::ACCESS_READWRITE )
{
switch (type)
{
@ -552,11 +591,8 @@ bool UAVObjectField::setValue(const QVariant& value, quint32 index)
case ENUM:
{
qint8 tmpenum = options.indexOf( value.toString() );
// Q_ASSERT(tmpenum >= 0); // To catch any programming errors where we set invalid values
if(tmpenum < 0)
return false;
else
memcpy(&data[offset + numBytesPerElement*index], &tmpenum, numBytesPerElement);
Q_ASSERT(tmpenum >= 0); // To catch any programming errors where we set invalid values
memcpy(&data[offset + numBytesPerElement*index], &tmpenum, numBytesPerElement);
break;
}
case STRING:
@ -573,7 +609,6 @@ bool UAVObjectField::setValue(const QVariant& value, quint32 index)
}
}
}
return true;
}
double UAVObjectField::getDouble(quint32 index)

View File

@ -56,7 +56,8 @@ public:
qint32 pack(quint8* dataOut);
qint32 unpack(const quint8* dataIn);
QVariant getValue(quint32 index = 0);
bool setValue(const QVariant& data, quint32 index = 0);
bool checkValue(const QVariant& data, quint32 index = 0);
void setValue(const QVariant& data, quint32 index = 0);
double getDouble(quint32 index = 0);
void setDouble(double value, quint32 index = 0);
quint32 getDataOffset();

View File

@ -162,19 +162,22 @@ void UAVSettingsImportExportFactory::importUAVSettings()
if (uavfield) {
QStringList list = f.attribute("values").split(",");
if (list.length() == 1) {
if (false == uavfield->setValue(f.attribute("values"))) {
qDebug() << "setValue returned false on: " << uavObjectName << f.attribute("values");
if (false == uavfield->checkValue(f.attribute("values"))) {
qDebug() << "checkValue returned false on: " << uavObjectName << f.attribute("values");
setError = true;
}
} else
uavfield->setValue(f.attribute("values"));
} else {
// This is an enum:
int i=0;
QStringList list = f.attribute("values").split(",");
foreach (QString element, list) {
if (false == uavfield->setValue(element,i++)) {
qDebug() << "setValue returned false on: " << uavObjectName << list;
if (false == uavfield->checkValue(element,i)) {
qDebug() << "checkValue(list) returned false on: " << uavObjectName << list;
setError = true;
}
} else
uavfield->setValue(element,i);
i++;
}
}
} else {