Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
singleton_argument.hpp
Go to the documentation of this file.
1 #ifndef STAN__GM__ARGUMENTS__SINGLETON__ARGUMENT__BETA
2 #define STAN__GM__ARGUMENTS__SINGLETON__ARGUMENT__BETA
3 
4 #include <iostream>
5 #include <boost/lexical_cast.hpp>
7 
8 namespace stan {
9 
10  namespace gm {
11 
12  template <typename T>
13  struct type_name {
14  static std::string name() { return typeid(T).name(); }
15  };
16 
17  // Specialize to something more readable
18  template <>
19  struct type_name<int> {
20  static std::string name() { return "int"; }
21  };
22 
23  template <>
24  struct type_name<unsigned int> {
25  static std::string name() { return "unsigned int"; }
26  };
27 
28  template <>
29  struct type_name<double> {
30  static std::string name() { return "double"; }
31  };
32 
33  template <>
34  struct type_name<bool> {
35  static std::string name() { return "boolean"; }
36  };
37 
38  template <>
39  struct type_name<std::string> {
40  static std::string name() { return "string"; }
41  };
42 
43  template<typename T>
45 
46  public:
47 
49  _constrained = false;
50  _name = "";
52  }
53 
54  singleton_argument(const std::string name): _validity("All") {
55  _name = name;
56  }
57 
58 
59  bool parse_args(std::vector<std::string>& args, std::ostream* out,
60  std::ostream* err, bool& help_flag) {
61  if (args.size() == 0)
62  return true;
63 
64  if ( (args.back() == "help") || (args.back() == "help-all") ) {
65  print_help(out, 0);
66  help_flag |= true;
67  args.clear();
68  return true;
69  }
70 
71  std::string name;
72  std::string value;
73  split_arg(args.back(), name, value);
74 
75  if (_name == name) {
76 
77  args.pop_back();
78 
79  T proposed_value = boost::lexical_cast<T>(value);
80 
81  if (!set_value(proposed_value)) {
82 
83  if (err) {
84  *err << proposed_value << " is not a valid value for "
85  << "\"" << _name << "\"" << std::endl;
86  *err << std::string(indent_width, ' ')
87  << "Valid values:" << print_valid() << std::endl;
88  }
89 
90  args.clear();
91  return false;
92  }
93 
94  }
95  return true;
96  }
97 
98  virtual void probe_args(argument* base_arg, std::stringstream& s) {
99 
100  s << "good" << std::endl;
102  base_arg->print(&s, 0, "");
103  s << std::endl;
104 
105  if (_constrained) {
106  s << "bad" << std::endl;
107  _value = _bad_value;
108  base_arg->print(&s, 0, "");
109  s << std::endl;
110  }
111 
113 
114  }
115 
116  void find_arg(std::string name,
117  std::string prefix,
118  std::vector<std::string>& valid_paths) {
119  if (name == _name) {
120  valid_paths.push_back(prefix + _name + "=<" + _value_type + ">");
121  }
122  }
123 
124  T value() { return _value; }
125 
126  bool set_value(const T& value) {
127 
128  if (is_valid(value)) {
129  _value = value;
130  return true;
131  }
132 
133  return false;
134  }
135 
136  std::string print_value() {
137  return boost::lexical_cast<std::string>(_value);
138  }
139 
140  std::string print_valid() {
141  return " " + _validity;
142  }
143 
144  bool is_default() {
145  return _value == _default_value;
146  }
147 
148 
149  protected:
150 
151  std::string _validity;
152  virtual bool is_valid(T value) { return true; }
153 
156 
158 
161 
162  };
163 
169 
170  } // gm
171 
172 } // stan
173 
174 #endif
virtual void print_help(std::ostream *s, const int depth, const bool recurse=false)
bool parse_args(std::vector< std::string > &args, std::ostream *out, std::ostream *err, bool &help_flag)
singleton_argument(const std::string name)
virtual void probe_args(argument *base_arg, std::stringstream &s)
singleton_argument< int > int_argument
singleton_argument< std::string > string_argument
static void split_arg(const std::string &arg, std::string &name, std::string &value)
Definition: argument.hpp:54
singleton_argument< double > real_argument
std::string _name
Definition: argument.hpp:76
singleton_argument< unsigned int > u_int_argument
void find_arg(std::string name, std::string prefix, std::vector< std::string > &valid_paths)
std::string name() const
Definition: argument.hpp:26
static std::string name()
virtual void print(std::ostream *s, const int depth, const std::string prefix)=0
singleton_argument< bool > bool_argument

     [ Stan Home Page ] © 2011–2014, Stan Development Team.