CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkAbstractFactory.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Library: CTK
4 
5  Copyright (c) Kitware Inc.
6 
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __ctkAbstractFactory_h
22 #define __ctkAbstractFactory_h
23 
24 // Qt includes
25 #include <QString>
26 #include <QHash>
27 #include <QSharedPointer>
28 #include <QStringList>
29 
30 #ifdef _MSC_VER
33 # pragma warning(disable: 4505)
34 #endif
35 
36 //----------------------------------------------------------------------------
41 template<typename BaseClassType>
43 {
44 public:
45  //explicit ctkAbstractFactoryItem();
48 
49  virtual bool load() = 0;
50 
51  QStringList instantiateErrorStrings()const;
52  QStringList instantiateWarningStrings()const;
53 
54  QStringList loadErrorStrings()const;
55  QStringList loadWarningStrings()const;
56 
57  BaseClassType* instantiate();
58  bool isInstantiated()const;
59  BaseClassType* instance()const;
60  virtual void uninstantiate();
61 
62  void setVerbose(bool value);
63  bool verbose()const;
64 
65 protected:
66 
67  void appendInstantiateErrorString(const QString& msg);
69 
70  void appendInstantiateWarningString(const QString& msg);
72 
73  void appendLoadErrorString(const QString& msg);
75 
76  void appendLoadWarningString(const QString& msg);
78 
80  virtual BaseClassType* instanciator() = 0;
81  BaseClassType* Instance;
82 
83 private:
84  QStringList InstantiateErrorStrings;
85  QStringList InstantiateWarningStrings;
86  QStringList LoadErrorStrings;
87  QStringList LoadWarningStrings;
88  bool Verbose;
89 };
90 
91 //----------------------------------------------------------------------------
99 template<typename BaseClassType>
101 {
102 public:
103 
105 
109  virtual void printAdditionalInfo();
110 
113  virtual BaseClassType * instantiate(const QString& itemKey);
114 
117  virtual BaseClassType * instance(const QString& itemKey);
118 
121  void uninstantiate(const QString& itemKey);
122 
125  virtual QString path(const QString& itemKey){ Q_UNUSED(itemKey); return QString(); }
126 
127  void setSharedItems(const QSharedPointer<HashType>& items);
128  QSharedPointer<HashType> sharedItems();
129 
131  QStringList itemKeys() const;
132 
135  virtual void registerItems(){}
136 
139  void setVerbose(bool value);
140  bool verbose()const;
141 
142 protected:
143 
144  void displayStatusMessage(const QtMsgType& type, const QString& description,
145  const QString& status, bool display);
146 
149  bool registerItem(const QString& key, const QSharedPointer<ctkAbstractFactoryItem<BaseClassType> > & item);
150 
152  ctkAbstractFactoryItem<BaseClassType> * item(const QString& itemKey)const;
153 
154  ctkAbstractFactoryItem<BaseClassType> * sharedItem(const QString& itemKey)const;
155 
156  typedef typename HashType::const_iterator ConstIterator;
157  typedef typename HashType::iterator Iterator;
158 
159 private:
160  /*
161  ctkAbstractFactory(const ctkAbstractFactory &); /// Not implemented
162  void operator=(const ctkAbstractFactory&); /// Not implemented
163  */
164  HashType RegisteredItemMap;
165  QSharedPointer<HashType> SharedRegisteredItemMap;
166 
167  bool Verbose;
168 };
169 
170 #include "ctkAbstractFactory.tpp"
171 
172 #endif
QStringList instantiateErrorStrings() const
void appendLoadErrorString(const QString &msg)
BaseClassType * instance() const
bool isInstantiated() const
virtual bool load()=0
void clearInstantiateErrorStrings()
QStringList instantiateWarningStrings() const
QStringList loadWarningStrings() const
void appendInstantiateErrorString(const QString &msg)
virtual BaseClassType * instanciator()=0
Must be reimplemented in subclasses to instanciate a BaseClassType*.
void appendInstantiateWarningString(const QString &msg)
void clearInstantiateWarningStrings()
virtual ~ctkAbstractFactoryItem()
void appendLoadWarningString(const QString &msg)
BaseClassType * instantiate()
QStringList loadErrorStrings() const
void setVerbose(bool value)
virtual void uninstantiate()
ctkAbstractFactory is the base class of all the factory where items need to be registered before bein...
HashType::const_iterator ConstIterator
bool registerItem(const QString &key, const QSharedPointer< ctkAbstractFactoryItem< BaseClassType > > &item)
Call the load method associated with the item. If succesfully loaded, add it to the internal map.
void uninstantiate(const QString &itemKey)
Uninstanciate the object. Do nothing if the item given by the key has not be instantiated nor registe...
void setSharedItems(const QSharedPointer< HashType > &items)
virtual BaseClassType * instantiate(const QString &itemKey)
Create an instance of the object. The item corresponding to the key should have been registered befor...
void displayStatusMessage(const QtMsgType &type, const QString &description, const QString &status, bool display)
ctkAbstractFactory()
Constructor/Desctructor.
QStringList itemKeys() const
Get list of all registered item keys.
virtual BaseClassType * instance(const QString &itemKey)
Return the instance associated with itemKey if any, otherwise return 0.
bool verbose() const
void setVerbose(bool value)
Enabled verbose output Warning and error message will be printed to standard outputs.
virtual ~ctkAbstractFactory()
virtual void registerItems()
Register items with the factory Method provided for convenience - Should be overloaded in subclasse.
QHash< QString, QSharedPointer< ctkAbstractFactoryItem< BaseClassType > > > HashType
virtual QString path(const QString &itemKey)
Get path associated with the item identified by itemKey Should be overloaded in subclasse.
virtual void printAdditionalInfo()
HashType::iterator Iterator
ctkAbstractFactoryItem< BaseClassType > * item(const QString &itemKey) const
Get a Factory item given its itemKey. Return 0 if any.
QSharedPointer< HashType > sharedItems()
ctkAbstractFactoryItem< BaseClassType > * sharedItem(const QString &itemKey) const