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>
228 return UniqueProduct(this->produce_raw(plugindescr));
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;
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 throw create_exception<std::invalid_argument>(
"Unable to find plugin for '", factory_name.c_str(),
"'");
277 return factory->create(param_list.begin()->second,params.c_str());
285 #define EXPLICIT_INSTANCE_PLUGIN(T) \
286 template class TPlugin<T::plugin_data, T::plugin_type>; \
287 template class TFactory<T>;
293 #define EXPLICIT_INSTANCE_PLUGIN_HANDLER(P) \
294 template class TPluginHandler<P>; \
295 template class TFactoryPluginHandler<P>; \
296 template class THandlerSingleton<TFactoryPluginHandler<P> >;
303 #define EXPLICIT_INSTANCE_HANDLER(T) \
304 template class TPlugin<T::plugin_data, T::plugin_type>; \
305 template class TFactory<T>; \
306 template class TPluginHandler<TFactory<T> >; \
307 template class TFactoryPluginHandler<TFactory<T> >; \
308 template class THandlerSingleton<TFactoryPluginHandler<TFactory<T> > >;
316 #define EXPLICIT_INSTANCE_DERIVED_FACTORY_HANDLER(T, F) \
317 template class TPlugin<T::plugin_data, T::plugin_type>; \
318 template class TFactory<T>; \
319 template class TPluginHandler<F>; \
320 template class TFactoryPluginHandler<F>; \
321 template class THandlerSingleton<TFactoryPluginHandler<F> >;
The type specific product cache.
vstream & cverr()
send errors to this stream adapter
TFactoryPluginHandler()
Initializes the plugin handler.
UniqueProduct produce_unique(const char *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
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
void set_caching(bool enable) const
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))
std::unique_ptr< P > UniqueProduct
typedef for the unique version of the product
vstream & cvdebug()
Short for debug output in non-debug build output send to this will be ignored.
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
tbb::mutex::scoped_lock CScopedLock
The scoped lock renamed to MIA style.
EXPORT_CORE const std::string plugin_help
standard string to print out help in the factory plug-in handler
the Base class for all plugn handlers that deal with factory plugins.
tbb::mutex CMutex
The mutex renamed to MIA style.
ProductPtr produce(const char *plugindescr) const
UniqueProduct produce_unique(const std::string &plugindescr) const
Parser for complex command line 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