Kaynağa Gözat

Introduces FileBased factory

Factored out functionality common to Library and Plugin factory.
Jean-Christophe Fillion-Robin 14 yıl önce
ebeveyn
işleme
5e9920942b

+ 2 - 0
Libs/Core/CMakeLists.txt

@@ -16,6 +16,8 @@ SET(KIT_export_directive "CTK_CORE_EXPORT")
 SET(KIT_SRCS
   ctkAbstractFactory.h
   ctkAbstractFactory.tpp
+  ctkAbstractFileBasedFactory.h
+  ctkAbstractFileBasedFactory.tpp
   ctkAbstractObjectFactory.h
   ctkAbstractObjectFactory.tpp
   ctkAbstractPluginFactory.h

+ 65 - 0
Libs/Core/ctkAbstractFileBasedFactory.h

@@ -0,0 +1,65 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  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.commontk.org/LICENSE
+
+  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 __ctkAbstractFileBasedFactory_h
+#define __ctkAbstractFileBasedFactory_h
+
+// Qt includes
+#include <QFileInfo>
+#include <QStringList>
+
+// CTK includes
+#include "ctkAbstractFactory.h"
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
+class ctkAbstractFactoryFileBasedItem : public ctkAbstractFactoryItem<BaseClassType>
+{
+public:
+  explicit ctkAbstractFactoryFileBasedItem(const QString& path);
+ 
+  /// Get path associated with the object identified by \a key
+  QString path()const;
+
+private:
+  QString               Path;
+};
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
+class ctkAbstractFileBasedFactory : public ctkAbstractFactory<BaseClassType>
+{
+public:
+
+  /// Constructor
+  explicit ctkAbstractFileBasedFactory();
+  virtual ~ctkAbstractFileBasedFactory();
+
+  /// Get path associated with the library identified by \a key
+  virtual QString path(const QString& key);
+
+private:
+  ctkAbstractFileBasedFactory(const ctkAbstractFileBasedFactory &);  /// Not implemented
+  void operator=(const ctkAbstractFileBasedFactory&); /// Not implemented
+};
+
+#include "ctkAbstractFileBasedFactory.tpp"
+
+#endif

+ 71 - 0
Libs/Core/ctkAbstractFileBasedFactory.tpp

@@ -0,0 +1,71 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  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.commontk.org/LICENSE
+
+  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 __ctkAbstractFileBasedFactory_tpp
+#define __ctkAbstractFileBasedFactory_tpp
+
+// CTK includes
+#include "ctkAbstractFileBasedFactory.h"
+
+//----------------------------------------------------------------------------
+// ctkFactoryFileBasedItem methods
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
+ctkAbstractFactoryFileBasedItem<BaseClassType>::ctkAbstractFactoryFileBasedItem(const QString& _path)
+  :ctkAbstractFactoryItem<BaseClassType>()
+  ,Path(_path)
+{
+}
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
+QString ctkAbstractFactoryFileBasedItem<BaseClassType>::path()const
+{ 
+  return this->Path; 
+}
+
+//----------------------------------------------------------------------------
+// ctkAbstractFileBasedFactory methods
+
+//-----------------------------------------------------------------------------
+template<typename BaseClassType>
+ctkAbstractFileBasedFactory<BaseClassType>::ctkAbstractFileBasedFactory()
+  :ctkAbstractFactory<BaseClassType>()
+{
+}
+  
+//-----------------------------------------------------------------------------
+template<typename BaseClassType>
+ctkAbstractFileBasedFactory<BaseClassType>::~ctkAbstractFileBasedFactory()
+{
+}
+
+//----------------------------------------------------------------------------
+template<typename BaseClassType>
+QString ctkAbstractFileBasedFactory<BaseClassType>::path(const QString& key)
+{
+  ctkAbstractFactoryFileBasedItem<BaseClassType>* _item =
+      dynamic_cast<ctkAbstractFactoryFileBasedItem<BaseClassType>*>(this->item(key));
+  Q_ASSERT(_item);
+  return _item->path();
+}
+
+#endif

+ 3 - 8
Libs/Core/ctkAbstractLibraryFactory.h

@@ -27,11 +27,11 @@
 #include <QStringList>
 
 // CTK includes
-#include "ctkAbstractFactory.h"
+#include "ctkAbstractFileBasedFactory.h"
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-class ctkFactoryLibraryItem : public ctkAbstractFactoryItem<BaseClassType>
+class ctkFactoryLibraryItem : public ctkAbstractFactoryFileBasedItem<BaseClassType>
 {
 protected:
   typedef typename QHash<QString, void*>::const_iterator ConstIterator;
@@ -41,7 +41,6 @@ public:
   explicit ctkFactoryLibraryItem(const QString& path);
  
   virtual bool load();
-  QString path()const;
   virtual QString loadErrorString()const;
 
   ///
@@ -60,14 +59,13 @@ public:
 
 private:
   QLibrary              Library;
-  QString               Path;
   QHash<QString, void*> ResolvedSymbols;
   QStringList           Symbols;
 };
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-class ctkAbstractLibraryFactory : public ctkAbstractFactory<BaseClassType>
+class ctkAbstractLibraryFactory : public ctkAbstractFileBasedFactory<BaseClassType>
 {
 public:
   /// 
@@ -86,9 +84,6 @@ public:
   /// The parameter \a key must be unique
   bool registerQLibrary(const QString& key, const QFileInfo& file);
 
-  /// Get path associated with the library identified by \a key
-  virtual QString path(const QString& key);
-
 protected:
   virtual ctkFactoryLibraryItem<BaseClassType>* createFactoryLibraryItem(
     const QFileInfo& library)const;

+ 3 - 21
Libs/Core/ctkAbstractLibraryFactory.tpp

@@ -30,8 +30,7 @@
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
 ctkFactoryLibraryItem<BaseClassType>::ctkFactoryLibraryItem(const QString& _path)
-  :ctkAbstractFactoryItem<BaseClassType>()
-  ,Path(_path)
+  :ctkAbstractFactoryFileBasedItem<BaseClassType>(_path)
 {
 }
 
@@ -54,13 +53,6 @@ bool ctkFactoryLibraryItem<BaseClassType>::load()
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-QString ctkFactoryLibraryItem<BaseClassType>::path()const
-{ 
-  return this->Path; 
-}
-
-//----------------------------------------------------------------------------
-template<typename BaseClassType>
 QString ctkFactoryLibraryItem<BaseClassType>::loadErrorString()const
 {
   return this->Library.errorString();
@@ -90,7 +82,7 @@ bool ctkFactoryLibraryItem<BaseClassType>::resolve()
       {
       if (this->verbose())
         {
-        qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->Path;
+        qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->path();
         }
       continue;
       }
@@ -125,7 +117,7 @@ void* ctkFactoryLibraryItem<BaseClassType>::symbolAddress(const QString& symbol)
 //-----------------------------------------------------------------------------
 template<typename BaseClassType>
 ctkAbstractLibraryFactory<BaseClassType>::ctkAbstractLibraryFactory()
-  :ctkAbstractFactory<BaseClassType>()
+  :ctkAbstractFileBasedFactory<BaseClassType>()
 {
 }
   
@@ -178,16 +170,6 @@ bool ctkAbstractLibraryFactory<BaseClassType>::registerQLibrary(
   return this->registerLibrary(key, file);
 }
 
-//----------------------------------------------------------------------------
-template<typename BaseClassType>
-QString ctkAbstractLibraryFactory<BaseClassType>::path(const QString& key)
-{
-  ctkFactoryLibraryItem<BaseClassType>* _item =
-      dynamic_cast<ctkFactoryLibraryItem<BaseClassType>*>(this->item(key));
-  Q_ASSERT(_item);
-  return _item->path();
-}
-
 //-----------------------------------------------------------------------------
 template<typename BaseClassType>
 ctkFactoryLibraryItem<BaseClassType>* ctkAbstractLibraryFactory<BaseClassType>::

+ 3 - 8
Libs/Core/ctkAbstractPluginFactory.h

@@ -26,16 +26,15 @@
 #include <QFileInfo>
 
 // CTK includes
-#include "ctkAbstractFactory.h"
+#include "ctkAbstractFileBasedFactory.h"
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-class ctkFactoryPluginItem : public ctkAbstractFactoryItem<BaseClassType>
+class ctkFactoryPluginItem : public ctkAbstractFactoryFileBasedItem<BaseClassType>
 {
 public:
   explicit ctkFactoryPluginItem(const QString& path);
   virtual bool load();
-  QString path()const;
   virtual QString loadErrorString()const;
 
 protected:
@@ -43,12 +42,11 @@ protected:
 
 private:
   QPluginLoader    Loader;
-  QString          Path;
 };
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-class ctkAbstractPluginFactory : public ctkAbstractFactory<BaseClassType>
+class ctkAbstractPluginFactory : public ctkAbstractFileBasedFactory<BaseClassType>
 {
 public:
   /// Constructor
@@ -57,9 +55,6 @@ public:
   /// Register a plugin in the factory
   virtual bool registerLibrary(const QString& key, const QFileInfo& file);
 
-  /// Get path associated with the plugin identified by \a key
-  virtual QString path(const QString& key);
-
 protected:
   virtual ctkAbstractFactoryItem<BaseClassType>* createFactoryPluginItem(const QFileInfo& file);
 

+ 2 - 19
Libs/Core/ctkAbstractPluginFactory.tpp

@@ -34,7 +34,7 @@
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
 ctkFactoryPluginItem<BaseClassType>::ctkFactoryPluginItem(const QString& _path)
-  :Path(_path)
+  :ctkAbstractFactoryFileBasedItem<BaseClassType>(_path)
 {
 }
 
@@ -48,13 +48,6 @@ bool ctkFactoryPluginItem<BaseClassType>::load()
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-QString ctkFactoryPluginItem<BaseClassType>::path()const
-{ 
-  return this->Path; 
-}
-
-//----------------------------------------------------------------------------
-template<typename BaseClassType>
 QString ctkFactoryPluginItem<BaseClassType>::loadErrorString()const
 { 
   return this->Loader.errorString();
@@ -94,7 +87,7 @@ BaseClassType* ctkFactoryPluginItem<BaseClassType>::instanciator()
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
 ctkAbstractPluginFactory<BaseClassType>::ctkAbstractPluginFactory()
-:ctkAbstractFactory<BaseClassType>()
+:ctkAbstractFileBasedFactory<BaseClassType>()
 {
 }
 
@@ -121,16 +114,6 @@ bool ctkAbstractPluginFactory<BaseClassType>::registerLibrary(const QString& key
 
 //----------------------------------------------------------------------------
 template<typename BaseClassType>
-QString ctkAbstractPluginFactory<BaseClassType>::path(const QString& key)
-{
-  ctkFactoryPluginItem<BaseClassType>* _item =
-      dynamic_cast<ctkFactoryPluginItem<BaseClassType>*>(this->item(key));
-  Q_ASSERT(_item);
-  return _item->path();
-}
-
-//----------------------------------------------------------------------------
-template<typename BaseClassType>
 ctkAbstractFactoryItem<BaseClassType>* ctkAbstractPluginFactory<BaseClassType>::createFactoryPluginItem(const QFileInfo& file)
 {
   return new ctkFactoryPluginItem<BaseClassType>(file.filePath());