changed verification method of enum conversions

This commit is contained in:
Dorian Zedler 2020-05-27 12:35:01 +02:00
parent 81ee19819c
commit b2d8558f1f
Signed by: dorian
GPG key ID: 989DE36109AFA354
2 changed files with 22 additions and 33 deletions

View file

@ -3,6 +3,7 @@
#include <QObject> #include <QObject>
#include <QMap> #include <QMap>
#include <QMetaEnum>
/*! /*!
* \mainpage ScStw Libraries documentation * \mainpage ScStw Libraries documentation
@ -222,6 +223,15 @@ public:
*/ */
static int firmwareCompare(QString a, QString b); static int firmwareCompare(QString a, QString b);
/*!
* \brief Function to convert a value to an enum
*/
template <typename Enum>
static Enum toEnumValue(const int &value, bool *ok)
{
QMetaEnum enumeration = QMetaEnum::fromType<Enum>();
return static_cast<Enum>(enumeration.keyToValue(enumeration.valueToKey(value), ok));
}
ScStw() : QObject(nullptr) {}; ScStw() : QObject(nullptr) {};
private: private:

View file

@ -27,51 +27,30 @@ QString ScStw::baseStationSettingToString(ScStw::BaseStationSetting s) {
} }
ScStw::BaseStationSetting ScStw::baseStationSettingfromInt(int i) { ScStw::BaseStationSetting ScStw::baseStationSettingfromInt(int i) {
if(i < 0 || i > 4) bool ok;
BaseStationSetting s = ScStw::toEnumValue<ScStw::BaseStationSetting>(i, &ok);
if(!ok)
return InvalidSetting; return InvalidSetting;
else else
return BaseStationSetting(i); return s;
} }
ScStw::SignalKey ScStw::signalKeyFromInt(int i) { ScStw::SignalKey ScStw::signalKeyFromInt(int i) {
if(i < 9000 || i > 9003) bool ok;
ScStw::SignalKey k = ScStw::toEnumValue<ScStw::SignalKey>(i, &ok);
if(!ok)
return InvalidSignal; return InvalidSignal;
else else
return SignalKey(i); return k;
} }
ScStw::SocketCommand ScStw::socketCommandFromInt(int i) { ScStw::SocketCommand ScStw::socketCommandFromInt(int i) {
QList<SocketCommand> allCommands = { bool ok;
InitializeSessionCommand, SocketCommand c = ScStw::toEnumValue<ScStw::SocketCommand>(i, &ok);
if(!ok)
StartTimersCommand,
StopTimersCommand,
ResetTimersCommand,
GetRaceStateCommand,
GetNextStartActionCommand,
GetExtensionsCommand,
GetTimersCommand,
GetNextStartActionDetailsCommand,
WriteSettingCommand,
ReadSettingCommand,
LoginAthleteCommand,
CreateAthleteCommand,
DeleteAthleteCommand,
GetAtheletesCommand,
GetAthleteResultsCommand,
UpdateFirmwareCommand,
UpdateSystemTimeCommand,
PairExtensionsCommand
};
if(!allCommands.contains(SocketCommand(i)))
return InvalidCommand; return InvalidCommand;
else else
return SocketCommand(i); return c;
} }
int ScStw::firmwareCompare(QString a, QString b) { int ScStw::firmwareCompare(QString a, QString b) {