From 10d07302179b83de4d574c437ac0428c9ba18f85 Mon Sep 17 00:00:00 2001 From: Daniel Burrows Date: Fri, 22 May 2009 07:29:53 -0700 Subject: Add support for dumping incremental expression trees. Current values aren't dumped at the moment. --- .../problemresolver/incremental_expression.cc | 15 ++++++++ .../problemresolver/incremental_expression.h | 40 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) (limited to 'src') diff --git a/src/generic/problemresolver/incremental_expression.cc b/src/generic/problemresolver/incremental_expression.cc index 619d84ba..1a3d16a4 100644 --- a/src/generic/problemresolver/incremental_expression.cc +++ b/src/generic/problemresolver/incremental_expression.cc @@ -91,11 +91,21 @@ bool and_e::get_value() return get_num_true() == get_children().size(); } +std::string and_e::get_name() +{ + return "and"; +} + bool or_e::get_value() { return get_num_true() > 0; } +std::string or_e::get_name() +{ + return "or"; +} + void not_e::child_modified(const cwidget::util::ref_ptr > &child, bool old_value, bool new_value) @@ -107,3 +117,8 @@ bool not_e::get_value() { return !child->get_value(); } + +void not_e::dump(std::ostream &out) +{ + out << "~" << child; +} diff --git a/src/generic/problemresolver/incremental_expression.h b/src/generic/problemresolver/incremental_expression.h index c0bbf03a..0456b95e 100644 --- a/src/generic/problemresolver/incremental_expression.h +++ b/src/generic/problemresolver/incremental_expression.h @@ -29,6 +29,8 @@ #include #include +#include + // A system of incrementally computed expressions stored as a DAG. // NOT THREADSAFE (the weak-reference system would utterly break in // the presence of threads without a lot of expensive locking, and @@ -187,6 +189,7 @@ public: } virtual T get_value() = 0; + virtual void dump(std::ostream &out) = 0; }; /** \brief Base class for expressions that can contain other @@ -242,6 +245,22 @@ public: children.erase(new_end, children.end()); } + + virtual std::string get_name() = 0; + + virtual void dump(std::ostream &out) + { + out << get_name() << "("; + for(std::vector > >::const_iterator + it = get_children().begin(); it != get_children().end(); ++it) + { + if(it != get_children().begin()) + out << ", "; + + (*it)->dump(out); + } + out << ")"; + } }; template @@ -285,6 +304,9 @@ expression_weak_ref::~expression_weak_ref() * * Variables can be modified arbitrarily; changes are immediately * propagated to parent expressions. + * + * It would be nice if the user could attach names for better + * printing of expressions, but that would take a lot of memory. */ template class var_e : public expression @@ -318,6 +340,11 @@ public: signal_value_changed(old_value, new_value); } } + + void dump(std::ostream &out) + { + out << "v" << this; + } }; /** \brief Boolean-specific expressions. */ @@ -383,6 +410,8 @@ public: } bool get_value(); + std::string get_name(); + void dump(std::ostream &out); }; class or_e : public counting_bool_e @@ -413,6 +442,8 @@ public: } bool get_value(); + std::string get_name(); + void dump(std::ostream &out); }; class not_e : public expression_container @@ -435,8 +466,17 @@ public: bool old_value, bool new_value); bool get_value(); + void dump(std::ostream &out); }; +template +std::ostream &operator<<(std::ostream &out, + const cwidget::util::ref_ptr > &o) +{ + o->dump(out); + return out; +} + // @} #endif -- cgit v1.2.3