]> 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/02/14 13:44:25 $
7   Version:   $Revision: 1.7 $
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   class BBTK_EXPORT Factory
39   {
40
41   public:
42
43     Factory();
44     ~Factory();
45
46     void LoadPackage( const std::string& name, 
47                       bool use_configuration_file = true );
48     void UnLoadPackage( const std::string& name );
49     void PrintPackages(bool details = true, bool adaptors = false) const;
50     void HelpPackage(const std::string& name, bool adaptors = false) const;
51     void HelpBlackBox(const std::string& name, std::string& package,
52                       bool full=true ) const;
53     void ShowGraphTypes(const std::string& name) const;
54     void InsertPackage( Package* );
55     void RemovePackage( Package* );
56
57     const Package* GetPackage(const std::string& name) const;
58     Package* GetPackage(const std::string& name);
59     
60     BlackBox* NewBlackBox(const std::string& type, 
61                           const std::string& name) const;
62     
63     BlackBox* NewAdaptor(TypeInfo typein,
64                          TypeInfo typeout,
65                          const std::string& name) const;
66     
67     Connection* NewConnection(BlackBox* from,
68                               const std::string& output,
69                               BlackBox* to,
70                               const std::string& input) const;
71
72     void WriteDotFilePackagesList(FILE *ff);
73
74     void Reset();
75     
76     typedef enum
77       {
78         Packages,
79         Categories,
80         Initials
81       }
82       IndexEntryType;
83     void CreateHtmlIndex(IndexEntryType type, const std::string& filename);
84
85   private:
86
87     bool DoLoadPackage(std::string libname,
88                        std::string pkgname,
89                        std::string path);
90
91     /// The structure storing info on a package
92     class PackageInfoType
93     {
94     public :
95       /// Ctor
96       PackageInfoType() {}
97       /// Dtor
98       ~PackageInfoType() {}
99       /// The pointer on the package
100       Package* mPackage;
101       /// The handler of the dynamic library 
102       DynamicLibraryHandler mDynamicLibraryHandler;
103     };
104     /// The type of map of packages
105     typedef std::map< std::string, PackageInfoType > PackageMapType;
106     /// The map of packages
107     PackageMapType mPackageMap;
108
109     void CloseAllPackages();
110     void ClosePackage(PackageMapType::iterator& i);
111
112   };
113   // class Factory
114
115
116   /// SYSTEM METHOD : Global method returning the global factory object pointer
117   inline Factory*& GlobalFactoryPointer() 
118   {
119     static Factory* f = 0;
120     return f;
121   }
122
123   /// SYSTEM METHOD : Global method returning the global factory object 
124   inline Factory* GetGlobalFactory() 
125   {
126     if (!GlobalFactoryPointer()) 
127     {
128        GlobalFactoryPointer() = new Factory;
129     }
130     return GlobalFactoryPointer();
131   }
132
133   /// SYSTEM METHOD : Deletes the global factory pointer
134   inline void DeleteGlobalFactory() 
135   {
136     if (GlobalFactoryPointer()) 
137     {
138       delete GlobalFactoryPointer();
139     }
140   }
141
142   inline void LoadPackage( const std::string& name, 
143                            bool use_configuration_file = true)
144   {
145     GetGlobalFactory()->LoadPackage(name,use_configuration_file);
146   }
147
148   inline void UnLoadPackage( const std::string& name )
149   { 
150     GetGlobalFactory()->UnLoadPackage(name);
151   }
152
153   inline void PrintPackages(bool details = true, bool adaptors = false)
154   {
155     GetGlobalFactory()->PrintPackages(details,adaptors);
156   }
157
158   inline void HelpPackage(const std::string& name, bool adaptors = false) 
159   {
160     GetGlobalFactory()->HelpPackage(name,adaptors);
161   }
162   
163   inline void HelpBlackBox(const std::string& name, bool full=true) 
164   {
165     std::string package; 
166     GetGlobalFactory()->HelpBlackBox(name, package, full);
167   }
168
169   inline void HelpBlackBox(const std::string& name, std::string& package,
170                            bool full=true
171                            )
172   {
173     GetGlobalFactory()->HelpBlackBox(name, package, full);
174   }
175
176
177   inline void ShowGraphTypes(const std::string& name)
178   {
179     GetGlobalFactory()->ShowGraphTypes(name);
180   }
181
182   inline void InsertPackage( Package* p)
183   {
184     GetGlobalFactory()->InsertPackage(p);
185   }
186
187   inline void RemovePackage( Package* p)
188   {
189     GetGlobalFactory()->RemovePackage(p);
190   }
191   
192   inline const Package* GetPackage(const std::string& name) 
193   {
194     return GetGlobalFactory()->GetPackage(name);
195   }
196     
197   inline BlackBox* NewBlackBox(const std::string& type, 
198                                const std::string& name) 
199   {
200     return GetGlobalFactory()->NewBlackBox(type,name);
201   }
202     
203   inline BlackBox* NewAdaptor(TypeInfo typein,
204                               TypeInfo typeout,
205                               const std::string& name) 
206   {
207     return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
208   }
209   
210   inline Connection* NewConnection(BlackBox* from,
211                                    const std::string& output,
212                                    BlackBox* to,
213                                    const std::string& input) 
214   {
215     return GetGlobalFactory()->NewConnection(from,output,to,input);
216   }
217   
218   inline void WriteDotFilePackagesList(FILE *ff)
219   {
220     GetGlobalFactory()->WriteDotFilePackagesList(ff);
221   }
222
223 }// namespace bbtk
224
225
226
227 #endif
228