summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/include/wsrep/provider_options.hpp
blob: 022e159e7ee459e9878c453c0fa0d01531e6807c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (C) 2021 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

/** @file provider_options.hpp
 *
 * A proxy interface to control provider configuration options.
 */

#ifndef WSREP_PROVIDER_OPTIONS_HPP
#define WSREP_PROVIDER_OPTIONS_HPP

#include "provider.hpp"

#include <functional>
#include <map>
#include <memory>
#include <string>

namespace wsrep
{
    /**
     * Proxy class for managing provider options.
     * Options are strings by default, or flagged with corresponding
     * provider_options::flag as either bool, integer or double.
     */
    class provider_options
    {
    public:
        struct flag
        {
            static const int deprecated = (1 << 0);
            static const int readonly = (1 << 1);
            static const int type_bool = (1 << 2);
            static const int type_integer = (1 << 3);
            static const int type_double = (1 << 4);
        };

        static const int flag_type_mask
            = flag::type_bool | flag::type_integer | flag::type_double;

        class option_value
        {
        public:
            virtual ~option_value() {}
            virtual const char* as_string() const = 0;
            virtual const void* get_ptr() const = 0;
        };

        class option_value_string : public option_value
        {
        public:
            option_value_string(const std::string& value)
                : value_(value)
            {
            }
            ~option_value_string() WSREP_OVERRIDE {}
            const char* as_string() const WSREP_OVERRIDE
            {
                return value_.c_str();
            }
            const void* get_ptr() const WSREP_OVERRIDE
            {
                return value_.c_str();
            }

        private:
            std::string value_;
        };

        class option_value_bool : public option_value
        {
        public:
            option_value_bool(bool value)
                : value_(value)
            {
            }
            ~option_value_bool() WSREP_OVERRIDE {}
            const char* as_string() const WSREP_OVERRIDE
            {
                if (value_)
                {
                    return "yes";
                }
                else
                {
                    return "no";
                }
            }
            const void* get_ptr() const WSREP_OVERRIDE { return &value_; }

        private:
            bool value_;
        };

        class option_value_int : public option_value
        {
        public:
            option_value_int(int64_t value)
                : value_(value)
                , value_str_(std::to_string(value))
            {
            }
            ~option_value_int() WSREP_OVERRIDE {}
            const char* as_string() const WSREP_OVERRIDE
            {
                return value_str_.c_str();
            }
            const void* get_ptr() const WSREP_OVERRIDE { return &value_; }

        private:
            int64_t value_;
            std::string value_str_;
        };

        class option_value_double : public option_value
        {
        public:
            option_value_double(double value)
                : value_(value)
                , value_str_(std::to_string(value))
            {
            }
            ~option_value_double() WSREP_OVERRIDE {}
            const char* as_string() const WSREP_OVERRIDE
            {
                return value_str_.c_str();
            }
            const void* get_ptr() const WSREP_OVERRIDE { return &value_; }

        private:
            double value_;
            std::string value_str_;
        };

        class option
        {
        public:
            option();
            /** Construct option with given values. Allocates
             * memory. */
            option(const std::string& name, std::unique_ptr<option_value> value,
                   std::unique_ptr<option_value> default_value, int flags);
            /** Non copy-constructible. */
            option(const option&) = delete;
            /** Non copy-assignable. */
            option& operator=(const option&) = delete;
            option(option&&) = delete;
            option& operator=(option&&) = delete;
            ~option();

            /**
             * Get name of the option.
             *
             * @return Name of the option.
             */
            const char* name() const { return name_.c_str(); }

            /**
             * Get the real name of the option. This displays the
             * option name as it is visible in provider options.
             */
            const char* real_name() const { return real_name_.c_str(); }

            /**
             * Get value of the option.
             *
             * @return Value of the option.
             */
            option_value* value() const { return value_.get(); }

            /**
             * Get default value of the option.
             *
             * @return Default value of the option.
             */
            option_value* default_value() const { return default_value_.get(); }

            /**
             * Get flags of the option
             *
             * @return Flag of the option
             */
            int flags() const { return flags_; }

            /**
             * Update the value of the option with new_value. The old
             * value is freed.
             */
            void update_value(std::unique_ptr<option_value> new_value);

        private:
            /** Sanitized name with dots replaced with underscores */
            std::string name_;
            /** Real name in provider */
            std::string real_name_;
            std::unique_ptr<option_value> value_;
            std::unique_ptr<option_value> default_value_;
            int flags_;
        };

        provider_options(wsrep::provider&);
        provider_options(const provider_options&) = delete;
        provider_options& operator=(const provider_options&) = delete;

        /**
         * Set initial options. This should be used to initialize
         * provider options after the provider has been loaded.
         * Individual options should be accessed through set()/get().
         *
         * @return Provider status code.
         */
        enum wsrep::provider::status initial_options();

        /**
         * Get the option with the given name
         *
         * @param name Name of the option to retrieve
         */
        const option* get_option(const std::string& name) const;

        /**
         * Set a value for the option.
         *
         * @return Wsrep provider status code.
         * @return wsrep::provider::error_size_exceeded if memory could
         *         not be allocated for the new value.
         */
        enum wsrep::provider::status set(const std::string& name,
                                         std::unique_ptr<option_value> value);

        /**
         * Create a new option with default value.
         */
        enum wsrep::provider::status
        set_default(const std::string& name,
                    std::unique_ptr<option_value> value,
                    std::unique_ptr<option_value> default_value, int flags);

        void for_each(const std::function<void(option*)>& fn);

    private:
        provider& provider_;
        using options_map = std::map<std::string, std::unique_ptr<option>>;
        options_map options_;
    };

    /**
     * Equality operator for provider option. Returns true if
     * the name part is equal.
     */
    bool operator==(const wsrep::provider_options::option&,
                    const wsrep::provider_options::option&);
} // namespace wsrep

#endif // WSREP_PROVIDER_OPTIONS_HPP