Stan  2.10.0
probability, sampling & optimization
singleton_argument.hpp
Go to the documentation of this file.
1 #ifndef STAN_SERVICES_ARGUMENTS_SINGLETON_ARGUMENT_HPP
2 #define STAN_SERVICES_ARGUMENTS_SINGLETON_ARGUMENT_HPP
3 
5 #include <boost/lexical_cast.hpp>
6 #include <iostream>
7 #include <string>
8 #include <vector>
9 
10 namespace stan {
11  namespace services {
12 
13  template <typename T>
14  struct type_name {
15  static std::string name() { return typeid(T).name(); }
16  };
17 
18  // Specialize to something more readable
19  template <>
20  struct type_name<int> {
21  static std::string name() { return "int"; }
22  };
23 
24  template <>
25  struct type_name<unsigned int> {
26  static std::string name() { return "unsigned int"; }
27  };
28 
29  template <>
30  struct type_name<double> {
31  static std::string name() { return "double"; }
32  };
33 
34  template <>
35  struct type_name<bool> {
36  static std::string name() { return "boolean"; }
37  };
38 
39  template <>
40  struct type_name<std::string> {
41  static std::string name() { return "string"; }
42  };
43 
44  template<typename T>
46  public:
48  _constrained = false;
49  _name = "";
51  }
52 
53  explicit singleton_argument(const std::string name): _validity("All") {
54  _name = name;
55  }
56 
57  bool parse_args(std::vector<std::string>& args,
60  bool& help_flag) {
61  if (args.size() == 0)
62  return true;
63 
64  if ( (args.back() == "help") || (args.back() == "help-all") ) {
65  print_help(info, 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  args.pop_back();
77 
78  T proposed_value = boost::lexical_cast<T>(value);
79 
80  if (!set_value(proposed_value)) {
81  std::stringstream message;
82  message << proposed_value
83  << " is not a valid value for "
84  << "\"" << _name << "\"";
85  err(message.str());
86  err(std::string(indent_width, ' ')
87  + "Valid values:" + print_valid());
88 
89  args.clear();
90  return false;
91  }
92  }
93  return true;
94  }
95 
96  virtual void probe_args(argument* base_arg,
98  w("good");
100  base_arg->print(w, 0, "");
101  w();
102 
103  if (_constrained) {
104  w("bad");
105  _value = _bad_value;
106  base_arg->print(w, 0, "");
107  w();
108  }
109 
111  }
112 
113  void find_arg(const std::string& name,
114  const std::string& prefix,
115  std::vector<std::string>& valid_paths) {
116  if (name == _name) {
117  valid_paths.push_back(prefix + _name + "=<" + _value_type + ">");
118  }
119  }
120 
121  T value() { return _value; }
122 
123  bool set_value(const T& value) {
124  if (is_valid(value)) {
125  _value = value;
126  return true;
127  }
128  return false;
129  }
130 
131  std::string print_value() {
132  return boost::lexical_cast<std::string>(_value);
133  }
134 
135  std::string print_valid() {
136  return " " + _validity;
137  }
138 
139  bool is_default() {
140  return _value == _default_value;
141  }
142 
143  protected:
144  std::string _validity;
145  virtual bool is_valid(T value) { return true; }
146 
149 
151 
154  };
155 
161 
162  } // services
163 } // stan
164 
165 #endif
singleton_argument< unsigned int > u_int_argument
Probability, optimization and sampling library.
singleton_argument(const std::string name)
static void split_arg(const std::string &arg, std::string &name, std::string &value)
Definition: argument.hpp:60
Template specification of functions in std for Stan.
virtual void print_help(interface_callbacks::writer::base_writer &w, const int depth, const bool recurse=false)
singleton_argument< std::string > string_argument
singleton_argument< bool > bool_argument
base_writer is an abstract base class defining the interface for Stan writer callbacks.
Definition: base_writer.hpp:20
singleton_argument< double > real_argument
void find_arg(const std::string &name, const std::string &prefix, std::vector< std::string > &valid_paths)
bool parse_args(std::vector< std::string > &args, interface_callbacks::writer::base_writer &info, interface_callbacks::writer::base_writer &err, bool &help_flag)
virtual void probe_args(argument *base_arg, stan::interface_callbacks::writer::base_writer &w)
virtual void print(interface_callbacks::writer::base_writer &w, const int depth, const std::string &prefix)=0
std::string name() const
Definition: argument.hpp:26
singleton_argument< int > int_argument

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