2008-11-05 19:48:16 +01:00
|
|
|
/**
|
|
|
|
* This file is part of the KDE project
|
|
|
|
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
|
|
|
|
*
|
|
|
|
* This library 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 library 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 library; see the file COPYING.LIB. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "value.h"
|
2008-11-05 19:48:16 +01:00
|
|
|
|
|
|
|
#include <QtCore/QVariant>
|
|
|
|
|
|
|
|
#include "sodephelpers_p.h"
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#define QT_NO_CAST_FROM_ASCII
|
|
|
|
#define QT_NO_CAST_TO_ASCII
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
namespace Jolie
|
|
|
|
{
|
|
|
|
|
|
|
|
class ValuePrivate
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
QVariant content;
|
2009-06-29 18:57:58 +02:00
|
|
|
QMap<QByteArray, Value::List> children;
|
2008-11-05 19:48:16 +01:00
|
|
|
};
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
} // namespace Jolie
|
|
|
|
|
|
|
|
using namespace Jolie;
|
|
|
|
|
2009-06-05 16:11:02 +02:00
|
|
|
static int _qtjolie_valueTypeId = qRegisterMetaType<Value>();
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value::Value()
|
|
|
|
: d(new ValuePrivate)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
Value::Value(const QByteArray &content)
|
2009-04-25 15:50:35 +02:00
|
|
|
: d(new ValuePrivate)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
d->content = content;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value::Value(qint32 content)
|
|
|
|
: d(new ValuePrivate)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
d->content = content;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value::Value(double content)
|
|
|
|
: d(new ValuePrivate)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
d->content = content;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value::Value(const Value &other)
|
|
|
|
: d(new ValuePrivate)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
*d = *other.d;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value::~Value()
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value &Value::operator=(const Value &other)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
*d = *other.d;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QList<QByteArray> Value::childrenNames() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
return d->children.keys();
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:57:58 +02:00
|
|
|
Value::List &Value::children(const QByteArray &name)
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
return d->children[name];
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:57:58 +02:00
|
|
|
const Value::List &Value::children(const QByteArray &name) const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
return d->children[name];
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QByteArray Value::toByteArray() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
2009-05-30 10:44:34 +02:00
|
|
|
if (isByteArray()) {
|
|
|
|
return d->content.toByteArray();
|
2008-11-05 19:48:16 +01:00
|
|
|
} else {
|
2009-05-30 10:44:34 +02:00
|
|
|
return QByteArray();
|
2008-11-05 19:48:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
qint32 Value::toInt() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
if (isInt()) {
|
|
|
|
return d->content.toInt();
|
2009-06-06 00:44:43 +02:00
|
|
|
} else if (isByteArray()) {
|
|
|
|
return d->content.toByteArray().toInt();
|
2008-11-05 19:48:16 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
double Value::toDouble() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
if (isDouble()) {
|
|
|
|
return d->content.toDouble();
|
2009-06-06 00:44:43 +02:00
|
|
|
} else if (isByteArray()) {
|
|
|
|
return d->content.toByteArray().toInt();
|
2008-11-05 19:48:16 +01:00
|
|
|
} else {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
bool Value::isByteArray() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
2009-05-30 10:44:34 +02:00
|
|
|
return d->content.type()==QVariant::ByteArray;
|
2008-11-05 19:48:16 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
bool Value::isInt() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
return d->content.type()==QVariant::Int;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
bool Value::isDouble() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
|
|
|
return d->content.type()==QVariant::Double;
|
|
|
|
}
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
bool Value::isValid() const
|
2008-11-05 19:48:16 +01:00
|
|
|
{
|
2009-05-30 10:44:34 +02:00
|
|
|
return isByteArray() || isInt() || isDouble();
|
2008-11-05 19:48:16 +01:00
|
|
|
}
|
2009-04-25 15:50:35 +02:00
|
|
|
|