source: nscp/include/settings/settings_core.hpp @ 291548e

0.4.00.4.10.4.2
Last change on this file since 291548e was 291548e, checked in by Michael Medin <michael@…>, 3 years ago

Tweaks to the settings subsystem (thread safeness) as well as additions to the command line client and such...

  • Property mode set to 100644
File size: 20.6 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#pragma once
22
23#include <types.hpp>
24#include <Singleton.h>
25#include <string>
26#include <map>
27#include <set>
28#include <boost/thread/thread.hpp>
29#include <boost/thread/locks.hpp>
30#include <boost/filesystem/path.hpp>
31#include <boost/regex.hpp>
32#include <strEx.h>
33#define BUFF_LEN 4096
34
35namespace settings {
36
37
38        template<class T>
39        struct instance_ptr_helper {
40                boost::shared_ptr<T> ptr_;
41                boost::unique_lock<boost::timed_mutex> *mutex_;
42                instance_ptr_helper(boost::shared_ptr<T> ptr, boost::timed_mutex &mutex, int timeout) : ptr_(ptr), mutex_(NULL) {
43                        mutex_ = new boost::unique_lock<boost::timed_mutex>(mutex, boost::get_system_time() + boost::posix_time::seconds(timeout));
44                        std::wcout << _T("creating (safe)...") << std::endl;
45                        if (!mutex_->owns_lock())
46                                throw settings_exception(_T("Failed to get mutex, cant get settings instance"));
47                }
48                instance_ptr_helper(boost::shared_ptr<T> ptr) : ptr_(ptr) {
49                        std::wcout << _T("creating (unsafe)...") << std::endl;
50                        delete mutex_;
51                }
52                ~instance_ptr_helper() {
53                        std::wcout << _T("destroying...") << std::endl;
54                }
55                boost::shared_ptr<T> operator* () const {
56                        return ptr_;
57                }
58
59                boost::shared_ptr<T> operator-> () const {
60                        return ptr_;
61                }
62                bool operator! () const {
63                        return !ptr_;
64                }
65        };
66        class settings_exception {
67                std::wstring error_;
68        public:
69                //////////////////////////////////////////////////////////////////////////
70                /// Constructor takes an error message.
71                /// @param error the error message
72                ///
73                /// @author mickem
74                settings_exception(std::wstring error) : error_(error) {}
75
76                //////////////////////////////////////////////////////////////////////////
77                /// Retrieve the error message from the exception.
78                /// @return the error message
79                ///
80                /// @author mickem
81                std::wstring getError() const { return error_; }
82                std::wstring getMessage() const { return error_; }
83        };
84        class KeyNotFoundException : public settings_exception {
85        public:
86                KeyNotFoundException(std::wstring path, std::wstring key) : settings_exception(_T("Key not found: ")+ path + _T(" ") + key) {}
87                KeyNotFoundException(std::wstring path) : settings_exception(_T("Key not found: ")+ path) {}
88                KeyNotFoundException(std::pair<std::wstring,std::wstring> key) : settings_exception(_T("Key not found: ")+ key.first + _T(" ") + key.second) {}
89        };
90
91        class logger_interface {
92                public:
93                        //////////////////////////////////////////////////////////////////////////
94                        /// Log an ERROR message.
95                        ///
96                        /// @param file the file where the event happened
97                        /// @param line the line where the event happened
98                        /// @param message the message to log
99                        ///
100                        /// @author mickem
101                        virtual void err(std::wstring file, int line, std::wstring message) = 0;
102                        //////////////////////////////////////////////////////////////////////////
103                        /// Log an WARNING message.
104                        ///
105                        /// @param file the file where the event happened
106                        /// @param line the line where the event happened
107                        /// @param message the message to log
108                        ///
109                        /// @author mickem
110                        virtual void warn(std::wstring file, int line, std::wstring message) = 0;
111                        //////////////////////////////////////////////////////////////////////////
112                        /// Log an INFO message.
113                        ///
114                        /// @param file the file where the event happened
115                        /// @param line the line where the event happened
116                        /// @param message the message to log
117                        ///
118                        /// @author mickem
119                        virtual void info(std::wstring file, int line, std::wstring message) = 0;
120                        //////////////////////////////////////////////////////////////////////////
121                        /// Log an DEBUG message.
122                        ///
123                        /// @param file the file where the event happened
124                        /// @param line the line where the event happened
125                        /// @param message the message to log
126                        ///
127                        /// @author mickem
128                        virtual void debug(std::wstring file, int line, std::wstring message) = 0;
129
130                        void quick_debug(std::wstring message) {
131                                debug(__FILEW__, __LINE__, message);
132                        }
133        };
134
135        class settings_interface;
136        typedef boost::shared_ptr<settings_interface> instance_ptr;
137        typedef boost::shared_ptr<settings_interface> instance_raw_ptr;
138
139        class settings_core {
140        public:
141                typedef std::list<std::wstring> string_list;
142                typedef enum {
143                        key_string = 100,
144                        key_integer = 200,
145                        key_bool = 300,
146                } key_type;
147                typedef std::pair<std::wstring,std::wstring> key_path_type;
148                struct key_description {
149                        std::wstring title;
150                        std::wstring description;
151                        key_type type;
152                        std::wstring defValue;
153                        bool advanced;
154                        key_description(std::wstring title_, std::wstring description_, settings_core::key_type type_, std::wstring defValue_, bool advanced_)
155                                : title(title_), description(description_), type(type_), defValue(defValue_), advanced(advanced_) {}
156                        key_description() : advanced(false), type(settings_core::key_string) {}
157                };
158                struct path_description {
159                        std::wstring title;
160                        std::wstring description;
161                        bool advanced;
162                        typedef std::map<std::wstring,key_description> keys_type;
163                        keys_type keys;
164                        path_description(std::wstring title_, std::wstring description_, bool advanced_) : title(title_), description(description_), advanced(advanced_) {}
165                        path_description() : advanced(false) {}
166                        void update(std::wstring title_, std::wstring description_, bool advanced_) {
167                                title = title_;
168                                description = description_;
169                                advanced = advanced_;
170                        }
171                };
172
173                struct mapped_key {
174                        mapped_key(key_path_type src_, key_path_type dst_) : src(src_), dst(dst_) {}
175                        key_path_type src;
176                        key_path_type dst;
177                };
178                typedef std::list<mapped_key> mapped_key_list_type;
179
180                //////////////////////////////////////////////////////////////////////////
181                /// Register a path with the settings module.
182                /// A registered key or path will be nicely documented in some of the settings files when converted.
183                ///
184                /// @param path The path to register
185                /// @param title The title to use
186                /// @param description the description to use
187                /// @param advanced advanced options will only be included if they are changed
188                ///
189                /// @author mickem
190                virtual void register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced = false) = 0;
191
192                //////////////////////////////////////////////////////////////////////////
193                /// Register a key with the settings module.
194                /// A registered key or path will be nicely documented in some of the settings files when converted.
195                ///
196                /// @param path The path to register
197                /// @param key The key to register
198                /// @param type The type of value
199                /// @param title The title to use
200                /// @param description the description to use
201                /// @param defValue the default value
202                /// @param advanced advanced options will only be included if they are changed
203                ///
204                /// @author mickem
205                virtual void register_key(std::wstring path, std::wstring key, key_type type, std::wstring title, std::wstring description, std::wstring defValue, bool advanced = false) = 0;
206                //////////////////////////////////////////////////////////////////////////
207                /// Get info about a registered key.
208                /// Used when writing settings files.
209                ///
210                /// @param path The path of the key
211                /// @param key The key of the key
212                /// @return the key description
213                ///
214                /// @author mickem
215                virtual key_description get_registred_key(std::wstring path, std::wstring key) = 0;
216
217                virtual settings_core::path_description get_registred_path(std::wstring path) = 0;
218
219                //////////////////////////////////////////////////////////////////////////
220                /// Get all registered sections
221                ///
222                /// @return a list of section paths
223                ///
224                /// @author mickem
225                virtual string_list get_reg_sections() = 0;
226                //////////////////////////////////////////////////////////////////////////
227                /// Get all keys for a registered section.
228                ///
229                /// @param path the path to find keys under
230                /// @return a list of key names
231                ///
232                /// @author mickem
233                virtual string_list get_reg_keys(std::wstring path) = 0;
234
235                //////////////////////////////////////////////////////////////////////////
236                /// Get the currently active settings interface.
237                ///
238                /// @return the currently active interface
239                ///
240                /// @author mickem
241                virtual instance_ptr get() = 0;
242                virtual instance_ptr get_no_wait() = 0;
243               
244                //////////////////////////////////////////////////////////////////////////
245                /// Get a settings interface
246                ///
247                /// @param type the type of settings interface to get
248                /// @return the settings interface
249                ///
250                /// @author mickem
251                //virtual settings_interface* get(settings_core::settings_type type) = 0;
252                // Conversion Functions
253                virtual void migrate(instance_ptr from, instance_ptr to) = 0;
254                virtual void migrate_to(instance_ptr to) = 0;
255                virtual void migrate_from(instance_ptr from) = 0;
256                virtual void migrate(std::wstring from, std::wstring to) = 0;
257                virtual void migrate_to(std::wstring to) = 0;
258                virtual void migrate_from(std::wstring from) = 0;
259
260                //////////////////////////////////////////////////////////////////////////
261                /// Overwrite the (current) settings store with default values.
262                ///
263                /// @author mickem
264                virtual void update_defaults() = 0;
265
266
267
268
269               
270                //////////////////////////////////////////////////////////////////////////
271                /// Boot the settings subsystem from the given file (boot.ini).
272                ///
273                /// @param file the file to use when booting.
274                ///
275                /// @author mickem
276                virtual void boot(std::wstring file = _T("boot.ini")) = 0;
277
278                virtual std::wstring find_file(std::wstring file, std::wstring fallback) = 0;
279
280                //////////////////////////////////////////////////////////////////////////
281                /// Get a string form the boot file.
282                ///
283                /// @param section section to read a value from.
284                /// @param key the key to read.
285                /// @param def a default value.
286                /// @return the value of the key or the default value.
287                ///
288                /// @author mickem
289                virtual std::wstring get_boot_string(std::wstring section, std::wstring key, std::wstring def) = 0;
290
291                //////////////////////////////////////////////////////////////////////////
292                /// Create an instance of a given type.
293                /// Used internally to create instances of various settings types.
294                ///
295                /// @param context the context to use
296                /// @return a new instance of given type.
297                ///
298                /// @author mickem
299                virtual instance_raw_ptr create_instance(std::wstring context) = 0;
300
301                //////////////////////////////////////////////////////////////////////////
302                /// Set the basepath for the settings subsystem.
303                /// In other words set where the settings files reside
304                ///
305                /// @param path the path to the settings files
306                ///
307                /// @author mickem
308                virtual void set_base(boost::filesystem::wpath path) = 0;
309
310                //////////////////////////////////////////////////////////////////////////
311                /// Get the basepath for the settings subsystem.
312                /// In other words get where the settings files reside
313                ///
314                /// @return the path to the settings files
315                ///
316                /// @author mickem
317                virtual boost::filesystem::wpath get_base() = 0;
318
319                //////////////////////////////////////////////////////////////////////////
320                /// Set the logging interface (will receive log messages)
321                ///
322                /// @param logger the new logger to use
323                ///
324                /// @author mickem
325                virtual void set_logger(logger_interface *logger) = 0;
326                //////////////////////////////////////////////////////////////////////////
327                /// Get the logging interface (will receive log messages)
328                ///
329                /// @return the logger to use
330                ///
331                /// @author mickem
332                virtual logger_interface* get_logger() = 0;
333
334                virtual std::wstring to_string() = 0;
335
336        };
337
338        class settings_interface {
339        public:
340                typedef std::list<std::wstring> string_list;
341
342                //////////////////////////////////////////////////////////////////////////
343                /// Set the core module to use
344                ///
345                /// @param core The core to use
346                ///
347                /// @author mickem
348                virtual void set_core(settings_core *core) = 0;
349
350                //////////////////////////////////////////////////////////////////////////
351                /// Empty all cached settings values and force a reload.
352                /// Notice this does not save so any "active" values will be flushed and new ones read from file.
353                ///
354                /// @author mickem
355                virtual void clear_cache() = 0;
356
357                //////////////////////////////////////////////////////////////////////////
358                /// Get the type of a key (String, int, bool)
359                ///
360                /// @param path the path to get type for
361                /// @param key the key to get the type for
362                /// @return the type of the key
363                ///
364                /// @author mickem
365                virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) = 0;
366
367                //////////////////////////////////////////////////////////////////////////
368                /// Get a string value if it does not exist exception will be thrown
369                ///
370                /// @param path the path to look up
371                /// @param key the key to lookup
372                /// @return the string value
373                ///
374                /// @author mickem
375                virtual std::wstring get_string(std::wstring path, std::wstring key) = 0;
376                //////////////////////////////////////////////////////////////////////////
377                /// Get a string value if it does not exist the default value will be returned
378                ///
379                /// @param path the path to look up
380                /// @param key the key to lookup
381                /// @param def the default value to use when no value is found
382                /// @return the string value
383                ///
384                /// @author mickem
385                virtual std::wstring get_string(std::wstring path, std::wstring key, std::wstring def) = 0;
386                //////////////////////////////////////////////////////////////////////////
387                /// Set or update a string value
388                ///
389                /// @param path the path to look up
390                /// @param key the key to lookup
391                /// @param value the value to set
392                ///
393                /// @author mickem
394                virtual void set_string(std::wstring path, std::wstring key, std::wstring value) = 0;
395
396                //////////////////////////////////////////////////////////////////////////
397                /// Get an integer value if it does not exist exception will be thrown
398                ///
399                /// @param path the path to look up
400                /// @param key the key to lookup
401                /// @return the string value
402                ///
403                /// @author mickem
404                virtual int get_int(std::wstring path, std::wstring key) = 0;
405                //////////////////////////////////////////////////////////////////////////
406                /// Get an integer value if it does not exist the default value will be returned
407                ///
408                /// @param path the path to look up
409                /// @param key the key to lookup
410                /// @param def the default value to use when no value is found
411                /// @return the string value
412                ///
413                /// @author mickem
414                virtual int get_int(std::wstring path, std::wstring key, int def) = 0;
415                //////////////////////////////////////////////////////////////////////////
416                /// Set or update an integer value
417                ///
418                /// @param path the path to look up
419                /// @param key the key to lookup
420                /// @param value the value to set
421                ///
422                /// @author mickem
423                virtual void set_int(std::wstring path, std::wstring key, int value) = 0;
424
425                //////////////////////////////////////////////////////////////////////////
426                /// Get a boolean value if it does not exist exception will be thrown
427                ///
428                /// @param path the path to look up
429                /// @param key the key to lookup
430                /// @return the string value
431                ///
432                /// @author mickem
433                virtual bool get_bool(std::wstring path, std::wstring key) = 0;
434                //////////////////////////////////////////////////////////////////////////
435                /// Get a boolean value if it does not exist the default value will be returned
436                ///
437                /// @param path the path to look up
438                /// @param key the key to lookup
439                /// @param def the default value to use when no value is found
440                /// @return the string value
441                ///
442                /// @author mickem
443                virtual bool get_bool(std::wstring path, std::wstring key, bool def) = 0;
444                //////////////////////////////////////////////////////////////////////////
445                /// Set or update a boolean value
446                ///
447                /// @param path the path to look up
448                /// @param key the key to lookup
449                /// @param value the value to set
450                ///
451                /// @author mickem
452                virtual void set_bool(std::wstring path, std::wstring key, bool value) = 0;
453
454                // Meta Functions
455                //////////////////////////////////////////////////////////////////////////
456                /// Get all (sub) sections (given a path).
457                /// If the path is empty all root sections will be returned
458                ///
459                /// @param path The path to get sections from (if empty root sections will be returned)
460                /// @return a list of sections
461                ///
462                /// @author mickem
463                virtual string_list get_sections(std::wstring path) = 0;
464                //////////////////////////////////////////////////////////////////////////
465                /// Get all keys for a path.
466                ///
467                /// @param path The path to get keys under
468                /// @return a list of keys
469                ///
470                /// @author mickem
471                virtual string_list get_keys(std::wstring path) = 0;
472                //////////////////////////////////////////////////////////////////////////
473                /// Does the section exists?
474                ///
475                /// @param path The path of the section
476                /// @return true/false
477                ///
478                /// @author mickem
479                virtual bool has_section(std::wstring path) = 0;
480                //////////////////////////////////////////////////////////////////////////
481                /// Does the key exists?
482                ///
483                /// @param path The path of the section
484                /// @param key The key to check
485                /// @return true/false
486                ///
487                /// @author mickem
488                virtual bool has_key(std::wstring path, std::wstring key) = 0;
489
490                virtual void add_path(std::wstring path) = 0;
491                // Misc Functions
492                //////////////////////////////////////////////////////////////////////////
493                /// Get a context.
494                /// The context is an identifier for the settings store for INI/XML it is the filename.
495                ///
496                /// @return the context
497                ///
498                /// @author mickem
499                virtual std::wstring get_context() = 0;
500                //////////////////////////////////////////////////////////////////////////
501                /// Set the context.
502                /// The context is an identifier for the settings store for INI/XML it is the filename.
503                ///
504                /// @param context the new context
505                ///
506                /// @author mickem
507                virtual void set_context(std::wstring context) = 0;
508
509                // Save/Load Functions
510                //////////////////////////////////////////////////////////////////////////
511                /// Reload the settings store
512                ///
513                /// @author mickem
514                virtual void reload() = 0;
515                //////////////////////////////////////////////////////////////////////////
516                /// Copy the settings store to another settings store
517                ///
518                /// @param other the settings store to save to
519                ///
520                /// @author mickem
521                virtual void save_to(instance_ptr other) = 0;
522                virtual void save_to(std::wstring other) = 0;
523                //////////////////////////////////////////////////////////////////////////
524                /// Save the settings store
525                ///
526                /// @author mickem
527                virtual void save() = 0;
528                //////////////////////////////////////////////////////////////////////////
529                /// Load from another settings store
530                ///
531                /// @param other the other settings store to load from
532                ///
533                /// @author mickem
534                virtual void load_from(instance_ptr other) = 0;
535                virtual void load_from(std::wstring other) = 0;
536                //////////////////////////////////////////////////////////////////////////
537                /// Load settings from the context.
538                ///
539                /// @author mickem
540                virtual void load() = 0;
541
542                virtual std::wstring to_string() = 0;
543
544                virtual std::wstring get_info() = 0;
545
546                static bool string_to_bool(std::wstring str) {
547                        return str == _T("true")||str == _T("1");
548                }
549
550
551        };
552
553}
Note: See TracBrowser for help on using the repository browser.