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;
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 virtual bool do_validate_parameter_string(
const std::string& s)
const;
155 typename I::Product *produce_raw(
const std::string& plugindescr)
const;
159 template <
typename Handler,
typename Chained,
bool chainable>
168 template <
typename I>
170 TPlugin<typename I::plugin_data, typename I::plugin_type>(name)
174 template <
typename I>
179 this->set_parameters(options);
180 this->check_parameters();
181 auto product = this->do_create();
183 product->set_module(this->get_module());
184 product->set_init_string(params);
188 catch (std::length_error& x) {
189 std::stringstream msg;
190 msg <<
"CParamList::set: Some string was not created properly\n";
191 msg <<
" options were:\n";
192 for (
auto i = options.begin();
193 i != options.end(); ++i) {
194 msg <<
" " << i->first <<
"=" << i->second <<
"\n";
196 cverr() << msg.str();
197 throw std::logic_error(
"Probably a race condition");
202 template <
typename I>
204 m_cache(this->get_descriptor())
209 template <
typename I>
212 cvdebug() << this->get_descriptor() <<
":Set cache policy to " << enable <<
"\n";
213 m_cache.enable_write(enable);
216 template <
typename I>
220 auto result = m_cache.get(plugindescr);
222 result.reset(this->produce_raw(plugindescr));
223 m_cache.add(plugindescr, result);
225 cvdebug() <<
"Use cached '" << plugindescr <<
"'\n";
229 template <
typename I>
236 template <
typename I>
239 os <<
" The string value will be used to construct a plug-in.";
240 return do_get_handler_type_string();
243 template <
typename I>
249 template <
typename I>
252 cvdebug() <<
"Check whether factory '" << this->get_descriptor() <<
"' can understand '" << s <<
"'\n";
255 auto colon_pos = s.find(
':');
256 auto plugin_name = s.substr(0, colon_pos);
257 if (this->plugin(plugin_name.c_str()))
262 template <
typename Handler,
typename Chained,
bool chainable>
266 if (param_list.
size() > 1) {
267 throw create_exception<std::invalid_argument>(
"Factory " , h.get_descriptor(),
268 ": No chaining supported but ", param_list.
size(),
269 " plugin descriptors were given. "
270 "If the description contains a '+' sign as part "
271 "of a parameter you must protect it by enclosing the "
272 "value in square brackets like this: [1e+6]");
275 cvdebug() <<
"TFactoryPluginHandler<P>::produce use '" << param_list.
begin()->first <<
"'\n";
276 const std::string& factory_name = param_list.
begin()->first;
285 cvdebug() <<
"TFactoryPluginHandler<"<< h.get_descriptor() <<
">::produce: "
286 "Create plugin from '" << factory_name <<
"'\n";
288 auto factory = h.plugin(factory_name.c_str());
290 throw create_exception<std::invalid_argument>(
"Factory " , h.get_descriptor(),
291 ":Unable to find plugin for '", factory_name,
"'");
292 return factory->create(param_list.
begin()->second,params.c_str());
296 template <
typename Handler,
typename ProductChained>
302 if (param_list.
size() == 1)
305 ProductChained *result =
new ProductChained();
307 for (
auto ipl = param_list.
begin(); ipl != param_list.
end(); ++ipl) {
309 const std::string& factory_name = ipl->first;
310 cvdebug() <<
"TFactoryPluginHandler<P>::produce use '" << factory_name <<
"\n";
320 auto factory = h.plugin(factory_name.c_str());
323 throw create_exception<std::invalid_argument>(
"Factory " , h.get_descriptor(),
324 "Unable to find plugin for '", factory_name,
"'");
327 auto r = factory->create(ipl->second,params.c_str());
328 result->push_back(
typename Product::Pointer(r));
330 result->set_init_string(params.c_str());
332 catch (std::exception& x) {
341 template <
typename I>
344 if (params.empty()) {
345 throw create_exception<std::invalid_argument>(
"Factory ", this->get_descriptor(),
": Empty description string given. "
346 "Supported plug-ins are '", this->get_plugin_names(),
"'. "
347 "Set description to 'help' for more information.");
352 if (param_list.size() < 1) {
353 throw create_exception<std::invalid_argument>(
"Factory " , this->get_descriptor(),
": Description string '"
354 , params ,
"' can not be interpreted. "
355 "Supported plug-ins are '" , this->get_plugin_names() ,
"'. "
356 "Set description to 'help' for more information.");
368 #define EXPLICIT_INSTANCE_PLUGIN(T) \
369 template class TPlugin<T::plugin_data, T::plugin_type>; \
370 template class TFactory<T>;
376 #define EXPLICIT_INSTANCE_PLUGIN_HANDLER(P) \
377 template class TPluginHandler<P>; \
378 template class TFactoryPluginHandler<P>; \
379 template class THandlerSingleton<TFactoryPluginHandler<P> >;
386 #define EXPLICIT_INSTANCE_HANDLER(T) \
387 template class TPlugin<T::plugin_data, T::plugin_type>; \
388 template class TFactory<T>; \
389 template class TPluginHandler<TFactory<T> >; \
390 template class TFactoryPluginHandler<TFactory<T> >; \
391 template class THandlerSingleton<TFactoryPluginHandler<TFactory<T> > >;
399 #define EXPLICIT_INSTANCE_DERIVED_FACTORY_HANDLER(T, F) \
400 template class TPlugin<T::plugin_data, T::plugin_type>; \
401 template class TFactory<T>; \
402 template class TPluginHandler<F>; \
403 template class TFactoryPluginHandler<F>; \
404 template class THandlerSingleton<TFactoryPluginHandler<F> >;