Lunchbox  1.15.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
plugin.h
1 
2 /* Copyright (c) 2013-2016, 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_PLUGIN_H
23 #define LUNCHBOX_PLUGIN_H
24 
25 #include <servus/uint128_t.h> // member
26 #include <functional>
27 
28 namespace lunchbox
29 {
47 template< class T > class Plugin
48 {
49 public:
54  typedef std::function< T* ( const typename T::InitDataT& )> Constructor;
55 
60  typedef std::function< bool ( const typename T::InitDataT& )> HandlesFunc;
61 
69  Plugin( const Constructor& constructor, const HandlesFunc& handles_ )
70  : _constructor( constructor ), _handles( handles_ )
71  , tag( servus::make_UUID( )) {}
72 
74  bool operator == ( const Plugin& rhs ) const
75  { return tag == rhs.tag; }
76 
78  bool operator != ( const Plugin& rhs ) const { return !(*this == rhs); }
79 
81  T* construct( const typename T::InitDataT& data )
82  { return _constructor( data ); }
83 
85  bool handles( const typename T::InitDataT& data )
86  { return _handles( data ); }
87 
88 private:
89  Constructor _constructor;
90  HandlesFunc _handles;
91 
92  // Makes Plugin comparable. See http://stackoverflow.com/questions/18665515
93  servus::uint128_t tag;
94 };
95 
96 }
97 
98 #endif
bool handles(const typename T::InitDataT &data)
Definition: plugin.h:85
std::function< T *(const typename T::InitDataT &)> Constructor
The constructor method / concrete factory for Plugin objects.
Definition: plugin.h:54
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
Manages a class deriving from a T interface.
Definition: plugin.h:47
Plugin(const Constructor &constructor, const HandlesFunc &handles_)
Construct a new Plugin.
Definition: plugin.h:69
bool operator==(const Plugin &rhs) const
Definition: plugin.h:74
bool operator!=(const Plugin &rhs) const
Definition: plugin.h:78
std::function< bool(const typename T::InitDataT &)> HandlesFunc
The method to check if the plugin can handle a given initData.
Definition: plugin.h:60
T * construct(const typename T::InitDataT &data)
Construct a new plugin instance.
Definition: plugin.h:81