Skip to content

Instantly share code, notes, and snippets.

@hone
Created October 12, 2009 02:39
Show Gist options
  • Save hone/208078 to your computer and use it in GitHub Desktop.
Save hone/208078 to your computer and use it in GitHub Desktop.
// cpp file
TheVisitor::TheVisitor()
{
depth = 0;
}
void TheVisitor::VisitElement( Element* thisele )
{
list<Attr*> attrcopy = thisele->Copyattrs();
list<Node*> nodecopy = thisele->Copynodes();
cout << "<" << thisele->getName();
// print attributes
for (list<Attr*>::iterator i = attrcopy.begin(); i != attrcopy.end(); i++)
{
(*i)->Accept(*this);
}
// print inner elements
if( nodecopy.empty() )
{
cout << " />" << endl;
}
else
{
cout << ">" << endl;
for ( list<Node*>::iterator n = nodecopy.begin(); n != nodecopy.end(); n++)
{
depth++;
(*n)->Accept(*this);
depth--;
}
printtab(depth);
cout << "</" << thisele->getName() << ">" << endl;
}
}
void TheVisitor::printtab()
for(int i = 0; i < depth; i++)
{
cout << "\t";
}
}
// in the header file
class TheVisitor : public Visitor
{
// other stuff here
private:
int depth;
void printtab();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment