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