Cocos2d-x 3.x Migration – std::string & nullptr

As I move our code base toward pure c++11 where it makes sense when changing or adding functionality. Once I moved from using cocos2d-x’s __String class to std::string and associated std::vector<std::string> in a couple places I figured it was a good candidate to migrate fully away from the __String class altogether.

This was mostly a simple find/replace exercise with various regular expressions, but one thing I wasn’t aware of is that the act of assigning nullptr to a std::string does not trigger a warning or error at compile time, but rather fails as expected at runtime. This was cause for more time spent to correctly handle all cases where the idea of no string or an empty string is necessary.

I would think there is a flag for each compiler to detect these at compile time, but I haven’t looked into it much since it wasn’t all that much effort to fix the code, but it did take much longer than if the compiler had flagged every location which it should be able to do.

I realize this is fairly specific to the migration process as well how the code was originally written, but still an unexpected annoyance in the process.

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