1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #ifndef BZDBLOCAL_H
14 #define BZDBLOCAL_H
15 
16 #include "common.h"
17 
18 // system headers
19 #include <string>
20 
21 // implementation headers
22 #include "StateDatabase.h"
23 
24 
25 class BZDBint;
26 class BZDBbool;
27 class BZDBfloat;
28 class BZDBcolor;
29 class BZDBstring;
30 
31 
32 // Utility Macros (... requires at least the default parameter)
33 /*#define BZDB_INT(name, ...)    BZDBint    name(#name, __VA_ARGS__)
34 #define BZDB_BOOL(name, ...)   BZDBbool   name(#name, __VA_ARGS__)
35 #define BZDB_FLOAT(name, ...)  BZDBfloat  name(#name, __VA_ARGS__)
36 #define BZDB_COLOR(name, ...)  BZDBcolor  name(#name, __VA_ARGS__)
37 #define BZDB_STRING(name, ...) BZDBstring name(#name, __VA_ARGS__) */
38 
39 
40 /******************************************************************************/
41 //
42 // Defaults to "saving-on-exit"
43 //
44 
45 class BZDBLocal
46 {
47 
48     friend class BZDBLocalManager;
49 
50 public:
getName()51     inline const std::string& getName() const
52     {
53         return name;
54     }
55 
56 protected:
BZDBLocal(const std::string & _name,bool save)57     BZDBLocal(const std::string& _name, bool save)
58         : name(_name), saveOnExit(save) {}
~BZDBLocal()59     virtual ~BZDBLocal()
60     {
61         return;
62     }
63     virtual void addCallbacks() = 0;
64     virtual void removeCallbacks() = 0;
65 
66 protected:
67     std::string name;
68     bool saveOnExit;
69 };
70 
71 
72 /******************************************************************************/
73 
74 class BZDBbool : public BZDBLocal
75 {
76 public:
77     BZDBbool(const std::string& name, bool defVal,  bool saveOnExit = true);
78     ~BZDBbool();
79 
80     void addCallbacks();
81     void removeCallbacks();
82 
83     inline operator bool() const
84     {
85         return data;
86     };
87 
88 private:
89     BZDBbool(const BZDBbool&);
90     BZDBbool& operator=(const BZDBbool&);
91 
92     void callback();
93     static void staticCallback(const std::string& name, void* data);
94 
95 private:
96     bool data;
97 };
98 
99 
100 /******************************************************************************/
101 
102 class BZDBint : public BZDBLocal
103 {
104 public:
105     BZDBint(const std::string& name, int defVal,
106             int min = INT_MIN, int max = INT_MAX,
107             bool neverZero = false, bool saveOnExit = true);
108     ~BZDBint();
109 
110     void addCallbacks();
111     void removeCallbacks();
112 
113     inline operator int() const
114     {
115         return data;
116     };
117 
getMin()118     inline int getMin() const
119     {
120         return min;
121     }
getMax()122     inline int getMax() const
123     {
124         return max;
125     }
getNeverZero()126     inline bool getNeverZero() const
127     {
128         return neverZero;
129     }
130 
131 private:
132     BZDBint(const BZDBint&);
133     BZDBint& operator=(const BZDBint&);
134 
135     void callback();
136     static void staticCallback(const std::string& name, void* data);
137 
138 private:
139     int data;
140     int min;
141     int max;
142     bool neverZero;
143 };
144 
145 
146 /******************************************************************************/
147 
148 class BZDBfloat : public BZDBLocal
149 {
150 public:
151     BZDBfloat(const std::string& name, float defVal,
152               float min = -MAXFLOAT, float max = +MAXFLOAT,
153               bool neverZero = false, bool saveOnExit = true);
154     ~BZDBfloat();
155 
156     void addCallbacks();
157     void removeCallbacks();
158 
159     inline operator float() const
160     {
161         return data;
162     };
163 
getMin()164     inline float getMin() const
165     {
166         return min;
167     }
getMax()168     inline float getMax() const
169     {
170         return max;
171     }
getNeverZero()172     inline bool getNeverZero() const
173     {
174         return neverZero;
175     }
176 
177 private:
178     BZDBfloat(const BZDBfloat&);
179     BZDBfloat& operator=(const BZDBfloat&);
180 
181     void callback();
182     static void staticCallback(const std::string& name, void* data);
183 
184 private:
185     float data;
186     float min;
187     float max;
188     bool neverZero;
189 };
190 
191 
192 /******************************************************************************/
193 
194 class BZDBcolor : public BZDBLocal
195 {
196 public:
197     BZDBcolor(const std::string& name,
198               float r, float g, float b, float a,
199               bool neverAlpha = false, bool saveOnExit = true);
200     ~BZDBcolor();
201 
202     void addCallbacks();
203     void removeCallbacks();
204 
205     inline operator const float*() const
206     {
207         return data;
208     };
209 
getNeverAlpha()210     inline bool getNeverAlpha() const
211     {
212         return neverAlpha;
213     }
214 
215 private:
216     BZDBcolor(const BZDBcolor&);
217     BZDBcolor& operator=(const BZDBcolor&);
218 
219     void callback();
220     static void staticCallback(const std::string& name, void* data);
221 
222 private:
223     float data[4];
224     bool neverAlpha;
225 };
226 
227 
228 /******************************************************************************/
229 
230 class BZDBstring : public BZDBLocal
231 {
232 public:
233     BZDBstring(const std::string& name, const std::string& defVal,
234                bool neverEmpty = false, bool saveOnExit = true);
235     ~BZDBstring();
236 
237     void addCallbacks();
238     void removeCallbacks();
239 
240     inline operator const std::string&() const
241     {
242         return data;
243     };
244 
getNeverEmpty()245     inline bool getNeverEmpty() const
246     {
247         return neverEmpty;
248     }
249 
250 private:
251     BZDBstring(const BZDBstring&);
252     BZDBstring& operator=(const BZDBstring&);
253 
254     void callback();
255     static void staticCallback(const std::string& name, void* data);
256 
257 private:
258     std::string data;
259     bool neverEmpty;
260 };
261 
262 
263 /******************************************************************************/
264 
265 class BZDBLocalManager
266 {
267 public:
268     void init();
269 
270     void kill(); // no real reason to use this...
271 
272     bool addEntry(BZDBLocal* entry);
273 
274     // add a callback for all variables (that doesn't use the callback data)
275     void addNameCallback(StateDatabase::Callback callback);
276 
277 public:
278     static BZDBLocalManager manager;
279 
280 private:
281     BZDBLocalManager();
282     ~BZDBLocalManager();
283     BZDBLocalManager(const BZDBLocalManager&);
284     BZDBLocalManager& operator=(const BZDBLocalManager&);
285 
286 private:
287     std::vector<BZDBLocal*>& getEntries();
288 };
289 
290 
291 #define BZDBLOCAL (BZDBLocalManager::manager)
292 
293 
294 /******************************************************************************/
295 
296 #endif // BZDBLOCAL_H
297 
298 
299 // Local Variables: ***
300 // mode: C++ ***
301 // tab-width: 4 ***
302 // c-basic-offset: 4 ***
303 // indent-tabs-mode: nil ***
304 // End: ***
305 // ex: shiftwidth=4 tabstop=4
306