1 /*=========================================================================
4 Module: $RCSfile: bbtkFactory.h,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1.1.1 $
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.
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.
17 =========================================================================*/
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.
24 *\brief Can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded.
29 #ifndef __bbtkFactory_h__
30 #define __bbtkFactory_h__
32 //#include "bbtkBlackBox.h"
33 #include "bbtkPackage.h"
34 #include "bbtkDynamicLibraryHandling.h"
41 class BBTK_EXPORT Factory
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* );
60 const Package* GetPackage(const std::string& name) const;
61 Package* GetPackage(const std::string& name);
63 BlackBox* NewBlackBox(const std::string& type,
64 const std::string& name) const;
66 BlackBox* NewAdaptor(TypeInfo typein,
68 const std::string& name) const;
70 Connection* NewConnection(BlackBox* from,
71 const std::string& output,
73 const std::string& input) const;
75 void WriteDotFilePackagesList(FILE *ff);
79 // usefull in many places.
80 // -> should be factorized : bbtk::Util class?
81 bool FileExists(std::string strFilename);
83 /// the methods for LoadPackage
84 std::string ExtractPackageName(const std::string &name,
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,
92 bool IsAtRoot(std::string cwd);
94 /// The structure storing info on a package
101 ~PackageInfoType() {}
102 /// The pointer on the package
104 /// The handler of the dynamic library
105 DynamicLibraryHandler mDynamicLibraryHandler;
107 /// The type of map of packages
108 typedef std::map< std::string, PackageInfoType > PackageMapType;
109 /// The map of packages
110 PackageMapType mPackageMap;
112 void CloseAllPackages();
113 void ClosePackage(PackageMapType::iterator& i);
119 /// SYSTEM METHOD : Global method returning the global factory object pointer
120 inline Factory*& GlobalFactoryPointer()
122 static Factory* f = 0;
126 /// SYSTEM METHOD : Global method returning the global factory object
127 inline Factory* GetGlobalFactory()
129 if (!GlobalFactoryPointer())
131 GlobalFactoryPointer() = new Factory;
133 return GlobalFactoryPointer();
136 /// SYSTEM METHOD : Deletes the global factory pointer
137 inline void DeleteGlobalFactory()
139 if (GlobalFactoryPointer())
141 delete GlobalFactoryPointer();
145 inline void LoadPackage( const std::string& name,
146 bool use_configuration_file = true, bool verbose = false )
148 GetGlobalFactory()->LoadPackage(name,use_configuration_file, verbose);
151 inline void UnLoadPackage( const std::string& name )
153 GetGlobalFactory()->UnLoadPackage(name);
156 inline void PrintPackages(bool details = true, bool adaptors = false)
158 GetGlobalFactory()->PrintPackages(details,adaptors);
161 inline void HelpPackage(const std::string& name, bool adaptors = false)
163 GetGlobalFactory()->HelpPackage(name,adaptors);
166 inline void HelpBlackBox(const std::string& name, bool full=true)
168 GetGlobalFactory()->HelpBlackBox(name,full);
172 inline void ShowGraphTypes(const std::string& name)
174 GetGlobalFactory()->ShowGraphTypes(name);
177 inline void InsertPackage( Package* p)
179 GetGlobalFactory()->InsertPackage(p);
182 inline void RemovePackage( Package* p)
184 GetGlobalFactory()->RemovePackage(p);
187 inline const Package* GetPackage(const std::string& name)
189 return GetGlobalFactory()->GetPackage(name);
192 inline BlackBox* NewBlackBox(const std::string& type,
193 const std::string& name)
195 return GetGlobalFactory()->NewBlackBox(type,name);
198 inline BlackBox* NewAdaptor(TypeInfo typein,
200 const std::string& name)
202 return GetGlobalFactory()->NewAdaptor(typein,typeout,name);
205 inline Connection* NewConnection(BlackBox* from,
206 const std::string& output,
208 const std::string& input)
210 return GetGlobalFactory()->NewConnection(from,output,to,input);
213 inline void WriteDotFilePackagesList(FILE *ff)
215 GetGlobalFactory()->WriteDotFilePackagesList(ff);