ctkAbstractFactory.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 #ifndef __ctkAbstractFactory_h
00022 #define __ctkAbstractFactory_h
00023 
00024 // Qt includes
00025 #include <QString>
00026 #include <QHash>
00027 #include <QSharedPointer>
00028 #include <QStringList>
00029 
00030 #ifdef _MSC_VER
00033 # pragma warning(disable: 4505)
00034 #endif
00035 
00036 //----------------------------------------------------------------------------
00037 template<typename BaseClassType>
00038 class ctkAbstractFactoryItem
00039 {
00040 public:
00041   explicit ctkAbstractFactoryItem(const QString& key);
00042   
00043   virtual QString loadErrorString()const;
00044   virtual bool load() = 0;
00045   BaseClassType* instantiate();
00046   bool instantiated();
00047   QString key();
00048   virtual void uninstantiate();
00049   void setVerbose(bool value);
00050   bool verbose();
00051 protected:
00052   virtual BaseClassType* instanciator() = 0;
00053   BaseClassType* Instance;
00054 private:
00055   QString Key;
00056   bool Verbose;
00057 };
00058 
00059 //----------------------------------------------------------------------------
00060 template<typename BaseClassType>
00061 class ctkAbstractFactory
00062 {
00063 protected:
00064   typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::const_iterator ConstIterator;
00065   typedef typename QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > >::iterator       Iterator;
00066 
00067 public:
00070   explicit ctkAbstractFactory();
00071   virtual ~ctkAbstractFactory();
00072   virtual void printAdditionalInfo();
00073 
00076   virtual BaseClassType * instantiate(const QString& itemKey);
00077 
00080   void uninstantiate(const QString& itemKey);
00081 
00084   QStringList names() const;
00085 
00089   virtual void registerItems(){}
00090 
00093   void setVerbose(bool value);
00094   bool verbose();
00095 
00096 protected:
00097 
00101   bool registerItem(const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
00102 
00105   ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
00106 
00107 private:
00108   ctkAbstractFactory(const ctkAbstractFactory &); 
00109   void operator=(const ctkAbstractFactory&); 
00110 
00111   QHash<QString, QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > > RegisteredItemMap;
00112 
00113   bool Verbose;
00114 };
00115 
00116 #include "ctkAbstractFactory.tpp"
00117 
00118 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1