1 /*=========================================================================
4 Module: $RCSfile: bbtkFactory.cxx,v $
7 Date: $Date: 2008/01/22 16:55:04 $
8 Version: $Revision: 1.2 $
11 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
12 l'Image). All rights reserved. See doc/license.txt or
13 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
15 This software is distributed WITHOUT ANY WARRANTY; without even
16 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 PURPOSE. See the above copyright notices for more information.
19 =========================================================================*/
22 *\brief Class bbtk::Factory : can load and unload dynamic libraries containing
23 * black boxes packages and create instances of the black boxes registered
24 * in the packages loaded.
26 #include "bbtkFactory.h"
27 #include "bbtkMessageManager.h"
28 #include "bbtkConnection.h"
29 #include "bbtkConfigurationFile.h"
30 #include "bbtkUtilities.h"
32 #include <sys/stat.h> // for struct stat stFileInfo
35 #include <direct.h> // for getcwd
42 # define getcwd _getcwd
45 #if defined(_MSC_VER) || defined(__BORLANDC__)
56 typedef Package* (*PackageAccessor)();
57 typedef void (*PackageDeleteFunction)();
60 //===================================================================
64 bbtkDebugMessage("Core",7,"Factory::Factory()"<<std::endl);
66 //===================================================================
68 //===================================================================
72 bbtkDebugMessageInc("Core",7,"Factory::~Factory()"<<std::endl);
74 bbtkDebugDecTab("Core",7);
76 //===================================================================
79 //===================================================================
80 void Factory::CloseAllPackages()
82 bbtkDebugMessageInc("Core",7,"Factory::CloseAllPackages()"<<std::endl);
83 while (mPackageMap.begin() != mPackageMap.end())
85 PackageMapType::iterator i = mPackageMap.begin();
88 bbtkDebugDecTab("Core",7);
90 //===================================================================
92 //===================================================================
95 bbtkDebugMessageInc("Core",7,"Factory::Reset()"<<std::endl);
97 bbtkDebugDecTab("Core",7);
99 //===================================================================
102 // ===================================================================================
104 bool Factory::DoLoadPackage(std::string libname,
110 #if defined(__GNUC__)
113 handler = dlopen(libname.c_str(),
114 BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
118 std::cout <<"[" <<libname<<"] can't be open" << std::endl;
119 std::cout << " " <<dlerror() << std::endl;
121 return false; // try next path
125 std::cout <<" -->[" <<libname<<"] found" << std::endl;
127 // Loads the Package accessor
129 std::string getpackname(pkgname);
130 getpackname += "GetPackage";
131 void *getpack = dlsym(handler, getpackname.c_str());
135 bbtkError("GetPackage : could not load package \""<<pkgname
136 <<"\" [symbol "<<getpackname<<"] :"<<dlerror());
139 // Verifies that the Package delete function is present
140 std::string delfname(pkgname);
141 delfname += "DeletePackage";
142 void *delf = dlsym(handler, delfname.c_str());
146 bbtkError("DeletePackage : could not load package \""<<pkgname
147 <<"\" [symbol "<<delfname<<"] :"<<dlerror());
150 #elif defined(_WIN32)
155 handler = LoadLibrary(libname.c_str());
158 std::cout <<" no handler for [" <<libname<<"];" << std::endl;
159 return false;// Problem with the found library
162 std::cout <<" --->[" <<libname<<"] found" << std::endl;
164 // Loads the Package accessor
166 std::string getpackname(pkgname);
167 getpackname += "GetPackage";
168 void *getpack = GetProcAddress(handler, getpackname.c_str());
171 FreeLibrary(handler);
172 bbtkError("[1]could not load package \""<<pkgname
173 <<"\" : "<<getpackname<<" symbol not found (is it a bbtk package lib ?)");
174 // look how to get the error message on win
178 // Verifies that the Package delete function is present
179 std::string delfname(pkgname);
180 delfname += "DeletePackage";
181 void *delf = GetProcAddress(handler, delfname.c_str());
184 FreeLibrary(handler);
185 bbtkError("[2]could not load package \""<<pkgname
186 <<"\" : "<<delfname<<" symbol not found (is it a bbtk package lib ?)");
187 // look how to get the error message on win
191 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
194 // Stores the package
195 PackageInfoType pack;
196 pack.mDynamicLibraryHandler = handler;
197 // Invokes the accessor to the PackageUnit pointer
198 pack.mPackage = ((PackageAccessor)getpack)();
200 mPackageMap[pkgname] = pack;
202 // Test bbtk build version ok
203 if ( pack.mPackage->GetBBTKVersion() != bbtk::GetVersion() )
205 std::string v(pack.mPackage->GetBBTKVersion());
206 UnLoadPackage(pkgname);
207 bbtkError(" package build with bbtk version "
209 << " whereas application build with version "
210 << bbtk::GetVersion());
213 std::string separator =
214 ConfigurationFile::GetInstance().Get_file_separator ();
215 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
216 std::string docreldoc = separator + "packages" + separator + pkgname
217 + separator + "bbdoc" + separator + "index.html";
218 std::string reldoc = ".." + separator + ".." + separator
220 std::string doc = path + separator + ".." + separator
221 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
224 //std::cout << "doc='"<<doc<<"'"<<std::endl;
226 pack.mPackage->SetDocURL(doc);
227 pack.mPackage->SetDocRelativeURL(reldoc);
229 //===================================================================
230 bbtkMessage("Output",2,pack.mPackage->GetName()<<" "
231 <<pack.mPackage->GetVersion()
233 <<pack.mPackage->GetBBTKVersion()<<") "
234 <<pack.mPackage->GetAuthor()
236 bbtkMessage("Output",2,pack.mPackage->GetDescription()<<std::endl);
237 //===================================================================
239 bbtkDebugDecTab("Core",7);
243 //===================================================================
244 /// \brief Loads a package.
246 /// The name is the system-independant name of the package (the name of the instance of bbtk::Package).
247 /// Tries to open the dynamic library :
248 /// - "libbb<name>.so" for linux systems,
249 /// - "bb<name>.dll" for windows systems.
250 /// If it succeeds, then tries to load the symbols "<name>GetPackage" and "<name>DeletePackage".
251 /// "<name>GetPackage" is called to get the pointer on the bbtk::Package of the library
252 /// ("<name>DeletePackage" is not used, its presence is just checked before loading the package).
254 /// now, filename is only the last name (no longer the full name!)
255 /// it will be searched within *all* the paths given in bbtk_config.xml
257 /// verbose = true (set by "config v") displays the loading process
258 void Factory::LoadPackage( const std::string& name,
259 bool use_configuration_file, bool verbose)
261 // Note : in the following :
262 // name : the user supplied name
263 // - abreviated name e.g. pkg pkg.so libbpkg libbbpkg.so
264 // - relative full name e.g. ./libbbpkg.so ../../libbbpkg.so
265 // - absolute full name e.g. /home/usrname/proj/lib/libbbpkg.so
266 // same for Windows, with c:, d: ...
268 bbtkDebugMessageInc("Core",7,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
269 bbtkMessage("Debug",1,"Factory::LoadPackage(\""<<name<<"\")"<<std::endl);
270 bbtkMessage("Debug",1,"use_configuration_file ["
271 << use_configuration_file << "]" << std::endl);
273 std::vector<std::string> package_paths;
274 std::string libname; // full path library name
275 std::string pkgname; // e.g. libbb<pkgname>.so
278 pkgname = Utilities::ExtractPackageName(name,upath);
280 bbtkMessage("Debug",1,"Package name ["<<pkgname<<"]"<<std::endl);
281 bbtkMessage("Debug",1,"Package path ["<<upath<<"]"<<std::endl);
283 // no loading package if already loaded
284 PackageMapType::iterator iUnload;
285 iUnload = mPackageMap.find(pkgname);
286 if (iUnload != mPackageMap.end())
288 bbtkMessage("Output",2,"["<<pkgname<<"] already loaded"<<std::endl);
292 // If path provided by user will be the first scanned :
293 // push it into vector of paths
294 if (upath.length()>0) package_paths.push_back(upath);
296 // Add the path of config file
297 if (use_configuration_file)
299 std::vector<std::string>::const_iterator pi;
300 for (pi =ConfigurationFile::GetInstance().Get_package_paths().begin();
301 pi!=ConfigurationFile::GetInstance().Get_package_paths().end();
303 package_paths.push_back(*pi);
308 bool foundFile = false;
310 std::vector<std::string>::iterator i;
311 for (i=package_paths.begin();i!=package_paths.end();++i)
314 std::string path = *i;
316 // we *really* want '.' to be the current working directory
318 char buf[2048]; // for getcwd
319 char * currentDir = getcwd(buf, 2048);
320 std::string cwd(currentDir);
324 libname = Utilities::MakeLibnameFromPath(path, pkgname);
326 bbtkMessage("Debug",2,"-> Trying to load ["<<libname<<"]"<<std::endl);
328 // Check if library exists
329 if ( !Utilities::FileExists(libname) )
331 // The following is *NOT* a debug time message :
332 // It's a user intended message.
333 // Please don't remove it.
335 std::cout <<" [" <<libname <<"] : doesn't exist" <<std::endl;
336 continue; // try next path
340 // Try to Load the library
342 ok = DoLoadPackage( libname, pkgname, path, verbose);
345 bbtkMessage("Debug",2," OK"<<std::endl);
346 break; // a package was found; we stop iterating
348 } //------------------ // end for ( package_paths.begin();i!=package_paths.end() )
351 if( !ok ) // nothing was loaded
355 bbtkError("could not find package ["<<pkgname<< "]");
359 #if defined(__GNUC__)
360 bbtkError("could not load package \""<< pkgname
361 <<"\" :" << std::endl << " " <<dlerror());
362 #elif defined(_WIN32)
363 bbtkError("could not load package \""<<pkgname
364 <<"\" : " << std::endl << " " <<libname<<" not found");
366 // look how to get the error message on win
368 // it is the bordel !! (the bloody fucking bordel, you mean?)
369 // look : http://msdn2.microsoft.com/en-us/library/ms680582.aspx
373 bbtkMessage("Output",2,"[" << libname << "] loaded" << std::endl);
377 //===================================================================
378 /// \brief UnLoads a package.
380 /// The package must have been previously loaded by LoadPackage.
381 /// If the entry is found in the map, calls ClosePackage
382 void Factory::UnLoadPackage( const std::string& userSuppliedName )
385 std::string name = Utilities::ExtractPackageName(userSuppliedName,path);
386 bbtkDebugMessageInc("Core",7,"Factory::UnLoadPackage(\""
387 <<name<<"\")"<<std::endl);
389 PackageMapType::iterator i;
390 i = mPackageMap.find(name);
391 if (i == mPackageMap.end())
393 bbtkError("cannot unload package \""<<name
394 <<"\" : package not loaded !");
397 bbtkDebugDecTab("Core",7);
399 //===================================================================
402 //===================================================================
403 /// \brief Close the package referenced by the iterator
405 /// If it is a dynamically loaded package
406 /// - Loads and calls the function "<name>DeletePackage" of the dynamic library (responsible for package desallocation)
407 /// - Closes the dynamic library
408 /// - Erases the package entry in the packages map
410 /// Else simply erases the package entry in the packages map
411 void Factory::ClosePackage(PackageMapType::iterator& i)
413 bbtkDebugMessageInc("Core",7,"Factory::ClosePackage(\""
414 <<i->second.mPackage->GetName()
417 if (i->second.mDynamicLibraryHandler)
420 // If it is a dynamically loaded package
421 // Loads the Package delete function
423 std::string delfname(i->second.mPackage->GetName());
424 delfname += "DeletePackage";
425 #if defined(__GNUC__)
426 void *delf = dlsym(i->second.mDynamicLibraryHandler, delfname.c_str());
429 bbtkError("could not close package \""
430 <<i->second.mPackage->GetName()
431 <<"\" :"<<dlerror());
433 #elif defined(_WIN32)
434 void *delf = GetProcAddress(i->second.mDynamicLibraryHandler,
438 bbtkError("could not close package \""
439 <<i->second.mPackage->GetName()
441 <<" symbol not found (how did you open it ???");
442 //<<"\" :"<<dlerror());
446 // deletes the package
447 ((PackageDeleteFunction)delf)();
449 // closes the dl handler
450 #if defined(__GNUC__)
451 dlclose(i->second.mDynamicLibraryHandler);
452 #elif defined(_WIN32)
454 FreeLibrary(i->second.mDynamicLibraryHandler);
459 // If it is a manually inserted package
460 delete i->second.mPackage;
463 // remove the entry in the map
464 mPackageMap.erase(i);
465 bbtkDebugDecTab("Core",7);
467 //===================================================================
471 //===================================================================
472 /// Displays the list of packages loaded
473 void Factory::PrintPackages(bool details, bool adaptors) const
475 bbtkDebugMessageInc("Core",9,"Factory::PrintPackages"<<std::endl);
477 PackageMapType::const_iterator i;
478 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
480 bbtkMessage("Help",1, i->first << std::endl);
482 i->second.mPackage->PrintBlackBoxes(false,adaptors);
486 bbtkDebugDecTab("Core",9);
488 //===================================================================
490 //===================================================================
491 /// Displays help on a package
492 void Factory::HelpPackage(const std::string& name/* &userSuppliedName */, bool adaptors) const
494 // std::string name = ExtractPackageName(userSuppliedName);
495 bbtkDebugMessageInc("Core",9,"Factory::HelpPackage(\""<<name<<"\")"
498 PackageMapType::const_iterator i = mPackageMap.find(name);
499 if ( i != mPackageMap.end() )
501 bbtkMessage("Help",1, "Package "<<i->first<<" ");
502 if (i->second.mPackage->GetVersion().length()>0)
503 bbtkMessageCont("Help",1,"v" <<i->second.mPackage->GetVersion());
504 if (i->second.mPackage->GetAuthor().length()>0)
505 bbtkMessageCont("Help",1,"- "<<i->second.mPackage->GetAuthor());
506 bbtkMessageCont("Help",1,std::endl);
507 bbtkIncTab("Help",1);
508 bbtkMessage("Help",1,i->second.mPackage->GetDescription()<<std::endl);
509 if (i->second.mPackage->GetNumberOfBlackBoxes()>0)
511 bbtkMessage("Help",1, "Black boxes : "<<std::endl);
512 i->second.mPackage->PrintBlackBoxes(true,adaptors);
516 bbtkMessage("Help",1, "No black boxes"<<std::endl);
518 bbtkDecTab("Help",1);
522 bbtkDebugDecTab("Core",9);
523 bbtkError("package \""<<name<<"\" unknown");
526 bbtkDebugDecTab("Core",9);
528 //===================================================================
530 //===================================================================
531 /// Prints help on the black box of type <name>
532 void Factory::HelpBlackBox(const std::string& name, bool full) const
534 bbtkDebugMessageInc("Core",9,"Factory::HelpBlackBox(\""<<name<<"\")"
538 PackageMapType::const_iterator i;
539 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
541 if (i->second.mPackage->ContainsBlackBox(name))
543 i->second.mPackage->HelpBlackBox(name,full);
548 bbtkDebugDecTab("Core",9);
551 bbtkError("No package of the factory contains any black box <"
555 //===================================================================
558 //===================================================================
559 /// Inserts a package in the factory
560 void Factory::InsertPackage( Package* p )
562 bbtkDebugMessageInc("Core",9,"Factory::InsertPackage(\""<<
563 p->GetName()<<"\")"<<std::endl);
565 PackageInfoType pack;
566 pack.mDynamicLibraryHandler = 0;
570 mPackageMap[p->GetName()] = pack;
571 bbtkDebugDecTab("Core",9);
573 //===================================================================
575 //===================================================================
576 /// Removes a package from the factory (and deletes it)
577 void Factory::RemovePackage( Package* p )
579 bbtkDebugMessageInc("Core",9,"Factory::RemovePackage(\""<<
580 p->GetName()<<"\")"<<std::endl);
582 PackageMapType::iterator i;
583 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
585 if (i->second.mPackage == p) break;
588 if (i!=mPackageMap.end())
594 bbtkError("Factory::RemovePackage(\""<<
595 p->GetName()<<"\") : package absent from factory");
598 bbtkDebugDecTab("Core",9);
600 //===================================================================
603 //===================================================================
604 /// Creates an instance of a black box of type <type> with name <name>
605 BlackBox* Factory::NewBlackBox(const std::string& type,
606 const std::string& name) const
608 bbtkDebugMessageInc("Core",7,"Factory::NewBlackBox(\""
609 <<type<<"\",\""<<name<<"\")"<<std::endl);
612 PackageMapType::const_iterator i;
613 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
615 b = i->second.mPackage->NewBlackBox(type,name);
620 bbtkError("black box type \""<<type<<"\" unknown");
623 bbtkDebugDecTab("Core",7);
626 //===================================================================
628 //===================================================================
629 /// Creates an instance of a black box of type <type> with name <name>
630 BlackBox* Factory::NewAdaptor(TypeInfo typein,
632 const std::string& name) const
634 bbtkDebugMessageInc("Core",8,"Factory::NewAdaptor(<"
635 <<TypeName(typein)<<">,<"
636 <<TypeName(typeout)<<">,\""
637 <<name<<"\")"<<bbtkendl);
641 PackageMapType::const_iterator i;
642 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
644 b = i->second.mPackage->NewAdaptor(typein,typeout,name);
650 <<TypeName(typein)<<"> to <"
652 <<"> adaptor available");
655 bbtkDebugDecTab("Core",7);
658 //===================================================================
660 //===================================================================
661 /// Creates an instance of a connection
662 Connection* Factory::NewConnection(BlackBox* from,
663 const std::string& output,
665 const std::string& input) const
667 bbtkDebugMessage("Core",7,"Factory::NewConnection(\""
668 <<from->bbGetName()<<"\",\""<<output<<"\",\""
669 <<to->bbGetName()<<"\",\""<<input
672 return new Connection(from,output,to,input);
675 // !!! WARNING : WE NEED TO TEST THE TYPE NAME EQUALITY
676 // BECAUSE IN DIFFERENT DYN LIBS THE type_info EQUALITY CAN
677 // BE FALSE (DIFFERENT INSTANCES !)
679 std::string t1 ( from->bbGetOutputType(output).name() );
680 std::string t2 ( to->bbGetInputType(input).name() );
684 //from->bbGetOutputType(output) ==
685 // to->bbGetInputType(input) )
687 c = new Connection(from,output,to,input);
691 // std::cout << "Adaptive connection "<<std::endl;
693 name = from->bbGetName() + "." + output + "-"
694 + to->bbGetName() + "." + input;
697 PackageMapType::const_iterator i;
698 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
700 b = i->second.mPackage->NewAdaptor(from->bbGetOutputType(output),
701 to->bbGetInputType(input),
707 bbtkError("did not find any <"
708 <<TypeName(from->bbGetOutputType(output))
710 <<TypeName(to->bbGetInputType(input))
713 c = new AdaptiveConnection(from,output,to,input,b);
715 bbtkDebugDecTab("Core",7);
720 //===================================================================
724 //===================================================================
725 const Package* Factory::GetPackage(const std::string& name) const
727 bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
730 PackageMapType::const_iterator i = mPackageMap.find(name);
731 if ( i != mPackageMap.end() )
733 bbtkDebugDecTab("Core",9);
734 return i->second.mPackage;
738 bbtkDebugDecTab("Core",9);
739 bbtkError("package \""<<name<<"\" unknown");
742 bbtkDebugDecTab("Core",9);
744 //===================================================================
746 //===================================================================
747 Package* Factory::GetPackage(const std::string& name)
749 bbtkDebugMessageInc("Core",9,"Factory::GetPackage(\""<<name<<"\")"
752 PackageMapType::const_iterator i = mPackageMap.find(name);
753 if ( i != mPackageMap.end() )
755 bbtkDebugDecTab("Core",9);
756 return i->second.mPackage;
760 bbtkDebugDecTab("Core",9);
761 bbtkError("package \""<<name<<"\" unknown");
764 bbtkDebugDecTab("Core",9);
766 //===================================================================
768 //===================================================================
769 void Factory::WriteDotFilePackagesList(FILE *ff)
771 bbtkDebugMessageInc("Core",9,"Factory::WriteDotFilePackagesList()"
775 fprintf( ff , "subgraph cluster_FACTORY {\n");
776 fprintf( ff , " label = \"PACKAGES\"%s\n", ";");
777 fprintf( ff , " style=filled%s\n",";");
778 fprintf( ff , " color=lightgrey%s\n",";");
779 fprintf( ff , " rankdir=TB%s\n",";");
781 PackageMapType::const_iterator i;
782 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
784 std::string url = GetPackage(i->first)->GetDocURL();
785 fprintf(ff," %s [shape=ellipse, URL=\"%s\"]%s\n",
789 fprintf( ff , "}\n\n");
790 bbtkDebugDecTab("Core",9);
792 //===================================================================
795 void Factory::ShowGraphTypes(const std::string& name) const
798 PackageMapType::const_iterator i;
799 for (i = mPackageMap.begin(); i!=mPackageMap.end(); ++i )
801 if (i->second.mPackage->ContainsBlackBox(name))
803 std::string separator = ConfigurationFile::GetInstance().Get_file_separator ();
805 // Don't pollute the file store with "doc_tmp" directories ...
806 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
807 std::string directory = "\"" + default_doc_dir + separator + "doc_tmp" +separator + "\"";
808 std::string filename2 = default_doc_dir + separator + "doc_tmp" + separator + "tmp.html";
811 std::string command("start \"Titre\" /D ");
813 std::string command("gnome-open ");
815 command=command + directory +" tmp.html";
817 ff=fopen(filename2.c_str(),"w");
819 fprintf(ff,"<html><head><title>TMP</title> <script type=\"text/javascript\"> <!--\n");
820 fprintf(ff," window.location=\"%s#%s\";\n" , i->second.mPackage->GetDocURL().c_str(),name.c_str() );
821 fprintf(ff,"//--></script></head><body></body></html>\n");
824 //fprintf(ff, "<a href=\"%s#%s\">Link</a>\n", i->second.mPackage->GetDocURL().c_str(),name.c_str() );
826 system( command.c_str() );
831 bbtkDebugDecTab("Core",9);
834 bbtkError("No package of the factory contains any black box <"