plasma-framework/private/qtjolie-branch/qtjolie/value.cpp

144 lines
2.6 KiB
C++
Raw Normal View History

/**
* 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.
*/
#include "value.h"
#include <QtCore/QVariant>
#include "sodephelpers_p.h"
namespace Jolie
{
class ValuePrivate
{
public:
QVariant content;
QMap<QByteArray, QList<Value> > children;
};
} // namespace Jolie
using namespace Jolie;
Value::Value()
: d(new ValuePrivate)
{
}
Value::Value(const QByteArray &content)
: d(new ValuePrivate)
{
d->content = content;
}
Value::Value(qint32 content)
: d(new ValuePrivate)
{
d->content = content;
}
Value::Value(double content)
: d(new ValuePrivate)
{
d->content = content;
}
Value::Value(const Value &other)
: d(new ValuePrivate)
{
*d = *other.d;
}
Value::~Value()
{
delete d;
}
Value &Value::operator=(const Value &other)
{
*d = *other.d;
return *this;
}
QList<QByteArray> Value::childrenNames() const
{
return d->children.keys();
}
QList<Value> & Value::children(const QByteArray &name)
{
return d->children[name];
}
const QList<Value> & Value::children(const QByteArray &name) const
{
return d->children[name];
}
QByteArray Value::toByteArray() const
{
if (isByteArray()) {
return d->content.toByteArray();
} else {
return QByteArray();
}
}
qint32 Value::toInt() const
{
if (isInt()) {
return d->content.toInt();
} else {
return 0;
}
}
double Value::toDouble() const
{
if (isDouble()) {
return d->content.toDouble();
} else {
return 0.0;
}
}
bool Value::isByteArray() const
{
return d->content.type()==QVariant::ByteArray;
}
bool Value::isInt() const
{
return d->content.type()==QVariant::Int;
}
bool Value::isDouble() const
{
return d->content.type()==QVariant::Double;
}
bool Value::isValid() const
{
return isByteArray() || isInt() || isDouble();
}