LCOV - code coverage report
Current view: top level - src/bindings/cpp/include - kdbplugin.hpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 15 17 88.2 %
Date: 2019-09-12 12:28:41 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Helpers for creating plugins
       5             :  *
       6             :  * Make sure to include kdberrors.h before including this file if you want
       7             :  * warnings/errors to be added.
       8             :  *
       9             :  * Proper usage:
      10             :  * @code
      11             : using namespace ckdb;
      12             : #include <kdberrors.h>
      13             : #include <kdbplugin.hpp>
      14             : 
      15             : typedef Delegator<elektra::YourPluginClass> YPC;
      16             : // then e.g. YPC::open(handle, errorKey);
      17             :  * @endcode
      18             :  *
      19             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
      20             :  *
      21             :  */
      22             : 
      23             : #ifndef KDBPLUGIN_HPP
      24             : #define KDBPLUGIN_HPP
      25             : 
      26             : #include <kdbplugin.h>
      27             : #include <key.hpp>
      28             : #include <keyset.hpp>
      29             : 
      30             : template <typename Delegated>
      31             : class Delegator
      32             : {
      33             : public:
      34             :         typedef Delegated * (*Builder) (kdb::KeySet config);
      35             : 
      36        1630 :         inline static Delegated * defaultBuilder (kdb::KeySet config)
      37             :         {
      38        3260 :                 return new Delegated (config);
      39             :         }
      40             : 
      41        2839 :         inline static int open (ckdb::Plugin * handle, ckdb::Key * errorKey, Builder builder = defaultBuilder)
      42             :         {
      43        8517 :                 kdb::KeySet config (elektraPluginGetConfig (handle));
      44        2839 :                 int ret = openHelper (handle, config, errorKey, builder);
      45        2839 :                 config.release ();
      46        5678 :                 return ret;
      47             :         }
      48             : 
      49         862 :         inline static int close (ckdb::Plugin * handle, ckdb::Key *)
      50             :         {
      51        2861 :                 delete get (handle);
      52         862 :                 return 1; // always successfully
      53             :         }
      54             : 
      55             :         inline static Delegated * get (ckdb::Plugin * handle)
      56             :         {
      57        4984 :                 return static_cast<Delegated *> (elektraPluginGetData (handle));
      58             :         }
      59             : 
      60             : private:
      61             :         /**This function avoid that every return path need to release the
      62             :          * configuration. */
      63        2839 :         inline static int openHelper (ckdb::Plugin * handle, kdb::KeySet & config, ckdb::Key * errorKey, Builder builder)
      64             :         {
      65       19873 :                 if (config.lookup ("/module"))
      66             :                 {
      67             :                         // suppress warnings if it is just a module
      68             :                         // don't buildup the Delegated then
      69             :                         return 0;
      70             :                 }
      71             : 
      72             :                 try
      73             :                 {
      74        3260 :                         elektraPluginSetData (handle, (*builder) (config));
      75             :                 }
      76           0 :                 catch (const char * msg)
      77             :                 {
      78             : #ifdef KDBERRORS_H
      79           0 :                         ELEKTRA_ADD_PLUGIN_MISBEHAVIOR_WARNINGF (errorKey, "Could not create C++ plugin. Reason: %s", msg);
      80             : #endif
      81             :                         return -1;
      82             :                 }
      83             : 
      84        1630 :                 return get (handle) != nullptr ? 1 : -1;
      85             :         }
      86             : };
      87             : 
      88             : #endif

Generated by: LCOV version 1.13