Commit 0fbc5bda authored by Thomas BOUTROUE's avatar Thomas BOUTROUE

Make all the CanOpenSubEntry.write*** methods return bool if changed

parent a822ca23
......@@ -5,12 +5,12 @@ CanOpenSubEntry::CanOpenSubEntry (const CanOpenDataType dataType,
const CanOpenAccessMode accessMode,
const CanOpenDataLen dataLen,
QObject * parent)
: QObject (parent)
: QObject (parent)
, m_haveBeenWritten (false)
, m_dataLen (CanOpenDataTypes::sizeOfType (dataType, dataLen))
, m_dataType (dataType)
, m_accessMode (accessMode)
, m_dataPtr (m_dataLen > 0 ? new qbyte [m_dataLen] : Q_NULLPTR)
, m_dataLen (CanOpenDataTypes::sizeOfType (dataType, dataLen))
, m_dataType (dataType)
, m_accessMode (accessMode)
, m_dataPtr (m_dataLen > 0 ? new qbyte [m_dataLen] : Q_NULLPTR)
{
for (quint pos = 0; pos < m_dataLen; ++pos) {
m_dataPtr [pos] = 0x00;
......@@ -60,23 +60,24 @@ void CanOpenSubEntry::read (qvoidptr dstPtr, const quint dstSize) const {
}
}
void CanOpenSubEntry::write (const qvoidptr srcPtr, const quint srcSize) {
bool CanOpenSubEntry::write (const qvoidptr srcPtr, const quint srcSize) {
bool changed = false;
if (srcPtr != Q_NULLPTR && m_dataPtr != Q_NULLPTR) {
m_haveBeenWritten = true;
const qbyteptr srcBytes = qbyteptr (srcPtr);
qbyteptr ownBytes = qbyteptr (m_dataPtr);
const quint ownSize = m_dataLen;
bool changed = false;
for (quint pos = 0; pos < srcSize && pos < ownSize; ++pos) {
if (ownBytes [pos] != srcBytes [pos]) {
ownBytes [pos] = srcBytes [pos];
changed = true;
}
}
if (changed) {
emit dataChanged ();
}
}
if (changed) {
emit dataChanged ();
}
return changed;
}
QVariant CanOpenSubEntry::readToQtVariant (void) const {
......@@ -152,82 +153,52 @@ QByteArray CanOpenSubEntry::readToQtByteArray (void) const {
return ret;
}
void CanOpenSubEntry::writeFromQtVariant (const QVariant & var) {
bool CanOpenSubEntry::writeFromQtVariant (const QVariant & var) {
switch (m_dataType) {
case CanOpenDataTypes::Bool: {
const bool tmp = var.value<bool> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<bool> (var.value<bool> ());
}
case CanOpenDataTypes::UInt8: {
const quint8 tmp = var.value<quint8> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<quint8> (var.value<quint8> ());
}
case CanOpenDataTypes::UInt16: {
const quint16 tmp = var.value<quint16> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<quint16> (var.value<quint16> ());
}
case CanOpenDataTypes::UInt32: {
const quint32 tmp = var.value<quint32> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<quint32> (var.value<quint32> ());
}
case CanOpenDataTypes::UInt64: {
const quint64 tmp = var.value<quint64> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<quint64> (var.value<quint64> ());
}
case CanOpenDataTypes::Int8: {
const qint8 tmp = var.value<qint8> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<qint8> (var.value<qint8> ());
}
case CanOpenDataTypes::Int16: {
const qint16 tmp = var.value<qint16> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<qint16> (var.value<qint16> ());
}
case CanOpenDataTypes::Int32: {
const qint32 tmp = var.value<qint32> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<qint32> (var.value<qint32> ());
}
case CanOpenDataTypes::Int64: {
const qint64 tmp = var.value<qint64> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<qint64> (var.value<qint64> ());
}
case CanOpenDataTypes::Real32: {
const float tmp = var.value<float> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<float> (var.value<float> ());
}
case CanOpenDataTypes::Real64: {
const double tmp = var.value<double> ();
write (&tmp, sizeof (tmp));
break;
return writeAs<double> (var.value<double> ());
}
case CanOpenDataTypes::VisibleStr: {
const QByteArray tmp = var.value<QString> ().toLatin1 ().leftJustified (int (m_dataLen), '\0', true);
write (tmp.data (), quint (tmp.size ()));
break;
return writeFromQtByteArray (var.value<QString> ().toLatin1 ().leftJustified (int (m_dataLen), '\0', true));
}
case CanOpenDataTypes::OctetStr: {
const QByteArray tmp = var.value<QString> ().toLocal8Bit ().leftJustified (int (m_dataLen), '\0', true);
write (tmp.data (), quint (tmp.size ()));
break;
return writeFromQtByteArray (var.value<QString> ().toLocal8Bit ().leftJustified (int (m_dataLen), '\0', true));
}
case CanOpenDataTypes::UnicodeStr: {
const QByteArray tmp = var.value<QString> ().toUtf8 ().leftJustified (int (m_dataLen), '\0', true);
write (tmp.data (), quint (tmp.size ()));
break;
return writeFromQtByteArray (var.value<QString> ().toUtf8 ().leftJustified (int (m_dataLen), '\0', true));
}
case CanOpenDataTypes::Domain: {
const QByteArray tmp = QByteArray::fromHex (var.value<QString> ().toLatin1 ()).leftJustified (int (m_dataLen) * 2, '0', true);
write (tmp.data (), quint (tmp.size ()));
break;
return writeFromQtByteArray (QByteArray::fromHex (var.value<QString> ().toLatin1 ()).leftJustified (int (m_dataLen) * 2, '0', true));
}
case CanOpenDataTypes::Int24:
case CanOpenDataTypes::Int40:
......@@ -246,8 +217,9 @@ void CanOpenSubEntry::writeFromQtVariant (const QVariant & var) {
case CanOpenDataTypes::UnknownType:
break;
}
return false;
}
void CanOpenSubEntry::writeFromQtByteArray (const QByteArray & bytes) {
write (bytes.constData (), quint (bytes.size ()));
bool CanOpenSubEntry::writeFromQtByteArray (const QByteArray & bytes) {
return write (bytes.constData (), quint (bytes.size ()));
}
......@@ -26,7 +26,7 @@ public:
void read (qvoidptr dstPtr, const quint dstSize) const;
void write (const qvoidptr srcPtr, const quint srcSize);
bool write (const qvoidptr srcPtr, const quint srcSize);
template<typename T> T readAs (void) const {
T ret;
......@@ -34,15 +34,15 @@ public:
return ret;
}
template<typename T> void writeAs (const T val) {
write (&val, sizeof (val));
template<typename T> bool writeAs (const T val) {
return write (&val, sizeof (val));
}
QVariant readToQtVariant (void) const;
QByteArray readToQtByteArray (void) const;
void writeFromQtVariant (const QVariant & var);
void writeFromQtByteArray (const QByteArray & bytes);
bool writeFromQtVariant (const QVariant & var);
bool writeFromQtByteArray (const QByteArray & bytes);
void resetWrittenFlag (void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment