More cleanup via initializer lists
[lsnes.git] / src / interface / setting.cpp
blob62ff68178d696cec9d1ca738bc24885f1cc65fd7
1 #include "interface/setting.hpp"
2 #include "library/register-queue.hpp"
3 #include "library/string.hpp"
5 core_setting_value::core_setting_value(const core_setting_value_param& p) throw(std::bad_alloc)
6 : iname(p.iname), hname(p.hname), index(p.index)
10 core_setting::core_setting(const core_setting_param& p)
11 : iname(p.iname), hname(p.hname), regex(p.regex ? p.regex : ""), dflt(p.dflt)
13 for(auto i : p.values)
14 values.push_back(core_setting_value(i));
17 bool core_setting::is_boolean() const throw()
19 if(values.size() != 2)
20 return false;
21 std::string a = values[0].iname;
22 std::string b = values[1].iname;
23 if(a > b)
24 std::swap(a, b);
25 return (a == "0" && b == "1");
28 bool core_setting::is_freetext() const throw()
30 return (values.size() == 0);
33 bool core_setting::validate(const std::string& value) const
35 if(values.size() != 0) {
36 for(auto i : values)
37 if(i.iname == value)
38 return true;
39 return false;
40 } else
41 return regex_match(regex, value);
44 core_setting_group::core_setting_group(std::initializer_list<core_setting_param> _settings)
46 for(auto i : _settings)
47 settings.insert(std::make_pair(i.iname, core_setting(i)));
50 core_setting_group::core_setting_group(std::vector<core_setting_param> _settings)
52 for(auto i : _settings)
53 settings.insert(std::make_pair(i.iname, core_setting(i)));
56 void core_setting_group::fill_defaults(std::map<std::string, std::string>& values) throw(std::bad_alloc)
58 for(auto i : settings)
59 if(!values.count(i.first))
60 values[i.first] = i.second.dflt;
63 std::set<std::string> core_setting_group::get_setting_set()
65 std::set<std::string> r;
66 for(auto i : settings)
67 r.insert(i.first);
68 return r;
71 std::vector<std::string> core_setting::hvalues() const throw(std::runtime_error)
73 std::vector<std::string> x;
74 if(values.size() == 0)
75 throw std::runtime_error("hvalues() not valid for freetext settings");
76 for(auto i : values)
77 x.push_back(i.hname);
78 return x;
81 std::string core_setting::hvalue_to_ivalue(const std::string& hvalue) const throw(std::runtime_error)
83 for(auto i : values)
84 if(i.hname == hvalue)
85 return i.iname;
86 throw std::runtime_error("Invalid hvalue for setting");
89 signed core_setting::ivalue_to_index(const std::string& ivalue) const throw(std::runtime_error)
91 for(auto i : values)
92 if(i.iname == ivalue)
93 return i.index;
94 throw std::runtime_error("Invalid ivalue for setting");