summaryrefslogtreecommitdiff
path: root/src/nodedumper.cc
diff options
context:
space:
mode:
authorMarius Kintel <kintel@sim.no>2010-03-02 18:22:31 (GMT)
committerMarius Kintel <marius@kintel.net>2010-10-31 00:42:34 (GMT)
commit393c5a19fedfa4f97ca939fbcf52c2ccab1cde6a (patch)
treefbcb75d32e8763aac3f0ad28528936a0ec11930b /src/nodedumper.cc
parent746159d1838e895e80725cdc892f7bef85feb1af (diff)
Committed current version of visitor refactoring
Diffstat (limited to 'src/nodedumper.cc')
-rw-r--r--src/nodedumper.cc98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/nodedumper.cc b/src/nodedumper.cc
new file mode 100644
index 0000000..1956a89
--- /dev/null
+++ b/src/nodedumper.cc
@@ -0,0 +1,98 @@
+#include "nodedumper.h"
+#include <string>
+#include <map>
+#include <list>
+#include "visitor.h"
+#include "state.h"
+#include "nodecache.h"
+
+#include <sstream>
+#include <iostream>
+
+// For compatibility with old dump() output
+#define NODEDUMPER_COMPAT_MODE
+#ifdef NODEDUMPER_COMPAT_MODE
+#include "dxflinextrudenode.h"
+#include "dxfrotextrudenode.h"
+#include "projectionnode.h"
+#endif
+
+
+bool NodeDumper::isCached(const AbstractNode &node)
+{
+ return !this->cache[node].empty();
+}
+
+void NodeDumper::handleIndent(const State &state)
+{
+ if (state.isPrefix()) {
+ this->currindent += "\t";
+ }
+ else if (state.isPostfix()) {
+ this->currindent.erase((this->currindent.length() >= 1) ?
+ this->currindent.length() - 1 : 0);
+ }
+}
+
+string NodeDumper::dumpChildren(const AbstractNode &node)
+{
+ std::stringstream dump;
+ if (!this->visitedchildren[node.index()].empty()) {
+ dump << " {\n";
+
+ for (ChildList::const_iterator iter = this->visitedchildren[node.index()].begin();
+ iter != this->visitedchildren[node.index()].end();
+ iter++) {
+ dump << this->cache[**iter] << "\n";
+ }
+
+ dump << this->currindent << "}";
+ }
+ else {
+#ifndef NODEDUMPER_COMPAT_MODE
+ dump << ";";
+#else
+ if (dynamic_cast<const AbstractPolyNode*>(&node) &&
+ !dynamic_cast<const ProjectionNode*>(&node) &&
+ !dynamic_cast<const DxfRotateExtrudeNode*>(&node) &&
+ !dynamic_cast<const DxfLinearExtrudeNode*>(&node)) dump << ";";
+ else dump << " {\n" << this->currindent << "}";
+#endif
+ }
+ return dump.str();
+}
+
+
+Response NodeDumper::visit(const State &state, const AbstractNode &node)
+{
+ if (isCached(node)) return PruneTraversal;
+ else handleIndent(state);
+ if (state.isPostfix()) {
+ std::stringstream dump;
+ dump << this->currindent << node;
+ dump << dumpChildren(node);
+ this->cache.insert(node, dump.str());
+ }
+
+ handleVisitedChildren(state, node);
+ return ContinueTraversal;
+}
+
+const string &NodeDumper::getDump() const
+{
+ assert(this->root);
+ return this->cache[*this->root];
+}
+
+void NodeDumper::handleVisitedChildren(const State &state, const AbstractNode &node)
+{
+ if (state.isPostfix()) {
+ this->visitedchildren.erase(node.index());
+ if (!state.parent()) {
+ this->root = &node;
+ }
+ else {
+ this->visitedchildren[state.parent()->index()].push_back(&node);
+ }
+ }
+}
contact: Jan Huwald // Impressum