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>
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 {
58 public:
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 
89  const_iterator begin()const;
90 
92  const_iterator end()const;
93 
100  bool add_plugin(PInterface plugin);
101 
102 protected:
103 
104 
109  void add_plugin_internal(PInterface plugin);
110 
111 
113 
114 
119  TPluginHandler();
121 
127  typename TPluginHandler<I>::Interface *plugin(const char *plugin) const;
128 
129 
130  void initialise(const CPluginSearchpath& searchpath);
131 
132 private:
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 
157 template <typename T>
159 {
160 public:
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();
189 protected:
190 
194 
195 private:
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
THandlerSingleton::const_iterator
T::const_iterator const_iterator
iterator to iterator over the actual plug-ins
Definition: handler.hh:172
utils.hh
searchpath.hh
TPluginHandler::const_iterator
CPluginMap::const_iterator const_iterator
the iterator to walk over the available plug-ins
Definition: handler.hh:68
CMutex
std::mutex CMutex
Definition: parallelcxx11.hh:34
plugin_base.hh
filetools.hh
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
module.hh
import_handler.hh
handlerbase.hh
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
HandlerHelpMap
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
EXPORT_HANDLER
#define EXPORT_HANDLER
Definition: core/cost.hh:32
THandlerSingleton
the singleton that a plug-in handler really is
Definition: handler.hh:158
CXMLElement
This class implements a facade for the xml Element.
Definition: xmlinterface.hh:49
TPluginHandler::PInterface
std::shared_ptr< I > PInterface
Definition: handler.hh:62
parallel.hh
TPluginHandler
The basic template of all plugin handlers.
Definition: handler.hh:56
CPluginHandlerBase
The base class for all plugin handlers.
Definition: handlerbase.hh:56
THandlerSingleton::m_creation_mutex
static CMutex m_creation_mutex
Definition: handler.hh:193
THandlerSingleton::Instance
T Instance
The instance of the plugin handler.
Definition: handler.hh:169
TPluginHandler::CPluginMap
std::map< std::string, PInterface > CPluginMap
a map containing the names and the available plug-ins
Definition: handler.hh:65
THandlerSingleton::value_type
T::CPluginMap::value_type value_type
the name,plug-in pair
Definition: handler.hh:175
CPluginSearchpath
Definition: searchpath.hh:34
TPluginHandler::Interface
I Interface
typedef for the plug-in interface provided by the class
Definition: handler.hh:60
defines.hh