Parcourir la source

Simplify service properties object.

ctkDictionary and ctkProperties are now both typedef for a QHash.
Sascha Zelzer il y a 12 ans
Parent
commit
17251bdd6d

+ 3 - 2
Libs/PluginFramework/CMakeLists.txt

@@ -9,8 +9,7 @@ set(KIT_export_directive "CTK_PLUGINFW_EXPORT")
 
 # Source files
 set(KIT_SRCS
-  ctkCaseInsensitiveString.cpp
-  ctkDictionary.cpp
+  ctkDictionary.h
   ctkLDAPExpr.cpp
   ctkLDAPExpr_p.h
   ctkLDAPSearchFilter.cpp
@@ -62,6 +61,8 @@ set(KIT_SRCS
   ctkServiceEvent.cpp
   ctkServiceException.cpp
   ctkServiceFactory.h
+  ctkServiceProperties_p.h
+  ctkServiceProperties.cpp
   ctkServiceReference.cpp
   ctkServiceReference_p.cpp
   ctkServiceRegistration.cpp

+ 0 - 95
Libs/PluginFramework/ctkCaseInsensitiveString.cpp

@@ -1,95 +0,0 @@
-/*=============================================================================
-
-  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 char* str)
-  : str(str)
-{
-}
-
-//----------------------------------------------------------------------------
-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());
-}
-
-//----------------------------------------------------------------------------
-QDataStream& operator<<(QDataStream &out, const ctkCaseInsensitiveString& str)
-{
-  out << QString(str);
-  return out;
-}
-
-//----------------------------------------------------------------------------
-QDataStream& operator>>(QDataStream &in, ctkCaseInsensitiveString& str)
-{
-  QString inStr;
-  in >> inStr;
-  str = inStr;
-  return in;
-}

+ 0 - 123
Libs/PluginFramework/ctkCaseInsensitiveString.h

@@ -1,123 +0,0 @@
-/*=============================================================================
-
-  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>
-
-/**
- * \ingroup PluginFramework
- *
- * ctkCaseInsensitiveString wraps a QString and can be
- * used in Qt container classes as a key type representing
- * case insensitive strings. However, case is preserved when
- * retrieving the actual QString.
- */
-class CTK_PLUGINFW_EXPORT ctkCaseInsensitiveString
-{
-
-public:
-
-  /**
-   * Wraps a null QString.
-   */
-  ctkCaseInsensitiveString();
-
-  /**
-   * Wraps the given character sequence.
-   *
-   * @param str The characters to be wrapped by this ctkCaseInsensitiveString
-   */
-  ctkCaseInsensitiveString(const char* str);
-
-  /**
-   * Wraps the given QString.
-   *
-   * @param str The QString to be wrapped by this ctkCaseInsensitiveString
-   */
-  ctkCaseInsensitiveString(const QString& str);
-
-  /**
-   * Copy constructor.
-   *
-   * @param str The ctkCaseInsensitiveString instance to copy.
-   */
-  ctkCaseInsensitiveString(const ctkCaseInsensitiveString& str);
-
-  /**
-   * Assignment operator.
-   *
-   * @param str The ctkCaseInsensitiveString instance which should be
-   *        assigned.
-   */
-  ctkCaseInsensitiveString& operator=(const ctkCaseInsensitiveString& str);
-
-  /**
-   * String comparison ignoring case.
-   *
-   * @param str The string with which to compare this instance.
-   * @return <code>true</code> if both strings are equal after being
-   *         converted to lower case strings, <code>false</code> otherwise.
-   */
-  bool operator==(const ctkCaseInsensitiveString& str) const;
-
-  /**
-   * Less than operator ignoring case.
-   *
-   * @param str The string with which to compare this instance.
-   * @return <code>true</code> if the lower case variant of the
-   *         current string is lexicographically less then
-   *         the lower case variant of <code>str</code>, <code>false</code>
-   *         otherwise.
-   */
-  bool operator<(const ctkCaseInsensitiveString& str) const;
-
-  /**
-   * Converts this ctkCaseInsensitiveString instance to a QString,
-   * preserving the original case.
-   */
-  operator QString() const;
-
-private:
-
-  QString str;
-};
-
-/**
- * \ingroup PluginFramework
- * @{
- *
- * Returns a hash value for the lower case string.
- *
- * @param str The string to be hashed.
- */
-uint CTK_PLUGINFW_EXPORT qHash(const ctkCaseInsensitiveString& str);
-
-CTK_PLUGINFW_EXPORT QDataStream& operator<<(QDataStream &out, const ctkCaseInsensitiveString& str);
-CTK_PLUGINFW_EXPORT QDataStream& operator>>(QDataStream &in, ctkCaseInsensitiveString& str);
-
-/** @}*/
-
-#endif // CTKCASEINSENSITIVESTRING_P_H

+ 0 - 54
Libs/PluginFramework/ctkDictionary.cpp

@@ -1,54 +0,0 @@
-/*=============================================================================
-
-  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 "ctkDictionary.h"
-
-#include <ctkException.h>
-
-//----------------------------------------------------------------------------
-ctkDictionary::ctkDictionary()
-{
-
-}
-
-//----------------------------------------------------------------------------
-ctkDictionary::ctkDictionary(const ctkDictionary& other)
-  : Super(other)
-{
-
-}
-
-//----------------------------------------------------------------------------
-ctkDictionary::ctkDictionary(const ctkProperties& properties)
-{
-  ctkProperties::ConstIterator end = properties.end();
-  for (ctkProperties::ConstIterator it = properties.begin(); it != end; ++it)
-  {
-    if (this->contains(it.key()))
-    {
-      QString msg("ctkProperties object contains case variants of the key: ");
-      msg += it.key();
-      throw ctkInvalidArgumentException(msg);
-    }
-    this->insert(it.key(), it.value());
-  }
-}

+ 3 - 29
Libs/PluginFramework/ctkDictionary.h

@@ -25,41 +25,15 @@
 
 #include <ctkPluginFrameworkExport.h>
 
-#include "ctkCaseInsensitiveString.h"
 #include "ctkPluginFramework_global.h"
 
 /**
  * \ingroup PluginFramework
  *
- * A QHash based dictionary class with case-insensitive keys. This class
- * uses ctkCaseInsensitiveString as key type and QVariant as values. Due
- * to the conversion capabilities of ctkCaseInsensitiveString, QString objects
- * can be used transparantly to insert or retrieve key-value pairs.
+ * A typedef for a QString to QVariant hash map. Typically used for service properties.
  *
- * <p>
- * Note that the case of the keys will be preserved.
+ * \note This is equivalent to using ctkProperties.
  */
-class CTK_PLUGINFW_EXPORT ctkDictionary : public QHash<ctkCaseInsensitiveString, QVariant>
-{
-
-public:
-
-  ctkDictionary();
-  ctkDictionary(const ctkDictionary& other);
-
-  /**
-   * Constructs a ctkDictionary object by using the entries of a
-   * ctkProperties object. The keys in <code>properties</code> must
-   * not contain case-variants of the same string.
-   *
-   * @param properties The ctkProperties object from which to copy the key-value pairs.
-   * @throws ctkInvalidArgumentException if <code>properties</code> contains case-variants of a key
-   */
-  ctkDictionary(const ctkProperties& properties);
-
-protected:
-
-  typedef QHash<ctkCaseInsensitiveString, QVariant> Super;
-};
+typedef QHash<QString,QVariant> ctkDictionary;
 
 #endif // CTKDICTIONARY_H

+ 5 - 3
Libs/PluginFramework/ctkLDAPExpr.cpp

@@ -281,11 +281,13 @@ bool ctkLDAPExpr::query( const QString &filter, const ctkDictionary &pd )
 }
 
 //----------------------------------------------------------------------------
-bool ctkLDAPExpr::evaluate( const ctkDictionary &p, bool matchCase ) const
+bool ctkLDAPExpr::evaluate( const ctkServiceProperties &p, bool matchCase ) const
 {
   if ((d->m_operator & SIMPLE) != 0) {
-    return compare(p[ matchCase ? d->m_attrName : d->m_attrName.toLower() ],
-      d->m_operator, d->m_attrValue);
+    // try case sensitive match first
+    int index = p.findCaseSensitive(d->m_attrName);
+    if (index < 0 && !matchCase) index = p.find(d->m_attrName);
+    return index < 0 ? false : compare(p.value(index), d->m_operator, d->m_attrValue);
   } else { // (d->m_operator & COMPLEX) != 0
     switch (d->m_operator) {
     case AND:

+ 7 - 7
Libs/PluginFramework/ctkLDAPExpr_p.h

@@ -23,7 +23,7 @@ limitations under the License.
 #define CTKLDAPEXPR_P_H
 
 #include "ctkPluginConstants.h"
-#include "ctkDictionary.h"
+#include "ctkServiceProperties_p.h"
 
 #include <QString>
 #include <QHash>
@@ -120,13 +120,13 @@ public:
    */
   bool isNull() const;
 
-  //! 
+  //!
   static bool query(const QString &filter, const ctkDictionary &pd);
 
   //! Evaluate this LDAP filter.
-  bool evaluate(const ctkDictionary &p, bool matchCase) const;
+  bool evaluate(const ctkServiceProperties &p, bool matchCase) const;
 
-  //! 
+  //!
   const QString toString() const;
 
 
@@ -149,13 +149,13 @@ private:
   //!
   bool compare(const QVariant &obj, int op, const QString &s) const;
 
-  //! 
+  //!
   static bool compareString(const QString &s1, int op, const QString &s2);
 
-  //! 
+  //!
   static QString fixupString(const QString &s);
 
-  //! 
+  //!
   static bool patSubstr(const QString &s, const QString &pat);
 
   //!

+ 1 - 0
Libs/PluginFramework/ctkPluginFramework_global.h

@@ -32,6 +32,7 @@
  * @{
  */
 typedef QHash<QString, QVariant> ctkProperties;
+typedef ctkProperties ctkDictionary;
 
 #if QT_VERSION < 0x040700
 #include <QSharedPointer>

+ 88 - 0
Libs/PluginFramework/ctkServiceProperties.cpp

@@ -0,0 +1,88 @@
+/*=============================================================================
+
+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 "ctkServiceProperties_p.h"
+
+#include <ctkException.h>
+
+//----------------------------------------------------------------------------
+ctkServiceProperties::ctkServiceProperties(const ctkProperties& props)
+{
+  for(ctkProperties::ConstIterator i = props.begin(), end = props.end();
+      i != end; ++i)
+  {
+    if (find(i.key()) != -1)
+    {
+      QString msg("ctkProperties object contains case variants of the key: ");
+      msg += i.key();
+      throw ctkInvalidArgumentException(msg);
+    }
+    ks.append(i.key());
+    vs.append(i.value());
+  }
+}
+
+//----------------------------------------------------------------------------
+QVariant ctkServiceProperties::value(const QString &key) const
+{
+  int index = find(key);
+  if (index < 0) return QVariant();
+  return vs[index];
+}
+
+//----------------------------------------------------------------------------
+QVariant ctkServiceProperties::value(int index) const
+{
+  return (index < 0 || index >= vs.size()) ? QVariant() : vs[index];
+}
+
+//----------------------------------------------------------------------------
+QStringList ctkServiceProperties::keys() const
+{
+  QStringList result;
+  for(int i = 0; i < ks.size(); ++i)
+  {
+    result.append(ks[i]);
+  }
+  return result;
+}
+
+//----------------------------------------------------------------------------
+int ctkServiceProperties::find(const QString &key) const
+{
+  for (int i = 0; i < ks.size(); ++i)
+  {
+    if (ks[i].compare(key, Qt::CaseInsensitive) == 0)
+      return i;
+  }
+  return -1;
+}
+
+//----------------------------------------------------------------------------
+int ctkServiceProperties::findCaseSensitive(const QString &key) const
+{
+  for (int i = 0; i < ks.size(); ++i)
+  {
+    if (ks[i] == key)
+      return i;
+  }
+  return -1;
+}

+ 54 - 0
Libs/PluginFramework/ctkServiceProperties_p.h

@@ -0,0 +1,54 @@
+/*=============================================================================
+
+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 CTKSERVICEPROPERTIES_P_H
+#define CTKSERVICEPROPERTIES_P_H
+
+#include <QVarLengthArray>
+#include <QVariant>
+
+#include "ctkPluginFramework_global.h"
+
+class ctkServiceProperties
+{
+
+private:
+
+  QVarLengthArray<QString,10> ks;
+  QVarLengthArray<QVariant,10> vs;
+
+  QMap<QString, QVariant> map;
+
+public:
+
+  ctkServiceProperties(const ctkProperties& props);
+
+  QVariant value(const QString& key) const;
+  QVariant value(int index) const;
+
+  int find(const QString& key) const;
+  int findCaseSensitive(const QString& key) const;
+
+  QStringList keys() const;
+
+};
+
+#endif // CTKSERVICEPROPERTIES_P_H

+ 1 - 6
Libs/PluginFramework/ctkServiceReference.cpp

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

+ 1 - 1
Libs/PluginFramework/ctkServiceReference_p.cpp

@@ -168,7 +168,7 @@ bool ctkServiceReferencePrivate::ungetService(QSharedPointer<ctkPlugin> plugin,
 }
 
 //----------------------------------------------------------------------------
-ctkDictionary ctkServiceReferencePrivate::getProperties() const
+const ctkServiceProperties& ctkServiceReferencePrivate::getProperties() const
 {
   return registration->properties;
 }

+ 2 - 2
Libs/PluginFramework/ctkServiceReference_p.h

@@ -26,7 +26,7 @@
 #include <QAtomicInt>
 #include <QSharedPointer>
 
-#include "ctkDictionary.h"
+#include "ctkServiceProperties_p.h"
 
 class QObject;
 
@@ -70,7 +70,7 @@ public:
    * @return A ctkDictionary object containing properties or being empty
    *         if service has been removed.
    */
-  ctkDictionary getProperties() const;
+  const ctkServiceProperties& getProperties() const;
 
   /**
    * Returns the property value to which the specified property key is mapped

+ 2 - 2
Libs/PluginFramework/ctkServiceRegistration_p.h

@@ -26,7 +26,7 @@
 #include <QHash>
 #include <QMutex>
 
-#include "ctkDictionary.h"
+#include "ctkServiceProperties_p.h"
 #include "ctkServiceReference.h"
 
 
@@ -75,7 +75,7 @@ public:
   /**
    * Service properties.
    */
-  ctkDictionary properties;
+  ctkServiceProperties properties;
 
   /**
    * Plugins dependent on this service. Integer is used as

+ 1 - 6
Libs/PluginFramework/service/event/ctkEvent.cpp

@@ -143,12 +143,7 @@ bool ctkEvent::containsProperty(const QString& name) const
 //----------------------------------------------------------------------------
 QStringList ctkEvent::getPropertyNames() const
 {
-  QStringList result;
-  foreach (ctkCaseInsensitiveString key, d->properties.keys())
-  {
-    result << key;
-  }
-  return result;
+  return d->properties.keys();
 }
 
 //----------------------------------------------------------------------------