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

List:       kde-commits
Subject:    KDE/kdepimlibs/akonadi
From:       Constantin Berzan <exit3219 () gmail ! com>
Date:       2009-05-28 9:21:27
Message-ID: 1243502487.741181.1883.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 973946 by cberzan:

* Add a test for the behaviour of AttributeFactory.
* Don't crash when someone forgets to register an attribute.  Display a warning \
instead.

I had initially planned to put the warning in AttributeFactory::createAttribute(), \
but that resulted in a lot of noise since e.g. akonadiconsole uses unknown \
attributes.  So I put the warning when Entity::attribute() tries to cast it instead.


 M  +3 -1      attributefactory.h  
 M  +17 -4     entity.h  
 M  +1 -0      tests/CMakeLists.txt  
 A             tests/attributefactorytest.cpp   [License: LGPL (v2+)]
 A             tests/attributefactorytest.h   [License: LGPL (v2+)]


--- trunk/KDE/kdepimlibs/akonadi/attributefactory.h #973945:973946
@@ -53,7 +53,8 @@
     //@endcond
 
     /**
-     * Register a custom attribute of type T.
+     * Registers a custom attribute of type T.
+     * The same attribute cannot be registered more than once.
      */
     template <typename T> inline static void registerAttribute()
     {
@@ -62,6 +63,7 @@
 
     /**
      * Creates an entity attribute object of the given type.
+     * If the type has not been registered, creates a DefaultAttribute.
      *
      * @param type The attribute type.
      */
--- trunk/KDE/kdepimlibs/akonadi/entity.h #973945:973946
@@ -30,6 +30,8 @@
 
 #include <akonadi/attribute.h>
 
+#include <KDebug>
+
 #include <QtCore/QHash>
 #include <QtCore/QSharedDataPointer>
 
@@ -165,8 +167,13 @@
       Q_UNUSED( option );
 
       const T dummy;
-      if ( hasAttribute( dummy.type() ) )
-        return static_cast<T*>( attribute( dummy.type() ) );
+      if ( hasAttribute( dummy.type() ) ) {
+        T* attr = dynamic_cast<T*>( attribute( dummy.type() ) );
+        if ( attr )
+          return attr;
+        kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type()
+          << ". Did you forget to call AttributeFactory::registerAttribute()?";
+      }
 
       T* attr = new T();
       addAttribute( attr );
@@ -179,8 +186,14 @@
     template <typename T> inline T* attribute() const
     {
       const T dummy;
-      if ( hasAttribute( dummy.type() ) )
-        return static_cast<T*>( attribute( dummy.type() ) );
+      if ( hasAttribute( dummy.type() ) ) {
+        T* attr = dynamic_cast<T*>( attribute( dummy.type() ) );
+        if ( attr )
+          return attr;
+        kWarning( 5250 ) << "Found attribute of unknown type" << dummy.type()
+          << ". Did you forget to call AttributeFactory::registerAttribute()?";
+      }
+
       return 0;
     }
 
--- trunk/KDE/kdepimlibs/akonadi/tests/CMakeLists.txt #973945:973946
@@ -91,6 +91,7 @@
 # testrunner tests
 add_akonadi_isolated_test(testenvironmenttest.cpp)
 add_akonadi_isolated_test(autoincrementtest.cpp)
+add_akonadi_isolated_test(attributefactorytest.cpp)
 add_akonadi_isolated_test(collectionjobtest.cpp)
 add_akonadi_isolated_test(collectionpathresolvertest.cpp)
 add_akonadi_isolated_test(collectionattributetest.cpp)


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

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