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 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>
181 auto product = this->do_create();
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>
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. " 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. " 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> >; The type specific product cache.
vstream & cverr()
send errors to this stream adapter
const_iterator begin() const
TFactoryPluginHandler()
Initializes the plugin handler.
static Product * apply(const Handler &h, const CComplexOptionParser ¶m_list, const std::string ¶ms)
UniqueProduct produce_unique(const std::string &plugindescr) const
ProductPtr produce(const std::string &plugindescr) const
TFactory(char const *const name)
I::UniqueProduct UniqueProduct
The unique pointer type of the the object this plug in hander produces.
#define cverb
define a shortcut to the raw output stream
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
ProductPtr produce(const char *plugindescr) const
TPluginHandler< I >::Interface * plugin(const char *plugin) const
I::SharedProduct ProductPtr
The shared pointer type of the the object this plug in hander produces.
std::map< std::string, std::string > CParsedOptions
vstream & cvmsg()
send messages to this stream adapter
This is tha base of all plugins that create "things", like filters, cost functions time step operator...
virtual Product * create(const CParsedOptions &options, char const *params) __attribute__((warn_unused_result))
const std::string & get_descriptor() const
std::unique_ptr< P > UniqueProduct
typedef for the unique version of the product
P Product
typedef to describe the product of the factory
I::Product Product
The type of the the object this plug in hander produces.
std::shared_ptr< P > SharedProduct
typedef for the shared version of the product
PPluginModule get_module() const
void set_caching(bool enable) const
const_iterator end() const
EXPORT_CORE const std::string plugin_help
standard string to print out help in the factory plug-in handler
static Product * apply(const Handler &h, const CComplexOptionParser ¶m_list, const std::string ¶ms)
the Base class for all plugn handlers that deal with factory plugins.
CParts::size_type size() const
UniqueProduct produce_unique(const char *plugindescr) const
const std::string get_plugin_names() const
Parser for complex command line options.
void set_parameters(const CParsedOptions &options)
The generic base for all plug-ins.
The basic template of all plugin handlers.
#define NS_MIA_END
conveniance define to end the mia namespace