source: nscp/include/nscapi/settings_object.hpp @ b67d231

0.4.00.4.10.4.2
Last change on this file since b67d231 was b67d231, checked in by Michael Medin <michael@…>, 12 months ago
  • Added log level = off to disable logging.
  • Added option in NSCAClient to hostname (auto-lc) to use lower case version of hostname. #533
  • Reworked how commands are read. If a command is defined without a section (default) no section will be added and instead a comment will be addded on how to add the section. This should (I hope) resolve the "missing command" for good.
  • Improved error messages for missing commands
  • Fixed scientific notation on performance data (#)
  • Property mode set to 100644
File size: 4.9 KB
Line 
1#pragma once
2
3#include <map>
4#include <list>
5#include <string>
6
7#include <boost/any.hpp>
8#include <boost/bind.hpp>
9#include <boost/function.hpp>
10#include <boost/shared_ptr.hpp>
11#include <boost/foreach.hpp>
12
13#include <unicode_char.hpp>
14#include <strEx.h>
15
16#include <nscapi/nscapi_core_wrapper.hpp>
17#include <settings/client/settings_client_interface.hpp>
18
19namespace nscapi {
20
21        namespace settings_objects {
22                class default_object_type {
23                        std::wstring path;
24                        std::wstring alias;
25                        std::wstring value;
26                        std::wstring parent;
27                };
28
29                template<class object_type>
30                class default_object_reader {
31                        static void read_object(boost::shared_ptr<nscapi::settings_proxy> proxy, object_type &object, bool oneliner) {}
32                        static void apply_parent(object_type &object, object_type &parent) {}
33                        static void post_process_object(object_type &object) {}
34                        static void init_default(object_type &object) {}
35                };
36
37
38                template<class t_object_type, class object_reader>
39                struct object_handler : boost::noncopyable {
40                        typedef boost::optional<t_object_type> optional_object;
41                        typedef std::map<std::wstring, t_object_type> object_list_type;
42                        typedef object_handler<t_object_type, object_reader> my_type;
43
44                        object_list_type object_list;
45                        object_list_type template_list;
46
47                        void add_missing(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) {
48                                if (has_object(alias))
49                                        return;
50                                add(proxy, path, alias, value, is_template);
51                        }
52
53                        t_object_type add(boost::shared_ptr<nscapi::settings_proxy> proxy, std::wstring path, std::wstring alias, std::wstring value, bool is_template = false) {
54                                optional_object previous = find_object(alias);
55                                if (previous) {
56                                        t_object_type p = *previous;
57                                        return p;
58                                }
59                                t_object_type object;
60                                object.alias = alias;
61                                object.value = value;
62                                object.path = path + _T("/") + alias;
63                                object.parent = _T("default");
64                                object.is_template = false;
65
66                                std::list<std::wstring> keys = proxy->get_keys(object.path);
67                                object_reader::read_object(proxy, object, keys.empty());
68
69
70                                if (!object.parent.empty() && object.parent != alias & object.parent != object.alias) {
71                                        t_object_type parent;
72                                        optional_object tmp = find_object(object.parent);
73                                        if (!tmp) {
74                                                parent = add(proxy, path, object.parent, _T(""), true);
75                                        } else {
76                                                parent = *tmp;
77                                        }
78                                        object_reader::apply_parent(object, parent);
79                                }
80                                if (is_template || object.is_template)
81                                        add_template(object);
82                                else
83                                        add_object(object);
84                                return object;
85                        }
86
87                        void rebuild(boost::shared_ptr<nscapi::settings_proxy> proxy) {
88                                std::list<t_object_type> tmp;
89                                BOOST_FOREACH(const typename object_list_type::value_type &t, object_list) {
90                                        tmp.push_back(t.second);
91                                }
92                                object_list.clear();
93                                BOOST_FOREACH(const t_object_type &o, tmp) {
94                                        std::wstring::size_type pos = o.path.find_last_of(_T("/"));
95                                        if (pos == std::wstring::npos)
96                                                continue;
97                                        add(proxy, o.path.substr(0, pos-1), o.path.substr(pos), o.host);
98                                }
99                        }
100
101
102                        optional_object find_object(std::wstring alias) {
103                                typename object_list_type::const_iterator cit = object_list.find(alias);
104                                if (cit != object_list.end())
105                                        return optional_object(cit->second);
106                                cit = template_list.find(alias);
107                                if (cit != template_list.end())
108                                        return optional_object(cit->second);
109                                return optional_object();
110                        }
111
112                        bool has_object(std::wstring alias) const {
113                                typename object_list_type::const_iterator cit = object_list.find(alias);
114                                if (cit != object_list.end())
115                                        return true;
116                                cit = template_list.find(alias);
117                                if (cit != template_list.end())
118                                        return true;
119                                return false;
120                        }
121
122                        bool empty() const {
123                                return object_list.empty();
124                        }
125
126
127                        std::wstring to_wstring() {
128                                std::wstringstream ss;
129                                ss << _T("Objects: ");
130                                BOOST_FOREACH(const typename object_list_type::value_type &t, object_list) {
131                                        ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} ");
132                                }
133                                ss << _T("Templates: ");
134                                BOOST_FOREACH(const typename object_list_type::value_type &t, template_list) {
135                                        ss << _T(", ") << t.first << _T(" = {") << t.second.to_wstring() + _T("} ");
136                                }
137                                return ss.str();
138                        }
139
140                        void add_object(t_object_type object) {
141                                object_reader::post_process_object(object);
142                                typename object_list_type::iterator cit = template_list.find(object.alias);
143                                if (cit != template_list.end())
144                                        template_list.erase(cit);
145                                object_list[object.alias] = object;
146                        }
147                        void add_template(t_object_type object) {
148                                typename object_list_type::const_iterator cit = object_list.find(object.alias);
149                                if (cit != object_list.end())
150                                        return;
151                                object_reader::post_process_object(object);
152                                template_list[object.alias] = object;
153                        }
154                };
155        }
156}
157
Note: See TracBrowser for help on using the repository browser.