1 //-----------------------------------------------------------------------------
2 // Vars
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __VARS_H__
6 #define __VARS_H__
7 
8 /**
9  * Variable status.
10  * Status defines variable type and domain in which variable can be
11  * modified.
12  */
13 enum
14 {
15   VF_NONE     = 0x00, /**< no flag */
16   VF_CHEAT    = 0x01, /**< value can only be changed if cheating */
17   VF_SYSTEM   = 0x02, /**< value can only be changed by the system */
18   VF_PERSISTENT = 0x04, /**< value is stored in config.ini */
19   VF_LATCH    = 0x08, /**< change needs restart */
20   VF_USER     = 0x10, /**< created by the user */
21   VF_TEMP     = 0x20, /**< temporal variables are to be replaced with local varibles. */
22   VF_DEBUG    = 0x40
23 };
24 
25 /**
26  * A variable.
27  * A variable is an object that can store a value. The value can be an integer,
28  * a float or a string. The variables have a direct impact on the engine
29  * comportment. Some features are enabled or disabled following the
30  * corresponding variable values.
31  */
32 class Var
33 {
34   public:
35     /**
36      * Constructor for variable storing int value.
37      * @param name the variable's name
38      * @param ivalue the new value (default is 0)
39      * @param flags the flags for the variable (default is VF_NONE)
40      */
41     Var(const char *name, int ivalue = 0, int flags = 0);
42 
43     /**
44      * Constructor for variable storing float value.
45      * @param name the variable's name
46      * @param fvalue the new value
47      * @param flags the flags for the variable (default is VF_NONE)
48      */
49     Var(const char *name, float fvalue, int flags = 0);
50 
51     /**
52      * Constructor for variable storing a string.
53      * @param name the variable's name
54      * @param svalue the new value
55      * @param flags the flags for the variable (default is VF_NONE)
56      */
57     Var(const char *name, const char *svalue, int flags = 0);
58 
59     /**
60      * The destructor will automatically destroy the variable and his
61      * content.
62      */
63     ~Var(void);
64 
65     /**
66      * Set a string value to the variable.
67      * @param svalue the new value of the variable
68      */
69     void SetString(const char *svalue);
70 
71     /**
72      * Set an integer value to the variable.
73      * @param ivalue the new value of the variable
74      */
75     void SetInteger(int ivalue);
76 
77     /**
78      * Set a float value to the variable.
79      * @param fvalue the new value of the variable
80      */
81     void SetFloat(float fvalue);
82 
83     /**
84      * Set a new value to the variable under the form of a string
85      * @param svalue the new value of the variable
86      * @see SetString()
87      */
88     void SetDefault(const char *svalue);
89 
90     /**
91      * Equal operator for variable storing an integer value. We can use
92      * this operator to set a new integer value to the variable.
93      * @param ivalue the new value of the variable
94      * @return the new value of the variable
95      */
96     int operator= (const int ivalue);
97 
98     /**
99      * Equal operator for variable storing a float value. We can use this
100      * operator to set a new float value to the variable.
101      * @param fvalue the new value of the variable
102      * @return the new value of the variable
103      */
104     float operator= (const float fvalue);
105 
106     /**
107      * Equal operator for variable storing a string. We can use this
108      * operator to set a new value under the form of a string.
109      * @param svalue the new value of the variable
110      */
111     void operator= (const char *svalue);
112 
113     char *  name;       /**< variable name */
114     float   fvalue;     /**< variable value in the floating form */
115     int     ivalue;     /**< variable value in the integer form */
116     char *  svalue;     /**< variable value in the string form */
117     int     flags;      /**< variable flags */
118 
119     /**
120      * Variable default value.
121      * A default value is assigned to the variable when the variable
122      * constructor is called. The constructor stores the given value
123      * in this variable. The default value is used by the console to
124      * display the default value into parenthesis.
125      */
126     char *  default_value;
127 
128     /**
129      * Pointer to the next variable in a variables list.
130      * In facts this pointer isn't used at all. Vars class is a much
131      * better method to have variables list.
132      */
133     Var * next;
134 };
135 
136 /**
137  * Variables list.
138  * This class defines a list that contains variables.
139  */
140 class Vars
141 {
142   private:
143     Var *list;        /**< The variables list */
144 
145   public:
146     Vars(void);
147 
148     /**
149      * The destructor destroys all temporary (VF_TEMP flag) variables
150      * of the list.
151      */
152     ~Vars(void);
153 
154 
155     /**
156      * Creates a variable storing a string and add it to the list.
157      * @param name the name of the variable
158      * @param string the value of the variable under the form of a string
159      * @param flags the flags of the variable (default is VF_NONE)
160      */
161     void CreateVar(const char *name, const char *string, int flags=0);
162 
163     /**
164      * Creates a variable storing a float and add it to the list.
165      * @param name the name of the variable
166      * @param fvalue the float value of the variable
167      * @param flags the flags of the variable (default is VF_NONE)
168      */
169     void CreateVar(const char *name, float fvalue, int flags=0);
170 
171     /**
172      * Creates a variable storing an integer and add it to the list.
173      * @param name the name of the variable
174      * @param ivalue the int value of the variable
175      * @param flags the flags of the variable (default is VF_NONE)
176      */
177     void CreateVar(const char *name, int ivalue, int flags=0);
178 
179 
180     /**
181      * Modify the value and the flags of an variable of the list
182      * @param name the name of the variable
183      * @param string the value of the variable under the form of a string
184      * @return true if the operation succeeded, false if not
185      */
186     bool SetKeyValue(const char *name, const char *string);
187 
188     /**
189      * Modify the value and the flags of an variable of the list
190      * @param name the name of the variable
191      * @param value the float value of the variable
192      * @return true if the operation succeeded, false if not
193      */
194     bool SetKeyValue(const char *name, const float value);
195 
196     /**
197      * Modify the value and the flags of an variable of the list
198      * @param name the name of the variable
199      * @param value the int value of the variable
200      * @return true if the operation succeeded, false if not
201      */
202     bool SetKeyValue(const char *name, const int value);
203 
204 
205     /**
206      * Register a variable if it doesnt exists. If it exists and is
207      * temporal, replace it.
208      * @param var the variable to add to the variables list
209      */
210     void RegisterVar(Var &var);
211 
212     /**
213      * Unregister a variable. If the variable is present in the variables
214      * list, it is removed of it.
215      * @param var the variable to remove from the variables list
216      */
217     void UnregisterVar(Var &var);
218 
219 
220     /**
221      * Get the value of a variable under the form of a string
222      * @param name the name of the variable
223      * @return the value of the variable
224      */
225     char * StringForKey(const char * name);
226 
227     /**
228      * Get the float value of a variable
229      * @param name the name of the variable
230      * @return the value of the variable
231      */
232     float ValueForKey(const char * name);
233 
234     /**
235      * Get the interger value of a variable
236      * @param name the name of the variable
237      * @return the value of the variable
238      */
239     int IntForKey(const char * name);
240 
241     /**
242      * Get the default value of a variable
243      * @param name the name of the variable
244      * @return the default value of the variable under the form of a string
245      */
246     char * DefaultForKey(const char *name);
247 
248 
249     /**
250      * Informs if the variable is in the variables list
251      * @param name the name of the variable
252      * @return a boolean expressions that is true if the variable is present
253      *         in the variables list, false if not
254      */
255     bool IsKey(const char *name);
256 
257     /**
258      * Get a pointer to a variable
259      * @param name the name of the variable
260      * @return the pointer to the variable or a NULL pointer if the variable
261      *         couldn't be found in the list
262      */
263     Var* GetVar(const char *name);
264 
265 
266     /**
267      * Print a list of variables contained in the variables list
268      */
269     void PrintList(void);
270 
271     /**
272      * Get the variables list length
273      * @return the number of variables stored in the list
274      */
275     int GetNumber(void);
276 
277     /**
278      * Get the name of the variable at position i in the list
279      * @param i the index of the variable
280      * @return the name of the variable at position i or an empty
281      *         expression if the input value is incorrect
282      */
283     char *GetName(int i);
284 
285     /**
286      * Save the variables with VF_PERSISTENT flag to the disk
287      * @param filename the name of destination file
288      */
289     void SaveToFile(char *filename);
290 };
291 
292 #endif  /* __VARS_H__ */
293