]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.h
*** empty log message ***
[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/09 11:16:57 $
7   Version:   $Revision: 1.13 $
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     bool FindWidgetAdaptor2(const DataInfo& typein,
83                             const DataInfo& typeout,
84                             std::string& widget,
85                             std::string& adaptor) const;
86
87     Connection* NewConnection(BlackBox* from,
88                               const std::string& output,
89                               BlackBox* to,
90                               const std::string& input) const;
91
92     void WriteDotFilePackagesList(FILE *ff);
93
94     void Reset();
95     
96     void CheckPackages() const;
97
98     typedef enum
99       {
100         Packages,
101         Categories,
102         Initials,
103         Adaptors
104       }
105       IndexEntryType;
106     void CreateHtmlIndex(IndexEntryType type, const std::string& filename);
107
108     /// Sets the executer who created the factory (if any)
109     void SetExecuter(Executer *e) { mExecuter = e; }
110     /// Gets the executer who created the factory (if any)
111     Executer* GetExecuter() { return mExecuter; }
112     /// Gets the executer who created the factory (if any) - const
113     const Executer* GetExecuter() const { return mExecuter; }
114
115
116   private:
117
118     bool DoLoadPackage(std::string libname,
119                        std::string pkgname,
120                        std::string path);
121
122   public:
123     /// The structure storing info on a package
124     class PackageInfoType
125     {
126     public :
127       /// Ctor
128       PackageInfoType() {}
129       /// Dtor
130       ~PackageInfoType() {}
131       /// The pointer on the package
132       Package* mPackage;
133       /// The handler of the dynamic library 
134       DynamicLibraryHandler mDynamicLibraryHandler;
135     };
136     /// The type of map of packages
137     typedef std::map< std::string, PackageInfoType > PackageMapType;
138
139     const PackageMapType& GetPackageMap() const { return mPackageMap; }
140
141   private:
142     /// The map of packages
143     PackageMapType mPackageMap;
144
145     /// The executer which created the factory (if any)
146     Executer* mExecuter;
147
148     void CloseAllPackages();
149     void ClosePackage(PackageMapType::iterator& i);
150
151   };
152   // class Factory
153
154
155
156   /*
157   /// SYSTEM METHOD : Global method returning the global factory object pointer
158   inline Factory*& GlobalFactoryPointer() 
159   {
160     static Factory* f = 0;
161     return f;
162   }
163
164   /// SYSTEM METHOD : Global method returning the global factory object 
165   inline Factory* GetGlobalFactory() 
166   {
167     if (!GlobalFactoryPointer()) 
168     {
169        GlobalFactoryPointer() = new Factory;
170     }
171     return GlobalFactoryPointer();
172   }
173
174   /// SYSTEM METHOD : Deletes the global factory pointer
175   inline void DeleteGlobalFactory() 
176   {
177     if (GlobalFactoryPointer()) 
178     {
179       delete GlobalFactoryPointer();
180     }
181   }
182
183   inline void LoadPackage( const std::string& name )
184   {
185     GetGlobalFactory()->LoadPackage(name);
186   }
187
188   inline void UnLoadPackage( const std::string& name )
189   { 
190     GetGlobalFactory()->UnLoadPackage(name);
191   }
192
193   inline void PrintPackages(bool details = true, bool adaptors = false)
194   {
195     GetGlobalFactory()->PrintPackages(details,adaptors);
196   }
197
198   inline void HelpPackage(const std::string& name, bool adaptors = false) 
199   {
200     GetGlobalFactory()->HelpPackage(name,adaptors);
201   }
202   
203   inline void HelpBlackBox(const std::string& name, bool full=true) 
204   {
205     std::string package; 
206     GetGlobalFactory()->HelpBlackBox(name, package, full);
207   }
208
209   inline void HelpBlackBox(const std::string& name, std::string& package,
210                            bool full=true
211                            )
212   {
213     GetGlobalFactory()->HelpBlackBox(name, package, full);
214   }
215
216
217   inline void ShowGraphTypes(const std::string& name)
218   {
219     GetGlobalFactory()->ShowGraphTypes(name);
220   }
221
222   inline void InsertPackage( Package* p)
223   {
224     GetGlobalFactory()->InsertPackage(p);
225   }
226
227   inline void RemovePackage( Package* p)
228   {
229     GetGlobalFactory()->RemovePackage(p);
230   }
231   
232   inline const Package* GetPackage(const std::string& name) 
233   {
234     return GetGlobalFactory()->GetPackage(name);
235   }
236     
237   inline BlackBox* NewBlackBox(const std::string& type, 
238                                const std::string& name) 
239   {
240     return GetGlobalFactory()->NewBlackBox(type,name);
241   }
242     
243   inline BlackBox* NewAdaptor(TypeInfo typein,
244                               TypeInfo typeout,
245                               const std::string& name) 
246   {
247     return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
248   }
249   
250   inline Connection* NewConnection(BlackBox* from,
251                                    const std::string& output,
252                                    BlackBox* to,
253                                    const std::string& input) 
254   {
255     return GetGlobalFactory()->NewConnection(from,output,to,input);
256   }
257   
258   inline void WriteDotFilePackagesList(FILE *ff)
259   {
260     GetGlobalFactory()->WriteDotFilePackagesList(ff);
261   }
262   */
263 }// namespace bbtk
264
265
266
267 #endif
268