/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* 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
*
*/
// $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 <libmona/plugin_handler.hh>
#include <libmona/monaPlugIns.hh>
#include <libmona/command_parser.hh>
MONA_NAMESPACE_BEGIN
/** this is a producer class, that creates objects based
on a prototype. The Prototy class must provide a function "clone",
that returns a copy of the prototype. */
template <typename Prototype>
class TProtoFactory {
public:
/** Constructor to create the Factory, and initialise it with a prototype */
TProtoFactory(const Prototype *prototype);
/** reset the factory to e new prototype */
void reset(const Prototype *prototype);
/** Produce a copy of the prototype */
Prototype *produce() const;
private:
std::auto_ptr<const Prototype> _M_prototype;
};
/** this is the class to load a certain plugin */
template <class T>
class TFactory: public CPluginBase {
public:
typedef T Product;
typedef boost::shared_ptr<T> ProductPtr;
/** initialise the plugin by the names
\remark what are these names and types good for?
*/
TFactory(char const * const name, char const * const type, char const * const data);
/** the creation function
\params options the options to initialise the plugin
\returns an instance of the requested object
*/
virtual ProductPtr create(const CParsedOptions& options) const = 0;
/** 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 PluginHandler>
static ProductPtr produce(char const *params, const PluginHandler& ph)
{
CComplexOptionParser param_list(params);
if (param_list.size() < 1)
return ProductPtr();
const std::string& factory_name = param_list.begin()->first;
if (factory_name == plugin_help) {
ph.print_help();
return ProductPtr();
}
const typename PluginHandler::HandledPluginType *factory = ph.find_plugin_by_name(factory_name.c_str());
if (factory)
return factory->create(param_list.begin()->second);
else
return ProductPtr();
}
/**
Just ensure a virtual destructor
*/
virtual ~TFactory();
};
template <typename Prototype>
TProtoFactory<Prototype>::TProtoFactory(const Prototype *prototype):
_M_prototype(prototype)
{
}
template <typename Prototype>
void TProtoFactory<Prototype>::reset(const Prototype *prototype)
{
_M_prototype.reset(prototype);
}
template <typename Prototype>
Prototype *TProtoFactory<Prototype>::produce()const
{
if (_M_prototype.get())
return _M_prototype->clone();
else
return NULL;
}
template <class T>
TFactory<T>::TFactory(char const * const name, char const * const type, char const * const data):
CPluginBase(name, type, data)
{
}
template <class T>
TFactory<T>::~TFactory()
{
}
MONA_NAMESPACE_END
#endif
/*
$Log: factory.hh,v $
Revision 1.4 2005/06/03 11:00:38 gerddie
add log entries to all files
*/