33 #ifndef GRAPHLAB_OPTIONS_MAP_HPP
34 #define GRAPHLAB_OPTIONS_MAP_HPP
39 #include <boost/lexical_cast.hpp>
41 #include <graphlab/util/stl_util.hpp>
42 #include <graphlab/util/generics/robust_cast.hpp>
66 const std::string &val) {
67 options[opt].strval = val;
69 options[opt].intval = boost::lexical_cast<
int>(val);
70 }
catch(boost::bad_lexical_cast& error) {options[opt].intval = 0; }
72 options[opt].dblval = boost::lexical_cast<
double>(val);
73 }
catch(boost::bad_lexical_cast& error) { options[opt].dblval = 0.0; }
75 if (val ==
"true" || val ==
"TRUE" ||
76 val ==
"yes" || val ==
"YES" || val ==
"1") options[opt].boolval =
true;
80 void set_option(
const std::string& opt,
const T& val) {
81 if (boost::is_convertible<T, std::string>::value) {
84 options[opt].strval =
robust_cast<std::string>(val);
94 inline bool is_set(
const std::string& opt)
const {
95 return options.find(opt) != options.end();
102 inline bool get_option(
const std::string& opt, std::string& val)
const {
103 std::map<std::string, option_values>::const_iterator i = options.find(opt);
104 if (i == options.end())
return false;
105 val = i->second.strval;
112 inline bool get_option(
const std::string& opt,
bool& val)
const {
113 std::map<std::string, option_values>::const_iterator i = options.find(opt);
114 if (i == options.end())
return false;
115 val = i->second.boolval;
123 template <
typename IntType>
124 inline bool get_option(
const std::string& opt, IntType& val)
const {
125 std::map<std::string, option_values>::const_iterator i = options.find(opt);
126 if (i == options.end())
return false;
127 val = i->second.intval;
134 inline bool get_option(
const std::string& opt,
float& val)
const {
135 std::map<std::string, option_values>::const_iterator i = options.find(opt);
136 if (i == options.end())
return false;
137 val = i->second.dblval;
144 inline bool get_option(
const std::string& opt,
double& val)
const {
145 std::map<std::string, option_values>::const_iterator i = options.find(opt);
146 if (i == options.end())
return false;
147 val = i->second.dblval;
172 std::string opt, value;
175 getline(s, opt,
'=');
176 if (s.bad() || s.eof())
return false;
178 getline(s, value,
' ');
179 if (s.bad())
return false;
195 std::vector<std::string> get_option_keys()
const {
196 std::map<std::string, option_values>::const_iterator iter = options.begin();
197 std::vector<std::string> ret;
198 while (iter != options.end()) {
199 ret.push_back(iter->first);
210 std::map<std::string, option_values> options;
215 std::ostream& operator<<(std::ostream& out,