00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00031 #ifndef DB_PLANNER_QVARIANT_CONVERT_H_
00032 #define DB_PLANNER_QVARIANT_CONVERT_H_
00033
00034 #include <sstream>
00035 #include <vector>
00036 #include <QVariant>
00037 using std::istringstream;
00038 using std::vector;
00039
00040 namespace db_planner {
00041
00043
00045 template <class FieldType>
00046 static bool QVariantConvert(const QVariant& var, FieldType* result) {
00047 istringstream stream(var.toString().toStdString());
00048 stream >> *result;
00049 return true;
00050 }
00051
00053
00057 template <class FieldType>
00058 static bool QVariantConvert(const QVariant& var, vector<FieldType>* result) {
00059 std::string data = var.toString().toStdString();
00060 const size_t last_char = data.length() - 1;
00061 if (data.size() >= 2 && data[0] == '{' && data[last_char] == '}') {
00062 for (size_t i = 1; i < last_char; ++i) if (data[i] == ',') data[i] = ' ';
00063 istringstream stream(data.substr(1, last_char - 1));
00064 FieldType field;
00065 while(stream >> field) result->push_back(field);
00066 return true;
00067 }
00068 return false;
00069 }
00070
00071 }
00072
00073
00074 #endif // DB_PLANNER_QVARIANT_CONVERT_H_