refs #472 Solution for using enums with dbus.

This commit is contained in:
Mathew Sutcliffe
2015-09-26 21:34:38 +01:00
parent a7f8b54c43
commit 9b1e42d71a
3 changed files with 38 additions and 12 deletions

View File

@@ -169,6 +169,19 @@ QVariant BlackMisc::fixQVariantFromDbusArgument(const QVariant &variant, int loc
// complex Qt type, e.g. QDateTime
return complexQtTypeFromDbusArgument(arg, localUserType);
}
else if (QMetaType(localUserType).flags() & QMetaType::IsEnumeration)
{
arg.beginStructure();
int i;
arg >> i;
arg.endStructure();
QVariant valueVariant = QVariant::fromValue(i);
bool ok = valueVariant.convert(localUserType);
Q_ASSERT_X(ok, Q_FUNC_INFO, "int could not be converted to enum");
Q_UNUSED(ok);
return valueVariant;
}
else
{
QVariant valueVariant(localUserType, nullptr);

View File

@@ -99,20 +99,29 @@ namespace BlackMisc
} // BlackMisc
/*!
* Non-member non-friend operator for streaming enums to QDBusArgument.
*
* \param argument
* \param enumType
* \return
* \remarks Currently outside namespace for OSX build, see https://dev.vatsim-germany.org/issues/184
* Operator for streaming enums to QDBusArgument.
*/
template <class ENUM> typename std::enable_if<std::is_enum<ENUM>::value, QDBusArgument>::type const &
operator>>(const QDBusArgument &argument, ENUM &enumType)
template <class E, typename std::enable_if<std::is_enum<E>::value, int>::type = 0>
QDBusArgument &operator <<(QDBusArgument &arg, const E &value)
{
uint e;
argument >> e;
enumType = static_cast<ENUM>(e);
return argument;
arg.beginStructure();
arg << static_cast<int>(value);
arg.endStructure();
return arg;
}
/*!
* Operator for streaming enums from QDBusArgument.
*/
template <class E, typename std::enable_if<std::is_enum<E>::value, int>::type = 0>
const QDBusArgument &operator >>(const QDBusArgument &arg, E &value)
{
int temp;
arg.beginStructure();
arg >> temp;
arg.endStructure();
value = static_cast<E>(temp);
return arg;
}
/*!

View File

@@ -320,6 +320,8 @@ namespace BlackMisc
}
template <class T, typename std::enable_if<std::is_base_of<CEmpty, T>::value, int>::type = 0>
static void marshallHelper(QDBusArgument &arg, const T &val, int) { static_cast<const typename T::CValueObject &>(val).marshallToDbus(arg); }
template <class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
static void marshallHelper(QDBusArgument &arg, const T &val, int) { arg << static_cast<int>(val); }
template <class T>
static void marshallHelper(QDBusArgument &arg, const T &val, ...) { arg << val; }
@@ -332,6 +334,8 @@ namespace BlackMisc
}
template <class T, typename std::enable_if<std::is_base_of<CEmpty, T>::value, int>::type = 0>
static void unmarshallHelper(const QDBusArgument &arg, T &val, int) { static_cast<typename T::CValueObject &>(val).unmarshallFromDbus(arg); }
template <class T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
static void unmarshallHelper(const QDBusArgument &arg, T &val, int) { int i; arg >> i; val = static_cast<T>(i); }
template <class T>
static void unmarshallHelper(const QDBusArgument &arg, T &val, ...) { arg >> val; }