]> Creatis software - bbtk.git/blob - kernel/src/bbtkFactory.h
75acb76c5908c1f144237f86431375c9e81429f6
[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/07 08:40:14 $
7   Version:   $Revision: 1.10 $
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     /// The structure storing info on a package
103     class PackageInfoType
104     {
105     public :
106       /// Ctor
107       PackageInfoType() {}
108       /// Dtor
109       ~PackageInfoType() {}
110       /// The pointer on the package
111       Package* mPackage;
112       /// The handler of the dynamic library 
113       DynamicLibraryHandler mDynamicLibraryHandler;
114     };
115     /// The type of map of packages
116     typedef std::map< std::string, PackageInfoType > PackageMapType;
117     /// The map of packages
118     PackageMapType mPackageMap;
119
120     /// The executer which created the factory (if any)
121     Executer* mExecuter;
122
123     void CloseAllPackages();
124     void ClosePackage(PackageMapType::iterator& i);
125
126   };
127   // class Factory
128
129
130
131   /*
132   /// SYSTEM METHOD : Global method returning the global factory object pointer
133   inline Factory*& GlobalFactoryPointer() 
134   {
135     static Factory* f = 0;
136     return f;
137   }
138
139   /// SYSTEM METHOD : Global method returning the global factory object 
140   inline Factory* GetGlobalFactory() 
141   {
142     if (!GlobalFactoryPointer()) 
143     {
144        GlobalFactoryPointer() = new Factory;
145     }
146     return GlobalFactoryPointer();
147   }
148
149   /// SYSTEM METHOD : Deletes the global factory pointer
150   inline void DeleteGlobalFactory() 
151   {
152     if (GlobalFactoryPointer()) 
153     {
154       delete GlobalFactoryPointer();
155     }
156   }
157
158   inline void LoadPackage( const std::string& name )
159   {
160     GetGlobalFactory()->LoadPackage(name);
161   }
162
163   inline void UnLoadPackage( const std::string& name )
164   { 
165     GetGlobalFactory()->UnLoadPackage(name);
166   }
167
168   inline void PrintPackages(bool details = true, bool adaptors = false)
169   {
170     GetGlobalFactory()->PrintPackages(details,adaptors);
171   }
172
173   inline void HelpPackage(const std::string& name, bool adaptors = false) 
174   {
175     GetGlobalFactory()->HelpPackage(name,adaptors);
176   }
177   
178   inline void HelpBlackBox(const std::string& name, bool full=true) 
179   {
180     std::string package; 
181     GetGlobalFactory()->HelpBlackBox(name, package, full);
182   }
183
184   inline void HelpBlackBox(const std::string& name, std::string& package,
185                            bool full=true
186                            )
187   {
188     GetGlobalFactory()->HelpBlackBox(name, package, full);
189   }
190
191
192   inline void ShowGraphTypes(const std::string& name)
193   {
194     GetGlobalFactory()->ShowGraphTypes(name);
195   }
196
197   inline void InsertPackage( Package* p)
198   {
199     GetGlobalFactory()->InsertPackage(p);
200   }
201
202   inline void RemovePackage( Package* p)
203   {
204     GetGlobalFactory()->RemovePackage(p);
205   }
206   
207   inline const Package* GetPackage(const std::string& name) 
208   {
209     return GetGlobalFactory()->GetPackage(name);
210   }
211     
212   inline BlackBox* NewBlackBox(const std::string& type, 
213                                const std::string& name) 
214   {
215     return GetGlobalFactory()->NewBlackBox(type,name);
216   }
217     
218   inline BlackBox* NewAdaptor(TypeInfo typein,
219                               TypeInfo typeout,
220                               const std::string& name) 
221   {
222     return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
223   }
224   
225   inline Connection* NewConnection(BlackBox* from,
226                                    const std::string& output,
227                                    BlackBox* to,
228                                    const std::string& input) 
229   {
230     return GetGlobalFactory()->NewConnection(from,output,to,input);
231   }
232   
233   inline void WriteDotFilePackagesList(FILE *ff)
234   {
235     GetGlobalFactory()->WriteDotFilePackagesList(ff);
236   }
237   */
238 }// namespace bbtk
239
240
241
242 #endif
243