/*
** Copyrigh (C) 1999-2004 Max-Planck-Institute of Cognitive Neurosience
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As an exception to this license, "NEC C&C Research Labs" may use
this software under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation.
*/
// $Id: factory.hh,v 1.4 2005/06/03 11:00:38 gerddie Exp $
/**
\author: Gert Wollny < wollny at cbs mpg de >
implements a plugin loading factory
*/
#ifndef FACTORY_HH
#define FACTORY_HH
#include <iostream>
#include <memory>
#include <boost/shared_ptr.hpp>
#include <mia/core/handler.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/plugin_base.hh>
#include <mia/core/optionparser.hh>
NS_MIA_BEGIN
/** this is the class to load a certain plugin */
template <typename P, typename D, typename T>
class TFactory: public TPlugin<D,T> {
public:
typedef P Product;
typedef boost::shared_ptr<P> ProductPtr;
/** initialise the plugin by the names
\remark what are these names and types good for?
*/
TFactory(char const * const name);
/** the creation function
\params options the options to initialise the plugin
\returns an instance of the requested object
*/
virtual ProductPtr create(const CParsedOptions& options);
/** this is the important part of the plugin loading mechanism:
\param params is a string that describes the options for loading the plugin
\param ph is the handler of a certain kind of plugins
\returns a entitity of the requested functionallity
*/
template <class H>
static ProductPtr produce(char const *params, const H& ph)
{
CComplexOptionParser param_list(params);
if (param_list.size() < 1)
return ProductPtr();
const std::string& factory_name = param_list.begin()->first;
cvdebug() << "TFactory<>::produce: Create plugin from '" << factory_name << "'\n";
if (factory_name == plugin_help) {
cvdebug() << "wait for the help\n";
ph.print_help(std::cerr);
return ProductPtr();
}
typename H::Interface *factory = ph.plugin(factory_name.c_str());
if (factory)
return factory->create(param_list.begin()->second);
else
return ProductPtr();
}
/// a selftest function, actual implementation in \a do_test
void test() const;
private:
virtual void do_test() const = 0;
virtual ProductPtr do_create() = 0;
};
template <typename P, typename D, typename T>
TFactory<P,D,T>::TFactory(char const * const name):
TPlugin<D,T>(name)
{
}
template <typename P, typename D, typename T>
typename TFactory<P,D,T>::ProductPtr TFactory<P,D,T>::create(const CParsedOptions& options)
{
this->set_parameters(options);
this->check_parameters();
return this->do_create();
}
template <typename P, typename D, typename T>
void TFactory<P,D,T>::test() const
{
this->do_test();
}
NS_MIA_END
#endif
/*
$Log: factory.hh,v $
Revision 1.4 2005/06/03 11:00:38 gerddie
add log entries to all files
*/