OXYGENHUB
◆ main openmsx / src / input / JoystickId.hh
965 B · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef JOYSTICK_ID_HH
2
#define JOYSTICK_ID_HH
3
4
#include "StringOp.hh"
5
#include "strCat.hh"
6
7
#include <optional>
8
#include <string_view>
9
10
namespace openmsx {
11
12
// openMSX specific joystick numbering,
13
//  different from SDL joystick device index and
14
//  different from SDL joystick instance ID.
15
class JoystickId {
16
public:
17
	static constexpr std::string_view PREFIX = "joy";
18
19
	explicit JoystickId(unsigned id_) : id(id_) {}
20
	[[nodiscard]] unsigned raw() const { return id; }
21
	[[nodiscard]] bool operator==(const JoystickId&) const = default;
22
	[[nodiscard]] std::string str() const { return strCat(PREFIX, id + 1); }
23
24
	[[nodiscard]] static std::optional<JoystickId> parse(std::string_view str) {
25
		if (!str.starts_with(PREFIX)) return std::nullopt;
26
		str.remove_prefix(PREFIX.size());
27
		auto n = StringOp::stringToBase<10, unsigned>(str);
28
		if (!n || *n == 0) return std::nullopt;
29
		return JoystickId(*n - 1);
30
	}
31
32
private:
33
	unsigned id;
34
};
35
36
} // namespace openmsx
37
38
#endif