Lunchbox  1.12.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
pluginFactory.h
1 
2 /* Copyright (c) 2013-2015, EPFL/Blue Brain Project
3  * Raphael Dumusc <raphael.dumusc@epfl.ch>
4  * Stefan.Eilemann@epfl.ch
5  *
6  * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 2.1 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef LUNCHBOX_PLUGINFACTORY_H
23 #define LUNCHBOX_PLUGINFACTORY_H
24 
25 #include <lunchbox/types.h>
26 #include <lunchbox/algorithm.h> // used inline
27 #include <lunchbox/debug.h> // LBTHROW
28 #include <lunchbox/dso.h> // used inline
29 #include <lunchbox/file.h> // searchDirectory() used inline
30 #include <servus/uri.h> // Default template type
31 
32 #include <boost/foreach.hpp> // used inline
33 #include <boost/lexical_cast.hpp> // used inline
34 #include <boost/noncopyable.hpp> // base class
35 #include <boost/unordered_map.hpp>
36 
37 namespace lunchbox
38 {
39 
58 template< class PluginT, class InitDataT = servus::URI >
59 class PluginFactory : public boost::noncopyable
60 {
61 public:
63  typedef std::vector< PluginHolder > Plugins;
64 
66  static PluginFactory& getInstance();
67 
76  PluginT* create( const InitDataT& initData );
77 
79  void register_( const Plugin< PluginT, InitDataT >& plugin );
80 
82  bool deregister( const Plugin< PluginT, InitDataT >& plugin );
83 
85  void deregisterAll();
86 
106  DSOs load( const int version, const std::string& path,
107  const std::string& pattern );
108  DSOs load( const int version, const Strings& paths,
109  const std::string& pattern );
110 
117  bool unload( DSO* dso );
119 
120 private:
121 #pragma warning( disable: 4251 )
122  Plugins _plugins;
123  typedef boost::unordered_map< DSO*, PluginHolder > PluginMap;
124  PluginMap _libraries;
125 #pragma warning( default: 4251 )
126 
127  void _load( DSOs& result, const int version, const std::string& path,
128  const std::string& pattern );
129 
130  ~PluginFactory();
131 };
132 
133 }
134 
135 #include "pluginFactory.ipp" // template implementation
136 
137 #endif // LUNCHBOX_PLUGINFACTORY_H
Helper to access dynamic shared objects (DSO)
Definition: dso.h:35
Basic type definitions not provided by the operating system.
DSOs load(const int version, const std::string &path, const std::string &pattern)
Load all compatible plugin libraries from a directory matching a pattern.
std::vector< std::string > Strings
A vector of std::strings.
Definition: types.h:214
PluginT * create(const InitDataT &initData)
Create a plugin instance.
Factory for Plugin classes.
Definition: pluginFactory.h:59
bool deregister(const Plugin< PluginT, InitDataT > &plugin)
Deregister a plugin type.
bool unload(DSO *dso)
Unload and deregister a previously loaded plugin.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
Manages a class deriving from a PluginT interface.
Definition: plugin.h:47
static PluginFactory & getInstance()
Get the single class instance.
void register_(const Plugin< PluginT, InitDataT > &plugin)
Register a plugin type.
void deregisterAll()
Unregister all plugin types.