CoinParam.hpp
Go to the documentation of this file.
1 /* $Id: CoinParam.hpp 1191 2009-07-25 08:38:12Z forrest $ */
2 #ifndef CoinParam_H
3 #define CoinParam_H
4 
5 /*
6  Copyright (C) 2002, International Business Machines
7  Corporation and others. All Rights Reserved.
8 */
9 
14 #include <vector>
15 #include <string>
16 
72 class CoinParam
73 {
74 
75 public:
76 
79 
92  typedef enum { coinParamInvalid = 0,
95 
103  typedef int (*CoinParamFunc)(CoinParam *param) ;
104 
106 
113 
116  CoinParam() ;
117 
124  CoinParam(std::string name, std::string help,
125  double lower, double upper, double dflt = 0.0,
126  bool display = true) ;
127 
132  CoinParam(std::string name, std::string help,
133  int lower, int upper, int dflt = 0,
134  bool display = true) ;
135 
150  CoinParam(std::string name, std::string help,
151  std::string firstValue, int dflt, bool display = true) ;
152 
161  CoinParam(std::string name, std::string help,
162  std::string dflt, bool display = true) ;
163 
166  CoinParam(std::string name, std::string help,
167  bool display = true) ;
168 
171  CoinParam(const CoinParam &orig) ;
172 
175  virtual CoinParam *clone() ;
176 
179  CoinParam &operator=(const CoinParam &rhs) ;
180 
183  virtual ~CoinParam() ;
184 
186 
189 
192  void appendKwd(std::string kwd) ;
193 
198  int kwdIndex(std::string kwd) const ;
199 
203  std::string kwdVal() const ;
204 
211  void setKwdVal(int value, bool printIt = false) ;
212 
219  void setKwdVal(const std::string value ) ;
220 
224  void printKwds() const ;
225 
226 
229  void setStrVal(std::string value) ;
230 
233  std::string strVal() const ;
234 
235 
238  void setDblVal(double value) ;
239 
242  double dblVal() const ;
243 
244 
247  void setIntVal(int value) ;
248 
251  int intVal() const ;
252 
253 
256  inline void setShortHelp(const std::string help) { shortHelp_ = help ; }
257 
260  inline std::string shortHelp() const { return (shortHelp_) ; }
261 
267  inline void setLongHelp(const std::string help) { longHelp_ = help ; }
268 
271  inline std::string longHelp() const { return (longHelp_) ; }
272 
281  void printLongHelp() const ;
282 
284 
287 
290  inline CoinParamType type() const { return (type_) ; }
291 
294  inline void setType(CoinParamType type) { type_ = type ; }
295 
298  inline std::string name() const { return (name_) ; }
299 
302  inline void setName(std::string name) { name_ = name ; processName() ; }
303 
311  int matches (std::string input) const ;
312 
319  std::string matchName() const ;
320 
327  inline void setDisplay(bool display) { display_ = display ; }
328 
331  inline bool display() const { return (display_) ; }
332 
335  inline CoinParamFunc pushFunc() { return (pushFunc_) ; }
336 
339  inline void setPushFunc(CoinParamFunc func) { pushFunc_ = func ; }
340 
343  inline CoinParamFunc pullFunc() { return (pullFunc_) ; }
344 
347  inline void setPullFunc(CoinParamFunc func) { pullFunc_ = func ; }
348 
350 
351 private:
352 
355 
357  void processName() ;
358 
360 
365 
367  std::string name_ ;
368 
370  unsigned int lengthName_ ;
371 
375  unsigned int lengthMatch_ ;
376 
378  double lowerDblValue_ ;
379 
381  double upperDblValue_ ;
382 
384  double dblValue_ ;
385 
388 
391 
393  int intValue_ ;
394 
396  std::string strValue_ ;
397 
399  std::vector<std::string> definedKwds_ ;
400 
404 
407 
410 
412  std::string shortHelp_ ;
413 
415  std::string longHelp_ ;
416 
418  bool display_ ;
420 
421 } ;
422 
426 typedef std::vector<CoinParam*> CoinParamVec ;
427 
431 std::ostream &operator<< (std::ostream &s, const CoinParam &param) ;
432 
433 /*
434  Bring in the utility functions for parameter handling (CbcParamUtils).
435 */
436 
444 namespace CoinParamUtils {
450  void setInputSrc(FILE *src) ;
451 
455  bool isCommandLine() ;
456 
460  bool isInteractive() ;
461 
469  std::string getStringField(int argc, const char *argv[], int *valid) ;
470 
478  int getIntField(int argc, const char *argv[], int *valid) ;
479 
487  double getDoubleField(int argc, const char *argv[], int *valid) ;
488 
501  int matchParam(const CoinParamVec &paramVec, std::string name,
502  int &matchNdx, int &shortCnt) ;
503 
534  std::string getCommand(int argc, const char *argv[],
535  const std::string prompt, std::string *pfx = 0) ;
536 
574  int lookupParam(std::string name, CoinParamVec &paramVec,
575  int *matchCnt = 0, int *shortCnt = 0, int *queryCnt = 0) ;
576 
584  void printIt(const char *msg) ;
585  void shortOrHelpOne(CoinParamVec &paramVec,int matchNdx, std::string
602  name, int numQuery) ;
603 
612  void shortOrHelpMany(CoinParamVec &paramVec,
613  std::string name, int numQuery) ;
614 
620  void printGenericHelp() ;
621 
634  void printHelp(CoinParamVec &paramVec, int firstParam, int lastParam,
635  std::string prefix,
636  bool shortHelp, bool longHelp, bool hidden) ;
637 }
638 
639 
640 #endif /* CoinParam_H */
641 
void setLongHelp(const std::string help)
Add a long help message to a parameter.
Definition: CoinParam.hpp:267
CoinParamFunc pullFunc_
Pull function.
Definition: CoinParam.hpp:409
void printGenericHelp()
Print a generic `how to use the command interface' help message.
void setPullFunc(CoinParamFunc func)
Set pull function.
Definition: CoinParam.hpp:347
void setName(std::string name)
Set the parameter keyword (name) string.
Definition: CoinParam.hpp:302
void shortOrHelpOne(CoinParamVec &paramVec, int matchNdx, std::string name, int numQuery)
Utility routine to print help given a short match or explicit request for help.
int upperIntValue_
Upper bound on value for an integer parameter.
Definition: CoinParam.hpp:390
void printLongHelp() const
Print long help.
std::string shortHelp() const
Retrieve the short help string.
Definition: CoinParam.hpp:260
CoinParam()
Default constructor.
unsigned int lengthMatch_
Minimum length required to declare a match for the parameter name.
Definition: CoinParam.hpp:375
bool isCommandLine()
Returns true if command line parameters are being processed.
void setPushFunc(CoinParamFunc func)
Set push function.
Definition: CoinParam.hpp:339
std::string kwdVal() const
Return the value-keyword that is the current value of the keyword parameter.
void setKwdVal(int value, bool printIt=false)
Set the value of the keyword parameter using the integer associated with a value-keyword.
CoinParamType type_
Parameter type (see CoinParamType)
Definition: CoinParam.hpp:364
std::string getStringField(int argc, const char *argv[], int *valid)
Attempt to read a string from the input.
std::string longHelp_
Long help.
Definition: CoinParam.hpp:415
double upperDblValue_
Upper bound on value for a double parameter.
Definition: CoinParam.hpp:381
std::string name_
Parameter name.
Definition: CoinParam.hpp:367
int intValue_
Integer parameter - current value.
Definition: CoinParam.hpp:393
double getDoubleField(int argc, const char *argv[], int *valid)
Attempt to read a real (double) from the input.
CoinParam & operator=(const CoinParam &rhs)
Assignment.
bool display() const
Get visibility of parameter.
Definition: CoinParam.hpp:331
void setStrVal(std::string value)
Set the value of a string parameter.
void setInputSrc(FILE *src)
Take command input from the file specified by src.
bool isInteractive()
Returns true if parameters are being obtained from stdin.
CoinParamFunc pushFunc_
Push function.
Definition: CoinParam.hpp:406
std::string longHelp() const
Retrieve the long help message.
Definition: CoinParam.hpp:271
void setShortHelp(const std::string help)
Add a short help string to a parameter.
Definition: CoinParam.hpp:256
std::vector< CoinParam * > CoinParamVec
A type for a parameter vector.
Definition: CoinParam.hpp:426
std::string matchName() const
Return the parameter keyword (name) string formatted to show the minimum match length.
void setIntVal(int value)
Set the value of a integer parameter.
int getIntField(int argc, const char *argv[], int *valid)
Attempt to read an integer from the input.
int currentKwd_
Current value for a keyword parameter (index into definedKwds_)
Definition: CoinParam.hpp:403
double lowerDblValue_
Lower bound on value for a double parameter.
Definition: CoinParam.hpp:378
void shortOrHelpMany(CoinParamVec &paramVec, std::string name, int numQuery)
Utility routine to print help given multiple matches.
void processName()
int(* CoinParamFunc)(CoinParam *param)
Type declaration for push and pull functions.
Definition: CoinParam.hpp:103
unsigned int lengthName_
Length of parameter name.
Definition: CoinParam.hpp:370
virtual CoinParam * clone()
Clone.
std::string strValue_
String parameter - current value.
Definition: CoinParam.hpp:396
int matchParam(const CoinParamVec &paramVec, std::string name, int &matchNdx, int &shortCnt)
Scan a parameter vector for parameters whose keyword (name) string matches name using minimal match r...
int lookupParam(std::string name, CoinParamVec &paramVec, int *matchCnt=0, int *shortCnt=0, int *queryCnt=0)
Look up the command keyword (name) in the parameter vector. Print help if requested.
void appendKwd(std::string kwd)
Add an additional value-keyword to a keyword parameter.
void printIt(const char *msg)
Utility to print a long message as filled lines of text.
virtual ~CoinParam()
Destructor.
int intVal() const
Get the value of a integer parameter.
std::ostream & operator<<(std::ostream &s, const CoinParam &param)
A stream output function for a CoinParam object.
bool display_
Display when processing lists of parameters?
Definition: CoinParam.hpp:418
void setType(CoinParamType type)
Set the type of the parameter.
Definition: CoinParam.hpp:294
double dblVal() const
Get the value of a double parameter.
CoinParamType
Enumeration for the types of parameters supported by CoinParam.
Definition: CoinParam.hpp:92
int lowerIntValue_
Lower bound on value for an integer parameter.
Definition: CoinParam.hpp:387
void setDisplay(bool display)
Set visibility of parameter.
Definition: CoinParam.hpp:327
CoinParamFunc pullFunc()
Get pull function.
Definition: CoinParam.hpp:343
void setDblVal(double value)
Set the value of a double parameter.
std::string getCommand(int argc, const char *argv[], const std::string prompt, std::string *pfx=0)
Get the next command keyword (name)
void printIt(const char *msg)
Utility to print a long message as filled lines of text.
A base class for `keyword value' command line parameters.
Definition: CoinParam.hpp:72
int matches(std::string input) const
Check if the specified string matches the parameter keyword (name) string.
int kwdIndex(std::string kwd) const
Return the integer associated with the specified value-keyword.
void printKwds() const
Prints the set of value-keywords defined for this keyword parameter.
std::string strVal() const
Get the value of a string parameter.
double dblValue_
Double parameter - current value.
Definition: CoinParam.hpp:384
void printHelp(CoinParamVec &paramVec, int firstParam, int lastParam, std::string prefix, bool shortHelp, bool longHelp, bool hidden)
Utility routine to print help messages for one or more parameters.
CoinParamType type() const
Return the type of the parameter.
Definition: CoinParam.hpp:290
std::vector< std::string > definedKwds_
Set of valid value-keywords for a keyword parameter.
Definition: CoinParam.hpp:399
std::string shortHelp_
Short help.
Definition: CoinParam.hpp:412
CoinParamFunc pushFunc()
Get push function.
Definition: CoinParam.hpp:335
std::string name() const
Return the parameter keyword (name) string.
Definition: CoinParam.hpp:298