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