StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
options.h
Go to the documentation of this file.
1 /*
2  * options.h
3  * HMM
4  *
5  * Created by Paul C Lott on 8/13/09.
6  * Copyright 2009 University of California, Davis. All rights reserved.
7  *
8  */
9 
10 #ifndef OPTIONS_H
11 #define OPTIONS_H
12 
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <vector>
17 #include <iostream>
18 #include <sstream>
19 #include <stdlib.h>
20 
21 namespace StochHMM{
22 
23 
24 //Options Types
26 
27 #define MAX_ALLOWABLE 10
28 
29 
30 
31 
32  /*!User defined option parameters
33  opt_parameters set up in beginning of your program and defines what options
34  are accepted by the program.
35  Used for designing allowed commandline options
36  option Types:
37  UNDEF_OPT - initialized value not defined
38  OPT_NONE - Functions as a bool (was it set or not)
39  OPT_INT - Integer value supplied as option
40  OPT_DOUBLE - Double value supplied as option
41  OPT_STRING - String value supplied as option
42  OPT_FLAG - Predefined allowed values (each functions as a bool)
43 
44  Colums of table:
45  Option tag- all possible alternatives separated by colon
46  Option type- what type will it be passed
47  Option required - If option is required to run the it should be true, else false
48  Option default - Default value for option
49  Secondary Tag - List of possible secondary tags.
50 
51  Usage:
52  opt_parameters commandline[] - {
53  {"-help:-h" ,OPT_NONE, false, "", {}},
54  {"-model:-m",OPT_STRING, true, "",{}},
55  {"-play:-p", OPT_FLAG, false, "", {"Ball", "Train"}
56  }
57  int opts_size=sizeof(commandline)/sizeof(commandline[0]); //required to automatically figure # of options defined
58 
59  options opt;
60  int main (int argc,const char *argv[]){
61  opt.set_paramenters(commandline,opt_size,usage); //sets up the options you've defined in commandline variable
62  opt.parse_commandline(argc,argv); //Parses the commandline and assigns the variables;
63  //To get the string value from model
64  string filename=sopt("-model");
65  //To see if scale was set
66  if (optset("-scale"))
67  double scale=sopt("-scale"); //Return value of scale option
68  return 1;
69  };
70  */
72  std::string commandline_tag; //Tag used on the commandline
73  optionType type; //Option Tag type
74  bool required; //Is the option required to be set in order to run
75  std::string preset; //Preset for the option
76  std::string allowable[MAX_ALLOWABLE]; //allowable options secondary tags
77  };
78 
79 
80 
81  // TODO: Need to make opt_data private and allow options to access
82  //Actual commandline options data stored in this struct
83 
84  //!Store the actual command line options data for each option defined by user
85  class opt_data{
86  public:
87  opt_data();
88  optionType type; //what datatype does the option store
89  bool set; //Has the parameter been set (OPT_NONE AND EVERYTHING ELSE)
90  bool default_value; //Is default value set
91  bool required; //Is the parameter required, else exit
92  bool restricted; //Are the entries restricted to allowable
93  union { //If its a double or int for OPT_INT or OPT_DOUBLE;
94  int i;
95  double d;
96  }data;
97  std::string str; //String value for OPT_STRING
98  std::map<std::string,bool> flags; //Allowable flags or entries for OPT_FLAG
99  };
100 
101 
102 
103  //!Parse, Store, and access commandline options data
104  class options{
105  public:
106  options();
107 
108  void set_parameters(opt_parameters*,int,const char*); //Setup the parameter you've defined in the options variable
109  void parse_commandline(int, const char**); //Parses the commandline
110 
111  bool getopt(const char*,int&); //Get int for option type OPT_INT
112  bool getopt(const char*,double&); //Get double for option OPT_DOUBLE
113  bool getopt(const char*,std::string&); //Get string from option OPT_STRING
114 
115  //Accessor functions
116  bool isFlagSet(const char*, const char*); //Get status of secondary option OPT_FLAG
117  bool isSet(const char*); // check to see if options has been set
118 
119 
120  std::string &sopt(const char*); //return string for option
121  int &iopt(const char*); //return int for option
122  double &dopt(const char*); //return double for option
123 
124  private:
125  std::map<std::string,opt_data*> opts; //map each option to string tag
126  std::map<std::string,std::string> alternatives; //Store the tag alternatives as keys and main as value
127  const char* usage;
128 
129  };
130 
131 }
132 #endif /*OPTIONS_H*/