]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/sensors.h
Various fixes of issues reported by static analyzers.
[stg.git] / projects / stargazer / plugins / other / smux / sensors.h
1 #ifndef __SENSORS_H__
2 #define __SENSORS_H__
3
4 #include <map>
5
6 #include "stg/users.h"
7 #include "stg/tariffs.h"
8 #include "stg/admins.h"
9 #include "stg/services.h"
10 #include "stg/corporations.h"
11 #include "stg/traffcounter.h"
12 #include "stg/user_property.h"
13
14 #include "stg/ObjectSyntax.h"
15
16 #include "value2os.h"
17 #include "types.h"
18
19 class Sensor {
20     public:
21         virtual ~Sensor() {}
22         virtual bool GetValue(ObjectSyntax_t * objectSyntax) const = 0;
23 #ifdef DEBUG
24         virtual std::string ToString() const = 0;
25 #endif
26 };
27
28 typedef std::map<OID, Sensor *> Sensors;
29
30 class TotalUsersSensor : public Sensor {
31     public:
32         explicit TotalUsersSensor(const USERS & u) : users(u) {}
33         virtual ~TotalUsersSensor() {}
34
35         bool GetValue(ObjectSyntax_t * objectSyntax) const
36         {
37         ValueToOS(users.Count(), objectSyntax);
38         return true;
39         }
40
41 #ifdef DEBUG
42         std::string ToString() const
43         { std::string res; x2str(users.Count(), res); return res; }
44 #endif
45
46     private:
47         const USERS & users;
48 };
49
50 class UsersSensor : public Sensor {
51     public:
52         explicit UsersSensor(USERS & u) : users(u) {}
53         virtual ~UsersSensor() {}
54
55         bool GetValue(ObjectSyntax_t * objectSyntax) const;
56 #ifdef DEBUG
57         std::string ToString() const;
58 #endif
59
60     private:
61         USERS & users;
62
63         virtual bool UserPredicate(USER_PTR userPtr) const = 0;
64 };
65
66 class ConnectedUsersSensor : public UsersSensor {
67     public:
68         explicit ConnectedUsersSensor(USERS & u) : UsersSensor(u) {}
69         virtual ~ConnectedUsersSensor() {}
70
71     private:
72         bool UserPredicate(USER_PTR userPtr) const
73         { return userPtr->GetConnected(); }
74 };
75
76 class AuthorizedUsersSensor : public UsersSensor {
77     public:
78         explicit AuthorizedUsersSensor(USERS & u) : UsersSensor(u) {}
79         virtual ~AuthorizedUsersSensor() {}
80
81     private:
82         bool UserPredicate(USER_PTR userPtr) const
83         { return userPtr->GetAuthorized(); }
84 };
85
86 class AlwaysOnlineUsersSensor : public UsersSensor {
87     public:
88         explicit AlwaysOnlineUsersSensor(USERS & u) : UsersSensor(u) {}
89         virtual ~AlwaysOnlineUsersSensor() {}
90
91     private:
92         bool UserPredicate(USER_PTR userPtr) const
93         { return userPtr->GetProperty().alwaysOnline; }
94 };
95
96 class NoCashUsersSensor : public UsersSensor {
97     public:
98         explicit NoCashUsersSensor(USERS & u) : UsersSensor(u) {}
99         virtual ~NoCashUsersSensor() {}
100
101     private:
102         bool UserPredicate(USER_PTR userPtr) const
103         { return userPtr->GetProperty().cash < 0; }
104 };
105
106 class DisabledDetailStatsUsersSensor : public UsersSensor {
107     public:
108         explicit DisabledDetailStatsUsersSensor(USERS & u) : UsersSensor(u) {}
109         virtual ~DisabledDetailStatsUsersSensor() {}
110
111     private:
112         bool UserPredicate(USER_PTR userPtr) const
113         { return userPtr->GetProperty().disabledDetailStat; }
114 };
115
116 class DisabledUsersSensor : public UsersSensor {
117     public:
118         explicit DisabledUsersSensor(USERS & u) : UsersSensor(u) {}
119         virtual ~DisabledUsersSensor() {}
120
121     private:
122         bool UserPredicate(USER_PTR userPtr) const
123         { return userPtr->GetProperty().disabled; }
124 };
125
126 class PassiveUsersSensor : public UsersSensor {
127     public:
128         explicit PassiveUsersSensor(USERS & u) : UsersSensor(u) {}
129         virtual ~PassiveUsersSensor() {}
130
131     private:
132         bool UserPredicate(USER_PTR userPtr) const
133         { return userPtr->GetProperty().passive; }
134 };
135
136 class CreditUsersSensor : public UsersSensor {
137     public:
138         explicit CreditUsersSensor(USERS & u) : UsersSensor(u) {}
139         virtual ~CreditUsersSensor() {}
140
141     private:
142         bool UserPredicate(USER_PTR userPtr) const
143         { return userPtr->GetProperty().credit > 0; }
144 };
145
146 class FreeMbUsersSensor : public UsersSensor {
147     public:
148         explicit FreeMbUsersSensor(USERS & u) : UsersSensor(u) {}
149         virtual ~FreeMbUsersSensor() {}
150
151     private:
152         bool UserPredicate(USER_PTR userPtr) const
153         { return userPtr->GetProperty().freeMb > 0; }
154 };
155
156 class TariffChangeUsersSensor : public UsersSensor {
157     public:
158         explicit TariffChangeUsersSensor(USERS & u) : UsersSensor(u) {}
159         virtual ~TariffChangeUsersSensor() {}
160
161     private:
162         bool UserPredicate(USER_PTR userPtr) const
163         { return !userPtr->GetProperty().nextTariff.ConstData().empty(); }
164 };
165
166 class ActiveUsersSensor : public UsersSensor {
167     public:
168         explicit ActiveUsersSensor(USERS & u) : UsersSensor(u) {}
169         virtual ~ActiveUsersSensor() {}
170
171     private:
172         bool UserPredicate(USER_PTR userPtr) const;
173 };
174
175 class TotalTariffsSensor : public Sensor {
176     public:
177         explicit TotalTariffsSensor(const TARIFFS & t) : tariffs(t) {}
178         virtual ~TotalTariffsSensor() {}
179
180         bool GetValue(ObjectSyntax_t * objectSyntax) const
181         {
182         ValueToOS(tariffs.Count(), objectSyntax);
183         return true;
184         }
185
186 #ifdef DEBUG
187         std::string ToString() const
188         { std::string res; x2str(tariffs.Count(), res); return res; }
189 #endif
190
191     private:
192         const TARIFFS & tariffs;
193 };
194
195 class TotalAdminsSensor : public Sensor {
196     public:
197         explicit TotalAdminsSensor(const ADMINS & a) : admins(a) {}
198         virtual ~TotalAdminsSensor() {}
199
200         bool GetValue(ObjectSyntax_t * objectSyntax) const
201         {
202         ValueToOS(admins.Count(), objectSyntax);
203         return true;
204         }
205
206 #ifdef DEBUG
207         std::string ToString() const
208         { std::string res; x2str(admins.Count(), res); return res; }
209 #endif
210
211     private:
212         const ADMINS & admins;
213 };
214
215 class TotalServicesSensor : public Sensor {
216     public:
217         explicit TotalServicesSensor(const SERVICES & s) : services(s) {}
218         virtual ~TotalServicesSensor() {}
219
220         bool GetValue(ObjectSyntax_t * objectSyntax) const
221         {
222         ValueToOS(services.Count(), objectSyntax);
223         return true;
224         }
225
226 #ifdef DEBUG
227         std::string ToString() const
228         { std::string res; x2str(services.Count(), res); return res; }
229 #endif
230
231     private:
232         const SERVICES & services;
233 };
234
235 class TotalCorporationsSensor : public Sensor {
236     public:
237         explicit TotalCorporationsSensor(const CORPORATIONS & c) : corporations(c) {}
238         virtual ~TotalCorporationsSensor() {}
239
240         bool GetValue(ObjectSyntax_t * objectSyntax) const
241         {
242         ValueToOS(corporations.Count(), objectSyntax);
243         return true;
244         }
245
246 #ifdef DEBUG
247         std::string ToString() const
248         { std::string res; x2str(corporations.Count(), res); return res; }
249 #endif
250
251     private:
252         const CORPORATIONS & corporations;
253 };
254
255 class TotalRulesSensor : public Sensor {
256     public:
257         explicit TotalRulesSensor(const TRAFFCOUNTER & t) : traffcounter(t) {}
258         virtual ~TotalRulesSensor() {}
259
260         bool GetValue(ObjectSyntax_t * objectSyntax) const
261         {
262         ValueToOS(traffcounter.RulesCount(), objectSyntax);
263         return true;
264         }
265
266 #ifdef DEBUG
267         std::string ToString() const
268         { std::string res; x2str(traffcounter.RulesCount(), res); return res; }
269 #endif
270
271     private:
272         const TRAFFCOUNTER & traffcounter;
273 };
274
275 template <typename T>
276 class ConstSensor : public Sensor {
277     public:
278         explicit ConstSensor(const T & v) : value(v) {}
279         virtual ~ConstSensor() {}
280
281         bool GetValue(ObjectSyntax * objectSyntax) const
282         { return ValueToOS(value, objectSyntax); }
283
284 #ifdef DEBUG
285         std::string ToString() const
286         { std::string res; x2str(value, res); return res; }
287 #endif
288
289     private:
290         T value;
291 };
292
293 #ifdef DEBUG
294 template <>
295 inline
296 std::string ConstSensor<std::string>::ToString() const
297 {
298 return value;
299 }
300 #endif
301
302 #endif