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