1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/***************************************************************************
* variant.cpp
* This file is part of the KDE project
* copyright (C)2004-2005 by Sebastian Sauer ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
***************************************************************************/
#include "variant.h"
#include "list.h"
#include <klocale.h>
using namespace Kross::Api;
Variant::Variant(const QVariant& value)
: Value<Variant, QVariant>(value)
{
}
Variant::~Variant()
{
}
const QString Variant::getClassName() const
{
return "Kross::Api::Variant";
}
const QString Variant::toString()
{
return getValue().toString();
}
/*
const QString Variant::getVariantType(Object::Ptr object)
{
switch( toVariant(object).type() ) {
case QVariant::CString:
case QVariant::String:
return "Kross::Api::Variant::String";
case QVariant::Map:
return "Kross::Api::Variant::Dict";
case QVariant::StringList:
case QVariant::List:
return "Kross::Api::Variant::List";
case QVariant::Double:
//return "Kross::Api::Variant::Double";
case QVariant::UInt:
//return "Kross::Api::Variant::UInt"; // python isn't able to differ between int and uint :-(
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Int:
return "Kross::Api::Variant::Integer";
case QVariant::Bool:
return "Kross::Api::Variant::Bool";
default: //Date, Time, DateTime, ByteArray, BitArray, Rect, Size, Color, Invalid, etc.
return "Kross::Api::Variant";
}
}
*/
const QVariant& Variant::toVariant(Object::Ptr object)
{
return Object::fromObject<Variant>( object.data() )->getValue();
}
const QString Variant::toString(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::String))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::String expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toString();
}
int Variant::toInt(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::Int))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Int expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toInt();
}
uint Variant::toUInt(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::UInt))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::UInt expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toUInt();
}
double Variant::toDouble(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::Double))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Double expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toDouble();
}
Q_LLONG Variant::toLLONG(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::LongLong))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::LLONG expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toLongLong();
}
Q_ULLONG Variant::toULLONG(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::ULongLong))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::ULLONG expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toULongLong();
}
bool Variant::toBool(Object::Ptr object)
{
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::Bool))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::Bool expected, but got %1.").arg(variant.typeName()).latin1()) );
return variant.toBool();
}
QStringList Variant::toStringList(Object::Ptr object)
{
List* list = dynamic_cast< List* >( object.data() );
if(list) {
QStringList l;
QValueList<Object::Ptr> valuelist = list->getValue();
QValueList<Object::Ptr>::Iterator it(valuelist.begin()), end(valuelist.end());
for(; it != end; ++it)
l.append( toString(*it) );
return l;
}
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::StringList))
throw Exception::Ptr( new Exception(QString("Kross::Api::Variant::StringList expected, but got '%1'.").arg(variant.typeName()).latin1()) );
return variant.toStringList();
}
QValueList<QVariant> Variant::toList(Object::Ptr object)
{
List* list = dynamic_cast< List* >( object.data() );
if(list) {
QValueList<QVariant> l;
QValueList<Object::Ptr> valuelist = list->getValue();
QValueList<Object::Ptr>::Iterator it(valuelist.begin()), end(valuelist.end());
for(; it != end; ++it)
l.append( toVariant(*it) );
return l;
}
const QVariant& variant = toVariant(object);
if(! variant.canCast(QVariant::List))
throw Exception::Ptr( new Exception(i18n("Kross::Api::Variant::List expected, but got '%1'.").arg(variant.typeName()).latin1()) );
return variant.toList();
}
|