]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.h
Started the devel of a wx package browser
[bbtk.git] / kernel / src / bbtkFactory.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkFactory.h,v $
5   Language:  C++
6   Date:      $Date: 2008/03/28 13:42:17 $
7   Version:   $Revision: 1.11 $
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 LoadPackage( const std::string& name );
50     void UnLoadPackage( const std::string& name );
51     void PrintPackages(bool details = true, bool adaptors = false) const;
52     void HelpPackage(const std::string& name, bool adaptors = false) const;
53     void HelpBlackBox(const std::string& name, std::string& package,
54                       bool full=true ) const;
55     void ShowGraphTypes(const std::string& name) const;
56     void InsertPackage( Package* );
57     void RemovePackage( Package* );
58
59     const Package* GetPackage(const std::string& name) const;
60     Package* GetPackage(const std::string& name);
61     
62     BlackBox* NewBlackBox(const std::string& type, 
63                           const std::string& name) const;
64     
65     BlackBox* NewAdaptor(TypeInfo typein,
66                          TypeInfo typeout,
67                          const std::string& name) const;
68     
69     Connection* NewConnection(BlackBox* from,
70                               const std::string& output,
71                               BlackBox* to,
72                               const std::string& input) const;
73
74     void WriteDotFilePackagesList(FILE *ff);
75
76     void Reset();
77     
78     typedef enum
79       {
80         Packages,
81         Categories,
82         Initials,
83         Adaptors
84       }
85       IndexEntryType;
86     void CreateHtmlIndex(IndexEntryType type, const std::string& filename);
87
88     /// Sets the executer who created the factory (if any)
89     void SetExecuter(Executer *e) { mExecuter = e; }
90     /// Gets the executer who created the factory (if any)
91     Executer* GetExecuter() { return mExecuter; }
92     /// Gets the executer who created the factory (if any) - const
93     const Executer* GetExecuter() const { return mExecuter; }
94
95
96   private:
97
98     bool DoLoadPackage(std::string libname,
99                        std::string pkgname,
100                        std::string path);
101
102   public:
103     /// The structure storing info on a package
104     class PackageInfoType
105     {
106     public :
107       /// Ctor
108       PackageInfoType() {}
109       /// Dtor
110       ~PackageInfoType() {}
111       /// The pointer on the package
112       Package* mPackage;
113       /// The handler of the dynamic library 
114       DynamicLibraryHandler mDynamicLibraryHandler;
115     };
116     /// The type of map of packages
117     typedef std::map< std::string, PackageInfoType > PackageMapType;
118
119     const PackageMapType& GetPackageMap() const { return mPackageMap; }
120
121   private:
122     /// The map of packages
123     PackageMapType mPackageMap;
124
125     /// The executer which created the factory (if any)
126     Executer* mExecuter;
127
128     void CloseAllPackages();
129     void ClosePackage(PackageMapType::iterator& i);
130
131   };
132   // class Factory
133
134
135
136   /*
137   /// SYSTEM METHOD : Global method returning the global factory object pointer
138   inline Factory*& GlobalFactoryPointer() 
139   {
140     static Factory* f = 0;
141     return f;
142   }
143
144   /// SYSTEM METHOD : Global method returning the global factory object 
145   inline Factory* GetGlobalFactory() 
146   {
147     if (!GlobalFactoryPointer()) 
148     {
149        GlobalFactoryPointer() = new Factory;
150     }
151     return GlobalFactoryPointer();
152   }
153
154   /// SYSTEM METHOD : Deletes the global factory pointer
155   inline void DeleteGlobalFactory() 
156   {
157     if (GlobalFactoryPointer()) 
158     {
159       delete GlobalFactoryPointer();
160     }
161   }
162
163   inline void LoadPackage( const std::string& name )
164   {
165     GetGlobalFactory()->LoadPackage(name);
166   }
167
168   inline void UnLoadPackage( const std::string& name )
169   { 
170     GetGlobalFactory()->UnLoadPackage(name);
171   }
172
173   inline void PrintPackages(bool details = true, bool adaptors = false)
174   {
175     GetGlobalFactory()->PrintPackages(details,adaptors);
176   }
177
178   inline void HelpPackage(const std::string& name, bool adaptors = false) 
179   {
180     GetGlobalFactory()->HelpPackage(name,adaptors);
181   }
182   
183   inline void HelpBlackBox(const std::string& name, bool full=true) 
184   {
185     std::string package; 
186     GetGlobalFactory()->HelpBlackBox(name, package, full);
187   }
188
189   inline void HelpBlackBox(const std::string& name, std::string& package,
190                            bool full=true
191                            )
192   {
193     GetGlobalFactory()->HelpBlackBox(name, package, full);
194   }
195
196
197   inline void ShowGraphTypes(const std::string& name)
198   {
199     GetGlobalFactory()->ShowGraphTypes(name);
200   }
201
202   inline void InsertPackage( Package* p)
203   {
204     GetGlobalFactory()->InsertPackage(p);
205   }
206
207   inline void RemovePackage( Package* p)
208   {
209     GetGlobalFactory()->RemovePackage(p);
210   }
211   
212   inline const Package* GetPackage(const std::string& name) 
213   {
214     return GetGlobalFactory()->GetPackage(name);
215   }
216     
217   inline BlackBox* NewBlackBox(const std::string& type, 
218                                const std::string& name) 
219   {
220     return GetGlobalFactory()->NewBlackBox(type,name);
221   }
222     
223   inline BlackBox* NewAdaptor(TypeInfo typein,
224                               TypeInfo typeout,
225                               const std::string& name) 
226   {
227     return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
228   }
229   
230   inline Connection* NewConnection(BlackBox* from,
231                                    const std::string& output,
232                                    BlackBox* to,
233                                    const std::string& input) 
234   {
235     return GetGlobalFactory()->NewConnection(from,output,to,input);
236   }
237   
238   inline void WriteDotFilePackagesList(FILE *ff)
239   {
240     GetGlobalFactory()->WriteDotFilePackagesList(ff);
241   }
242   */
243 }// namespace bbtk
244
245
246
247 #endif
248