OXYGENHUB
◆ main openmsx / src / imgui / ImGuiWatchExpr.hh
2.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef IMGUI_WATCH_EXPR_HH
2
#define IMGUI_WATCH_EXPR_HH
3
4
#include "ImGuiPart.hh"
5
6
#include "TclObject.hh"
7
8
#include <expected>
9
#include <vector>
10
11
namespace openmsx {
12
13
class SymbolManager;
14
15
class ImGuiWatchExpr final : public ImGuiPart
16
{
17
public:
18
	struct WatchExpr {
19
		static constexpr std::string_view prefix = "we#";
20
21
		WatchExpr() : id(++lastId) {}
22
		WatchExpr(std::string description_, std::string exprStr_, TclObject format_)
23
			: id(++lastId)
24
			, description(std::move(description_))
25
			, exprStr(std::move(exprStr_)) // leave 'expression' empty
26
			, format(std::move(format_)) {}
27
28
		[[nodiscard]] unsigned getId() const { return id; }
29
		[[nodiscard]] std::string getIdStr() const { return strCat(prefix, id); }
30
		[[nodiscard]] const auto& getDescription() const { return description; }
31
		[[nodiscard]] const auto& getExpression()  const { return exprStr; }
32
		[[nodiscard]] const auto& getFormat()      const { return format; }
33
		void setDescription(const TclObject& d) { description = d.getString(); }
34
		void setExpression (const TclObject& e) { exprStr = e.getString(); expression.reset(); }
35
		void setFormat     (const TclObject& f) { format = f.getString(); }
36
37
		unsigned id = 0;
38
		std::string description;
39
		std::string exprStr;
40
		std::optional<TclObject> expression; // cache, generate from 'expression'
41
		TclObject format;
42
43
		static inline unsigned lastId = 0;
44
	};
45
46
public:
47
	explicit ImGuiWatchExpr(ImGuiManager& manager);
48
49
	[[nodiscard]] zstring_view iniName() const override { return "watch expr"; }
50
	void save(ImGuiTextBuffer& buf) override;
51
	void loadStart() override;
52
	void loadLine(std::string_view name, zstring_view value) override;
53
	void paint(MSXMotherBoard* motherBoard) override;
54
	void refreshSymbols();
55
56
	[[nodiscard]] auto& getWatchExprs() { return watches; }
57
58
private:
59
	void drawRow(int row);
60
	void checkSort();
61
62
public:
63
	bool show = false;
64
65
private:
66
	SymbolManager& symbolManager;
67
68
	std::vector<WatchExpr> watches;
69
70
	[[nodiscard]] std::expected<TclObject, std::string> evalExpr(WatchExpr& watch, Interpreter& interp) const;
71
72
	int selectedRow = -1;
73
74
	static constexpr auto persistentElements = std::tuple{
75
		PersistentElement{"show", &ImGuiWatchExpr::show},
76
		// manually handle 'watches'
77
	};
78
};
79
80
} // namespace openmsx
81
82
#endif