[thin_debug] Factor out reusable componments
This commit is contained in:
44
base/command_interpreter.cc
Normal file
44
base/command_interpreter.cc
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "base/command_interpreter.h"
|
||||
|
||||
using namespace dbg;
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
strings read_input(std::istream &in) {
|
||||
using namespace boost::algorithm;
|
||||
|
||||
std::string input;
|
||||
getline(in, input);
|
||||
|
||||
strings toks;
|
||||
split(toks, input, is_any_of(" \t"), token_compress_on);
|
||||
|
||||
return toks;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
void command_interpreter::do_once() {
|
||||
if (in_.eof())
|
||||
throw runtime_error("input closed");
|
||||
|
||||
out_ << "> ";
|
||||
strings args = read_input(in_);
|
||||
|
||||
std::map<std::string, command::ptr>::iterator it;
|
||||
it = commands_.find(args[0]);
|
||||
if (it == commands_.end())
|
||||
out_ << "Unrecognised command" << endl;
|
||||
else {
|
||||
try {
|
||||
it->second->exec(args, out_);
|
||||
} catch (std::exception &e) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
60
base/command_interpreter.h
Normal file
60
base/command_interpreter.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef DBG_COMMAND_INTERPRETER
|
||||
#define DBG_COMMAND_INTERPRETER
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace dbg {
|
||||
typedef std::vector<std::string> strings;
|
||||
|
||||
class command {
|
||||
public:
|
||||
typedef std::shared_ptr<command> ptr;
|
||||
|
||||
virtual ~command() {}
|
||||
virtual void exec(strings const &args, std::ostream &out) = 0;
|
||||
};
|
||||
|
||||
class command_interpreter {
|
||||
public:
|
||||
typedef std::shared_ptr<command_interpreter> ptr;
|
||||
|
||||
command_interpreter(std::istream &in, std::ostream &out)
|
||||
: in_(in),
|
||||
out_(out),
|
||||
exit_(false) {
|
||||
}
|
||||
|
||||
void register_command(std::string const &str, command::ptr cmd) {
|
||||
commands_.insert(make_pair(str, cmd));
|
||||
}
|
||||
|
||||
void enter_main_loop() {
|
||||
while (!exit_)
|
||||
do_once();
|
||||
}
|
||||
|
||||
void exit_main_loop() {
|
||||
exit_ = true;
|
||||
}
|
||||
|
||||
private:
|
||||
void do_once();
|
||||
|
||||
std::istream &in_;
|
||||
std::ostream &out_;
|
||||
std::map <std::string, command::ptr> commands_;
|
||||
bool exit_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
53
base/output_formatter.cc
Normal file
53
base/output_formatter.cc
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <string>
|
||||
|
||||
#include "base/output_formatter.h"
|
||||
|
||||
using namespace dbg;
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
void indent(int depth, std::ostream &out) {
|
||||
for (int i = 0; i < depth * 2; i++)
|
||||
out << ' ';
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
void xml_formatter::output(std::ostream &out,
|
||||
int depth,
|
||||
boost::optional<std::string> name) {
|
||||
indent(depth, out);
|
||||
out << "<fields";
|
||||
if (name && (*name).length())
|
||||
out << " id=\"" << *name << "\"";
|
||||
|
||||
/* output non-child fields */
|
||||
std::vector<field_type>::const_iterator it;
|
||||
for (it = fields_.begin(); it != fields_.end(); ++it) {
|
||||
if (string const *s = boost::get<string>(&it->get<1>())) {
|
||||
out << " " << it->get<0>() << "=\"" << *s << "\"";
|
||||
}
|
||||
}
|
||||
|
||||
if (children_.size() == 0) {
|
||||
out << " />" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
/* output child fields */
|
||||
out << ">" << endl;
|
||||
for (it = children_.begin(); it != children_.end(); ++it) {
|
||||
if (!boost::get<string>(&it->get<1>())) {
|
||||
formatter::ptr f = boost::get<formatter::ptr>(it->get<1>());
|
||||
f->output(out, depth + 1, it->get<0>());
|
||||
}
|
||||
}
|
||||
|
||||
indent(depth, out);
|
||||
out << "</fields>" << endl;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
59
base/output_formatter.h
Normal file
59
base/output_formatter.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef DBG_OUTPUT_FORMATTER_H
|
||||
#define DBG_OUTPUT_FORMATTER_H
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/tuple/tuple.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace dbg {
|
||||
class formatter {
|
||||
public:
|
||||
typedef std::shared_ptr<formatter> ptr;
|
||||
|
||||
virtual ~formatter() {}
|
||||
|
||||
typedef boost::optional<std::string> maybe_string;
|
||||
|
||||
void field(std::string const &name, std::string const &value) {
|
||||
fields_.push_back(field_type(name, value));
|
||||
}
|
||||
|
||||
void child(std::string const &name, formatter::ptr t) {
|
||||
children_.push_back(field_type(name, t));
|
||||
}
|
||||
|
||||
virtual void output(std::ostream &out, int depth = 0,
|
||||
boost::optional<std::string> name = boost::none) = 0;
|
||||
|
||||
protected:
|
||||
typedef boost::variant<std::string, ptr> value;
|
||||
typedef boost::tuple<std::string, value> field_type;
|
||||
|
||||
std::vector<field_type> fields_;
|
||||
std::vector<field_type> children_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
field(formatter &t, std::string const &name, T const &value) {
|
||||
t.field(name, boost::lexical_cast<std::string>(value));
|
||||
}
|
||||
|
||||
//--------------------------------
|
||||
|
||||
class xml_formatter : public formatter {
|
||||
public:
|
||||
virtual void output(std::ostream &out, int depth = 0,
|
||||
boost::optional<std::string> name = boost::none);
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user