]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.h
34e435d3082c8e30b12d146686cce34f573bda70
[bbtk.git] / kernel / src / bbtkFactory.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkFactory.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/08 06:59:30 $
7   Version:   $Revision: 1.12 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See doc/license.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *\file
20  *\brief Class bbtk::Factory : can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded.
21  */
22 /**
23  *\class bbtk::Factory
24  *\brief Can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded.
25  *
26  */
27
28
29 #ifndef __bbtkFactory_h__
30 #define __bbtkFactory_h__
31
32 //#include "bbtkBlackBox.h"
33 #include "bbtkPackage.h"
34 #include "bbtkDynamicLibraryHandling.h"
35
36 namespace bbtk
37 {
38
39   class Executer;
40
41   class BBTK_EXPORT Factory
42   {
43
44   public:
45
46     Factory();
47     ~Factory();
48     
49     void GetPackagesList(std::vector<std::string>&);
50     void LoadPackage( const std::string& name );
51     void UnLoadPackage( const std::string& name );
52     void PrintPackages(bool details = true, bool adaptors = false) const;
53     void HelpPackage(const std::string& name, bool adaptors = false) const;
54     void HelpBlackBox(const std::string& name, std::string& package,
55                       bool full=true ) const;
56     void ShowGraphTypes(const std::string& name) const;
57     void InsertPackage( Package* );
58     void RemovePackage( Package* );
59
60     const Package* GetPackage(const std::string& name) const;
61     Package* GetPackage(const std::string& name);
62     
63     BlackBox* NewBlackBox(const std::string& type, 
64                           const std::string& name) const;
65     
66     BlackBox* NewAdaptor(const DataInfo& typein,
67                          const DataInfo& typeout,
68                          const std::string& name) const;
69
70     BlackBox* NewWidgetAdaptor(const DataInfo& typein,
71                                const DataInfo& typeout,
72                                const std::string& name) const;
73
74     bool FindAdaptor(const DataInfo& typein,
75                      const DataInfo& typeout,
76                      std::string& adaptor) const;
77
78     bool FindWidgetAdaptor(const DataInfo& typein,
79                            const DataInfo& typeout,
80                            std::string& adaptor) const;
81     
82     Connection* NewConnection(BlackBox* from,
83                               const std::string& output,
84                               BlackBox* to,
85                               const std::string& input) const;
86
87     void WriteDotFilePackagesList(FILE *ff);
88
89     void Reset();
90     
91     typedef enum
92       {
93         Packages,
94         Categories,
95         Initials,
96         Adaptors
97       }
98       IndexEntryType;
99     void CreateHtmlIndex(IndexEntryType type, const std::string& filename);
100
101     /// Sets the executer who created the factory (if any)
102     void SetExecuter(Executer *e) { mExecuter = e; }
103     /// Gets the executer who created the factory (if any)
104     Executer* GetExecuter() { return mExecuter; }
105     /// Gets the executer who created the factory (if any) - const
106     const Executer* GetExecuter() const { return mExecuter; }
107
108
109   private:
110
111     bool DoLoadPackage(std::string libname,
112                        std::string pkgname,
113                        std::string path);
114
115   public:
116     /// The structure storing info on a package
117     class PackageInfoType
118     {
119     public :
120       /// Ctor
121       PackageInfoType() {}
122       /// Dtor
123       ~PackageInfoType() {}
124       /// The pointer on the package
125       Package* mPackage;
126       /// The handler of the dynamic library 
127       DynamicLibraryHandler mDynamicLibraryHandler;
128     };
129     /// The type of map of packages
130     typedef std::map< std::string, PackageInfoType > PackageMapType;
131
132     const PackageMapType& GetPackageMap() const { return mPackageMap; }
133
134   private:
135     /// The map of packages
136     PackageMapType mPackageMap;
137
138     /// The executer which created the factory (if any)
139     Executer* mExecuter;
140
141     void CloseAllPackages();
142     void ClosePackage(PackageMapType::iterator& i);
143
144   };
145   // class Factory
146
147
148
149   /*
150   /// SYSTEM METHOD : Global method returning the global factory object pointer
151   inline Factory*& GlobalFactoryPointer() 
152   {
153     static Factory* f = 0;
154     return f;
155   }
156
157   /// SYSTEM METHOD : Global method returning the global factory object 
158   inline Factory* GetGlobalFactory() 
159   {
160     if (!GlobalFactoryPointer()) 
161     {
162        GlobalFactoryPointer() = new Factory;
163     }
164     return GlobalFactoryPointer();
165   }
166
167   /// SYSTEM METHOD : Deletes the global factory pointer
168   inline void DeleteGlobalFactory() 
169   {
170     if (GlobalFactoryPointer()) 
171     {
172       delete GlobalFactoryPointer();
173     }
174   }
175
176   inline void LoadPackage( const std::string& name )
177   {
178     GetGlobalFactory()->LoadPackage(name);
179   }
180
181   inline void UnLoadPackage( const std::string& name )
182   { 
183     GetGlobalFactory()->UnLoadPackage(name);
184   }
185
186   inline void PrintPackages(bool details = true, bool adaptors = false)
187   {
188     GetGlobalFactory()->PrintPackages(details,adaptors);
189   }
190
191   inline void HelpPackage(const std::string& name, bool adaptors = false) 
192   {
193     GetGlobalFactory()->HelpPackage(name,adaptors);
194   }
195   
196   inline void HelpBlackBox(const std::string& name, bool full=true) 
197   {
198     std::string package; 
199     GetGlobalFactory()->HelpBlackBox(name, package, full);
200   }
201
202   inline void HelpBlackBox(const std::string& name, std::string& package,
203                            bool full=true
204                            )
205   {
206     GetGlobalFactory()->HelpBlackBox(name, package, full);
207   }
208
209
210   inline void ShowGraphTypes(const std::string& name)
211   {
212     GetGlobalFactory()->ShowGraphTypes(name);
213   }
214
215   inline void InsertPackage( Package* p)
216   {
217     GetGlobalFactory()->InsertPackage(p);
218   }
219
220   inline void RemovePackage( Package* p)
221   {
222     GetGlobalFactory()->RemovePackage(p);
223   }
224   
225   inline const Package* GetPackage(const std::string& name) 
226   {
227     return GetGlobalFactory()->GetPackage(name);
228   }
229     
230   inline BlackBox* NewBlackBox(const std::string& type, 
231                                const std::string& name) 
232   {
233     return GetGlobalFactory()->NewBlackBox(type,name);
234   }
235     
236   inline BlackBox* NewAdaptor(TypeInfo typein,
237                               TypeInfo typeout,
238                               const std::string& name) 
239   {
240     return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
241   }
242   
243   inline Connection* NewConnection(BlackBox* from,
244                                    const std::string& output,
245                                    BlackBox* to,
246                                    const std::string& input) 
247   {
248     return GetGlobalFactory()->NewConnection(from,output,to,input);
249   }
250   
251   inline void WriteDotFilePackagesList(FILE *ff)
252   {
253     GetGlobalFactory()->WriteDotFilePackagesList(ff);
254   }
255   */
256 }// namespace bbtk
257
258
259
260 #endif
261