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 <QMap>
#include <QMetaEnum>
/*!
* \mainpage ScStw Libraries documentation
@ -222,6 +223,15 @@ public:
*/
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) {};
private:

View file

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