removing/simplifying uses of enable_if for JSON streaming operators

This commit is contained in:
Mathew Sutcliffe
2014-03-28 15:03:56 +00:00
parent 3ac59f0872
commit bfce2d186f
2 changed files with 143 additions and 68 deletions

View File

@@ -7,6 +7,115 @@
namespace BlackMisc namespace BlackMisc
{ {
const QJsonValue &operator >>(const QJsonValue &json, int &value)
{
value = json.toInt();
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, qlonglong &value)
{
value = static_cast<qlonglong>(json.toInt());
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, qulonglong &value)
{
value = static_cast<qulonglong>(json.toInt());
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, uint &value)
{
value = static_cast<uint>(json.toInt());
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, qint16 &value)
{
value = static_cast<qint16>(json.toInt());
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, QString &value)
{
value = json.toString();
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, double &value)
{
value = json.toDouble();
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, bool &value)
{
value = json.toBool();
return json;
}
const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value)
{
value = QDateTime::fromString(json.toString());
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value)
{
value = json.toInt();
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, qlonglong &value)
{
value = static_cast<qlonglong>(json.toInt());
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, qulonglong &value)
{
value = static_cast<qulonglong>(json.toInt());
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, uint &value)
{
value = static_cast<uint>(json.toInt());
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, qint16 &value)
{
value = static_cast<qint16>(json.toInt());
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, QString &value)
{
value = json.toString();
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, double &value)
{
value = json.toDouble();
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, bool &value)
{
value = json.toBool();
return json;
}
const QJsonValueRef &operator >>(const QJsonValueRef &json, QDateTime &value)
{
value = QDateTime::fromString(json.toString());
return json;
}
QJsonArray &operator<<(QJsonArray &json, const int value) QJsonArray &operator<<(QJsonArray &json, const int value)
{ {
json.append(QJsonValue(value)); json.append(QJsonValue(value));

View File

@@ -25,71 +25,27 @@
namespace BlackMisc namespace BlackMisc
{ {
//! \name Streaming operators for QJsonValue (to value) //! \name Streaming operators for QJsonValue (to value)
//! \remarks JSONVALUE: QJsonValue / QJsonValueRef //! \ingroup JSON
//! @{
template<class JSONVALUE> typename const QJsonValue &operator >>(const QJsonValue &json, int &value);
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type const QJsonValue &operator >>(const QJsonValue &json, qlonglong &value);
const &operator>>(const JSONVALUE &json, int &value) const QJsonValue &operator >>(const QJsonValue &json, qulonglong &value);
{ const QJsonValue &operator >>(const QJsonValue &json, uint &value);
value = json.toInt(); const QJsonValue &operator >>(const QJsonValue &json, qint16 &value);
return json; const QJsonValue &operator >>(const QJsonValue &json, QString &value);
} const QJsonValue &operator >>(const QJsonValue &json, double &value);
template<class JSONVALUE> typename const QJsonValue &operator >>(const QJsonValue &json, bool &value);
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value);
const &operator>>(const JSONVALUE &json, qlonglong &value) const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value);
{ const QJsonValueRef &operator >>(const QJsonValueRef &json, qlonglong &value);
value = static_cast<qlonglong>(json.toInt()); const QJsonValueRef &operator >>(const QJsonValueRef &json, qulonglong &value);
return json; const QJsonValueRef &operator >>(const QJsonValueRef &json, uint &value);
} const QJsonValueRef &operator >>(const QJsonValueRef &json, qint16 &value);
template<class JSONVALUE> typename const QJsonValueRef &operator >>(const QJsonValueRef &json, QString &value);
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type const QJsonValueRef &operator >>(const QJsonValueRef &json, double &value);
const &operator>>(const JSONVALUE &json, qulonglong &value) const QJsonValueRef &operator >>(const QJsonValueRef &json, bool &value);
{ const QJsonValueRef &operator >>(const QJsonValueRef &json, QDateTime &value);
value = static_cast<qulonglong>(json.toInt()); //! @}
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, uint &value)
{
value = static_cast<uint>(json.toInt());
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, qint16 &value)
{
value = static_cast<qint16>(json.toInt());
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, QString &value)
{
value = json.toString();
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, double &value)
{
value = json.toDouble();
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, bool &value)
{
value = json.toBool();
return json;
}
template<class JSONVALUE> typename
std::enable_if < std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value, JSONVALUE >::type
const &operator>>(const JSONVALUE &json, QDateTime &value)
{
value = QDateTime::fromString(json.toString());
return json;
}
//! \brief Specialized JSON serialization for enum //! \brief Specialized JSON serialization for enum
//! \remarks needs to be in global namespace //! \remarks needs to be in global namespace
@@ -104,9 +60,19 @@ namespace BlackMisc
//! \brief Specialized JSON deserialization for enum //! \brief Specialized JSON deserialization for enum
//! \ingroup JSON //! \ingroup JSON
template<class JSONVALUE, class ENUM> typename template<class ENUM> typename
std::enable_if < std::is_enum<ENUM>::value &&(std::is_same<JSONVALUE, QJsonValue>::value || std::is_same<JSONVALUE, QJsonValueRef>::value), JSONVALUE >::type std::enable_if<std::is_enum<ENUM>::value, QJsonValue>::type
const &operator>>(const JSONVALUE &json, ENUM &value) const &operator>>(const QJsonValue &json, ENUM &value)
{
value = static_cast<ENUM>(json.toInt());
return json;
}
//! \brief Specialized JSON deserialization for enum
//! \ingroup JSON
template<class ENUM> typename
std::enable_if<std::is_enum<ENUM>::value, QJsonValueRef>::type
const &operator>>(const QJsonValueRef &json, ENUM &value)
{ {
value = static_cast<ENUM>(json.toInt()); value = static_cast<ENUM>(json.toInt());
return json; return json;