My Project
handler.hh
Go to the documentation of this file.
1/* -*- mia-c++ -*-
2 *
3 * This file is part of MIA - a toolbox for medical image analysis
4 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5 *
6 * MIA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef ph_handler_hh
22#define ph_handler_hh
23
24#include <string>
25#include <map>
26#include <set>
27#include <vector>
28#include <ostream>
29#include <atomic>
30#include <boost/any.hpp>
31
32#include <mia/core/utils.hh>
33#include <mia/core/filetools.hh>
34
35#include <mia/core/defines.hh>
36#include <mia/core/parallel.hh>
37#include <mia/core/module.hh>
42
44
45
55template <typename I>
57{
58public:
60 typedef I Interface;
61
62 typedef std::shared_ptr<I> PInterface;
63
65 typedef std::map<std::string, PInterface> CPluginMap;
66
68 typedef typename CPluginMap::const_iterator const_iterator;
69
70
74 virtual ~TPluginHandler();
75
77 size_t size() const;
78
82 const std::string get_plugin_names() const;
83
85 const std::set<std::string> get_set() const;
86
87
90
93
100 bool add_plugin(PInterface plugin);
101
102protected:
103
104
110
111
113
114
121
127 typename TPluginHandler<I>::Interface *plugin(const char *plugin) const;
128
129
130 void initialise(const CPluginSearchpath& searchpath);
131
132private:
133 virtual void do_initialise();
134
135 void do_add_dependend_handlers(HandlerHelpMap& handler_map) const;
136
137 std::vector<PPluginModule> m_modules;
138 CPluginMap m_plugins;
139
140 virtual void do_print_short_help(std::ostream& os) const;
141 virtual void do_print_help(std::ostream& os) const;
142 virtual void do_get_xml_help(CXMLElement& root) const;
143
144 static const char *const m_help;
145
146};
147
148
157template <typename T>
159{
160public:
161
166 static void set_search_path(const CPluginSearchpath& searchpath);
167
169 typedef T Instance;
170
172 typedef typename T::const_iterator const_iterator;
173
175 typedef typename T::CPluginMap::value_type value_type;
176
180 static const T& instance();
181
182 static bool add_plugin(typename T::PInterface p);
183
188 static const T *pointer();
189protected:
190
194
195private:
196 // the constructor is private because you must not derive the singleton
197 // derive the handler if you need specific funcionality, and then
198 // template the singleton with the derived handler.
200
201 static T& do_instance(bool require_initialization);
202
203 static CPluginSearchpath m_searchpath;
204 static bool m_is_created;
205 static CMutex m_initialization_mutex;
206 static std::atomic<bool> m_is_initialized;
207
208};
209
210
212
213#endif
The base class for all plugin handlers.
Definition: handlerbase.hh:57
This class implements a facade for the xml Element.
Definition: xmlinterface.hh:50
the singleton that a plug-in handler really is
Definition: handler.hh:159
static const T & instance()
static void set_search_path(const CPluginSearchpath &searchpath)
static const T * pointer()
static CMutex m_creation_mutex
Definition: handler.hh:193
T::CPluginMap::value_type value_type
the name,plug-in pair
Definition: handler.hh:175
T::const_iterator const_iterator
iterator to iterator over the actual plug-ins
Definition: handler.hh:172
static bool add_plugin(typename T::PInterface p)
T Instance
The instance of the plugin handler.
Definition: handler.hh:169
The basic template of all plugin handlers.
Definition: handler.hh:57
const std::set< std::string > get_set() const
std::map< std::string, PInterface > CPluginMap
a map containing the names and the available plug-ins
Definition: handler.hh:65
const_iterator end() const
TPluginHandler()
Initializes the plugin handler based on the build-in search path.
void initialise(const CPluginSearchpath &searchpath)
bool add_plugin(PInterface plugin)
void add_plugin_internal(PInterface plugin)
CPluginMap::const_iterator const_iterator
the iterator to walk over the available plug-ins
Definition: handler.hh:68
std::shared_ptr< I > PInterface
Definition: handler.hh:62
const std::string get_plugin_names() const
I Interface
typedef for the plug-in interface provided by the class
Definition: handler.hh:60
size_t size() const
virtual ~TPluginHandler()
const_iterator begin() const
TPluginHandler< I >::Interface * plugin(const char *plugin) const
#define EXPORT_HANDLER
Definition: core/cost.hh:32
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
std::map< std::string, const CPluginHandlerBase * > HandlerHelpMap
A map that is used to collect the plug-in handlers used in a program.
Definition: handlerbase.hh:45
std::mutex CMutex