Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Source for cpptemplate plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include "cpptemplate.hpp"
11 : #include "cpptemplate_delegate.hpp"
12 :
13 : #include <kdberrors.h>
14 : #include <kdbhelper.h>
15 :
16 : using std::exception;
17 :
18 : using elektra::CppTemplateDelegate;
19 :
20 : using CppKey = kdb::Key;
21 : using CppKeySet = kdb::KeySet;
22 :
23 : namespace
24 : {
25 :
26 : /**
27 : * @brief This function returns a key set containing the plugin contract.
28 : *
29 : * @return A key set specifying the capabilities of the plugin
30 : */
31 33 : CppKeySet getContract ()
32 : {
33 : return CppKeySet{ 30,
34 : keyNew ("system/elektra/modules/cpptemplate", KEY_VALUE, "cpptemplate plugin waits for your orders", KEY_END),
35 : keyNew ("system/elektra/modules/cpptemplate/exports", KEY_END),
36 : keyNew ("system/elektra/modules/cpptemplate/exports/open", KEY_FUNC, elektraCppTemplateOpen, KEY_END),
37 : keyNew ("system/elektra/modules/cpptemplate/exports/close", KEY_FUNC, elektraCppTemplateClose, KEY_END),
38 : keyNew ("system/elektra/modules/cpptemplate/exports/get", KEY_FUNC, elektraCppTemplateGet, KEY_END),
39 : keyNew ("system/elektra/modules/cpptemplate/exports/set", KEY_FUNC, elektraCppTemplateSet, KEY_END),
40 : keyNew ("system/elektra/modules/cpptemplate/exports/error", KEY_FUNC, elektraCppTemplateError, KEY_END),
41 : keyNew ("system/elektra/modules/cpptemplate/exports/checkconf", KEY_FUNC, elektraCppTemplateCheckConfig, KEY_END),
42 : #include ELEKTRA_README
43 : keyNew ("system/elektra/modules/cpptemplate/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END),
44 33 : KS_END };
45 : }
46 :
47 : } // end namespace
48 :
49 : extern "C" {
50 :
51 : typedef Delegator<CppTemplateDelegate> delegator;
52 :
53 : /** @see elektraDocOpen */
54 49 : int elektraCppTemplateOpen (Plugin * handle, Key * key)
55 : {
56 49 : int status = ELEKTRA_PLUGIN_STATUS_ERROR;
57 :
58 : try
59 : {
60 : // - The function below calls the constructor `CppTemplateDelegate(config)`.
61 : // - After the call to `delegator::open` you can retrieve a pointer to the delegate via `delegator::get (handle)`.
62 49 : status = delegator::open (handle, key);
63 : }
64 0 : catch (exception const & error)
65 : {
66 0 : ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (key, "Uncaught Exception: %s", error.what ());
67 : }
68 :
69 49 : return status;
70 : }
71 :
72 : /** @see elektraDocClose */
73 49 : int elektraCppTemplateClose (Plugin * handle, Key * key)
74 : {
75 : // The function `delegator::close` calls the destructor of `CppTemplateDelegate`.
76 49 : return delegator::close (handle, key);
77 : }
78 :
79 : /** @see elektraDocGet */
80 36 : int elektraCppTemplateGet (Plugin * handle, KeySet * returned, Key * parentKey)
81 : {
82 72 : CppKeySet keys{ returned };
83 72 : CppKey parent{ parentKey };
84 :
85 108 : if (parent.getName () == "system/elektra/modules/cpptemplate")
86 : {
87 99 : keys.append (getContract ());
88 : }
89 : else
90 : {
91 : // This is only an example, to show you how to call a method of the delegate
92 9 : keys.append (delegator::get (handle)->getConfig (parent));
93 : }
94 :
95 36 : parent.release ();
96 36 : keys.release ();
97 72 : return ELEKTRA_PLUGIN_STATUS_SUCCESS;
98 : }
99 :
100 : /** @see elektraDocSet */
101 2 : int elektraCppTemplateSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
102 : {
103 2 : return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
104 : }
105 :
106 : /** @see elektraDocError */
107 2 : int elektraCppTemplateError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
108 : {
109 2 : return ELEKTRA_PLUGIN_STATUS_SUCCESS;
110 : }
111 :
112 : /** @see elektraDocCheckConf */
113 1 : int elektraCppTemplateCheckConfig (Key * errorKey ELEKTRA_UNUSED, KeySet * conf ELEKTRA_UNUSED)
114 : {
115 1 : return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
116 : }
117 :
118 49 : Plugin * ELEKTRA_PLUGIN_EXPORT
119 : {
120 : // clang-format off
121 : return elektraPluginExport ("cpptemplate",
122 : ELEKTRA_PLUGIN_OPEN, &elektraCppTemplateOpen,
123 : ELEKTRA_PLUGIN_CLOSE, &elektraCppTemplateClose,
124 : ELEKTRA_PLUGIN_GET, &elektraCppTemplateGet,
125 : ELEKTRA_PLUGIN_SET, &elektraCppTemplateSet,
126 : ELEKTRA_PLUGIN_ERROR, &elektraCppTemplateError,
127 49 : ELEKTRA_PLUGIN_END);
128 : }
129 :
130 : } // end extern "C"
|