/*========================================================================= Program: bbtk Module: $RCSfile: bbtkFactory.h,v $ Language: C++ Date: $Date: 2008/01/22 15:02:00 $ Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ /** *\file *\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. */ /** *\class bbtk::Factory *\brief Can load and unload dynamic libraries containing black boxes packages and create instances of the black boxes registered in the packages loaded. * */ #ifndef __bbtkFactory_h__ #define __bbtkFactory_h__ //#include "bbtkBlackBox.h" #include "bbtkPackage.h" #include "bbtkDynamicLibraryHandling.h" namespace bbtk { class BBTK_EXPORT Factory { public: Factory(); ~Factory(); void LoadPackage( const std::string& name, bool use_configuration_file = true, bool verbose = false ); void UnLoadPackage( const std::string& name ); void PrintPackages(bool details = true, bool adaptors = false) const; void HelpPackage(const std::string& name, bool adaptors = false) const; void HelpBlackBox(const std::string& name, bool full=true) const; void ShowGraphTypes(const std::string& name) const; void InsertPackage( Package* ); void RemovePackage( Package* ); const Package* GetPackage(const std::string& name) const; Package* GetPackage(const std::string& name); BlackBox* NewBlackBox(const std::string& type, const std::string& name) const; BlackBox* NewAdaptor(TypeInfo typein, TypeInfo typeout, const std::string& name) const; Connection* NewConnection(BlackBox* from, const std::string& output, BlackBox* to, const std::string& input) const; void WriteDotFilePackagesList(FILE *ff); void Reset(); // usefull in many places. // -> should be factorized : bbtk::Util class? bool FileExists(std::string strFilename); private: /// the methods for LoadPackage std::string ExtractPackageName(const std::string &name, std::string& path); std::string ExpandLibName(const std::string &name, bool v); std::string MakeLibnameFromPath(std::string path, std::string pkgname); bool DoLoadPackage(std::string libname, std::string pkgname, std::string path, bool v); bool IsAtRoot(std::string cwd); private: /// The structure storing info on a package class PackageInfoType { public : /// Ctor PackageInfoType() {} /// Dtor ~PackageInfoType() {} /// The pointer on the package Package* mPackage; /// The handler of the dynamic library DynamicLibraryHandler mDynamicLibraryHandler; }; /// The type of map of packages typedef std::map< std::string, PackageInfoType > PackageMapType; /// The map of packages PackageMapType mPackageMap; void CloseAllPackages(); void ClosePackage(PackageMapType::iterator& i); }; // class Factory /// SYSTEM METHOD : Global method returning the global factory object pointer inline Factory*& GlobalFactoryPointer() { static Factory* f = 0; return f; } /// SYSTEM METHOD : Global method returning the global factory object inline Factory* GetGlobalFactory() { if (!GlobalFactoryPointer()) { GlobalFactoryPointer() = new Factory; } return GlobalFactoryPointer(); } /// SYSTEM METHOD : Deletes the global factory pointer inline void DeleteGlobalFactory() { if (GlobalFactoryPointer()) { delete GlobalFactoryPointer(); } } inline void LoadPackage( const std::string& name, bool use_configuration_file = true, bool verbose = false ) { GetGlobalFactory()->LoadPackage(name,use_configuration_file, verbose); } inline void UnLoadPackage( const std::string& name ) { GetGlobalFactory()->UnLoadPackage(name); } inline void PrintPackages(bool details = true, bool adaptors = false) { GetGlobalFactory()->PrintPackages(details,adaptors); } inline void HelpPackage(const std::string& name, bool adaptors = false) { GetGlobalFactory()->HelpPackage(name,adaptors); } inline void HelpBlackBox(const std::string& name, bool full=true) { GetGlobalFactory()->HelpBlackBox(name,full); } inline void ShowGraphTypes(const std::string& name) { GetGlobalFactory()->ShowGraphTypes(name); } inline void InsertPackage( Package* p) { GetGlobalFactory()->InsertPackage(p); } inline void RemovePackage( Package* p) { GetGlobalFactory()->RemovePackage(p); } inline const Package* GetPackage(const std::string& name) { return GetGlobalFactory()->GetPackage(name); } inline BlackBox* NewBlackBox(const std::string& type, const std::string& name) { return GetGlobalFactory()->NewBlackBox(type,name); } inline BlackBox* NewAdaptor(TypeInfo typein, TypeInfo typeout, const std::string& name) { return GetGlobalFactory()->NewAdaptor(typein,typeout,name); } inline Connection* NewConnection(BlackBox* from, const std::string& output, BlackBox* to, const std::string& input) { return GetGlobalFactory()->NewConnection(from,output,to,input); } inline void WriteDotFilePackagesList(FILE *ff) { GetGlobalFactory()->WriteDotFilePackagesList(ff); } }// namespace bbtk #endif