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 #include "StringOp.hh"
8 #include "stl.hh"
9 #include <algorithm>
10 
11 namespace openmsx {
12 
14 
16  : baseMap(std::move(map))
17 {
18  sort(begin(baseMap), end(baseMap), Comp());
19 }
20 
22 {
23  auto it = lower_bound(begin(baseMap), end(baseMap), str, Comp());
25  if ((it == end(baseMap)) || !cmp(it->first, str)) {
26  throw CommandException("not a valid value: " + str);
27  }
28  return it->second;
29 }
30 
32 {
33  for (auto& p : baseMap) {
34  if (p.second == value) {
35  return p.first;
36  }
37  }
38  UNREACHABLE; return "";
39 }
40 
41 std::vector<string_ref> EnumSettingBase::getPossibleValues() const
42 {
43  std::vector<string_ref> result;
44  for (auto& p : baseMap) {
45  result.push_back(p.first);
46  }
47  return result;
48 }
49 
51 {
52  TclObject valueList;
54  result.addListElement(valueList);
55 }
56 
57 void EnumSettingBase::tabCompletionBase(std::vector<std::string>& tokens) const
58 {
60  false); // case insensitive
61 }
62 
63 } // namespace openmsx
string_ref::const_iterator end(const string_ref &x)
Definition: string_ref.hh:135
int fromStringBase(string_ref str) const
Definition: EnumSetting.cc:21
EnumSettingBase(BaseMap &&m)
Definition: EnumSetting.cc:15
STL namespace.
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
string_ref toStringBase(int value) const
Definition: EnumSetting.cc:31
std::vector< std::pair< std::string, int > > BaseMap
Definition: EnumSetting.hh:18
void tabCompletionBase(std::vector< std::string > &tokens) const
Definition: EnumSetting.cc:57
CmpTupleElement< 0, StringOp::caseless > Comp
Definition: EnumSetting.cc:13
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
std::vector< string_ref > getPossibleValues() const
Definition: EnumSetting.cc:41
void addListElement(string_ref element)
Definition: TclObject.cc:110
static void completeString(std::vector< std::string > &tokens, const RANGE &possibleValues, bool caseSensitive=true)
Definition: Completer.hh:88
void additionalInfoBase(TclObject &result) const
Definition: EnumSetting.cc:50
string_ref::const_iterator begin(const string_ref &x)
Definition: string_ref.hh:134
void addListElements(ITER begin, ITER end)
Definition: TclObject.hh:79
#define UNREACHABLE
Definition: unreachable.hh:56