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