Cocos2d-x 3.x Migration – C++11 Subtle Mistakes

I’ll probably repeat myself that c++11 makes developing in c++ much more pleasant. However, there are some interesting issues when migrating from various class structures to the new std:: template containers and algorithms.

std::remove removes all elements of a given type, not just the first one found. Normally I try and use the for (auto& element : arr ), but in these cases I use the full iterator syntax instead. Could also use an index based loop, or no doubt another approach which I haven’t learned.

// v is vector<string> may contain duplicate strings
for ( auto& el : v ) {
    // tried to use remove, but removes all found elements
    v.erase(std::remove(v.begin(), v.end(), el), v.end()); 

// instead use iterator pointer
for ( auto it = v.begin(); it != v.end(); it++ )
  std::string element = (*it);
  if(element == "string to match")
     // want to remove first string match
     v.erase(it); // iterator is invalid afterward

[I’ll continue to append new issues as they’re encountered]

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.


class CC_DLL Visitable
 /** 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)

// in visit(const __Dictionary *p)

// in visit(const __Set *p)

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


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

What I hope to see at WWDC 2014

These are some of the items I hope Apple announces at their big keynote for both the public and developers alike. I’m excited to sit in on some of the sessions this year, and I’m hoping that Apple is doubling down on the developer side of things for both iOS and OSX. The following lists some of the things that I hope to see at WWDC (or at least at the fall event).

  • Apple TV SDK and Store Platforms
  • Apple TV Controller Support
  • Macbook Air 12″ with Detachable Screen that can run iOS as an iPad :]
  • MacMini refresh
  • iCloud Storage Increase on all tiers
  • Improvements to iCloud and Apple’s cloud services
  • Inter-app communication similar to intents and contracts
  • Split Screen functionality for any device (smaller “HUD” app format, 2 at once)
  • AirDrop to and from both OSX and iOS
  • AirPlay to iOS
  • File Store so that any app can open a file from the global store w/permission
  • Improvements to Finder to better support tabbed browsing and power users
  • Apple’s Fitness Accessory and Health SDK
  • Apple Payment Service
  • Pressure sensitive stylus capabilities
  • Major improvements and additions to SpriteKit
  • Xcode C++ Refactoring Support to extract methods and other refactors
  • Support to access iCloud / GameCenter from any platform

Live Keynote Stream:

Thoughts and Predictions from others: