FlagsService.h
10.9 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- mode:c++ -*-
* FlagsService.h
*
* Created on: Aug 19, 2010
* Author: Paolo Frasconi
*/
#ifndef FLAGSSERVICE_H_
#define FLAGSSERVICE_H_
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <string>
#include <stdexcept>
#include <map>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <typeinfo>
#include <vector>
/**
* @class FlagsTraits
*
* @brief Contains traits of flags. The name of a flag is used by
* Prolog's predicates set_klog_flag and get_klog_flag. The
* description is for the interactive help.
*/
class FlagsTraits {
private:
std::string name;
std::string description;
public:
FlagsTraits(const std::string& name, const std::string& description) {
this->name = name;
this->description = description;
}
std::string& get_name(void) { return name; }
std::string& get_description(void) { return description; }
virtual void dummy(void) {}; // make dynamic_cast possible...
};
class BoolFlagsTraits : public FlagsTraits {
private:
bool* ref;
public:
BoolFlagsTraits(bool* ref, const std::string& name, const std::string& description):
FlagsTraits(name,description) {
this->ref = ref;
}
bool* get_ref(void) {return ref;}
};
class IntFlagsTraits : public FlagsTraits {
private:
int* ref;
public:
IntFlagsTraits(int* ref, const std::string& name, const std::string& description):
FlagsTraits(name,description) {
this->ref = ref;
}
int* get_ref(void) {return ref;}
};
class UnsignedFlagsTraits : public FlagsTraits {
private:
unsigned* ref;
public:
UnsignedFlagsTraits(unsigned* ref, const std::string& name, const std::string& description):
FlagsTraits(name,description) {
this->ref = ref;
}
unsigned* get_ref(void) {return ref;}
};
class DoubleFlagsTraits : public FlagsTraits {
private:
double* ref;
public:
DoubleFlagsTraits(double* ref, const std::string& name, const std::string& description):
FlagsTraits(name,description) {
this->ref = ref;
}
double* get_ref(void) {return ref;}
};
class StringFlagsTraits : public FlagsTraits {
private:
std::string* ref;
public:
StringFlagsTraits(std::string* ref, const std::string& name, const std::string& description):
FlagsTraits(name,description) {
this->ref = ref;
}
std::string* get_ref(void) {return ref;}
};
/**
* @class FlagsServiceClient
*
* Interface for clients of the Flags service. Every class that needs
* to use the Flags service should inherit from this. A flag is a
* prolog reference to a data member of the class using the
* service. The class is expected to push its own flags using the
* method new_flag. Currently only bool, int, unsigned, double, and
* std::string are valid types for data members accessible via
* flags. In their constructors, derived classes should initialize the
* base class with a string (id) that is used to identify the specific
* object of that class. Then Prolog can communicate with the class
* via the predicates set_klog_flag(+Id,+FlagName,+FlagValue) and
* get_klog_flag(+Id,+FlagName,-FlagValue). Prolog values are cast
* into the right type of the object variable automatically. The
* derived class is responsible for raising exceptions if the values
* are not as expected. Exceptions for invalid flag names are raised
* here.
*/
class FlagsServiceClient {
private:
std::string id_for_flags_service;
typedef std::map<std::string,FlagsTraits*> FMap;
FMap flags_traits;
public:
std::string id_str(void) const {return id_for_flags_service;}
void new_flag(bool* ref, const std::string& name, const std::string& description) {
flags_traits[name] = new BoolFlagsTraits(ref,name,description);
}
void new_flag(int* ref, const std::string& name, const std::string& description) {
flags_traits[name] = new IntFlagsTraits(ref,name,description);
}
void new_flag(unsigned* ref, const std::string& name, const std::string& description) {
flags_traits[name] = new UnsignedFlagsTraits(ref,name,description);
}
void new_flag(double* ref, const std::string& name, const std::string& description) {
flags_traits[name] = new DoubleFlagsTraits(ref,name,description);
}
void new_flag(std::string* ref, const std::string& name, const std::string& description) {
flags_traits[name] = new StringFlagsTraits(ref,name,description);
}
std::string document_flags(void) const {
std::ostringstream outs;
outs.setf (std::ios::left, std::ios::adjustfield);
for (FMap::const_iterator i=flags_traits.begin(); i!=flags_traits.end(); ++i) {
outs << std::setw(23) << id_for_flags_service + "::" + i->second->get_name() << " [";
BoolFlagsTraits* pb; IntFlagsTraits* pi; UnsignedFlagsTraits* pu; DoubleFlagsTraits* pd; StringFlagsTraits* ps;
if ((pb=dynamic_cast<BoolFlagsTraits*>(i->second)) != NULL)
outs << *(pb->get_ref());
else if ((pi=dynamic_cast<IntFlagsTraits*>(i->second)) != NULL)
outs << *(pi->get_ref());
else if ((pu=dynamic_cast<UnsignedFlagsTraits*>(i->second)) != NULL)
outs << *(pu->get_ref());
else if ((pd=dynamic_cast<DoubleFlagsTraits*>(i->second)) != NULL)
outs << *(pd->get_ref());
else if ((ps=dynamic_cast<StringFlagsTraits*>(i->second)) != NULL)
outs << *(ps->get_ref());
else
throw std::runtime_error("Nasty bug - unrecognized type in document_flags");
outs << "]" << std::endl << i->second->get_description() << std::endl << std::endl;
}
return outs.str();
}
virtual ~FlagsServiceClient();
// virtual void set_flag_callback(const std::string& flag_name, const std::string& flag_value) = 0;
// virtual std::string get_flag_callback(const std::string& flag_name) const = 0;
void set_flag(const std::string& flag_name, const std::string& flag_value) {
FMap::iterator found = flags_traits.find(flag_name);
if (found==flags_traits.end())
throw std::runtime_error("Unknown flag name");
BoolFlagsTraits* pb; IntFlagsTraits* pi; UnsignedFlagsTraits* pu; DoubleFlagsTraits* pd; StringFlagsTraits* ps;
if ((pb=dynamic_cast<BoolFlagsTraits*>(found->second)) != NULL)
*(pb->get_ref()) = (flag_value=="true"||flag_value=="on"||flag_value=="yes");
else if ((pi=dynamic_cast<IntFlagsTraits*>(found->second)) != NULL)
*(pi->get_ref()) = atoi(flag_value.c_str());
else if ((pu=dynamic_cast<UnsignedFlagsTraits*>(found->second)) != NULL)
*(pu->get_ref()) = unsigned(atoi(flag_value.c_str()));
else if ((pd=dynamic_cast<DoubleFlagsTraits*>(found->second)) != NULL)
*(pd->get_ref()) = atof(flag_value.c_str());
else if ((ps=dynamic_cast<StringFlagsTraits*>(found->second)) != NULL)
*(ps->get_ref()) = flag_value;
else
throw std::runtime_error("Nasty bug - unrecognized type in set_flag");
}
std::string get_flag(const std::string& flag_name) {
FMap::iterator found = flags_traits.find(flag_name);
std::stringstream oss;
if (found==flags_traits.end())
throw std::runtime_error("Unknown flag name");
BoolFlagsTraits* pb; IntFlagsTraits* pi; UnsignedFlagsTraits* pu; DoubleFlagsTraits* pd; StringFlagsTraits* ps;
if ((pb=dynamic_cast<BoolFlagsTraits*>(found->second)) != NULL)
oss << *(pb->get_ref());
else if ((pi=dynamic_cast<IntFlagsTraits*>(found->second)) != NULL)
oss << *(pi->get_ref());
else if ((pu=dynamic_cast<UnsignedFlagsTraits*>(found->second)) != NULL)
oss << *(pu->get_ref());
else if ((pd=dynamic_cast<DoubleFlagsTraits*>(found->second)) != NULL)
oss << *(pd->get_ref());
else if ((ps=dynamic_cast<StringFlagsTraits*>(found->second)) != NULL)
oss << *(ps->get_ref());
else
throw std::runtime_error("Nasty bug - unrecognized type in get_flag");
return oss.str();
}
FlagsServiceClient(const std::string& id_for_flags_service);
};
/**
* @class FlagsService
*
* A singleton storing all the clients of the flags service. Prolog
* calls for seting, gettin, and documenting flags are intercepted
* here and dispatched to the right client by looking up the client's
* id in the internal storage of this object.
*/
class FlagsService {
public:
typedef std::map<std::string,FlagsServiceClient*> Map;
private:
Map pool;
FlagsService() {};
FlagsService(FlagsService const& rhs);
FlagsService& operator=(FlagsService const& rhs);
FlagsServiceClient* find_range_checked(const std::string& client_id) const {
Map::const_iterator found = pool.find(client_id);
if ( found == pool.end() )
throw std::out_of_range("Flags client identifier not registered");
return found->second;
}
public:
static FlagsService& get_instance() {
static FlagsService instance;
return instance;
}
// static unsigned to_unsigned(const std::string& s) { return unsigned(atoi(s.c_str())); }
// static int to_int(const std::string& s) { return atoi(s.c_str()); }
// static double to_double(const std::string& s) { return atof(s.c_str()); }
// static bool to_bool(const std::string& s) {
// std::string r=s;
// std::transform(r.begin(), r.end(), r.begin(), ::tolower);
// return (r=="true" || r=="yes" || r=="on" || r=="t");
// }
// template <typename T>
// static std::string to_string(const T x) {
// std::stringstream oss; oss << x; return oss.str();
// }
// Register a new client in the pool. The client must respond to the
// FlagsClient interface defined above.
void register_flags_service_client(FlagsServiceClient* c) {
Map::iterator found = pool.find(c->id_str());
if ( found != pool.end() ) {
throw std::out_of_range("Duplicate client identifier in register_client()");
}
pool[c->id_str()] = c;
}
void unregister_flags_service_client(FlagsServiceClient* c) {
Map::iterator found = pool.find(c->id_str());
if ( found == pool.end() ) {
throw std::out_of_range("Unknown client identifier in unregister_client()");
}
pool.erase(c->id_str());
}
void set_flag(const std::string client_id, const std::string& flag_name, const std::string& flag_value) {
//find_range_checked(client_id)->set_flag_callback(flag_name, flag_value);
find_range_checked(client_id)->set_flag(flag_name, flag_value);
}
std::string get_flag(const std::string client_id, const std::string& flag_name) {
// return find_range_checked(client_id)->get_flag_callback(flag_name);
return find_range_checked(client_id)->get_flag(flag_name);
}
std::string document_flags(const std::string client_id) {
return find_range_checked(client_id)->document_flags();
}
bool is_registered_client(const std::string& client_id) const {
Map::const_iterator found = pool.find(client_id);
return ( found != pool.end() );
}
void get_client_ids(std::vector<std::string>& ids) const {
for(Map::const_iterator it = pool.begin(); it != pool.end(); ++it) {
ids.push_back(it->first);
}
}
void get_client_ids_alt(Map::const_iterator& begin, Map::const_iterator& end) const {
begin = pool.begin();
end = pool.end();
}
};
extern FlagsService& The_FlagsService;
#endif /* FLAGSSERVICE_H_ */