Move memorywatch stuff to dedicated namespace

This commit is contained in:
Ilari Liusvaara 2014-05-14 10:50:49 +03:00
parent 6c9d34123d
commit 3a85ccc506
10 changed files with 121 additions and 151 deletions

View file

@ -27,7 +27,7 @@ struct memwatch_printer
/**
* Get a printer object corresponding to this object.
*/
gcroot_pointer<memorywatch_item_printer> get_printer_obj(
gcroot_pointer<memorywatch::item_printer> get_printer_obj(
std::function<gcroot_pointer<mathexpr>(const std::string& n)> vars);
//Fields.
enum position_category {
@ -71,7 +71,7 @@ struct memwatch_item
*
* If bytes == 0, returns NULL.
*/
memorywatch_memread_oper* get_memread_oper();
memorywatch::memread_oper* get_memread_oper();
/**
* Translate compatiblity item.
*/
@ -173,8 +173,8 @@ private:
std::map<std::string, std::u32string> window_vars;
std::map<std::string, bool> used_memorywatches;
void erase_unused_watches();
void memorywatch_output(const std::string& name, const std::string& value);
memorywatch_set watch_set;
void watch_output(const std::string& name, const std::string& value);
memorywatch::set watch_set;
};
#endif

View file

@ -10,12 +10,14 @@ namespace framebuffer
class font2;
}
struct memorywatch_output_fb : public memorywatch_item_printer
namespace memorywatch
{
memorywatch_output_fb();
~memorywatch_output_fb();
struct output_fb : public item_printer
{
output_fb();
~output_fb();
void set_rqueue(framebuffer::queue& rqueue);
void set_dtor_cb(std::function<void(memorywatch_output_fb&)> cb);
void set_dtor_cb(std::function<void(output_fb&)> cb);
void show(const std::string& iname, const std::string& val);
void reset();
bool cond_enable;
@ -32,7 +34,8 @@ struct memorywatch_output_fb : public memorywatch_item_printer
framebuffer::color halo;
//State variables.
framebuffer::queue* queue;
std::function<void(memorywatch_output_fb&)> dtor_cb;
std::function<void(output_fb&)> dtor_cb;
};
}
#endif

View file

@ -6,10 +6,12 @@
#include <string>
#include <functional>
struct memorywatch_output_list : public memorywatch_item_printer
namespace memorywatch
{
memorywatch_output_list();
~memorywatch_output_list();
struct output_list : public item_printer
{
output_list();
~output_list();
void set_output(std::function<void(const std::string& n, const std::string& v)> _fn);
void show(const std::string& iname, const std::string& val);
void reset();
@ -18,5 +20,6 @@ struct memorywatch_output_list : public memorywatch_item_printer
//State variables.
std::function<void(const std::string& n, const std::string& v)> fn;
};
}
#endif

View file

@ -5,12 +5,15 @@
#include <string>
#include <functional>
struct memorywatch_output_null : public memorywatch_item_printer
namespace memorywatch
{
memorywatch_output_null();
~memorywatch_output_null();
struct output_null : public item_printer
{
output_null();
~output_null();
void show(const std::string& iname, const std::string& val);
void reset();
};
}
#endif

View file

