Kaynağa Gözat

Make dictionary keys case insensitive in Plugin Framework.

Sascha Zelzer 14 yıl önce
ebeveyn
işleme
252d0da523

+ 1 - 0
Libs/PluginFramework/CMakeLists.txt

@@ -9,6 +9,7 @@ SET(KIT_export_directive "CTK_PLUGINFW_EXPORT")
   
 # Source files
 SET(KIT_SRCS
+  ctkCaseInsensitiveString.cpp
   ctkLDAPExpr.cpp
   ctkLDAPExpr_p.h
   ctkLDAPSearchFilter.cpp

+ 6 - 1
Libs/PluginFramework/EventBus/ctkEvent.cpp

@@ -112,7 +112,12 @@ QVariant ctkEvent::property(const QString& name) const
 
 QStringList ctkEvent::propertyNames() const
 {
-  return d->properties.keys();
+  QStringList result;
+  foreach (ctkCaseInsensitiveString key, d->properties.keys())
+  {
+    result << key;
+  }
+  return result;
 }
 
 const QString& ctkEvent::topic() const

+ 65 - 0
Libs/PluginFramework/ctkCaseInsensitiveString.cpp

@@ -0,0 +1,65 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkCaseInsensitiveString.h"
+
+#include <QHash>  // for qHash(const QString&)
+
+ctkCaseInsensitiveString::ctkCaseInsensitiveString()
+{
+}
+
+ctkCaseInsensitiveString::ctkCaseInsensitiveString(const QString& str)
+  : str(str)
+{
+}
+
+ctkCaseInsensitiveString::ctkCaseInsensitiveString(const ctkCaseInsensitiveString& str)
+  : str(str.str)
+{
+}
+
+ctkCaseInsensitiveString& ctkCaseInsensitiveString::operator=(const ctkCaseInsensitiveString& str)
+{
+  this->str = str.str;
+  return *this;
+}
+
+bool ctkCaseInsensitiveString::operator==(const ctkCaseInsensitiveString& str) const
+{
+  return this->str.toLower() == str.str.toLower();
+}
+
+bool ctkCaseInsensitiveString::operator<(const ctkCaseInsensitiveString& str) const
+{
+  return this->str.toLower() < str.str.toLower();
+}
+
+ctkCaseInsensitiveString::operator QString() const
+{
+  return this->str;
+}
+
+uint qHash(const ctkCaseInsensitiveString& str)
+{
+  return qHash(QString(str).toLower());
+}

+ 52 - 0
Libs/PluginFramework/ctkCaseInsensitiveString.h

@@ -0,0 +1,52 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#ifndef CTKCASEINSENSITIVESTRING_P_H
+#define CTKCASEINSENSITIVESTRING_P_H
+
+#include <QString>
+
+#include <ctkPluginFrameworkExport.h>
+
+class CTK_PLUGINFW_EXPORT ctkCaseInsensitiveString
+{
+
+public:
+
+  ctkCaseInsensitiveString();
+  ctkCaseInsensitiveString(const QString& str);
+  ctkCaseInsensitiveString(const ctkCaseInsensitiveString& str);
+
+  ctkCaseInsensitiveString& operator=(const ctkCaseInsensitiveString& str);
+  bool operator==(const ctkCaseInsensitiveString& str) const;
+  bool operator<(const ctkCaseInsensitiveString& str) const;
+
+  operator QString() const;
+
+private:
+
+  QString str;
+};
+
+uint CTK_PLUGINFW_EXPORT qHash(const ctkCaseInsensitiveString& str);
+
+#endif // CTKCASEINSENSITIVESTRING_P_H

+ 4 - 2
Libs/PluginFramework/ctkPluginFramework_global.h

@@ -26,8 +26,10 @@
 #include <QHash>
 #include <QStringList>
 
-typedef QHash<QString, QVariant> ServiceProperties;
-typedef QHash<QString, QVariant> ctkDictionary;
+#include "ctkCaseInsensitiveString.h"
+
+typedef QHash<ctkCaseInsensitiveString, QVariant> ServiceProperties;
+typedef QHash<ctkCaseInsensitiveString, QVariant> ctkDictionary;
 typedef QHash<QString, QVariant> ctkProperties;
 
 #if QT_VERSION < 0x040700

+ 6 - 1
Libs/PluginFramework/ctkServiceReference.cpp

@@ -61,7 +61,12 @@ QStringList ctkServiceReference::getPropertyKeys() const
 
   QMutexLocker lock(&d->registration->propsLock);
 
-  return d->registration->properties.keys();
+  QStringList result;
+  foreach (ctkCaseInsensitiveString key, d->registration->properties.keys())
+  {
+    result << key;
+  }
+  return result;
 }
 
 QSharedPointer<ctkPlugin> ctkServiceReference::getPlugin() const

+ 1 - 19
Libs/PluginFramework/ctkServices.cpp

@@ -48,25 +48,7 @@ ServiceProperties ctkServices::createServiceProperties(const ServiceProperties&
                                                        long sid)
 {
   static qlonglong nextServiceID = 1;
-  ServiceProperties props;
-
-  if (!in.isEmpty())
-  {
-    for (ServiceProperties::const_iterator it = in.begin(); it != in.end(); ++it)
-    {
-      const QString key = it.key();
-      const QString lcKey = it.key().toLower();
-      for (QListIterator<QString> i(props.keys()); i.hasNext(); )
-      {
-        if (lcKey == i.next())
-        {
-          throw std::invalid_argument(std::string("Several entries for property: ") + key.toStdString());
-        }
-      }
-
-      props.insert(lcKey, in.value(key));
-    }
-  }
+  ServiceProperties props = in;
 
   if (!classes.isEmpty())
   {