[go: up one dir, main page]

Menu

[r789]: / mia2 / mia / core / factory.hh  Maximize  Restore  History

Download this file

151 lines (118 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
** Copyrigh (C) 1999-2006 Max-Planck-Institute of Cognitive Neurosience
** 2007 Gert Wollny <gert at die.upm.es>
**
** 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 <mia/core/handler.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/plugin_base.hh>
#include <mia/core/optionparser.hh>
#ifndef EXPORT_HANDLER
# ifdef WIN32
# define EXPORT_HANDLER __declspec(dllimport)
# else
# define EXPORT_HANDLER
# endif
#endif
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);
private:
virtual bool do_test() const = 0;
virtual ProductPtr do_create() = 0;
};
template <typename P>
class TFactoryPluginHandler: public TPluginHandler< P > {
protected:
//! \name Constructors
//@{
/*! \brief Initializes the plugin handler based on a given plugin search path list
\param searchpath list of directories to search for plugins
*/
TFactoryPluginHandler(const std::list<boost::filesystem::path>& searchpath);
//@}
public:
typename P::ProductPtr produce(const char *plugindescr) const;
};
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>
TFactoryPluginHandler<P>::TFactoryPluginHandler(const std::list<boost::filesystem::path>& searchpath):
TPluginHandler< P >(searchpath)
{
}
template <typename P>
typename P::ProductPtr TFactoryPluginHandler<P>::produce(char const *params)const
{
typedef typename P::ProductPtr ProductPtr;
CComplexOptionParser param_list(params);
if (param_list.size() < 1)
return ProductPtr();
cvdebug() << "TFactoryPluginHandler<P>::produce use '" << param_list.begin()->first << "'\n";
const std::string& factory_name = param_list.begin()->first;
if (factory_name == plugin_help) {
cvdebug() << "print help\n";
cvmsg() << "\n";
this->print_help(cverb);
return ProductPtr();
}
cvdebug() << "TFactoryPluginHandler<>::produce: Create plugin from '" << factory_name << "'\n";
P *factory = this->plugin(factory_name.c_str());
if (factory)
return factory->create(param_list.begin()->second);
else
return ProductPtr();
}
NS_MIA_END
#endif
/*
$Log: factory.hh,v $
Revision 1.4 2005/06/03 11:00:38 gerddie
add log entries to all files
*/