LCOV - code coverage report
Current view: top level - lunchbox - plugin.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 7 7 100.0 %
Date: 2016-11-11 05:21:33 Functions: 5 5 100.0 %

          Line data    Source code
       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             : {
      30             : /**
      31             :  * Manages a class deriving from a T interface.
      32             :  *
      33             :  * Plugin classes deriving from T must implement the following
      34             :  * prototype for their constructor:
      35             :  * @code
      36             :  * DerivedPluginClass( const T::InitDataT& initData );
      37             :  * @endcode
      38             :  *
      39             :  * T must also implement the following method to be registered:
      40             :  * @code
      41             :  * static bool handles( const T::InitDataT& initData );
      42             :  * @endcode
      43             :  *
      44             :  * Note this requires a 'typedef [foo] InitDataT' in T.
      45             :  * @version 1.11.0
      46             :  */
      47          14 : template< class T > class Plugin
      48             : {
      49             : public:
      50             :     /**
      51             :      * The constructor method / concrete factory for Plugin objects.
      52             :      * @version 1.11.0
      53             :      */
      54             :     typedef std::function< T* ( const typename T::InitDataT& )> Constructor;
      55             : 
      56             :     /**
      57             :      * The method to check if the plugin can handle a given initData.
      58             :      * @version 1.11.0
      59             :      */
      60             :     typedef std::function< bool ( const typename T::InitDataT& )> HandlesFunc;
      61             : 
      62             :     /**
      63             :      * Construct a new Plugin.
      64             :      * @param constructor_ The constructor method for Plugin objects.
      65             :      * @param handles_ The method to check if the plugin can handle the
      66             :      * initData.
      67             :      * @version 1.11.0
      68             :      */
      69           4 :     Plugin( const Constructor& constructor, const HandlesFunc& handles_ )
      70             :         : _constructor( constructor ), _handles( handles_ )
      71           4 :         , tag( servus::make_UUID( )) {}
      72             : 
      73             :     /** @return true if the plugins wrap the same plugin. @version 1.11.0 */
      74             :     bool operator == ( const Plugin& rhs ) const
      75             :         { return tag == rhs.tag; }
      76             : 
      77             :     /** @return false if the plugins do wrap the same plugin. @version 1.11.0 */
      78             :     bool operator != ( const Plugin& rhs ) const { return !(*this == rhs); }
      79             : 
      80             :     /** Construct a new plugin instance. @version 1.14 */
      81           2 :     T* construct( const typename T::InitDataT& data )
      82           2 :         { return _constructor( data ); }
      83             : 
      84             :     /** @return true if this plugin handles the given request. @version 1.14 */
      85           4 :     bool handles( const typename T::InitDataT& data )
      86           4 :         { 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

Generated by: LCOV version 1.11