Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / library / memorywatch.hpp
blobe0f22d059dae5be64b6ce03bb6f821fa3c30b02c
1 #ifndef _library__memorywatch__hpp__included__
2 #define _library__memorywatch__hpp__included__
4 #include "mathexpr.hpp"
5 #include <list>
6 #include <set>
7 #include <map>
9 class memory_space;
11 namespace memorywatch
13 /**
14 * Read memory operator.
16 struct memread_oper : public mathexpr::operinfo
18 /**
19 * Ctor
21 memread_oper();
22 /**
23 * Dtor
25 ~memread_oper();
26 /**
27 * Evaluate the operator.
29 * Note: The first promise is for the address.
31 void evaluate(mathexpr::value target, std::vector<std::function<mathexpr::value()>> promises);
32 //Fields.
33 unsigned bytes; //Number of bytes to read.
34 bool signed_flag; //Is signed?
35 bool float_flag; //Is float?
36 int endianess; //Endianess (-1 => little, 0 => host, 1 => Big).
37 uint64_t scale_div; //Scale divisor.
38 uint64_t addr_base; //Address base.
39 uint64_t addr_size; //Address size (0 => All).
40 memory_space* mspace; //Memory space to read.
43 /**
44 * Memory watch item printer.
46 struct item_printer : public GC::item
48 /**
49 * Dtor.
51 virtual ~item_printer();
52 /**
53 * Show the watched value.
55 virtual void show(const std::string& iname, const std::string& val) = 0;
56 /**
57 * Reset the printer.
59 virtual void reset() = 0;
60 protected:
61 void trace();
64 /**
65 * Memory watch item.
67 struct item
69 /**
70 * Ctor.
72 * Parameter t: The type of the result.
74 item(mathexpr::typeinfo& t)
75 : expr(GC::obj_tag(), &t)
78 /**
79 * Get the value as string.
81 std::string get_value();
82 /**
83 * Print the value to specified printer.
85 * Parameter iname: The name of the watch.
87 void show(const std::string& iname);
88 //Fields.
89 GC::pointer<item_printer> printer; //Printer to use.
90 GC::pointer<mathexpr::mathexpr> expr; //Expression to watch.
91 std::string format; //Formatting to use.
94 /**
95 * A set of memory watches.
97 struct set
99 /**
100 * Dtor.
102 ~set();
104 * Call reset on all items and their printers in the set.
106 void reset();
108 * Call reset and then show on all items in the set.
110 void refresh();
112 * Get the longest name (by UTF-8 length) in the set.
114 * Returns: The longest nontrivial name, or "" if none.
116 const std::string& get_longest_name()
118 return get_longest_name(set::utflength_rate);
121 * Get the longest name (by arbitrary function) in the set.
123 * Parameter rate: Get length for name function.
124 * Returns: The longest nontrivial name, or "" if none.
126 const std::string& get_longest_name(std::function<size_t(const std::string& n)> rate);
128 * Get the set of memory watch names.
130 std::set<std::string> names_set();
132 * Get specified memory watch item.
134 * Parameter name: The name of the item.
135 * Returns: The item.
136 * Throws std::runtime_error: No such item in set.
138 item& get(const std::string& name);
140 * Get specified memory watch item (without throwing).
142 * Parameter name: The name of the item.
143 * Returns: The item, or NULL if no such item exists.
145 item* get_soft(const std::string& name);
147 * Create a new memory watch item.
149 * Parameter name: The name of the new item.
150 * Parameter item: The new item. All fields are shallow-copied.
152 item* create(const std::string& name, item& item);
154 * Destroy a memory watch item.
156 * Parameter name: The name of the item to destroy.
158 void destroy(const std::string& name);
160 * Call routine for all roots.
162 void foreach(std::function<void(item& item)> cb);
164 * Swap set with another.
166 void swap(set& s) throw();
167 private:
168 static size_t utflength_rate(const std::string& s);
169 std::map<std::string, item> roots;
173 #endif