File: Parameters.h

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (251 lines) | stat: -rw-r--r-- 9,578 bytes parent folder | download | duplicates (5)
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
#ifndef _Parameters_h
#define _Parameters_h

#include <iostream>
#include <ostream>
#include <string>
#include <cstdlib>
#include <cstdio>
//#include "macros.h"
//#include "DebugStream.h"
//#include "StringUtils.h"

using std::string;
using std::istream;
using namespace std;

/*
CLASS
  Parameters

  A utility class used to manage program parameters. The class supports 
  setting default values for parameters, reading values from a parameters
  file and accessing parameters values from other parts of the program.

KEYWORDS
  parameters

AUTHORS
  Meir Fuchs (mailto: meirfux@math.tau.ac.il)

  Copyright: SAMBA group, Tel-Aviv Univ. Israel, 1997.

CHANGES LOG
<UL>
<LI>9.01.05 Dina:
Bug fix: adding check to iterator end() to findInsertionPoint result
to paramType, getInt, getString, getFloat functions 
</LI>
<LI>17.05.04 Oranit Dror:
Adding new methods: dump() and empty()
</LI>
</UL>

GOALS
  Aid in managing program parameters. The Parameters class's main goal is to
  relieve programmers from the need to rewrite specialized parameters reading
  code sections for each of the programs. The Parameters class holds integer,
  floating point or string values in static storage indexed using the
  parameter's name. Class also supplies method for parsing strings.

USAGE
  The following section covers several issues regarding the Parameters class
  and its usage. Users should understand the issues covered below before
  using the class.

USAGE: SETTING DEFAULT PARAMETERS
  Default parameters are set using the addParameter methods. Note that the 
  type of the parameter is set according to the addParameter arguments. If
  a parameter is set using addParameter with an integer argument then
  subsequent updates (using updateParameter) to the same parameter will all 
  be stored as integers. Therefore the following code should output a 0:
  EXAMPLE
    Parameters::addParameter("Dummy", 3);
    Parameters::updateParameter("Dummy", "This should set it to zero");
    cout << Parameters::getstring("Dummy");
  END
  
  Note also that when setting defuault values of float parameters always use
  a decimal point or else these parameters will be added as intgers. For
  example:
  EXAMPLE
    Parameters::addParameter("CubeSize", 1.0);  OK
    Parameters::addParameter("CubeSize", 1);    Not OK. Integer parameter
  END

USAGE: READING PARAMETERS FROM FILE
  The readParameters method recieves an input stream from which parameters are
  to be read. Files are structured so that each line specifies the value of a
  parameter. Each line gives the parameter name, a white space and then the
  parameter value. Lines whose first non white-space charachter is # are
  ignored. A basic schema for using the Parameters class is to set the default
  values using addParameter calls and then calling readParameters to read in
  parameters with other values or new parameters. The following example works
  as such using the Parameters::dump method to print all the parameters
  and their values:
  EXAMPLE
    Parameters::addParameter("CubeSize", 1.0);
    Parameters::addParameter("MinVote", 8);
    ifstream params("params");
    Parameters::readParameters(params);
    params.close();
    Parameters::dump(cout);
  END
  With the following parameters file:
  EXAMPLE
    CubeSize 0.5
    File  pdb4hhb.ent
  END
  The following output should result:
  EXAMPLE
    CubeSize (Float) 0.5
    File     (Str)   pdb4hhb.ent
    MinVote  (Int)   8
  END

USAGE: ACCESSING PARAMETERS VALUES
  using the getInt, getFloat and getstring methods one may access the 
  parameters values. Note that a value will always be returned even if the 
  parameter is not stored as the same type. The get methods attempt to
  convert the parameter type to the requested return type of the method.
  The follwing code should produce 3 1's as its output:
  EXAMPLE:
    Parameters::addParameter("MaxMix", 1);   OK added an integer parameter
    cout << Parameters::getInt("MaxMix");
    cout << Parameters::getFloat("MaxMix");
    cout << Parameters::getstring("MaxMix");
  END
  Also note that parameters names are case sensitive.

USAGE: SUBCLASSING AND PERFORMANCE
  The Parameters engine keeps the parameters in a sorted list. Although
  finding a parameter and its value in this list is considerably fast most
  users will not want this overhead of searching for the parameter using
  string comparisons inside their main loops, as part of a code which can be 
  executed a great number of times. 
  The idea is to subclass the Parameters class and hold the values which
  require direct and fast access in seperate static variables. All parameters
  are accessed not throguh the getParameter methods but rather through
  specialized methods of the subclass. The following is an example of such an
  implementation. Notice the readParameters method.
  EXAMPLE:
    static int min_vote = 8;         // Default values
    static float cube_size = 1.0;

    class ProgParams : protected Parameters
    {
      int minVote()  { return min_vote };

      float cubeSize() { return cube_size };

      // file name is not held in static variable. Don't care about parameter
      // access time.
      string fileName() { return getstring("FileName"); }

      int readParameters(char* paramsfile) {
        addParameter("MinVote", min_vote);
        addParameter("CubeSize", cube_size);

        ifstream params(paramsfile);
        Parameters::readParameters(params);
        params.close();
        
        min_vote = getInt("MinVote");
        cube_size = getFloat("CubeSize");
      }
    } 
  END
*/
class Parameters 
{
public:
  //// Used by the paramType method. See below.
  enum ParamType { Undef, Int, Float, Str };

