Add <functional> to files that use std::function
[lsnes.git] / include / library / memorywatch.hpp
blobe3fb6498b06dc3eb7670a2011c461dd397d0ae69
1 #ifndef _library__memorywatch__hpp__included__
2 #define _library__memorywatch__hpp__included__
4 #include "mathexpr.hpp"
5 #include <functional>
6 #include <list>
7 #include <set>
8 #include <map>
10 class memory_space;
12 namespace memorywatch
14 /**
15 * Read memory operator.
17 struct memread_oper : public mathexpr::operinfo
19 /**
20 * Ctor
22 memread_oper();
23 /**
24 * Dtor
26 ~memread_oper();
27 /**
28 * Evaluate the operator.
30 * Note: The first promise is for the address.
32 void evaluate(mathexpr::value target, std::vector<std::function<mathexpr::value()>> promises);
33 //Fields.
34 unsigned bytes; //Number of bytes to read.
35 bool signed_flag; //Is signed?
36 bool float_flag; //Is float?
37 int endianess; //Endianess (-1 => little, 0 => host, 1 => Big).
38 uint64_t scale_div; //Scale divisor.
39 uint64_t addr_base; //Address base.
40 uint64_t addr_size; //Address size (0 => All).
41 memory_space* mspace; //Memory space to read.
44 /**
45 * Memory watch item printer.
47 struct item_printer : public GC::item
49 /**
50 * Dtor.
52 virtual ~item_printer();
53 /**
54 * Show the watched value.
56 virtual void show(const std::string& iname, const std::string& val) = 0;
57 /**
58 * Reset the printer.
60 virtual void reset() = 0;
61 protected:
62 void trace();
65 /**
66 * Memory watch item.
68 struct item
70 /**
71 * Ctor.
73 * Parameter t: The type of the result.
75 item(mathexpr::typeinfo& t)
76 : expr(GC::obj_tag(), &t)
79 /**
80 * Get the value as string.
82 std::string get_value();
83 /**
84 * Print the value to specified printer.
86 * Parameter iname: The name of the watch.
88 void show(const std::string& iname);
89 //Fields.
90 GC::pointer<item_printer> printer; //Printer to use.
91 GC::pointer<mathexpr::mathexpr> expr; //Expression to watch.
92 std::string format; //Formatting to use.
95 /**
96 * A set of memory watches.
98 struct set
101 * Dtor.
103 ~set();
105 * Call reset on all items and their printers in the set.
107 void reset();
109 * Call reset and then show on all items in the set.
111 void refresh();
113 * Get the longest name (by UTF-8 length) in the set.
115 * Returns: The longest nontrivial name, or "" if none.
117 const std::string& get_longest_name()
119 return get_longest_name(set::utflength_rate);
122 * Get the longest name (by arbitrary function) in the set.
124 * Parameter rate: Get length for name function.
125 * Returns: The longest nontrivial name, or "" if none.
127 const std::string& get_longest_name(std::function<size_t(const std::string& n)> rate);
129 * Get the set of memory watch names.
131 std::set<std::string> names_set();
133 * Get specified memory watch item.
135 * Parameter name: The name of the item.
136 * Returns: The item.
137 * Throws std::runtime_error: No such item in set.
139 item& get(const std::string& name);
141 * Get specified memory watch item (without throwing).
143 * Parameter name: The name of the item.
144 * Returns: The item, or NULL if no such item exists.
146 item* get_soft(const std::string& name);
148 * Create a new memory watch item.
150 * Parameter name: The name of the new item.
151 * Parameter item: The new item. All fields are shallow-copied.
153 item* create(const std::string& name, item& item);
155 * Destroy a memory watch item.
157 * Parameter name: The name of the item to destroy.
159 void destroy(const std::string& name);
161 * Call routine for all roots.
163 void foreach(std::function<void(item& item)> cb);
165 * Swap set with another.
167 void swap(set& s) throw();
168 private:
169 static size_t utflength_rate(const std::string& s);
170 std::map<std::string, item> roots;
174 #endif