Minor device connection manager clean-up.

Also fix a minor bug where the wrong error code was reported in
a rare edge case.
This commit is contained in:
sawinglogz 2020-07-09 21:19:54 -04:00
parent bdda964bf2
commit d5cb629069

View File

@ -61,7 +61,7 @@ public:
XmlRecorder(class QFile * file, const QString & tag = XmlRecorder::TAG); // record XML to the given file XmlRecorder(class QFile * file, const QString & tag = XmlRecorder::TAG); // record XML to the given file
XmlRecorder(QString & string, const QString & tag = XmlRecorder::TAG); // record XML to the given string XmlRecorder(QString & string, const QString & tag = XmlRecorder::TAG); // record XML to the given string
virtual ~XmlRecorder(); // write the epilogue and close the recorder virtual ~XmlRecorder(); // write the epilogue and close the recorder
XmlRecorder* close(); // convenience function to close out a substream and return its parent XmlRecorder* closeSubstream(); // convenience function to close out a substream and return its parent
inline QXmlStreamWriter & xml() { return *m_xml; } inline QXmlStreamWriter & xml() { return *m_xml; }
inline void lock() { m_mutex.lock(); } inline void lock() { m_mutex.lock(); }
inline void unlock() { m_mutex.unlock(); } inline void unlock() { m_mutex.unlock(); }
@ -119,7 +119,7 @@ public:
XmlReplay(class QFile * file, const QString & tag = XmlRecorder::TAG); // replay XML from the given file XmlReplay(class QFile * file, const QString & tag = XmlRecorder::TAG); // replay XML from the given file
XmlReplay(QXmlStreamReader & xml, const QString & tag = XmlRecorder::TAG); // replay XML from the given stream XmlReplay(QXmlStreamReader & xml, const QString & tag = XmlRecorder::TAG); // replay XML from the given stream
virtual ~XmlReplay(); virtual ~XmlReplay();
XmlReplay* close(); // convenience function to close out a substream and return its parent XmlReplay* closeSubstream(); // convenience function to close out a substream and return its parent
template<class T> inline T* getNextEvent(const QString & id = ""); // typesafe accessor to retrieve and consume the next matching event template<class T> inline T* getNextEvent(const QString & id = ""); // typesafe accessor to retrieve and consume the next matching event
@ -202,7 +202,7 @@ public:
// Deserialize this event's contents from an XML stream. The instance is first created via createInstance() based on the tag. // Deserialize this event's contents from an XML stream. The instance is first created via createInstance() based on the tag.
friend QXmlStreamReader & operator>>(QXmlStreamReader & xml, XmlReplayEvent & event); friend QXmlStreamReader & operator>>(QXmlStreamReader & xml, XmlReplayEvent & event);
// Write the opening tag and its contents, but don't close it. // Write the tag's attributes and contents.
void writeTag(QXmlStreamWriter & xml) const; void writeTag(QXmlStreamWriter & xml) const;
// Event subclass registration and instance creation // Event subclass registration and instance creation
@ -404,10 +404,7 @@ XmlRecorder::XmlRecorder(XmlRecorder* parent, const QString & id, const QString
m_xml = new QXmlStreamWriter(&null); m_xml = new QXmlStreamWriter(&null);
} }
m_xml->setAutoFormatting(true); prologue();
m_xml->setAutoFormattingIndent(2);
// Substreams handle their own prologue.
// TODO: move writeStartElement out of writeTag so that we can use the default prologue here.
} }
// Initialize a child recording substream. // Initialize a child recording substream.
@ -446,8 +443,7 @@ XmlRecorder::~XmlRecorder()
} }
// Close out a substream and return its parent. // Close out a substream and return its parent.
// TODO: rename to closeSubstream for clarity XmlRecorder* XmlRecorder::closeSubstream()
XmlRecorder* XmlRecorder::close()
{ {
auto parent = m_parent; auto parent = m_parent;
delete this; delete this;
@ -459,7 +455,7 @@ void XmlRecorder::prologue()
Q_ASSERT(m_xml); Q_ASSERT(m_xml);
m_xml->setAutoFormatting(true); m_xml->setAutoFormatting(true);
m_xml->setAutoFormattingIndent(2); m_xml->setAutoFormattingIndent(2);
m_xml->writeStartElement(m_tag); m_xml->writeStartElement(m_tag); // open enclosing tag
} }
void XmlRecorder::epilogue() void XmlRecorder::epilogue()
@ -538,8 +534,7 @@ XmlReplay::~XmlReplay()
} }
// Close out a substream and return its parent. // Close out a substream and return its parent.
// TODO: rename to closeSubstream for clarity XmlReplay* XmlReplay::closeSubstream()
XmlReplay* XmlReplay::close()
{ {
auto parent = m_parent; auto parent = m_parent;
delete this; delete this;
@ -725,7 +720,6 @@ void XmlReplayEvent::writeTag(QXmlStreamWriter & xml) const
#else #else
QString timestamp = time.toString(Qt::ISODateWithMs); QString timestamp = time.toString(Qt::ISODateWithMs);
#endif #endif
xml.writeStartElement(tag());
xml.writeAttribute("time", timestamp); xml.writeAttribute("time", timestamp);
// Call this event's overridable write method. // Call this event's overridable write method.
@ -734,6 +728,7 @@ void XmlReplayEvent::writeTag(QXmlStreamWriter & xml) const
QXmlStreamWriter & operator<<(QXmlStreamWriter & xml, const XmlReplayEvent & event) QXmlStreamWriter & operator<<(QXmlStreamWriter & xml, const XmlReplayEvent & event)
{ {
xml.writeStartElement(event.tag());
event.writeTag(xml); event.writeTag(xml);
xml.writeEndElement(); xml.writeEndElement();
return xml; return xml;
@ -1604,7 +1599,7 @@ qint64 SerialPortConnection::write(const char *data, qint64 maxSize)
if (!replayEvent) { if (!replayEvent) {
qWarning() << "writing data past replay"; qWarning() << "writing data past replay";
event.set("len", -1); event.set("len", -1);
event.set("error", QSerialPort::ReadError); event.set("error", QSerialPort::WriteError);
} }
bool ok; bool ok;
@ -1641,10 +1636,10 @@ void SerialPortConnection::close()
if (m_opened) { if (m_opened) {
// close event substream first // close event substream first
if (m_record) { if (m_record) {
m_record = m_record->close(); m_record = m_record->closeSubstream();
} }
if (m_replay) { if (m_replay) {
m_replay = m_replay->close(); m_replay = m_replay->closeSubstream();
} }
} }