Cocos2d-x 3.x Migration – Visitor

This will no longer be necessary once std:: containers are used everywhere instead of the deprecated value classes. Until then, however, I’ll be using the following to pretty print out arrays and dictionaries.

CCDataVisitor.h

class CC_DLL Visitable
 {
 public:
 /** should call concrete visit method */
 virtual void acceptVisitor(DataVisitor &visitor) = 0;
 virtual ~Visitable() {}
 };

Also updated the PrettyPrinter class in CCDataVisitor.cpp

Changed the three visit methods:

// in visit(const __Array *p)
obj->acceptVisitor(); 

// in visit(const __Dictionary *p)
element->getObject()->acceptVisitor(); 

// in visit(const __Set *p)
(*it)->acceptVisitor(); 

Replaced with:

// in visit(const __Array *p)
Visitable* v = dynamic_cast<Visitable*>(obj);
if(v) v->acceptVisitor(v);

// in visit(const __Dictionary *p)
Visitable* v = dynamic_cast<Visitable*>(element->getObject()); 
if(v) v->acceptVisitor(v);

// in visit(const __Set *p)
Visitable* v = dynamic_cast<Visitable*>((*it)); 
if(v) v->acceptVisitor(v);

Added `public Visitable` to each of the following classes: __Bool, __Integer, __Float, __Double, __String, __Array, __Dictionary, __Set

Example:

class CC_DLL __Bool : public Ref, public Clonable, public Visitable

Leave a Reply

Your email address will not be published. Required fields are marked *

*