[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: Re: readEntry and booleans
From: Thomas Braxton <brax108 () cox ! net>
Date: 2006-01-04 20:34:21
Message-ID: 200601041434.21995.brax108 () cox ! net
[Download RAW message or body]
forgot to attach the diffs :)
in the second diff kconfigtest issues a warning about loss of information
instead of crashing.
["assert.diff" (text/x-diff)]
Index: kconfigbase.cpp
===================================================================
--- kconfigbase.cpp (revision 494324)
+++ kconfigbase.cpp (working copy)
@@ -1265,8 +1265,9 @@
case QVariant::String:
writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
return;
+ case QVariant::List:
+ Q_ASSERT(prop.canConvert(QVariant::StringList));
case QVariant::StringList:
- case QVariant::List:
writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
return;
case QVariant::ByteArray: {
Index: kconfigbase.h
===================================================================
--- kconfigbase.h (revision 494324)
+++ kconfigbase.h (working copy)
@@ -1590,6 +1590,9 @@
template <typename T>
QList<T> KConfigBase::readEntry( const char* pKey, const QList<T>& aDefault) const
{
+ QVariant::Type wanted = QVariant(T()).type();
+ Q_ASSERT(QVariant(QVariant::String).canConvert(wanted));
+
if (!hasKey(pKey))
return aDefault;
@@ -1603,8 +1606,10 @@
QList<T> list;
if (!vList.isEmpty()) {
- foreach (QVariant aValue, vList)
+ foreach (QVariant aValue, vList) {
+ Q_ASSERT(aValue.convert(wanted));
list.append( qvariant_cast<T>(aValue) );
+ }
}
return list;
@@ -1615,10 +1620,15 @@
bool bPersistent, bool bGlobal, bool bNLS )
{
QVariantList vList;
- foreach(T aValue, rValue)
- vList.append(aValue);
- writeEntry( pKey, QVariant(vList), bPersistent, bGlobal, bNLS );
+ if (!rValue.isEmpty()) {
+ Q_ASSERT(QVariant(rValue.first()).canConvert(QVariant::String));
+
+ foreach(T aValue, rValue)
+ vList.append(aValue);
+ }
+
+ writeEntry( pKey, vList, bPersistent, bGlobal, bNLS );
}
#ifdef KDE3_SUPPORT
Index: tests/kconfigtest.cpp
===================================================================
--- tests/kconfigtest.cpp (revision 494324)
+++ tests/kconfigtest.cpp (working copy)
@@ -45,7 +45,7 @@
#define COLORENTRY QColor("steelblue")
#define FONTENTRY QFont("Times", 16, QFont::Normal)
#define VARIANTLISTENTRY (QVariantList() << true << false << QString("joe") << 10023)
-
+#define VARIANTLISTENTRY2 (QVariantList() << POINTENTRY << SIZEENTRY)
void KConfigTest::initTestCase()
{
KConfig sc( "kconfigtest" );
@@ -86,6 +86,9 @@
sc.writeEntry( "listOfByteArraysEntry1", BYTEARRAYLISTENTRY1 );
sc.writeEntry( "stringListEntry", STRINGLISTENTRY );
sc.writeEntry( "variantListEntry", VARIANTLISTENTRY );
+
+ // This _should_ cause a crash
+// sc.writeEntry( "variantListEntry2", VARIANTLISTENTRY2 );
sc.sync();
}
["qwarning.diff" (text/x-diff)]
Index: kconfigbase.cpp
===================================================================
--- kconfigbase.cpp (revision 494324)
+++ kconfigbase.cpp (working copy)
@@ -1265,8 +1265,11 @@
case QVariant::String:
writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
return;
+ case QVariant::List:
+ if (!prop.canConvert(QVariant::StringList))
+ qWarning() << "not all types in" << pKey << "can convert to QString,"
+ " information will be lost" << endl;
case QVariant::StringList:
- case QVariant::List:
writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
return;
case QVariant::ByteArray: {
Index: kconfigbase.h
===================================================================
--- kconfigbase.h (revision 494324)
+++ kconfigbase.h (working copy)
@@ -23,6 +23,7 @@
#ifndef KCONFIGBASE_H
#define KCONFIGBASE_H
+#include <QtCore/QDebug>
#include <qvariant.h>
#include <kdelibs_export.h>
@@ -1590,6 +1591,11 @@
template <typename T>
QList<T> KConfigBase::readEntry( const char* pKey, const QList<T>& aDefault) const
{
+ QVariant::Type wanted = QVariant(T()).type();
+ if (!QVariant(QVariant::String).canConvert(wanted))
+ qWarning() << "QString cannot convert to " << QVariant::typeToName(wanted)
+ << " information will be lost" << endl;
+
if (!hasKey(pKey))
return aDefault;
@@ -1603,8 +1609,12 @@
QList<T> list;
if (!vList.isEmpty()) {
- foreach (QVariant aValue, vList)
+ foreach (QVariant aValue, vList) {
+ if (!aValue.convert(wanted))
+ qWarning() << "conversion to " << QVariant::typeToName(wanted)
+ << "information has been lost" << endl;
list.append( qvariant_cast<T>(aValue) );
+ }
}
return list;
@@ -1615,10 +1625,19 @@
bool bPersistent, bool bGlobal, bool bNLS )
{
QVariantList vList;
- foreach(T aValue, rValue)
- vList.append(aValue);
- writeEntry( pKey, QVariant(vList), bPersistent, bGlobal, bNLS );
+ if (!rValue.isEmpty()) {
+ QVariant dummy = rValue.first();
+ if (!dummy.canConvert(QVariant::String))
+ qWarning() << QVariant::typeToName(dummy.type())
+ << "cannot convert to QString information will be lost"
+ << endl;
+
+ foreach(T aValue, rValue)
+ vList.append(aValue);
+ }
+
+ writeEntry( pKey, vList, bPersistent, bGlobal, bNLS );
}
#ifdef KDE3_SUPPORT
Index: tests/kconfigtest.cpp
===================================================================
--- tests/kconfigtest.cpp (revision 494324)
+++ tests/kconfigtest.cpp (working copy)
@@ -45,7 +45,7 @@
#define COLORENTRY QColor("steelblue")
#define FONTENTRY QFont("Times", 16, QFont::Normal)
#define VARIANTLISTENTRY (QVariantList() << true << false << QString("joe") << 10023)
-
+#define VARIANTLISTENTRY2 (QVariantList() << POINTENTRY << SIZEENTRY)
void KConfigTest::initTestCase()
{
KConfig sc( "kconfigtest" );
@@ -86,6 +86,9 @@
sc.writeEntry( "listOfByteArraysEntry1", BYTEARRAYLISTENTRY1 );
sc.writeEntry( "stringListEntry", STRINGLISTENTRY );
sc.writeEntry( "variantListEntry", VARIANTLISTENTRY );
+
+ // This _should_ cause a crash
+ sc.writeEntry( "variantListEntry2", VARIANTLISTENTRY2 );
sc.sync();
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic