00001 //-------------------------------------------------- 00012 //--------------------------------------------------- 00013 00014 #include "algorithm_factory.h" 00015 #include "interfaces.h" 00016 #include "algorithm.h" 00017 #include "configuration.h" 00018 00019 #include <qstring.h> 00020 #include <iostream> 00021 00022 namespace imedgine 00023 { 00024 00025 //-------------------------------------------------- 00026 00027 void AlgorithmFactory::loadAlgorithm(AlgorithmFactory::key_type key, std::string library_path) 00028 throw(AlgorithmFactoryException) 00029 { 00030 std::cout << library_path.c_str() << std::endl; 00031 QPluginLoader loader(QString(library_path.c_str())); 00032 00033 QObject *plugin = loader.instance(); 00034 if (!plugin) 00035 { 00036 throw(AlgorithmFactoryException( 00037 AlgorithmFactoryException::LOADING_LIBRARY_FAILED, library_path)); 00038 } 00039 AlgorithmInterface *interface = qobject_cast<AlgorithmInterface *>(plugin); 00040 00041 if (library_handle_map_.insert( 00042 std::make_pair(key, interface)).second == false) 00043 { 00044 throw AlgorithmFactoryException( 00045 AlgorithmFactoryException::DUPLICATE_LIBRARY_NAME, key); 00046 } 00047 } 00048 00049 00050 //-------------------------------------------------- 00051 00052 bool AlgorithmFactory::isAlgorithmLoaded 00053 (AlgorithmFactory::key_type algorithm_key) 00054 { 00055 key_to_library_handle_iterator lib_handle_iter = 00056 library_handle_map_.find(algorithm_key); 00057 00058 if (lib_handle_iter == library_handle_map_.end()) 00059 { 00060 return(false); 00061 } 00062 return(true); 00063 } 00064 00065 //-------------------------------------------------- 00066 00067 bool AlgorithmFactory::unloadAlgorithm( 00068 AlgorithmFactory::key_type algorithm_key) 00069 { 00070 key_to_library_handle_iterator lib_handle_iter = 00071 library_handle_map_.find(algorithm_key); 00072 00073 if (lib_handle_iter == library_handle_map_.end()) 00074 { 00075 return(false); 00076 } 00077 00078 if (lib_handle_iter->second != 0) 00079 { 00080 //lib_handle_iter->second->unload(); 00081 delete lib_handle_iter->second; 00082 } 00083 library_handle_map_.erase(lib_handle_iter); 00084 00085 return(false); 00086 } 00087 00088 //-------------------------------------------------- 00089 00090 AlgorithmFactory::algorithm_pointer_type AlgorithmFactory::createAlgorithm( 00091 AlgorithmFactory::key_type algorithm_key) const 00092 throw(AlgorithmNotFoundException, NullPointerException) 00093 { 00094 key_to_library_handle_const_iterator lib_handle_iter = 00095 library_handle_map_.find(algorithm_key); 00096 00097 if (lib_handle_iter == library_handle_map_.end()) 00098 { 00099 throw(AlgorithmNotFoundException(algorithm_key)); 00100 } 00101 00102 if (lib_handle_iter->second == 0) 00103 { 00104 throw(NullPointerException("createAlgorithm")); 00105 } 00106 00107 return(algorithm_pointer_type(lib_handle_iter->second->getAlgorithm())); 00108 } 00109 00110 //-------------------------------------------------- 00111 00112 config_widget_pointer_type AlgorithmFactory::createConfigWidget( 00113 AlgorithmFactory::key_type algorithm_key) const 00114 throw(AlgorithmNotFoundException,NullPointerException) 00115 { 00116 key_to_library_handle_const_iterator lib_handle_iter = 00117 library_handle_map_.find(algorithm_key); 00118 00119 if (lib_handle_iter == library_handle_map_.end()) 00120 { 00121 throw(AlgorithmNotFoundException(algorithm_key)); 00122 } 00123 00124 if (lib_handle_iter->second == 0) 00125 { 00126 throw(NullPointerException("createConfigWidget")); 00127 } 00128 config_widget_pointer_type config_widget = lib_handle_iter->second->getConfigWidget(); 00129 if (config_widget == 0) 00130 { 00131 throw(NullPointerException("createConfigWidget")); 00132 } 00133 return(config_widget); 00134 } 00135 00136 //-------------------------------------------------- 00137 00138 configuration_pointer_type AlgorithmFactory::createConfiguration 00139 (AlgorithmFactory::key_type algorithm_key) const 00140 throw(AlgorithmNotFoundException,NullPointerException) 00141 { 00142 key_to_library_handle_const_iterator lib_handle_iter = 00143 library_handle_map_.find(algorithm_key); 00144 00145 if (lib_handle_iter == library_handle_map_.end()) 00146 { 00147 throw(AlgorithmNotFoundException(algorithm_key)); 00148 } 00149 00150 if (lib_handle_iter->second == 0) 00151 { 00152 throw(NullPointerException("createConfiguration")); 00153 } 00154 00155 configuration_pointer_type configuration = 00156 configuration_pointer_type(lib_handle_iter->second->getConfiguration()); 00157 if (!configuration.get()) 00158 { 00159 throw(NullPointerException("createConfiguration")); 00160 } 00161 00162 return(configuration); 00163 } 00164 00165 } // namespace imedgine 00166