@ -8,19 +8,21 @@
class memory_space;
namespace memorywatch
{
/**
* Read memory operator.
*/
struct memorywatch_memread_oper : public mathexpr_operinfo
struct memread_oper : public mathexpr_operinfo
{
/**
* Ctor
*/
memorywatch_memread_oper();
memread_oper();
/**
* Dtor
*/
~memorywatch_memread_oper();
~memread_oper();
/**
* Evaluate the operator.
*
@ -41,12 +43,12 @@ struct memorywatch_memread_oper : public mathexpr_operinfo
/**
* Memory watch item printer.
*/
struct memorywatch_item_printer : public garbage_collectable
struct item_printer : public garbage_collectable
{
/**
* Dtor.
*/
virtual ~memorywatch_item_printer();
virtual ~item_printer();
/**
* Show the watched value.
*/
@ -62,14 +64,14 @@ protected:
/**
* Memory watch item.
*/
struct memorywatch_item
struct item
{
/**
* Ctor.
*
* Parameter t: The type of the result.
*/
memorywatch_item(mathexpr_typeinfo& t)
item(mathexpr_typeinfo& t)
: expr(gcroot_pointer_object_tag(), &t)
{
}
@ -84,7 +86,7 @@ struct memorywatch_item
*/
void show(const std::string& iname);
//Fields.
gcroot_pointer<memorywatch_item_printer> printer; //Printer to use.
gcroot_pointer<item_printer> printer; //Printer to use.
gcroot_pointer<mathexpr> expr; //Expression to watch.
std::string format; //Formatting to use.
};
@ -92,12 +94,12 @@ struct memorywatch_item
/**
* A set of memory watches.
*/
struct memorywatch_set
struct set
{
/**
* Dtor.
*/
~memorywatch_set();
~set();
/**
* Call reset on all items and their printers in the set.
*/
@ -113,7 +115,7 @@ struct memorywatch_set
*/
const std::string& get_longest_name()
{
return get_longest_name(memorywatch_set::utflength_rate);
return get_longest_name(set::utflength_rate);
}
/**
* Get the longest name (by arbitrary function) in the set.
@ -125,7 +127,7 @@ struct memorywatch_set
/**
* Get the set of memory watch names.
*/
std::set<std::string> set();
std::set<std::string> names_set();
/**
* Get specified memory watch item.
*
@ -133,21 +135,21 @@ struct memorywatch_set
* Returns: The item.
* Throws std::runtime_error: No such item in set.
*/
memorywatch_item& get(const std::string& name);
item& get(const std::string& name);
/**
* Get specified memory watch item (without throwing).
*
* Parameter name: The name of the item.
* Returns: The item, or NULL if no such item exists.
*/
memorywatch_item* get_soft(const std::string& name);
item* get_soft(const std::string& name);
/**
* Create a new memory watch item.
*
* Parameter name: The name of the new item.
* Parameter item: The new item. All fields are shallow-copied.
*/
memorywatch_item* create(const std::string& name, memorywatch_item& item);
item* create(const std::string& name, item& item);
/**
* Destroy a memory watch item.
*
@ -157,14 +159,15 @@ struct memorywatch_set
/**
* Call routine for all roots.
*/
void foreach(std::function<void(memorywatch_item& item)> cb);
void foreach(std::function<void(item& item)> cb);
/**
* Swap set with another.
*/
void swap(memorywatch_set& s) throw();
void swap(set& s) throw();
private:
static size_t utflength_rate(const std::string& s);
std::map<std::string, memorywatch_item> roots;
std::map<std::string, item> roots;
};
}
#endif

View file

@ -29,7 +29,6 @@
namespace
{
std::map<std::string, std::pair<framebuffer::font2*, size_t>> fonts_in_use;
std::map<std::string, std::u32string> memorywatch_vars;
framebuffer::font2& get_builtin_font2()
{
@ -155,22 +154,22 @@ void memwatch_printer::unserialize(const JSON::node& node)
onscreen_halo_color = json_signed_default(node, "onscreen_halo_color", false);
}
gcroot_pointer<memorywatch_item_printer> memwatch_printer::get_printer_obj(
gcroot_pointer<memorywatch::item_printer> memwatch_printer::get_printer_obj(
std::function<gcroot_pointer<mathexpr>(const std::string& n)> vars)
{
gcroot_pointer<memorywatch_item_printer> ptr;
memorywatch_output_list* l;
memorywatch_output_fb* f;
gcroot_pointer<memorywatch::item_printer> ptr;
memorywatch::output_list* l;
memorywatch::output_fb* f;
std::string _enabled = (enabled != "") ? enabled : "true";
switch(position) {
case PC_DISABLED:
ptr = gcroot_pointer<memorywatch_item_printer>(new memorywatch_output_null);
ptr = gcroot_pointer<memorywatch::item_printer>(new memorywatch::output_null);
break;
case PC_MEMORYWATCH:
ptr = gcroot_pointer<memorywatch_item_printer>(new memorywatch_output_list);
l = dynamic_cast<memorywatch_output_list*>(ptr.as_pointer());
ptr = gcroot_pointer<memorywatch::item_printer>(new memorywatch::output_list);
l = dynamic_cast<memorywatch::output_list*>(ptr.as_pointer());
l->cond_enable = cond_enable;
try {
if(l->cond_enable)
@ -183,10 +182,10 @@ gcroot_pointer<memorywatch_item_printer> memwatch_printer::get_printer_obj(
l->set_output(dummy_target_fn);
break;
case PC_ONSCREEN:
ptr = gcroot_pointer<memorywatch_item_printer>(new memorywatch_output_fb);
f = dynamic_cast<memorywatch_output_fb*>(ptr.as_pointer());
ptr = gcroot_pointer<memorywatch::item_printer>(new memorywatch::output_fb);
f = dynamic_cast<memorywatch::output_fb*>(ptr.as_pointer());
f->font = NULL;
f->set_dtor_cb([](memorywatch_output_fb& obj) { put_font(obj.font); });
f->set_dtor_cb([](memorywatch::output_fb& obj) { put_font(obj.font); });
f->cond_enable = cond_enable;
std::string while_parsing = "(unknown)";
try {
@ -265,11 +264,11 @@ void memwatch_item::unserialize(const JSON::node& node)
addr_size = json_unsigned_default(node, "addr_size", 0);
}
memorywatch_memread_oper* memwatch_item::get_memread_oper()
memorywatch::memread_oper* memwatch_item::get_memread_oper()
{
if(!bytes)
return NULL;
memorywatch_memread_oper* o = new memorywatch_memread_oper;
memorywatch::memread_oper* o = new memorywatch::memread_oper;
o->bytes = bytes;
o->signed_flag = signed_flag;
o->float_flag = float_flag;
@ -401,8 +400,8 @@ std::string memwatch_set::get_string(const std::string& name, JSON::printer* pri
void memwatch_set::watch(struct framebuffer::queue& rq)
{
//Set framebuffer for all FB watches.
watch_set.foreach([&rq](memorywatch_item& i) {
memorywatch_output_fb* fb = dynamic_cast<memorywatch_output_fb*>(i.printer.as_pointer());
watch_set.foreach([&rq](memorywatch::item& i) {
memorywatch::output_fb* fb = dynamic_cast<memorywatch::output_fb*>(i.printer.as_pointer());
if(fb)
fb->set_rqueue(rq);
});
@ -490,7 +489,7 @@ void memwatch_set::clear_multi(const std::set<std::string>& names)
void memwatch_set::rebuild(std::map<std::string, memwatch_item>& nitems)
{
{
memorywatch_set new_set;
memorywatch::set new_set;
std::map<std::string, gcroot_pointer<mathexpr>> vars;
auto vars_fn = [&vars](const std::string& n) -> gcroot_pointer<mathexpr> {
if(!vars.count(n))
@ -502,7 +501,7 @@ void memwatch_set::rebuild(std::map<std::string, memwatch_item>& nitems)
mathexpr_operinfo* memread_oper = i.second.get_memread_oper();
try {
gcroot_pointer<mathexpr> rt_expr;
gcroot_pointer<memorywatch_item_printer> rt_printer;
gcroot_pointer<memorywatch::item_printer> rt_printer;
std::vector<gcroot_pointer<mathexpr>> v;
try {
rt_expr = mathexpr::parse(*expression_value(), i.second.expr, vars_fn);
@ -519,13 +518,13 @@ void memwatch_set::rebuild(std::map<std::string, memwatch_item>& nitems)
rt_printer = i.second.printer.get_printer_obj(vars_fn);
//Set final callback for list objects (since it wasn't known on creation).
auto list_obj = dynamic_cast<memorywatch_output_list*>(rt_printer.as_pointer());
auto list_obj = dynamic_cast<memorywatch::output_list*>(rt_printer.as_pointer());
if(list_obj)
list_obj->set_output([this](const std::string& n, const std::string& v) {
this->memorywatch_output(n, v);
this->watch_output(n, v);
});
memorywatch_item it(*expression_value());
memorywatch::item it(*expression_value());
*vars_fn(i.first) = *rt_expr;
it.expr = vars_fn(i.first);
it.printer = rt_printer;
@ -541,7 +540,7 @@ void memwatch_set::rebuild(std::map<std::string, memwatch_item>& nitems)
garbage_collectable::do_gc();
}
void memwatch_set::memorywatch_output(const std::string& name, const std::string& value)
void memwatch_set::watch_output(const std::string& name, const std::string& value)
{
used_memorywatches[name] = true;
window_vars[name] = utf8::to32(value);

View file

@ -3,9 +3,11 @@
#include "utf8.hpp"
#include "minmax.hpp"
namespace memorywatch
{
namespace
{
struct memorywatch_fb_object : public framebuffer::object
struct fb_object : public framebuffer::object
{
struct params
{
@ -20,8 +22,8 @@ namespace
framebuffer::color bg;
framebuffer::color halo;
};
memorywatch_fb_object(const params& _p, const std::string& _msg);
~memorywatch_fb_object() throw();
fb_object(const params& _p, const std::string& _msg);
~fb_object() throw();
void operator()(struct framebuffer::fb<false>& scr) throw();
void operator()(struct framebuffer::fb<true>& scr) throw();
void clone(framebuffer::queue& q) const throw(std::bad_alloc);
@ -31,19 +33,19 @@ namespace
std::u32string msg;
};
memorywatch_fb_object::memorywatch_fb_object(const memorywatch_fb_object::params& _p, const std::string& _msg)
fb_object::fb_object(const fb_object::params& _p, const std::string& _msg)
: p(_p)
{
msg = utf8::to32(_msg);
}
memorywatch_fb_object::~memorywatch_fb_object() throw() {}
void memorywatch_fb_object::operator()(struct framebuffer::fb<false>& scr) throw() { draw(scr); }
void memorywatch_fb_object::operator()(struct framebuffer::fb<true>& scr) throw() { draw(scr); }
fb_object::~fb_object() throw() {}
void fb_object::operator()(struct framebuffer::fb<false>& scr) throw() { draw(scr); }
void fb_object::operator()(struct framebuffer::fb<true>& scr) throw() { draw(scr); }
void memorywatch_fb_object::clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
void fb_object::clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
template<bool ext> void memorywatch_fb_object::draw(struct framebuffer::fb<ext>& scr) throw()
template<bool ext> void fb_object::draw(struct framebuffer::fb<ext>& scr) throw()
{
p.x += scr.get_origin_x();
p.y += scr.get_origin_y();
@ -126,30 +128,30 @@ namespace
}
}
memorywatch_output_fb::memorywatch_output_fb()
output_fb::output_fb()
{
font = NULL;
}
memorywatch_output_fb::~memorywatch_output_fb()
output_fb::~output_fb()
{
if(dtor_cb)
dtor_cb(*this);
}
void memorywatch_output_fb::set_rqueue(framebuffer::queue& rqueue)
void output_fb::set_rqueue(framebuffer::queue& rqueue)
{
queue = &rqueue;
}
void memorywatch_output_fb::set_dtor_cb(std::function<void(memorywatch_output_fb&)> cb)
void output_fb::set_dtor_cb(std::function<void(output_fb&)> cb)
{
dtor_cb = cb;
}
void memorywatch_output_fb::show(const std::string& iname, const std::string& val)
void output_fb::show(const std::string& iname, const std::string& val)
{
memorywatch_fb_object::params p;
fb_object::params p;
try {
if(cond_enable) {
enabled->reset();
@ -171,14 +173,15 @@ void memorywatch_output_fb::show(const std::string& iname, const std::string& va
p.fg = fg;
p.bg = bg;
p.halo = halo;
queue->create_add<memorywatch_fb_object>(p, val);
queue->create_add<fb_object>(p, val);
} catch(...) {
}
}
void memorywatch_output_fb::reset()
void output_fb::reset()
{
enabled->reset();
pos_x->reset();
pos_y->reset();
}
}

View file

@ -1,19 +1,21 @@
#include "memorywatch-list.hpp"
memorywatch_output_list::memorywatch_output_list()
namespace memorywatch
{
output_list::output_list()
{
}
memorywatch_output_list::~memorywatch_output_list()
output_list::~output_list()
{
}
void memorywatch_output_list::set_output(std::function<void(const std::string& n, const std::string& v)> _fn)
void output_list::set_output(std::function<void(const std::string& n, const std::string& v)> _fn)
{
fn = _fn;
}
void memorywatch_output_list::show(const std::string& iname, const std::string& val)
void output_list::show(const std::string& iname, const std::string& val)
{
if(cond_enable) {
try {
@ -28,6 +30,7 @@ void memorywatch_output_list::show(const std::string& iname, const std::string&
fn(iname, val);
}
void memorywatch_output_list::reset()
void output_list::reset()
{
}
}

View file

@ -1,17 +1,20 @@
#include "memorywatch-null.hpp"
memorywatch_output_null::memorywatch_output_null()
namespace memorywatch
{
output_null::output_null()
{
}
memorywatch_output_null::~memorywatch_output_null()
output_null::~output_null()
{
}
void memorywatch_output_null::show(const std::string& iname, const std::string& val)
void output_null::show(const std::string& iname, const std::string& val)
{
}
void memorywatch_output_null::reset()
void output_null::reset()
{
}
}

View file

@ -6,6 +6,8 @@
#include <sstream>
#include "string.hpp"
namespace memorywatch
{
namespace
{
template<typename T> T* pointer_cast(char* ptr)
@ -14,14 +16,14 @@ namespace
}
}
memorywatch_memread_oper::memorywatch_memread_oper()
memread_oper::memread_oper()
: mathexpr_operinfo("(readmemory)")
{
}
memorywatch_memread_oper::~memorywatch_memread_oper() {}
memread_oper::~memread_oper() {}
void memorywatch_memread_oper::evaluate(mathexpr_value target, std::vector<std::function<mathexpr_value()>> promises)
void memread_oper::evaluate(mathexpr_value target, std::vector<std::function<mathexpr_value()>> promises)
{
if(promises.size() != 1)
throw mathexpr_error(mathexpr_error::ARGCOUNT, "Memory read operator takes 1 argument");
@ -130,15 +132,15 @@ namespace
bool uppercasehex;
*/
memorywatch_item_printer::~memorywatch_item_printer()
item_printer::~item_printer()
{
}
void memorywatch_item_printer::trace()
void item_printer::trace()
{
}
std::string memorywatch_item::get_value()
std::string item::get_value()
{
if(format == "") {
//Default.
@ -207,7 +209,7 @@ std::string memorywatch_item::get_value()
return out.str();
}
void memorywatch_item::show(const std::string& n)
void item::show(const std::string& n)
{
std::string x;
try {
@ -224,13 +226,13 @@ void memorywatch_item::show(const std::string& n)
}
memorywatch_set::~memorywatch_set()
set::~set()
{
roots.clear();
garbage_collectable::do_gc();
}
void memorywatch_set::reset()
void set::reset()
{
for(auto& i : roots) {
if(i.second.printer)
@ -239,7 +241,7 @@ void memorywatch_set::reset()
}
}
void memorywatch_set::refresh()
void set::refresh()
{
for(auto& i : roots)
i.second.expr->reset();
@ -247,7 +249,7 @@ void memorywatch_set::refresh()
i.second.show(i.first);
}
std::set<std::string> memorywatch_set::set()
std::set<std::string> set::names_set()
{
std::set<std::string> r;
for(auto i : roots)
@ -255,7 +257,7 @@ std::set<std::string> memorywatch_set::set()
return r;
}
memorywatch_item& memorywatch_set::get(const std::string& name)
item& set::get(const std::string& name)
{
auto i = get_soft(name);
if(!i)
@ -263,20 +265,20 @@ memorywatch_item& memorywatch_set::get(const std::string& name)
return *i;
}
memorywatch_item* memorywatch_set::get_soft(const std::string& name)
item* set::get_soft(const std::string& name)
{
if(!roots.count(name))
return NULL;
return &(roots.find(name)->second);
}
memorywatch_item* memorywatch_set::create(const std::string& name, memorywatch_item& item)
item* set::create(const std::string& name, item& item)
{
roots.insert(std::make_pair(name, item));
return &(roots.find(name)->second);
}
void memorywatch_set::destroy(const std::string& name)
void set::destroy(const std::string& name)
{
if(!roots.count(name))
return;
@ -284,7 +286,7 @@ void memorywatch_set::destroy(const std::string& name)
garbage_collectable::do_gc();
}
const std::string& memorywatch_set::get_longest_name(std::function<size_t(const std::string& n)> rate)
const std::string& set::get_longest_name(std::function<size_t(const std::string& n)> rate)
{
static std::string empty;
size_t best_len = 0;
@ -299,71 +301,19 @@ const std::string& memorywatch_set::get_longest_name(std::function<size_t(const
return *best;
}
size_t memorywatch_set::utflength_rate(const std::string& n)
size_t set::utflength_rate(const std::string& n)
{
return utf8::strlen(n);
}
void memorywatch_set::foreach(std::function<void(memorywatch_item& item)> cb)
void set::foreach(std::function<void(item& item)> cb)
{
for(auto& i : roots)
cb(i.second);
}
void memorywatch_set::swap(memorywatch_set& s) throw()
void set::swap(set& s) throw()
{
std::swap(roots, s.roots);
}
#ifdef TEST_MEMORYWATCH
#include "mathexpr-ntype.hpp"
struct stdout_item_printer : public memorywatch_item_printer
{
~stdout_item_printer()
{
}
void show(const std::string& n, const std::string& v)
{
std::cout << n << " --> " << v << std::endl;
}
void reset()
{
}
};
int main2(int argc, char** argv)
{
memorywatch_set mset;
gcroot_pointer<memorywatch_item_printer> printer(new stdout_item_printer);
std::function<gcroot_pointer<mathexpr>(const std::string&)> vars_fn = [&mset]
(const std::string& n) -> gcroot_pointer<mathexpr> {
auto p = mset.get_soft(n);
if(!p) {
memorywatch_item i(*expression_value());
p = mset.create(n, i);
}
return p->expr;
};
for(int i = 1; i < argc; i++) {
regex_results r = regex("([^=]+)=\\[(.*)\\](.*)", argv[i]);
if(!r)
throw std::runtime_error("Bad argument '" + std::string(argv[i]) + "'");
*vars_fn(r[1]) = *mathexpr::parse(*expression_value(), r[3], vars_fn);
mset.get(r[1]).format = r[2];
mset.get(r[1]).printer = printer;
}
garbage_collectable::do_gc();
garbage_collectable::do_gc();
mset.refresh();
return 0;
}
int main(int argc, char** argv)
{
int r = main2(argc, argv);
garbage_collectable::do_gc();
return r;
}
#endif