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>
38 #include <mia/core/plugin_base.hh>
39 #include <mia/core/handlerbase.hh>
40 #include <mia/core/searchpath.hh>
42 
44 
45 
55 template <typename I>
57 public:
59  typedef I Interface;
60 
61  typedef std::shared_ptr<I> PInterface;
62 
64  typedef std::map<std::string, PInterface> CPluginMap;
65 
67  typedef typename CPluginMap::const_iterator const_iterator;
68 
69 
73  virtual ~TPluginHandler();
74 
76  size_t size() const;
77 
81  const std::string get_plugin_names() const;
82 
84  const std::set<std::string> get_set() const;
85 
86 
88  const_iterator begin()const;
89 
91  const_iterator end()const;
92 
99  bool add_plugin(PInterface plugin);
100 
101 protected:
102 
103 
108  void add_plugin_internal(PInterface plugin);
109 
110 
112 
113 
118  TPluginHandler();
120 
126  typename TPluginHandler<I>::Interface *plugin(const char *plugin) const;
127 
128 
129  void initialise(const CPluginSearchpath& searchpath);
130 
131 private:
132  virtual void do_initialise();
133 
134  void do_add_dependend_handlers(HandlerHelpMap& handler_map) const;
135 
136  std::vector<PPluginModule> m_modules;
137  CPluginMap m_plugins;
138 
139  virtual void do_print_short_help(std::ostream& os) const;
140  virtual void do_print_help(std::ostream& os) const;
141  virtual void do_get_xml_help(CXMLElement& root) const;
142 
143  static const char * const m_help;
144 
145 };
146 
147 
156 template <typename T>
158 public:
159 
164  static void set_search_path(const CPluginSearchpath& searchpath);
165 
167  typedef T Instance;
168 
170  typedef typename T::const_iterator const_iterator;
171 
173  typedef typename T::CPluginMap::value_type value_type;
174 
178  static const T& instance();
179 
180  static bool add_plugin(typename T::PInterface p);
181 
186  static const T* pointer();
187 protected:
188 
192 
193 private:
194  // the constructor is private because you must not derive the singleton
195  // derive the handler if you need specific funcionality, and then
196  // template the singleton with the derived handler.
198 
199  static T& do_instance(bool require_initialization);
200 
201  static CPluginSearchpath m_searchpath;
202  static bool m_is_created;
203  static CMutex m_initialization_mutex;
204  static std::atomic<bool> m_is_initialized;
205 
206 };
207 
208 
210 
211 #endif
I Interface
typedef for the plug-in interface provided by the class
Definition: handler.hh:59
the singleton that a plug-in handler really is
Definition: handler.hh:157
std::map< std::string, PInterface > CPluginMap
a map containing the names and the available plug-ins
Definition: handler.hh:64
T::const_iterator const_iterator
iterator to iterator over the actual plug-ins
Definition: handler.hh:170
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
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:35
#define EXPORT_HANDLER
Definition: core/cost.hh:32
T Instance
The instance of the plugin handler.
Definition: handler.hh:167
The base class for all plugin handlers.
Definition: handlerbase.hh:56
std::shared_ptr< I > PInterface
Definition: handler.hh:61
CPluginMap::const_iterator const_iterator
the iterator to walk over the available plug-ins
Definition: handler.hh:67
This class implements a facade for the xml Element.
Definition: xmlinterface.hh:49
static CMutex m_creation_mutex
Definition: handler.hh:191
T::CPluginMap::value_type value_type
the name,plug-in pair
Definition: handler.hh:173
std::mutex CMutex
The basic template of all plugin handlers.
Definition: handler.hh:56
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36