openMSX
EnumSetting.cc
Go to the documentation of this file.
1 #include "EnumSetting.hh"
2 #include "TclObject.hh"
3 #include "Completer.hh"
4 #include "CommandException.hh"
5 #include "stringsp.hh"
6 #include "unreachable.hh"
7 
8 namespace openmsx {
9 
10 int EnumSettingPolicyBase::fromStringBase(const std::string& str) const
11 {
12  // An alternative we used in the past is to use StringOp::caseless
13  // as the map comparator functor. This requires to #include StringOp.hh
14  // in the header file. Because this header is included in many other
15  // files, we prefer not to do that.
16  // These maps are usually very small, so there is no disadvantage on
17  // using a O(n) search here (instead of O(log n)).
18  for (auto& p : baseMap) {
19  if (strcasecmp(str.c_str(), p.first.c_str()) == 0) {
20  return p.second;
21  }
22  }
23  throw CommandException("not a valid value: " + str);
24 }
25 
26 std::string EnumSettingPolicyBase::toStringBase(int value) const
27 {
28  for (auto& p : baseMap) {
29  if (p.second == value) {
30  return p.first;
31  }
32  }
33  UNREACHABLE; return "";
34 }
35 
36 std::vector<std::string> EnumSettingPolicyBase::getPossibleValues() const
37 {
38  std::vector<std::string> result;
39  for (auto& p : baseMap) {
40  try {
41  int value = p.second;
42  checkSetValueBase(value);
43  result.push_back(p.first);
44  } catch (MSXException&) {
45  // ignore
46  }
47  }
48  return result;
49 }
50 
51 void EnumSettingPolicyBase::tabCompletionBase(std::vector<std::string>& tokens) const
52 {
54  false); // case insensitive
55 }
56 
58 {
59  TclObject valueList(result.getInterpreter());
60  valueList.addListElements(this->getPossibleValues());
61  result.addListElement(valueList);
62 }
63 
64 } // namespace openmsx