[dbg] Hide implementations of shared components
This commit is contained in:
@@ -6,7 +6,42 @@ using namespace std;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
strings read_input(std::istream &in) {
|
||||
class command_interpreter_impl : public command_interpreter {
|
||||
public:
|
||||
typedef std::shared_ptr<command_interpreter> ptr;
|
||||
|
||||
command_interpreter_impl(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_;
|
||||
};
|
||||
|
||||
//--------------------------------
|
||||
|
||||
strings read_input(std::istream &in)
|
||||
{
|
||||
using namespace boost::algorithm;
|
||||
|
||||
std::string input;
|
||||
@@ -21,7 +56,8 @@ namespace {
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
void command_interpreter::do_once() {
|
||||
void command_interpreter_impl::do_once()
|
||||
{
|
||||
if (in_.eof())
|
||||
throw runtime_error("input closed");
|
||||
|
||||
@@ -42,3 +78,11 @@ void command_interpreter::do_once() {
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
command_interpreter::ptr
|
||||
dbg::create_command_interpreter(std::istream &in, std::ostream &out)
|
||||
{
|
||||
return command_interpreter::ptr(new command_interpreter_impl(in, out));
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
@@ -26,33 +26,13 @@ namespace dbg {
|
||||
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_;
|
||||
virtual void register_command(std::string const &str, command::ptr cmd) = 0;
|
||||
virtual void enter_main_loop() = 0;
|
||||
virtual void exit_main_loop() = 0;
|
||||
};
|
||||
|
||||
command_interpreter::ptr
|
||||
create_command_interpreter(std::istream &in, std::ostream &out);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
@@ -8,6 +8,31 @@ using namespace std;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
class abstract_formatter : public 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));
|
||||
}
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
class xml_formatter : public abstract_formatter {
|
||||
public:
|
||||
void output(std::ostream &out, int depth = 0,
|
||||
boost::optional<std::string> name = boost::none);
|
||||
};
|
||||
|
||||
void indent(int depth, std::ostream &out) {
|
||||
for (int i = 0; i < depth * 2; i++)
|
||||
out << ' ';
|
||||
@@ -51,3 +76,11 @@ void xml_formatter::output(std::ostream &out,
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
formatter::ptr
|
||||
dbg::create_xml_formatter()
|
||||
{
|
||||
return formatter::ptr(new xml_formatter());
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
@@ -17,26 +17,10 @@ namespace dbg {
|
||||
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 field(std::string const &name, std::string const &value) = 0;
|
||||
virtual void child(std::string const &name, formatter::ptr t) = 0;
|
||||
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>
|
||||
@@ -45,13 +29,7 @@ namespace dbg {
|
||||
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);
|
||||
};
|
||||
formatter::ptr create_xml_formatter();
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user