[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 1:24:42
Message-ID: 200601031924.42572.brax108 () cox ! net
[Download RAW message or body]

On Tuesday 03 January 2006 16:59, David Faure wrote:
> On Tuesday 03 January 2006 23:00, Cornelius Schumacher wrote:
> > compare
> >   readEntry( "key", QVariant( false ) ).toBool()
> > to
> >   readBoolEntry( "key", false )
>
> Since booleans convert so badly to QVariant, maybe we should keep
> readBoolEntry or add a readEntry( key, bool ) overload?

The problem wasn't with booleans, it was with zeroes. (i.e. 
false/int(0)/uint(0))

As far as I can tell this member template fixes everything
template <typename T> T readEntry(key, const T&) const

This seems to work, I ran unsermake check on kdecore and it passes.
The only problem seems to be, all uses of QVariant::Bool/Int must be replaced 
& toInt()/toBool() must be replaced to compile. All in all I think the code 
looks a lot cleaner. You pass in a bool and you get out a bool, so code that 
looked like this

if (sc.readEntry(key, QVariant::Bool).toBool())

now looks like this

if (sc.readEntry(key, false))

Regards,
Thomas

["diff" (text/x-diff)]

Index: kconfigbase.h
===================================================================
--- kconfigbase.h	(revision 494016)
+++ kconfigbase.h	(working copy)
@@ -186,6 +186,26 @@
    */
   QVariant readEntry( const char *pKey, const QVariant &aDefault) const;
 
+  /**
+   * Reads the value of an entry specified by @p pKey in the current group.
+   *
+   * @param pKey The key to search for.
+   * @param aDefault A default value returned if the key was not found.
+   * @return The value for this key, or @p aDefault.
+   * @since 4.0
+   */
+  template <typename T>
+  T readEntry( const char* pKey, const T& aDefault) const
+    { return qvariant_cast<T>(readEntry(pKey, QVariant(aDefault))); }
+
+  /**
+   * Reads the value of an entry specified by @p pKey in the current group.
+   * @copydoc readEntry(const char*, const T&) const
+   */
+  template <typename T>
+  T readEntry( const QString& pKey, const T& aDefault) const
+    { return qvariant_cast<T>(readEntry(pKey, QVariant(aDefault))); }
+
   // these two are here temporarily for porting, remove before KDE4
   QVariant readPropertyEntry( const QString& pKey, const QVariant& aDefault) const KDE_DEPRECATED;
   QVariant readPropertyEntry( const char *pKey, const QVariant& aDefault) const KDE_DEPRECATED;
Index: tests/kconfigtest.cpp
===================================================================
--- tests/kconfigtest.cpp	(revision 494016)
+++ tests/kconfigtest.cpp	(working copy)
@@ -141,8 +141,8 @@
   QCOMPARE( sc2.readEntry( "stringEntry5", QString("test") ), QString( "test" ) );
   QVERIFY( !sc2.hasKey( "stringEntry6" ) );
   QCOMPARE( sc2.readEntry( "stringEntry6", QString("foo") ), QString( "foo" ) );
-  QCOMPARE( sc2.readEntry( "boolEntry1", BOOLENTRY1 ).toBool(), BOOLENTRY1 );
-  QCOMPARE( sc2.readEntry( "boolEntry2", QVariant::Bool ).toBool(), BOOLENTRY2 );
+  QCOMPARE( sc2.readEntry( "boolEntry1", BOOLENTRY1 ), BOOLENTRY1 );
+  QCOMPARE( sc2.readEntry( "boolEntry2", false ), BOOLENTRY2 );
 
 #if 0
   QString s;
@@ -156,7 +156,7 @@
   }
 #endif
 
-  QCOMPARE( sc2.readEntry( "byteArrayEntry1", QVariant::ByteArray ).toByteArray(),
+  QCOMPARE( sc2.readEntry( "byteArrayEntry1", QByteArray() ),
             QByteArray( STRINGENTRY1 ) );
 }
 
@@ -183,15 +183,15 @@
   KConfig sc2( "kconfigtest" );
   sc2.setGroup("ComplexTypes");
 
-  QCOMPARE( sc2.readEntry( "pointEntry", QPoint() ).toPoint(), POINTENTRY );
-  QCOMPARE( sc2.readEntry( "sizeEntry", SIZEENTRY ).toSize(), SIZEENTRY);
-  QCOMPARE( sc2.readEntry( "rectEntry", QVariant::Rect ).toRect(), RECTENTRY );
-  QCOMPARE( sc2.readEntry( "dateTimeEntry", QDateTime() ).toString(),
+  QCOMPARE( sc2.readEntry( "pointEntry", QPoint() ), POINTENTRY );
+  QCOMPARE( sc2.readEntry( "sizeEntry", SIZEENTRY ), SIZEENTRY);
+  QCOMPARE( sc2.readEntry( "rectEntry", QRect(1,2,3,4) ), RECTENTRY );
+  QCOMPARE( sc2.readEntry( "dateTimeEntry", QDateTime() ).toString(Qt::ISODate),
             DATETIMEENTRY.toString(Qt::ISODate) );
-  QCOMPARE( sc2.readEntry( "dateTimeEntry", QDate() ).toString(),
+  QCOMPARE( sc2.readEntry( "dateTimeEntry", QDate() ).toString(Qt::ISODate),
             DATETIMEENTRY.date().toString(Qt::ISODate) );
-  QCOMPARE( sc2.readEntry( "colorEntry", QColor(Qt::black) ).toString(),
+  QCOMPARE( QVariant(sc2.readEntry( "colorEntry", QColor(Qt::black) )).toString(),
             QVariant(COLORENTRY).toString() );
   QCOMPARE( qvariant_cast<QColor>(sc2.readEntry( "colorEntry" )), COLORENTRY );
-  QCOMPARE( sc2.readEntry( "fontEntry", QVariant::Font ).toString(), QVariant(FONTENTRY).toString() );
+  QCOMPARE( sc2.readEntry( "fontEntry", QFont() ), FONTENTRY );
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic