Created
November 28, 2018 11:52
-
-
Save stephenquan/339274af58bc09ce3885a0c2602755a7 to your computer and use it in GitHub Desktop.
qvariant_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <QObject> | |
#include <QVariant> | |
#include <QDebug> | |
using namespace std; | |
template <typename A> | |
A arg(const QVariant& arg) | |
{ | |
return arg.value<A>(); | |
} | |
template<> | |
int arg<int>(const QVariant& arg) | |
{ | |
bool ok = false; | |
int intVal = arg.toInt(&ok); | |
if (ok) return intVal; | |
double dblVal = arg.toDouble(&ok); | |
if (ok) return static_cast<int>(dblVal + 0.5); | |
return 0; | |
} | |
template <typename A> | |
QVariant makeReturn(const A& val) | |
{ | |
return QVariant(val); | |
} | |
template <typename A1, typename A2, typename RET> | |
QVariant add(const QVariant& a1, const QVariant& a2) | |
{ | |
return makeReturn<RET>(static_cast<RET>(arg<A1>(a1) + arg<A2>(a2))); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
Q_UNUSED(argc) | |
Q_UNUSED(argv) | |
qDebug() << add<int,int,int>("2.142857", "3.571428"); | |
qDebug() << add<int,double,int>("2.142857", "3.571428"); | |
qDebug() << add<int,double,double>("2.142857", "3.571428"); | |
qDebug() << add<double,double,double>("2.142857", "3.571428"); | |
//qDebug() << makeReturn<double>(3.14159265358979323); | |
//qDebug() << QVariant(3.14159265358979323); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment