Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Implements a way to build backends
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 :
11 : #ifndef TOOLS_BACKEND_BUILDER_HPP
12 : #define TOOLS_BACKEND_BUILDER_HPP
13 :
14 :
15 : #include <memory>
16 : #include <set>
17 : #include <vector>
18 :
19 : #include <kdb.hpp>
20 :
21 : #include <backend.hpp>
22 : #include <plugin.hpp>
23 : #include <plugindatabase.hpp>
24 : #include <pluginspec.hpp>
25 :
26 : namespace kdb
27 : {
28 :
29 : namespace tools
30 : {
31 :
32 : /**
33 : * @brief Used as argument of constructor of *BackendBuilder
34 : *
35 : * Avoids the implementation of 5 Constructors for each of the
36 : * *BackendBuilder.
37 : */
38 1482 : class BackendBuilderInit
39 : {
40 : private:
41 : // typedef std::shared_ptr<PluginDatabase> PluginDatabasePtr;
42 : // -> moved to plugindatabase.hpp since it is used in public API
43 : PluginDatabasePtr pluginDatabase;
44 :
45 : BackendFactory backendFactory;
46 :
47 : public:
48 : BackendBuilderInit ();
49 : explicit BackendBuilderInit (PluginDatabasePtr const & plugins);
50 : explicit BackendBuilderInit (BackendFactory const & bf);
51 : BackendBuilderInit (PluginDatabasePtr const & plugins, BackendFactory const & bf);
52 : BackendBuilderInit (BackendFactory const & bf, PluginDatabasePtr const & plugins);
53 :
54 : PluginDatabasePtr getPluginDatabase () const
55 : {
56 848 : return pluginDatabase;
57 : }
58 :
59 : BackendFactory getBackendFactory () const
60 : {
61 848 : return backendFactory;
62 : }
63 : };
64 :
65 : /**
66 : * @brief Highlevel interface to build a backend.
67 : *
68 : * Automatically reorders plugins and has different modes which Backend
69 : * should be built.
70 : */
71 836 : class BackendBuilder : public BackendInterface
72 : {
73 : private:
74 : /// Defines order in which plugins should be added
75 : PluginSpecVector toAdd;
76 :
77 : /// Metadata to be added
78 : std::set<std::string> metadata;
79 :
80 : /// Needed plugins (not yet added)
81 : std::vector<std::string> neededPlugins;
82 :
83 : /// Recommended plugins (not yet added)
84 : std::vector<std::string> recommendedPlugins;
85 :
86 : // typedef std::shared_ptr<PluginDatabase> PluginDatabasePtr;
87 : // -> moved to plugindatabase.hpp since it is used in public API
88 : PluginDatabasePtr pluginDatabase;
89 :
90 : BackendFactory backendFactory;
91 :
92 : private:
93 : void sort ();
94 : void collectNeeds (std::vector<std::string> & needs) const;
95 : void collectRecommends (std::vector<std::string> & recommends) const;
96 : void removeProvided (std::vector<std::string> & needs) const;
97 : void removeMetadata (std::set<std::string> & needsMetadata) const;
98 :
99 : protected:
100 : KeySet backendConf;
101 :
102 : public:
103 : explicit BackendBuilder (BackendBuilderInit const & bbi = BackendBuilderInit ());
104 :
105 : typedef PluginSpecVector::const_iterator const_iterator;
106 :
107 : const_iterator begin () const
108 : {
109 351 : return toAdd.begin ();
110 : }
111 : const_iterator end () const
112 : {
113 301 : return toAdd.end ();
114 : }
115 : const_iterator cbegin () const
116 : {
117 158 : return toAdd.begin ();
118 : }
119 : const_iterator cend () const
120 : {
121 72 : return toAdd.end ();
122 : }
123 :
124 : PluginDatabasePtr const & getPluginDatabase () const
125 : {
126 1 : return pluginDatabase;
127 : }
128 :
129 : BackendFactory const & getBackendFactory () const
130 : {
131 852 : return backendFactory;
132 : }
133 :
134 : ~BackendBuilder ();
135 :
136 : void addPlugins (PluginSpecVector const & plugins)
137 : {
138 2258 : for (auto const & plugin : plugins)
139 : {
140 282 : addPlugin (plugin);
141 : }
142 : }
143 :
144 : void addPlugin (PluginSpec const & plugin);
145 : void remPlugin (PluginSpec const & plugin);
146 :
147 : void needMetadata (std::string metadata);
148 : void needPlugin (std::string provider);
149 : std::vector<std::string> resolveNeeds (bool addRecommends = true);
150 :
151 : void recommendPlugin (std::string provider);
152 :
153 : void fillPlugins (BackendInterface & b) const;
154 :
155 : void setBackendConfig (KeySet const & ks);
156 : KeySet getBackendConfig ();
157 : };
158 :
159 : /**
160 : * @brief Build global plugins
161 : */
162 2 : class GlobalPluginsBuilder : public BackendBuilder
163 : {
164 : public:
165 : explicit GlobalPluginsBuilder (BackendBuilderInit const & bbi = BackendBuilderInit ());
166 : static const char * globalPluginsPath;
167 : void serialize (kdb::KeySet & ret);
168 : };
169 :
170 : /**
171 : * @brief High-level functionality to build a mountpoint
172 : *
173 : * will enforce resolver and storage to be present
174 : */
175 2592 : class MountBackendBuilder : public MountBackendInterface, public BackendBuilder
176 : {
177 : Key mountpoint;
178 :
179 : /**
180 : * Contains the keys of system/elektra/mountpoints.
181 : * It is needed to detect if a mountpoint already exists.
182 : */
183 : KeySet mountConf;
184 :
185 : std::string configfile;
186 :
187 : public:
188 641 : void addPlugin (PluginSpec const & spec)
189 : {
190 1080 : return BackendBuilder::addPlugin (spec);
191 : }
192 :
193 : explicit MountBackendBuilder (BackendBuilderInit const & bbi = BackendBuilderInit ());
194 :
195 : void setMountpoint (Key mountpoint, KeySet mountConf);
196 : std::string getMountpoint () const;
197 :
198 : void setBackendConfig (KeySet const & ks);
199 :
200 : void useConfigFile (std::string file);
201 : std::string getConfigFile () const;
202 :
203 : void serialize (kdb::KeySet & ret);
204 :
205 : void status (std::ostream & os) const;
206 : bool validated () const;
207 : };
208 : } // namespace tools
209 : } // namespace kdb
210 :
211 : #endif
|