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
     break;
   }
}

[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.

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

WWDC 2014 – Finally!

I’m glad I was lucky enough to be able to attend WWDC 2014. It was a big announcement for developers building within the Apple ecosystem. Also for consumers though, even if by way of many smaller new products, services, and improvements. I was hoping to see some new hardware like many attendees, yet I am glad they are showing signs of better appreciating developers more than in the past few years.

It was fun to see all of the features that catch it up with Android, as well as the improved integrations and long awaited capabilities and APIs that some thought would never come to fruition.

It’s great to see them finally drop the pricing tiers for iCloud storage to be competitive with everyone else. I do wish they’d bump up their free tier, but someone needs to pay for their new massive data centers. All I ask for now is that they’ve improved their quality of service and I believe they already have since their major components seem pretty solid from iTunes to Push Notifications along with the Key-Value store.

The newly announced iCloud Drive is something I plan to test out right away, and I might consider using CloudKit for future developments. They mentioned that iCloud Drive and the new Photos were built on top of CloudKit and I’m excited to play around with this new service. The pricing structure is a little vague, and the service will require lock-in to the Apple ecosystem. I think for smaller apps or custom systems this isn’t a major issue, but it will definitely be a major decision point for any real business. While cross-platform support for a social service is necessary there should be plenty of opportunity to succeed only off the iOS/OSX market, for at least the next few years. Who knows, maybe Apple will open it up more in the future as they are supporting accessing iCloud Drive from the web on Windows.

WWDC is a conference that has often strayed from it’s core definition of being for developers and not the media. This year however they found their way back onto the path and are now sprinting along it in stride. While there was plenty of excitement for many of their announcements the one that received a brief stunned silence and was followed by a collective “What?! (did they seriously just announce that)”. Some of us in the developer community have been discussing for a while now that Apple needs to eventually move to an existing modern language or create their own, but I don’t think anyone expected them to actually announce it for another few years.

Swift is not exactly innovative from an academic perspective as it borrows most or all of its design from other languages, just as most languages before it. The fundamental purpose for creating it was stated in the keynote and that is a language that is “safe, fast, modern, and interactive”. I would also argue that one of the next slides is just as important and that is to have it interoperate and work seamlessly with objective-c (and thus c). It was designed to coexist and work with Cocoa and CocoaTouch so that their entire base of developers can transition easily into using the new language at whatever pace they want.

Learning new languages is something I enjoy, most recently with Go, and previously with the myriad of languages: VBScript, Perl, PHP, Scheme, Python, C#, F#, and of course Javascript. Objective-c does have some respectable design choices as with any other language, but I have only been learning and writing in it for the purpose of publishing into the App Store. Now with Swift I feel like I can use a language I know I’ll enjoy more than ObjC while also allowing me to publish into the store.

Apple is planning to iterate on the current spec through its 1.0 release and is not promising source backward compatibility until such release, as the same with any other language or platform. They will add other language or runtime features in the future where it makes sense, but I expect them to follow the same methodological process they use with everything they make.

I’m pleased with the direction they are going with respect to iOS and OSX both evolving to work well together yet maintain their individual purposes for existing. That said I can envision a time where developers write a majority of their code on an iPad or a device with a laptop form factor running iOS with support for external monitors. Until then I’m pleased that they currently don’t plan to radically change things. Versions shouldn’t matter that much, yet a major point release is usually the indication of more revolutionary changes than evolutionary ones. I believe they’ll move to OSX 11.0  only when they’re ready to do.

Those that I’m excited about include AirDrop between all devices, decreased iCloud storage pricing, and of course Swift.

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: https://www.apple.com/apple-events/june-2014/

Thoughts and Predictions from others: