21 #ifndef mia_core_factory_hh
22 #define mia_core_factory_hh
50 public TPlugin<typename P::plugin_data, typename P::plugin_type> {
77 virtual Product *create(
const CParsedOptions& options,
char const *params) __attribute__((warn_unused_result));
80 virtual Product *do_create() const __attribute__((warn_unused_result)) = 0 ;
119 ProductPtr produce(
const std::string& plugindescr)
const;
122 ProductPtr
produce(
const char *plugindescr)
const{
123 return produce(std::string(plugindescr));
133 UniqueProduct produce_unique(
const std::string& plugindescr)
const;
137 return produce_unique(std::string(plugindescr));
145 void set_caching(
bool enable)
const;
149 std::string get_handler_type_string_and_help(std::ostream& os)
const;
151 std::string do_get_handler_type_string()
const;
153 typename I::Product *produce_raw(
const std::string& plugindescr)
const;
163 template <
typename I>
165 TPlugin<typename I::plugin_data, typename I::plugin_type>(name)
169 template <
typename I>
174 this->set_parameters(options);
175 this->check_parameters();
176 auto product = this->do_create();
178 product->set_module(this->get_module());
179 product->set_init_string(params);
183 catch (std::length_error& x) {
184 std::stringstream msg;
185 msg <<
"CParamList::set: Some string was not created properly\n";
186 msg <<
" options were:\n";
187 for (
auto i = options.begin();
188 i != options.end(); ++i) {
189 msg <<
" " << i->first <<
"=" << i->second <<
"\n";
191 cverr() << msg.str();
192 throw std::logic_error(
"Probably a race condition");
197 template <
typename I>
199 m_cache(this->get_descriptor())
204 template <
typename I>
207 cvdebug() << this->get_descriptor() <<
":Set cache policy to " << enable <<
"\n";
208 m_cache.enable_write(enable);
211 template <
typename I>
215 auto result = m_cache.get(plugindescr);
217 result.reset(this->produce_raw(plugindescr));
218 m_cache.add(plugindescr, result);
220 cvdebug() <<
"Use cached '" << plugindescr <<
"'\n";
224 template <
typename I>
231 template <
typename I>
234 os <<
" The string value will be used to construct a plug-in.";
235 return do_get_handler_type_string();
238 template <
typename I>
244 template <
typename I>
247 if (params.empty()) {
248 throw create_exception<std::invalid_argument>(
"Factory ", this->get_descriptor(),
": Empty description string given. "
249 "Supported plug-ins are '", this->get_plugin_names(),
"'. "
250 "Set description to 'help' for more information.");
255 if (param_list.size() < 1) {
256 throw create_exception<std::invalid_argument>(
"Factory " , this->get_descriptor(),
": Description string '"
257 , params ,
"' can not be interpreted. "
258 "Supported plug-ins are '" , this->get_plugin_names() ,
"'. "
259 "Set description to 'help' for more information.");
262 cvdebug() <<
"TFactoryPluginHandler<P>::produce use '" << param_list.begin()->first <<
"'\n";
263 const std::string& factory_name = param_list.begin()->first;
265 if (factory_name == plugin_help) {
268 this->print_help(
cverb);
272 cvdebug() <<
"TFactoryPluginHandler<>::produce: Create plugin from '" << factory_name <<
"'\n";
274 auto factory = this->plugin(factory_name.c_str());
276 return factory->create(param_list.begin()->second,params.c_str());
284 #define EXPLICIT_INSTANCE_PLUGIN(T) \
285 template class TPlugin<T::plugin_data, T::plugin_type>; \
286 template class TFactory<T>;
292 #define EXPLICIT_INSTANCE_PLUGIN_HANDLER(P) \
293 template class TPluginHandler<P>; \
294 template class TFactoryPluginHandler<P>; \
295 template class THandlerSingleton<TFactoryPluginHandler<P> >;
302 #define EXPLICIT_INSTANCE_HANDLER(T) \
303 template class TPlugin<T::plugin_data, T::plugin_type>; \
304 template class TFactory<T>; \
305 template class TPluginHandler<TFactory<T> >; \
306 template class TFactoryPluginHandler<TFactory<T> >; \
307 template class THandlerSingleton<TFactoryPluginHandler<TFactory<T> > >;
315 #define EXPLICIT_INSTANCE_DERIVED_FACTORY_HANDLER(T, F) \
316 template class TPlugin<T::plugin_data, T::plugin_type>; \
317 template class TFactory<T>; \
318 template class TPluginHandler<F>; \
319 template class TFactoryPluginHandler<F>; \
320 template class THandlerSingleton<TFactoryPluginHandler<F> >;