  //// readParameters recieves an input stream and reads parameters off this
  // input stream. See the usage section for details of how a parameters
  // file may be structured.
  static void readParameters(istream& paramStream);
  
  ////
  // Returns true if no parameters are defined. <br>
  // Author: Oranit Dror (oranit@tau.ac.il)   
  static bool empty(); 

  // GROUP: Setting parameters

  //// Adds an integer parameter. The integer value added will actually be
  // stored as an integer. Subsequent updates to the same parameter using
  // updateParameter will all be stored as integers.
  static void addParameter(const string& paramName, const int value);

  //// Adds a float parameter. The float value added will actually be
  // stored as a float. Subsequent updates to the same parameter using
  // updateParameter will all be stored as floats.
  static void addParameter(const string& paramName, const double value);

  //// Adds a string parameter. The string value added will actually be
  // stored as a string. Subsequent updates to the same parameter using
  // updateParameter will all be stored as strings.
  static void addParameter(const string& paramName, const string& value);

  //// Update the parameter value without changing the parameter type. The
  // value parameter is converted to the parameter's type if this parameter
  // already exists. If the parameter is not yet listed then updateParameter
  // adds a new parameter of string type.
  static void updateParameter(const string& paramName, 
                              const char* const value);

  // GROUP: Getting parameters values.
  
  //// Returns the storage type of the given parameter. If a parameter
  // of the given name does not exist then Undef is returned. See enum
  // ParamType above for possible return values.
  static ParamType paramType(const string& paramName);

  //// Gets the integer value of a given parameter. If parameter is not of
  // integer type then its value is converted to integer. If parameter does
  // not exist a 0 is returned.
  static int       getInt(const string& paramName, const int& defaultValue=0);

  //// Gets the float value of a given parameter. If parameter is not of
  // float type then its value is converted to float. If parameter does
  // not exist a 0 is returned.
  static float     getFloat(const string& paramName, const float& defaultValue=0.0);

  //// Gets the string value of a given parameter. If parameter is not of
  // string type then its value is converted to string. If parameter does
  // not exist an empty string is returned.
  static string    getString(const string& paramName, const string& defaultValue=string());

  // GROUP: Other methods



  //// Output all listed parameters. Used for debugging.
  static void   dump(ostream& out);

  //// Output all listed parameters. Used for debugging.
  //static void   dump(DebugStream& out, const unsigned int msgLevel);

  //// 
  // Output all listed parameters. <br>
  // Author: Oranit Dror (oranit@tau.ac.il) 
  static void dump(FILE* outputFile);

  //// A utility method. nextToken recieves an argument string, finds the first
  // white-space delimited token in this string and returns it while cutting 
  // this token off of the argument string (It it passed by reference). Tokens
  // are returned without any spaces. This method may be used repetitively to
  // tokenize a string.
  static string nextToken(string& str);

protected:
  //// Constructor is protected since all methods are static. No need to 
  // actually form an instance of this class.
  Parameters();
};

#endif