source: GTP/trunk/Lib/Vis/Preprocessing/src/Environment.h @ 1004

Revision 1004, 11.4 KB checked in by mattausch, 18 years ago (diff)

environment as a singleton

RevLine 
[372]1// ===================================================================
2// $Id: environ.h,v 1.1 2004/02/16 14:46:00 bittner Exp $
3//
4// environ.h
5//     Header file for environment operations, i.e.. reading
6//     environment file, reading command line parameters etc.
7//
8// Class: CEnvironment
9//
10/// Initial coding by
11/// @author             Tomas Kopal
12
13#ifndef __ENVIRONMENT_H__
14#define __ENVIRONMENT_H__
15
16// forward declarations
17
18#include "common.h"
19
[860]20namespace GtpVisibilityPreprocessor {
21
[372]22class Vector3;
23
24
25/// Enumeration type used to distinguish the type of the environment variable
26enum EOptType {
27  /// the integer variable
28  optInt,
29  /// the Real variable
30  optFloat,
31  /// the bool variable
32  optBool,
33  /// the vector variable
34  optVector,
35  /// the string variable
36  optString
37};
38
39class COption
40{
41public:
42  COption() {
43    name = NULL;
44    value = NULL;
45    abbrev = NULL;
46    defaultValue = NULL;
47    description = NULL;
48    pointer = NULL;
49  }
50
51  virtual ~COption() {
52    if (name)
53      delete name;
54    if (value)
55      delete value;
56    if (abbrev)
57      delete abbrev;
58    if (defaultValue)
59      delete defaultValue;
60    if (description)
61      delete description;
62   
63  }
64
65  EOptType type;
66  char *name;
67  char *value;
68  char *abbrev;
69  char *defaultValue;
70
71  char *description;
72  void *pointer;
73  friend ostream &operator <<(ostream &s, const COption &o) {
74    s<<o.name<<"\t";
75    if (o.abbrev)
76      s<<o.abbrev;
77    else
78      s<<"no abbrev";
79    s<<"\t";
80    if (o.value)
81      s<<o.value;
82    else
83      s<<"not set";
84    s<<"\t";
85    if (o.defaultValue)
86      s<<"["<<o.defaultValue<<"]";
87    else
88      s<<"[not set]";
89    s<<"\t";
90    if (o.description)
91      s<<endl<<o.description;
92    return s;
93  }
94 
95};
96
97/** CEnvironment class.
98  This class is used for reading command line, reading environment file and
99  managing options. Each recognizable option must be registered in the
100  constructor of this class.
101 */
102class Environment {
103  /**@name Variables */
104  //@{
105private:
106  /// Maximal number of options
107  int maxOptions;
108  /// Number of columns in the parameter table.
109  int numParams;
110  /// Number of rows in the parameter table.
111  int paramRows;
112  /// String with prefixes with non-optional parameters.
113  char *optionalParams;
114  /// Current row in the parameter table.
115  int curRow;
116
117  /** Parameter table itself.
118    The organization of this table is two dimensional array of character
119    strings. First column are parameters, all the other columns are options
120    prefixed by char passed to function GetCmdlineParams and paired with
121    corresponding parameters.
122   */
123  char ***params;
124
125  /// Number of registered options.
126  int numOptions;
127  /** Options table itself.
128    This table is two dimensional array of character strings with four
129    columns. First column contains name of the option, second it's value,
130    third eventual abbreviation, and finally the fourth contains the default
131    value specified when registering the option.
132    */
133  COption *options;
134  //  char ***options;
135  /** Options types.
136    This is auxiliary table to the table "options". It contains the type of
137    each option as specified when registering.
138    */
139  //  EOptType *optTypes;
140  //@}
141
142  /**@name Methods */
143  //@{
144  /** Auxiliary method for parsing environment file.
145    Function gets one string from buffer, skipping leading whitespaces, and
146    appends it at the end of the string parameter. Returns pointer to the next
147    string in the buffer with trailing whitespaces skipped. If no string in
148    the buffer can't be found, returns NULL.
149    @param buffer Input line for parsing.
150    @param string Buffer to append gathered string to.
151    @return Pointer to next string or NULL if not succesful.
152    */
153  char* ParseString(char *buffer, char *string) const;
154  /** Method for checking variable type.
155    This method is used to check if the value of the variable can be taken
156    as the specified type.
157    @param value Tested value of the variable.
158    @param type The type of the variable.
159    @return true if the variable is of the specified type, false elsewhere.
160    */
161  bool CheckType(const char *value, const EOptType type) const;
162  //@}
163
164  int
165  FindOption(const char *name,
166             const bool isFatal=false) const;
167
168  bool
169  ParseBool(const char *name) const;
170  void
171  ParseVector(const char *name, Vector3 &v) const;
172
173
174
175public: 
[1004]176       
177public:
[372]178
[1004]179        /** Returns the resource manager as a singleton.
180        */
181        static Environment *GetSingleton();
[372]182
[1004]183        static void DelSingleton();
[372]184
[1004]185    /// This method is used as a help on available command line options.
186        virtual void PrintUsage(ostream &s) const;
187
188        virtual void SetStaticOptions();
189
190
[372]191  /** Method for registering new option.
192      Using this method is possible to register new option with it's name, type,
193      abbreviation and default value.
194      @param name Name of the option.
195      @param type The type of the variable.
196      @param abbrev If not NULL, alias usable as a command line option.
197      @param defValue Implicit value used if not specified explicitly.
198  */
199  void RegisterOption(const char *name, const EOptType type,
200                      const char *abbrev, const char *defValue = NULL);
201 
202  /** Check if the specified switch is present in the command line.
203    Primary use of this function is to check for presence of some special
204    switch, e.g. -h for help. It can be used anytime.
205    @param argc Number of command line arguments.
206    @param argv Array of command line arguments.
207    @param swtch Switch we are checking for.
208    @return true if found, false elsewhere.
209   */
210  bool CheckForSwitch(const int argc,
[492]211                                          char **argv,
212                                          const char swtch) const;
[372]213  /** First pass of parsing command line.
214    This function parses command line and gets from there all non-optional
215    parameters (i.e. not prefixed by the dash) and builds a table of
216    parameters. According to param optParams, it also writes some optional
217    parameters to this table and pair them with corresponding non-optional
218    parameters.
219    @param argc Number of command line arguments.
220    @param argv Array of command line arguments.
221    @param optParams String consisting of prefixes to non-optional parameters.
222                     All prefixes must be only one character wide !
223   */
224  void ReadCmdlineParams(const int argc,
[492]225                                                 char **argv,
226                                                 const char *optParams);
[372]227  /** Reading of the environment file.
228    This function reads the environment file.
229    @param filename The name of the environment file to read.
230    @return true if OK, false in case of error.
231    */
232  bool ReadEnvFile(const char *filename);
233  /** Second pass of the command line parsing.
234    Performs parsing of the command line ignoring all parameters and options
235    read in the first pass. Builds table of options.
236    @param argc Number of command line arguments.
237    @param argv Array of command line arguments.
238    @param index Index of non-optional parameter, of which options should
239                 be read in.
240   */
[492]241  void ParseCmdline(const int argc, char **argv, const int index);
[372]242
243  /** Clears the environment data structures.
244    This function is intended to clear all data structures corresponding to
245    particular non-optional parameter to allow options for the next
246    non-optional parameter to be read. After this clearing, function
247    ReadEnvFile() can be called again to obtain next load of options.
248    */
249  //  void ClearEnvironment();
250 
251  /** Parameters number query function.
252    This function returns number of non-optional parameters specified on
253    the command line.
254    */
255  int GetParamNum() const { return paramRows; }
256  /** Returns the indexed parameter or corresponding optional parameter.
257   This function is used for queries on individual non-optional parameters.
258   The table must be constructed to allow this function to operate (i.e. the
259   first pass of command line parsing must be done).
260   @param name If space (' '), returns the non-optional parameter, else returns
261               the corresponding optional parameter.
262   @param index Index of the non-optional parameter to which all other options
263                corresponds.
264   @param value Return value of the queried parameter.
265   @return true if OK, false in case of error or if the parameter wasn't
266           specified.
267   */
268  bool GetParam(const char name, const int index, char *value) const;
269
270  /**@name Options query functions.
271   These functions are used for queries on individual options. The table must
272   be constructed to allow these functions to operate (i.e. the environment
273   file must be read and the second pass of command line parsing be done).
274   @param name Name of the option to retrieve.
275   @param value Return value of the queried option.
276   @param isFatal Boolean value specifying, if the function should exit or
277                  return false in case of error.
278   @return true if OK, false in case of error.
279   */
280  //@{
281  /// returns true, if the specified option has been specified
282  bool OptionPresent(const char *name) const;
283  /// returns named option as a integral value.
284  bool GetIntValue(const char *name,
285                   int &value,
286                   const bool isFatal = false) const;
287 
288  /// returns named option as a floating point value.
289  bool GetDoubleValue(const char *name,
290                      double &value,
291                      const bool isFatal = false) const;
292
293  /// returns named option as a floating point value.
294  bool GetRealValue(const char *name,
295                    Real &value,
296                    const bool isFatal = false) const;
297
298  bool GetFloatValue(const char *name,
299                     float &value,
300                     const bool isFatal = false) const;
301
302  /// returns named option as a boolean value.
303  bool GetBoolValue(const char *name,
304                    bool &value,
305                    const bool isFatal = false) const;
306
307  bool GetBool(const char *name, const bool isFatal = false) const;
308 
309  /// returns named option as a 3D vector.
310  bool GetVectorValue(const char *name,
311                      Vector3 &v,
312                      const bool isFatal = false) const;
313
314  /// returns named option as a character string.
315  bool GetStringValue(const char *name,
[1004]316                                          char *value,
317                                          const bool isFatal = false) const;
[372]318 
319  //@}
320  /**@name Options set functions.
321   These functions are used for setting individual options.
322   @param name Name of the option to set.
323   @param value Value to set the option to.
324   */
325  //@{
326  /// sets named option as a integral value.
327  void SetInt(const char *name, const int value);
328  /// sets named option as a floating point value.
329  void SetFloat(const char *name, const Real value);
330  /// sets named option as a boolean value.
331  void SetBool(const char *name, const bool value);
332  /// sets named option as a 3D vector.
333  void SetVector(const char *name,
334                 const Vector3 &v);
335  /// sets named option as a character string.
336  void SetString(const char *name, const char *value);
337  //@}
338  //@}
339
[938]340  bool Parse(const int argc, char **argv, bool useExePath);
[372]341
342  void
343  CodeString(char *buff, int max);
344
345  void
346  DecodeString(char *buff, int max);
347
348  void
349  SaveCodedFile(char *filenameText,
350                char *filenameCoded);
351
[1004]352protected:
353
354        /**@name Methods */  //@{
355  /// Constructor of the CEnvironment class.
356  Environment();
357  /// Destructor of the CEnvironment class.
358  virtual ~Environment();
359
360private:
361
362        static Environment *sEnvironment;
[372]363};
364
365// global environment value
[1004]366//extern Environment *environment;
[372]367
[860]368}
369
[372]370#endif // __ENVIRON_H__
371
Note: See TracBrowser for help on using the repository browser.