From ae22fc2decb9255bed0cbf38df6d3f516537f873 Mon Sep 17 00:00:00 2001 From: Stefan Lobas Date: Fri, 4 Mar 2022 13:32:35 +0100 Subject: [PATCH 1/6] Changed the behavior of array writing to X-Plane: Writing is now limited to changed values only to circumvent problems like here: https://forums.x-plane.org/index.php?/forums/topic/232388-toliss-and-hardware-cockpit/ --- extplane-plugin/xplaneplugin.cpp | 16 ++++++++++++++-- extplane-server/datarefs/dataref.h | 4 ++++ extplane-server/datarefs/floatarraydataref.cpp | 15 +++++++++++++++ extplane-server/datarefs/floatarraydataref.h | 2 ++ extplane-server/datarefs/intarraydataref.cpp | 15 +++++++++++++++ extplane-server/datarefs/intarraydataref.h | 1 + protocoldefs.h | 2 +- 7 files changed, 52 insertions(+), 3 deletions(-) diff --git a/extplane-plugin/xplaneplugin.cpp b/extplane-plugin/xplaneplugin.cpp index 6f105e1..8ddea13 100644 --- a/extplane-plugin/xplaneplugin.cpp +++ b/extplane-plugin/xplaneplugin.cpp @@ -210,13 +210,25 @@ void XPlanePlugin::changeDataRef(DataRef *ref) case extplaneRefTypeFloatArray: { FloatArrayDataRef *faRef = qobject_cast(ref); - XPLMSetDatavf(ref->ref(), faRef->valueArray(), 0, faRef->value().size()); + while(!faRef->changedIndices.empty()) { + XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices.front().lower, + faRef->changedIndices.front().lower, + faRef->changedIndices.front().upper - faRef->changedIndices.front().lower + 1 + ); + faRef->changedIndices.pop_front(); + } break; } case extplaneRefTypeIntArray: { IntArrayDataRef *iaRef = qobject_cast(ref); - XPLMSetDatavi(ref->ref(), iaRef->valueArray(), 0, iaRef->value().size()); + while(!iaRef->changedIndices.empty()) { + XPLMSetDatavi(ref->ref(), iaRef->valueArray() + iaRef->changedIndices.front().lower, + iaRef->changedIndices.front().lower, + iaRef->changedIndices.front().upper - iaRef->changedIndices.front().lower + 1 + ); + iaRef->changedIndices.pop_front(); + } break; } case extplaneRefTypeInt: diff --git a/extplane-server/datarefs/dataref.h b/extplane-server/datarefs/dataref.h index eb7509a..ac939d3 100644 --- a/extplane-server/datarefs/dataref.h +++ b/extplane-server/datarefs/dataref.h @@ -18,6 +18,10 @@ enum { extplaneRefTypeData = 32 }; +typedef struct { + int lower; + int upper; +} indexPair; /** * Base class for DataRefs. diff --git a/extplane-server/datarefs/floatarraydataref.cpp b/extplane-server/datarefs/floatarraydataref.cpp index 275bb07..43ca4e9 100644 --- a/extplane-server/datarefs/floatarraydataref.cpp +++ b/extplane-server/datarefs/floatarraydataref.cpp @@ -30,6 +30,7 @@ std::vector &FloatArrayDataRef::value() { void FloatArrayDataRef::updateValue() { Q_ASSERT(_length > 0); bool valuesChanged = false; + for(int i=0;i<_length;i++){ if(_values.at(i) != _valueArray[i]) { _values[i] = _valueArray[i]; @@ -53,12 +54,18 @@ QString FloatArrayDataRef::valueString() { } void FloatArrayDataRef::setValue(QString &newValue) { + indexPair indPair; + + indPair.lower = 0; + indPair.upper = 0; // Check that value starts with [ and ends with ] if(!newValue.startsWith('[') || !newValue.endsWith(']')) { INFO << "Invalid array value" << newValue; return; } + changedIndices.clear(); + // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); QStringList values = arrayString.split(','); @@ -72,6 +79,14 @@ void FloatArrayDataRef::setValue(QString &newValue) { float value = values[i].toFloat(&ok); if(ok) { _valueArray[i] = value; + if(changedIndices.empty() || changedIndices.back().upper != (i-1)) + { + indPair.lower = i; + indPair.upper = i; + changedIndices.push_back(indPair); + } else { + changedIndices.back().upper = i; + } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; } diff --git a/extplane-server/datarefs/floatarraydataref.h b/extplane-server/datarefs/floatarraydataref.h index 0388416..cf5405c 100644 --- a/extplane-server/datarefs/floatarraydataref.h +++ b/extplane-server/datarefs/floatarraydataref.h @@ -11,6 +11,7 @@ #include "dataref.h" #include +#include class FloatArrayDataRef : public DataRef { Q_OBJECT @@ -19,6 +20,7 @@ class FloatArrayDataRef : public DataRef { FloatArrayDataRef(QObject *parent, QString name, void* ref); ~FloatArrayDataRef(); std::vector &value(); + std::list changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue); diff --git a/extplane-server/datarefs/intarraydataref.cpp b/extplane-server/datarefs/intarraydataref.cpp index 47a92e4..d939941 100644 --- a/extplane-server/datarefs/intarraydataref.cpp +++ b/extplane-server/datarefs/intarraydataref.cpp @@ -43,12 +43,19 @@ QString IntArrayDataRef::valueString() { } void IntArrayDataRef::setValue(QString &newValue) { + indexPair indPair; + + indPair.lower = 0; + indPair.upper = 0; + // Check that value starts with [ and ends with ] if(!newValue.startsWith('[') || !newValue.endsWith(']')) { INFO << "Invalid array value" << newValue; return; } + changedIndices.clear(); + // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); QStringList values = arrayString.split(','); @@ -62,6 +69,14 @@ void IntArrayDataRef::setValue(QString &newValue) { int value = values[i].toInt(&ok); if(ok) { _valueArray[i] = value; + if(changedIndices.empty() || changedIndices.back().upper != (i-1)) + { + indPair.lower = i; + indPair.upper = i; + changedIndices.push_back(indPair); + } else { + changedIndices.back().upper = i; + } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; } diff --git a/extplane-server/datarefs/intarraydataref.h b/extplane-server/datarefs/intarraydataref.h index f76b1bb..f0063c7 100644 --- a/extplane-server/datarefs/intarraydataref.h +++ b/extplane-server/datarefs/intarraydataref.h @@ -12,6 +12,7 @@ class IntArrayDataRef : public DataRef { IntArrayDataRef(QObject *parent, QString name, void* ref); ~IntArrayDataRef(); std::vector &value(); + std::list changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue); diff --git a/protocoldefs.h b/protocoldefs.h index 2e1448f..c5e0162 100644 --- a/protocoldefs.h +++ b/protocoldefs.h @@ -5,7 +5,7 @@ // Network protocol, currently always 1 #define EXTPLANE_PROTOCOL 1 // Feature revision, every time we add a new feature or bug fix, this should be incremented so that clients can know how old the plugin is -#define EXTPLANE_VERSION 1010 +#define EXTPLANE_VERSION 1011 // Base of udp ports. Bind to this + client_id #define UDP_OUT_PORT_BASE 52000 From e3c4bf05123435f596baf2621997ab21ae8e5cd8 Mon Sep 17 00:00:00 2001 From: Stefan Lobas Date: Fri, 4 Mar 2022 13:43:17 +0100 Subject: [PATCH 2/6] - Corrected missing include - removed blank line --- extplane-server/datarefs/floatarraydataref.cpp | 3 +-- extplane-server/datarefs/intarraydataref.h | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extplane-server/datarefs/floatarraydataref.cpp b/extplane-server/datarefs/floatarraydataref.cpp index 43ca4e9..eb2a15c 100644 --- a/extplane-server/datarefs/floatarraydataref.cpp +++ b/extplane-server/datarefs/floatarraydataref.cpp @@ -29,8 +29,7 @@ std::vector &FloatArrayDataRef::value() { // Copies values from valuearray to value list and emits changed as needed void FloatArrayDataRef::updateValue() { Q_ASSERT(_length > 0); - bool valuesChanged = false; - + bool valuesChanged = false; for(int i=0;i<_length;i++){ if(_values.at(i) != _valueArray[i]) { _values[i] = _valueArray[i]; diff --git a/extplane-server/datarefs/intarraydataref.h b/extplane-server/datarefs/intarraydataref.h index f0063c7..4dd41d0 100644 --- a/extplane-server/datarefs/intarraydataref.h +++ b/extplane-server/datarefs/intarraydataref.h @@ -4,6 +4,7 @@ #include "dataref.h" #include #include +#include class IntArrayDataRef : public DataRef { Q_OBJECT From d27b214c31168604e14f37823c42788a75eb0ac9 Mon Sep 17 00:00:00 2001 From: Stefan <39441897+StefanLobas@users.noreply.github.com> Date: Fri, 4 Mar 2022 13:45:04 +0100 Subject: [PATCH 3/6] Update floatarraydataref.cpp removed spaces --- extplane-server/datarefs/floatarraydataref.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extplane-server/datarefs/floatarraydataref.cpp b/extplane-server/datarefs/floatarraydataref.cpp index eb2a15c..544952d 100644 --- a/extplane-server/datarefs/floatarraydataref.cpp +++ b/extplane-server/datarefs/floatarraydataref.cpp @@ -29,7 +29,7 @@ std::vector &FloatArrayDataRef::value() { // Copies values from valuearray to value list and emits changed as needed void FloatArrayDataRef::updateValue() { Q_ASSERT(_length > 0); - bool valuesChanged = false; + bool valuesChanged = false; for(int i=0;i<_length;i++){ if(_values.at(i) != _valueArray[i]) { _values[i] = _valueArray[i]; From 98b98d6c1453bfd5a6ffe9d755dedb9162835dea Mon Sep 17 00:00:00 2001 From: Stefan Lobas Date: Fri, 4 Mar 2022 15:04:57 +0100 Subject: [PATCH 4/6] Changed list allocation to dynamic allocation to prevent X-Plane from crashing --- extplane-plugin/xplaneplugin.cpp | 10 +++++----- extplane-server/datarefs/floatarraydataref.cpp | 10 ++++++---- extplane-server/datarefs/floatarraydataref.h | 2 +- extplane-server/datarefs/intarraydataref.cpp | 10 ++++++---- extplane-server/datarefs/intarraydataref.h | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/extplane-plugin/xplaneplugin.cpp b/extplane-plugin/xplaneplugin.cpp index 8ddea13..67a7431 100644 --- a/extplane-plugin/xplaneplugin.cpp +++ b/extplane-plugin/xplaneplugin.cpp @@ -210,12 +210,12 @@ void XPlanePlugin::changeDataRef(DataRef *ref) case extplaneRefTypeFloatArray: { FloatArrayDataRef *faRef = qobject_cast(ref); - while(!faRef->changedIndices.empty()) { - XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices.front().lower, - faRef->changedIndices.front().lower, - faRef->changedIndices.front().upper - faRef->changedIndices.front().lower + 1 + while(!faRef->changedIndices->empty()) { + XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices->front().lower, + faRef->changedIndices->front().lower, + faRef->changedIndices->front().upper - faRef->changedIndices->front().lower + 1 ); - faRef->changedIndices.pop_front(); + faRef->changedIndices->pop_front(); } break; } diff --git a/extplane-server/datarefs/floatarraydataref.cpp b/extplane-server/datarefs/floatarraydataref.cpp index eb2a15c..841ef64 100644 --- a/extplane-server/datarefs/floatarraydataref.cpp +++ b/extplane-server/datarefs/floatarraydataref.cpp @@ -16,10 +16,12 @@ FloatArrayDataRef::FloatArrayDataRef(QObject *parent, QString name, void *ref) : _type = extplaneRefTypeFloatArray; _length = 0; _valueArray = nullptr; + changedIndices = new std::list; } FloatArrayDataRef::~FloatArrayDataRef() { if(_valueArray) delete [] _valueArray; + if(changedIndices) delete changedIndices; } std::vector &FloatArrayDataRef::value() { @@ -63,7 +65,7 @@ void FloatArrayDataRef::setValue(QString &newValue) { return; } - changedIndices.clear(); + changedIndices->clear(); // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); @@ -78,13 +80,13 @@ void FloatArrayDataRef::setValue(QString &newValue) { float value = values[i].toFloat(&ok); if(ok) { _valueArray[i] = value; - if(changedIndices.empty() || changedIndices.back().upper != (i-1)) + if(changedIndices->empty() || changedIndices->back().upper != (i-1)) { indPair.lower = i; indPair.upper = i; - changedIndices.push_back(indPair); + changedIndices->push_back(indPair); } else { - changedIndices.back().upper = i; + changedIndices->back().upper = i; } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; diff --git a/extplane-server/datarefs/floatarraydataref.h b/extplane-server/datarefs/floatarraydataref.h index cf5405c..4b86d9a 100644 --- a/extplane-server/datarefs/floatarraydataref.h +++ b/extplane-server/datarefs/floatarraydataref.h @@ -20,7 +20,7 @@ class FloatArrayDataRef : public DataRef { FloatArrayDataRef(QObject *parent, QString name, void* ref); ~FloatArrayDataRef(); std::vector &value(); - std::list changedIndices; + std::list* changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue); diff --git a/extplane-server/datarefs/intarraydataref.cpp b/extplane-server/datarefs/intarraydataref.cpp index d939941..250f824 100644 --- a/extplane-server/datarefs/intarraydataref.cpp +++ b/extplane-server/datarefs/intarraydataref.cpp @@ -8,10 +8,12 @@ IntArrayDataRef::IntArrayDataRef(QObject *parent, QString name, void *ref) : Dat _type = extplaneRefTypeIntArray; _length = 0; _valueArray = nullptr; + changedIndices = new std::list; } IntArrayDataRef::~IntArrayDataRef() { if(_valueArray) delete [] _valueArray; + if(changedIndices) delete changedIndices; } std::vector & IntArrayDataRef::value() { @@ -54,7 +56,7 @@ void IntArrayDataRef::setValue(QString &newValue) { return; } - changedIndices.clear(); + changedIndices->clear(); // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); @@ -69,13 +71,13 @@ void IntArrayDataRef::setValue(QString &newValue) { int value = values[i].toInt(&ok); if(ok) { _valueArray[i] = value; - if(changedIndices.empty() || changedIndices.back().upper != (i-1)) + if(changedIndices->empty() || changedIndices->back().upper != (i-1)) { indPair.lower = i; indPair.upper = i; - changedIndices.push_back(indPair); + changedIndices->push_back(indPair); } else { - changedIndices.back().upper = i; + changedIndices->back().upper = i; } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; diff --git a/extplane-server/datarefs/intarraydataref.h b/extplane-server/datarefs/intarraydataref.h index 4dd41d0..21f04ce 100644 --- a/extplane-server/datarefs/intarraydataref.h +++ b/extplane-server/datarefs/intarraydataref.h @@ -13,7 +13,7 @@ class IntArrayDataRef : public DataRef { IntArrayDataRef(QObject *parent, QString name, void* ref); ~IntArrayDataRef(); std::vector &value(); - std::list changedIndices; + std::list* changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue); From 1c5334292f138cc01da0a78640c81f9678ba6fed Mon Sep 17 00:00:00 2001 From: Stefan Lobas Date: Fri, 11 Mar 2022 19:06:10 +0100 Subject: [PATCH 5/6] Bugfix preventing X-Plane to crash when subscribing IntArrayDatarefs Was different here compared to FloatArrayDataref and caused X-Plane to crash. --- extplane-server/datarefs/intarraydataref.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extplane-server/datarefs/intarraydataref.cpp b/extplane-server/datarefs/intarraydataref.cpp index 250f824..b1c0fb7 100644 --- a/extplane-server/datarefs/intarraydataref.cpp +++ b/extplane-server/datarefs/intarraydataref.cpp @@ -83,7 +83,7 @@ void IntArrayDataRef::setValue(QString &newValue) { INFO << "Invalid value " << values.at(i) << "in array"; } } - emit changed(this); + updateValue(); } void IntArrayDataRef::setLength(int newLength) From 3a28b569190105e8b2c11a5787875e50a9257598 Mon Sep 17 00:00:00 2001 From: Stefan Lobas Date: Fri, 11 Mar 2022 19:15:46 +0100 Subject: [PATCH 6/6] Changed self-defined struct to std::pair and reverted back to scope-bound resource managed allocation instead of dynamic allocation for the list of pairs. --- extplane-plugin/xplaneplugin.cpp | 18 ++++++------- extplane-server/datarefs/dataref.h | 5 ---- .../datarefs/floatarraydataref.cpp | 16 +++--------- extplane-server/datarefs/floatarraydataref.h | 2 +- extplane-server/datarefs/intarraydataref.cpp | 26 +++++++------------ extplane-server/datarefs/intarraydataref.h | 3 +-- 6 files changed, 23 insertions(+), 47 deletions(-) diff --git a/extplane-plugin/xplaneplugin.cpp b/extplane-plugin/xplaneplugin.cpp index 67a7431..c5b722a 100644 --- a/extplane-plugin/xplaneplugin.cpp +++ b/extplane-plugin/xplaneplugin.cpp @@ -142,7 +142,7 @@ void XPlanePlugin::updateDataRef(DataRef *ref) { { IntArrayDataRef *iaRef = qobject_cast(ref); int arrayLength = iaRef->value().size(); - if(arrayLength <= 0) { + if(arrayLength == 0) { arrayLength = XPLMGetDatavi(iaRef->ref(), nullptr, 0, 0); iaRef->setLength(arrayLength); } @@ -210,12 +210,12 @@ void XPlanePlugin::changeDataRef(DataRef *ref) case extplaneRefTypeFloatArray: { FloatArrayDataRef *faRef = qobject_cast(ref); - while(!faRef->changedIndices->empty()) { - XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices->front().lower, - faRef->changedIndices->front().lower, - faRef->changedIndices->front().upper - faRef->changedIndices->front().lower + 1 + while(!faRef->changedIndices.empty()) { + XPLMSetDatavf(ref->ref(), faRef->valueArray() + faRef->changedIndices.front().first, + faRef->changedIndices.front().first, + faRef->changedIndices.front().second - faRef->changedIndices.front().first + 1 ); - faRef->changedIndices->pop_front(); + faRef->changedIndices.pop_front(); } break; } @@ -223,9 +223,9 @@ void XPlanePlugin::changeDataRef(DataRef *ref) { IntArrayDataRef *iaRef = qobject_cast(ref); while(!iaRef->changedIndices.empty()) { - XPLMSetDatavi(ref->ref(), iaRef->valueArray() + iaRef->changedIndices.front().lower, - iaRef->changedIndices.front().lower, - iaRef->changedIndices.front().upper - iaRef->changedIndices.front().lower + 1 + XPLMSetDatavi(ref->ref(), iaRef->valueArray() + iaRef->changedIndices.front().first, + iaRef->changedIndices.front().first, + iaRef->changedIndices.front().second - iaRef->changedIndices.front().first + 1 ); iaRef->changedIndices.pop_front(); } diff --git a/extplane-server/datarefs/dataref.h b/extplane-server/datarefs/dataref.h index ac939d3..869b658 100644 --- a/extplane-server/datarefs/dataref.h +++ b/extplane-server/datarefs/dataref.h @@ -18,11 +18,6 @@ enum { extplaneRefTypeData = 32 }; -typedef struct { - int lower; - int upper; -} indexPair; - /** * Base class for DataRefs. */ diff --git a/extplane-server/datarefs/floatarraydataref.cpp b/extplane-server/datarefs/floatarraydataref.cpp index 95b5aee..e965724 100644 --- a/extplane-server/datarefs/floatarraydataref.cpp +++ b/extplane-server/datarefs/floatarraydataref.cpp @@ -16,12 +16,10 @@ FloatArrayDataRef::FloatArrayDataRef(QObject *parent, QString name, void *ref) : _type = extplaneRefTypeFloatArray; _length = 0; _valueArray = nullptr; - changedIndices = new std::list; } FloatArrayDataRef::~FloatArrayDataRef() { if(_valueArray) delete [] _valueArray; - if(changedIndices) delete changedIndices; } std::vector &FloatArrayDataRef::value() { @@ -55,18 +53,12 @@ QString FloatArrayDataRef::valueString() { } void FloatArrayDataRef::setValue(QString &newValue) { - indexPair indPair; - - indPair.lower = 0; - indPair.upper = 0; // Check that value starts with [ and ends with ] if(!newValue.startsWith('[') || !newValue.endsWith(']')) { INFO << "Invalid array value" << newValue; return; } - changedIndices->clear(); - // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); QStringList values = arrayString.split(','); @@ -80,13 +72,11 @@ void FloatArrayDataRef::setValue(QString &newValue) { float value = values[i].toFloat(&ok); if(ok) { _valueArray[i] = value; - if(changedIndices->empty() || changedIndices->back().upper != (i-1)) + if(changedIndices.empty() || changedIndices.back().second != (i-1)) { - indPair.lower = i; - indPair.upper = i; - changedIndices->push_back(indPair); + changedIndices.push_back(std::pair(i, i)); } else { - changedIndices->back().upper = i; + changedIndices.back().second = i; } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; diff --git a/extplane-server/datarefs/floatarraydataref.h b/extplane-server/datarefs/floatarraydataref.h index 4b86d9a..7849fd1 100644 --- a/extplane-server/datarefs/floatarraydataref.h +++ b/extplane-server/datarefs/floatarraydataref.h @@ -20,7 +20,7 @@ class FloatArrayDataRef : public DataRef { FloatArrayDataRef(QObject *parent, QString name, void* ref); ~FloatArrayDataRef(); std::vector &value(); - std::list* changedIndices; + std::list> changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue); diff --git a/extplane-server/datarefs/intarraydataref.cpp b/extplane-server/datarefs/intarraydataref.cpp index b1c0fb7..1bcae02 100644 --- a/extplane-server/datarefs/intarraydataref.cpp +++ b/extplane-server/datarefs/intarraydataref.cpp @@ -1,26 +1,26 @@ #include "intarraydataref.h" #include "../../util/console.h" -#include #include +#include IntArrayDataRef::IntArrayDataRef(QObject *parent, QString name, void *ref) : DataRef(parent, name, ref) { _typeString = "ia"; _type = extplaneRefTypeIntArray; _length = 0; _valueArray = nullptr; - changedIndices = new std::list; } IntArrayDataRef::~IntArrayDataRef() { if(_valueArray) delete [] _valueArray; - if(changedIndices) delete changedIndices; } std::vector & IntArrayDataRef::value() { return _values; } +// Copies values from valuearray to value list and emits changed as needed void IntArrayDataRef::updateValue() { + Q_ASSERT(_length > 0); bool valuesChanged = false; for(int i=0;i<_length;i++){ if(_values.at(i) != _valueArray[i]) { @@ -45,19 +45,12 @@ QString IntArrayDataRef::valueString() { } void IntArrayDataRef::setValue(QString &newValue) { - indexPair indPair; - - indPair.lower = 0; - indPair.upper = 0; - // Check that value starts with [ and ends with ] if(!newValue.startsWith('[') || !newValue.endsWith(']')) { INFO << "Invalid array value" << newValue; return; } - changedIndices->clear(); - // Remove [] and split values QString arrayString = newValue.mid(1, newValue.length() - 2); QStringList values = arrayString.split(','); @@ -71,13 +64,11 @@ void IntArrayDataRef::setValue(QString &newValue) { int value = values[i].toInt(&ok); if(ok) { _valueArray[i] = value; - if(changedIndices->empty() || changedIndices->back().upper != (i-1)) + if(changedIndices.empty() || changedIndices.back().second != (i-1)) { - indPair.lower = i; - indPair.upper = i; - changedIndices->push_back(indPair); + changedIndices.push_back(std::pair(i, i)); } else { - changedIndices->back().upper = i; + changedIndices.back().second = i; } } else if(!values.at(i).isEmpty()) { INFO << "Invalid value " << values.at(i) << "in array"; @@ -88,14 +79,15 @@ void IntArrayDataRef::setValue(QString &newValue) { void IntArrayDataRef::setLength(int newLength) { - Q_ASSERT(newLength > 0); + Q_ASSERT(newLength >= 0); + _values.resize(newLength); std::fill(_values.begin(), _values.end(), -9999); if(_valueArray) delete[] _valueArray; _valueArray = new int[newLength]; _length = newLength; } -int* IntArrayDataRef::valueArray() +int *IntArrayDataRef::valueArray() { Q_ASSERT(_valueArray); return _valueArray; diff --git a/extplane-server/datarefs/intarraydataref.h b/extplane-server/datarefs/intarraydataref.h index 21f04ce..eb604d7 100644 --- a/extplane-server/datarefs/intarraydataref.h +++ b/extplane-server/datarefs/intarraydataref.h @@ -2,7 +2,6 @@ #define IntArrayDataRef_H #include "dataref.h" -#include #include #include @@ -13,7 +12,7 @@ class IntArrayDataRef : public DataRef { IntArrayDataRef(QObject *parent, QString name, void* ref); ~IntArrayDataRef(); std::vector &value(); - std::list* changedIndices; + std::list> changedIndices; virtual void updateValue(); virtual QString valueString(); virtual void setValue(QString &newValue);