]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/plr/server/src/configuration
update
[l4.git] / l4 / pkg / plr / server / src / configuration
1 // vi: ft=cpp
2 #pragma once
3
4 /*
5  * configuration --
6  *
7  * Handling of Romain config stuff
8  *
9  * (c) 2011-2013 Björn Döbel <doebel@os.inf.tu-dresden.de>,
10  *     economic rights: Technische Universität Dresden (Germany)
11  * This file is part of TUD:OS and distributed under the terms of the
12  * GNU General Public License 2.
13  * Please see the COPYING-GPL-2 file for details.
14  */
15
16 #include <map>
17 #include "manager"
18 #include "log"
19 #include "exceptions"
20 EXTERN_C_BEGIN
21 #include "iniparser.h"
22 EXTERN_C_END
23
24 namespace Romain
25 {
26
27 class Configuration
28 {
29         std::map<char const *, dictionary*> _files;
30         public:
31                 Configuration() { }
32
33                 dictionary *get(char const* filename)
34                 {
35                         dictionary* d = _files[filename];
36                         if (!d) {
37                                 d = iniparser_load(filename);
38                                 _check(!d, "file not found");
39                                 _files[filename] = d;
40                         }
41                         return d;
42                 }
43 };
44
45 extern Configuration globalconfig;
46
47
48 struct ConfigValue
49 {
50         dictionary *_dict;
51         ConfigValue()
52         {
53                 _dict = globalconfig.get("rom/romain.ini");
54                 _check(!_dict, "romain.ini not found?");
55         }
56 };
57
58
59 struct ConfigIntValue : public ConfigValue
60 {
61         int _val;
62         ConfigIntValue(char const * const key, int _default = -1)
63                 : ConfigValue()
64         {
65                 _val = iniparser_getint(_dict, key, _default);
66         }
67         
68         operator int() { return _val; }
69         operator unsigned int() { return _val; }
70         operator l4_addr_t() { return static_cast<l4_addr_t>(_val); }
71 };
72
73
74 struct ConfigStringValue : public ConfigValue
75 {
76         char const *_val;
77         ConfigStringValue(char const * const key, char const *_default = 0)
78                 : ConfigValue()
79         {
80                 _val = iniparser_getstring(_dict, key, const_cast<char*>(_default));
81         }
82
83         operator char const *() { return _val; }
84 };
85
86
87 struct ConfigBoolValue : public ConfigValue
88 {
89         bool _val;
90         ConfigBoolValue(char const * const key, bool _default = false)
91                 : ConfigValue()
92         {
93                 _val = iniparser_getboolean(_dict, key, _default);
94         }
95
96         operator bool () { return _val; }
97 };
98
99
100 /*
101  * Simple observer configuration
102  *
103  * Takes a string and an IM and creates and registers an
104  * observer of the same name with the IM.
105  */
106 class ObserverConfig
107 {
108         public:
109                 ObserverConfig() { }
110
111                 ObserverConfig(Romain::InstanceManager *im,
112                                char const *observerName)
113                 {
114                         configure(im, observerName);
115                 }
116
117         protected:
118                 void configure(Romain::InstanceManager *im,
119                                char const *observerName)
120                 {
121                         im->register_fault_observer(
122                                 Romain::ObserverFactory::CreateObserver(observerName));
123                 }
124 };
125
126
127 class StringObserverConfig : public ObserverConfig
128 {
129         public:
130         StringObserverConfig(char const * const key,
131                              Romain::InstanceManager *im)
132         {
133                 char const *c = ConfigStringValue(key);
134                 if (c)
135                         configure(im, c);
136         }
137 };
138
139
140 class BoolObserverConfig : public ObserverConfig
141 {
142         public:
143         BoolObserverConfig(char const * const key,
144                            Romain::InstanceManager *im,
145                            char const * const observer)
146         {
147                 bool b = ConfigBoolValue(key);
148                 if (b)
149                         configure(im, observer);
150         }
151 };
152
153 }