mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-21 04:45:31 +08:00
refs #472 Solution for using enums with dbus.
This commit is contained in:
@@ -169,6 +169,19 @@ QVariant BlackMisc::fixQVariantFromDbusArgument(const QVariant &variant, int loc
|
|||||||
// complex Qt type, e.g. QDateTime
|
// complex Qt type, e.g. QDateTime
|
||||||
return complexQtTypeFromDbusArgument(arg, localUserType);
|
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
|
else
|
||||||
{
|
{
|
||||||
QVariant valueVariant(localUserType, nullptr);
|
QVariant valueVariant(localUserType, nullptr);
|
||||||
|
|||||||
@@ -99,20 +99,29 @@ namespace BlackMisc
|
|||||||
} // BlackMisc
|
} // BlackMisc
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Non-member non-friend operator for streaming enums to QDBusArgument.
|
* 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
|
|
||||||
*/
|
*/
|
||||||
template <class ENUM> typename std::enable_if<std::is_enum<ENUM>::value, QDBusArgument>::type const &
|
template <class E, typename std::enable_if<std::is_enum<E>::value, int>::type = 0>
|
||||||
operator>>(const QDBusArgument &argument, ENUM &enumType)
|
QDBusArgument &operator <<(QDBusArgument &arg, const E &value)
|
||||||
{
|
{
|
||||||
uint e;
|
arg.beginStructure();
|
||||||
argument >> e;
|
arg << static_cast<int>(value);
|
||||||
enumType = static_cast<ENUM>(e);
|
arg.endStructure();
|
||||||
return argument;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -320,6 +320,8 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
template <class T, typename std::enable_if<std::is_base_of<CEmpty, T>::value, int>::type = 0>
|
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); }
|
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>
|
template <class T>
|
||||||
static void marshallHelper(QDBusArgument &arg, const T &val, ...) { arg << val; }
|
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>
|
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); }
|
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>
|
template <class T>
|
||||||
static void unmarshallHelper(const QDBusArgument &arg, T &val, ...) { arg >> val; }
|
static void unmarshallHelper(const QDBusArgument &arg, T &val, ...) { arg >> val; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user