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-2015 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 <boost/any.hpp>
30 
31 #include <mia/core/utils.hh>
32 #include <mia/core/filetools.hh>
33 
34 #include <mia/core/defines.hh>
35 #include <mia/core/module.hh>
36 #include <mia/core/plugin_base.hh>
37 #include <mia/core/handlerbase.hh>
38 #include <mia/core/searchpath.hh>
40 
42 
43 
53 template <typename I>
55 public:
57  typedef I Interface;
58 
59  typedef std::shared_ptr<I> PInterface;
60 
62  typedef std::map<std::string, PInterface> CPluginMap;
63 
65  typedef typename CPluginMap::const_iterator const_iterator;
66 
67 
71  virtual ~TPluginHandler();
72 
74  size_t size() const;
75 
79  const std::string get_plugin_names() const;
80 
82  const std::set<std::string> get_set() const;
83 
84 
86  const_iterator begin()const;
87 
89  const_iterator end()const;
90 
97  bool add_plugin(PInterface plugin);
98 
99 protected:
100 
101 
106  void add_plugin_internal(PInterface plugin);
107 
108 
110 
111 
116  TPluginHandler();
118 
124  typename TPluginHandler<I>::Interface *plugin(const char *plugin) const;
125 
126 
127  void initialise(const CPluginSearchpath& searchpath);
128 
129 private:
130  virtual void do_initialise();
131 
132  void do_add_dependend_handlers(HandlerHelpMap& handler_map) const;
133 
134  std::vector<PPluginModule> m_modules;
135  CPluginMap m_plugins;
136 
137  virtual void do_print_short_help(std::ostream& os) const;
138  virtual void do_print_help(std::ostream& os) const;
139  virtual void do_get_xml_help(CXMLElement& root) const;
140 
141  static const char * const m_help;
142 
143 };
144 
145 
154 template <typename T>
156 public:
157 
162  static void set_search_path(const CPluginSearchpath& searchpath);
163 
165  typedef T Instance;
166 
168  typedef typename T::const_iterator const_iterator;
169 
171  typedef typename T::CPluginMap::value_type value_type;
172 
176  static const T& instance();
177 
178  static bool add_plugin(typename T::PInterface p);
179 
184  static const T* pointer();
185 protected:
186 
190 
191 private:
192  // the constructor is private because you must not derive the singleton
193  // derive the handler if you need specific funcionality, and then
194  // template the singleton with the derived handler.
196 
197  static T& do_instance(bool require_initialization);
198 
199  static CPluginSearchpath m_searchpath;
200  static bool m_is_created;
201  static CMutex m_initialization_mutex;
202  static bool m_is_initialized;
203 
204 };
205 
206 
208 
209 #endif
I Interface
typedef for the plug-in interface provided by the class
Definition: handler.hh:57
the singleton that a plug-in handler really is
Definition: handler.hh:155
std::map< std::string, PInterface > CPluginMap
a map containing the names and theavailabe plug-ins
Definition: handler.hh:62
T::const_iterator const_iterator
iterator to iterator over the actual plug-ins
Definition: handler.hh:168
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:43
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:36
#define EXPORT_HANDLER
Definition: core/cost.hh:32
T Instance
The instance of the plugin handler.
Definition: handler.hh:165
The base class for all plugin handlers.
Definition: handlerbase.hh:57
std::shared_ptr< I > PInterface
Definition: handler.hh:59
CPluginMap::const_iterator const_iterator
the iterator to walk over the available plug-ins
Definition: handler.hh:65
This class implements a facade for the xml Element.
Definition: xmlinterface.hh:42
tbb::mutex CMutex
The mutex renamed to MIA style.
Definition: defines.hh:35
static CMutex m_creation_mutex
Definition: handler.hh:189
T::CPluginMap::value_type value_type
the name,plug-in pair
Definition: handler.hh:171
The basic template of all plugin handlers.
Definition: handler.hh:54
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:46