OXYGENHUB
◆ main openmsx / src / settings / EnumSetting.cc
1.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "EnumSetting.hh"
2
3
#include "CommandException.hh"
4
#include "Completer.hh"
5
#include "TclObject.hh"
6
7
#include "StringOp.hh"
8
#include "ranges.hh"
9
10
#include <algorithm>
11
12
namespace openmsx {
13
14
EnumSettingBase::EnumSettingBase(Map&& map)
15
	: baseMap(std::move(map))
16
{
17
	std::ranges::sort(baseMap, StringOp::caseless{}, &MapEntry::name);
18
}
19
20
int EnumSettingBase::fromStringBase(std::string_view str) const
21
{
22
	if (auto s = binary_find(baseMap, str, StringOp::caseless{}, &MapEntry::name)) {
23
		return s->value;
24
	}
25
	throw CommandException("not a valid value: ", str);
26
}
27
28
std::string_view EnumSettingBase::toStringBase(int value) const
29
{
30
	auto it = std::ranges::find(baseMap, value, &MapEntry::value);
31
	assert(it != baseMap.end());
32
	return it->name;
33
}
34
35
void EnumSettingBase::additionalInfoBase(TclObject& result) const
36
{
37
	TclObject valueList;
38
	valueList.addListElements(getPossibleValues());
39
	result.addListElement(valueList);
40
}
41
42
void EnumSettingBase::tabCompletionBase(std::vector<std::string>& tokens) const
43
{
44
	Completer::completeString(tokens, getPossibleValues(),
45
	                          false); // case insensitive
46
}
47
48
} // namespace openmsx