Skip to content

Commit

Permalink
Fix crash on exit + minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov committed Jan 25, 2025
1 parent 77ebbd1 commit 9d6cbb8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DeviceBlock : public RobotsBlock

void run() override
{
const robotModel::DeviceInfo deviceInfo = robotModel::DeviceInfo::create<Device>();
const robotModel::DeviceInfo &deviceInfo = robotModel::DeviceInfo::create<Device>();
const QString portProperty = eval<QString>("Port");
const QString port = !portProperty.isEmpty() ? portProperty :
deviceInfo.name()[0].toUpper() + deviceInfo.name().mid(1) + "Port";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>

#include <QReadWriteLock>

#include "direction.h"
#include "kitBase/kitBaseDeclSpec.h"

Expand All @@ -30,12 +32,15 @@ namespace robotModel {
class ROBOTS_KIT_BASE_EXPORT DeviceInfo
{
public:
/// Constructs invalid DeviceInfo instance.
DeviceInfo();

/// Creates a new instance of a Device descriptor. The resulting object will
/// correspond to a given type only if Q_OBJECT macro is used inside its declaration.
/// @warning Given device type must contain friendlyName() and direction() static functions
/// and Q_OBJECT macro.
template <typename T>
static DeviceInfo create()
static DeviceInfo &create()
{
// This line performs Q_OBJECT macro checking in the given type declaration.
// Without Q_OBJECT macro incorrect metaObject will be passed and it will lead
Expand All @@ -47,15 +52,13 @@ class ROBOTS_KIT_BASE_EXPORT DeviceInfo
const bool simulated = property(metaObject, "simulated") == "true";
const Direction direction = property(metaObject, "direction").toLower() == "input" ? input : output;
DeviceInfo result(metaObject, name, friendlyName, simulated, direction);
mCreatedInfos[QString(metaObject->className())] = result;
return result;
QWriteLocker w(&mRWLock);
auto &r = mCreatedInfos[QString(metaObject->className())] = std::move(result);
return r;
}

/// Deserializes inner string representation obtained by toString().
static DeviceInfo fromString(const QString &string);

/// Constructs invalid DeviceInfo instance.
DeviceInfo();
static DeviceInfo &fromString(const QString &string);

/// Serializes given device info into inner string representation.
QString toString() const;
Expand Down Expand Up @@ -115,6 +118,7 @@ class ROBOTS_KIT_BASE_EXPORT DeviceInfo

static QString property(const QMetaObject * const metaObject, const QString &name);

static QReadWriteLock mRWLock;
static QMap<QString, DeviceInfo> mCreatedInfos;

const QMetaObject *mDeviceType;
Expand Down
16 changes: 8 additions & 8 deletions plugins/robots/common/kitBase/src/robotModel/deviceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

using namespace kitBase::robotModel;

QMap<QString, DeviceInfo> DeviceInfo::mCreatedInfos = QMap<QString, DeviceInfo>();
QMap<QString, DeviceInfo> DeviceInfo::mCreatedInfos = {{QString(), DeviceInfo()}};
QReadWriteLock DeviceInfo::mRWLock;


DeviceInfo::DeviceInfo()
: mDeviceType(nullptr)
Expand Down Expand Up @@ -90,17 +92,15 @@ QString DeviceInfo::toString() const
return QString(mDeviceType ? mDeviceType->className() : QString());
}

DeviceInfo DeviceInfo::fromString(const QString &string)
DeviceInfo &DeviceInfo::fromString(const QString &string)
{
if (string.isEmpty()) {
return DeviceInfo();
}

if (!mCreatedInfos.contains(string)) {
QReadLocker r(&mRWLock);
auto i = mCreatedInfos.find(string);
if (i == mCreatedInfos.end()) {
throw qReal::Exception(QString("QMetaObject for %1 not found").arg(string));
}

return mCreatedInfos[string];
return *i;
}

QString DeviceInfo::property(const QMetaObject * const metaObject, const QString &name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void SensorsConfiguration::deserialize(const QDomElement &element)

const PortInfo port = PortInfo::fromString(sensorNode.attribute("port"));

const DeviceInfo type = DeviceInfo::fromString(sensorNode.attribute("type"));
const DeviceInfo &type = DeviceInfo::fromString(sensorNode.attribute("type"));

const QString positionStr = sensorNode.attribute("position", "0:0");
const QStringList splittedStr = positionStr.split(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void TrikBrick::reset()
}

mTimers.clear();

Q_EMIT resetCompleted();
}

void TrikBrick::printToShell(const QString &msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<context>
<name>trik::TrikBrick</name>
<message>
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+82"/>
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+84"/>
<location line="+76"/>
<source>2d model shell part was not found</source>
<translation type="unfinished"></translation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<context>
<name>trik::TrikBrick</name>
<message>
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+82"/>
<location filename="../../../../plugins/robots/interpreters/trikKitInterpreterCommon/src/trikbrick.cpp" line="+84"/>
<location line="+76"/>
<source>2d model shell part was not found</source>
<translation>Консоль 2d модели не найдена</translation>
Expand Down

0 comments on commit 9d6cbb8

Please sign in to comment.