Skip to content

Instantly share code, notes, and snippets.

@Tsiannian
Last active January 4, 2016 11:19
Show Gist options
  • Save Tsiannian/8614638 to your computer and use it in GitHub Desktop.
Save Tsiannian/8614638 to your computer and use it in GitHub Desktop.
a template helper to create ccb class loader
template <typename T> class Template_creator
{
public:
static T* create(){
T* ptr = new T();
if(ptr != nullptr && ptr->init()){
ptr->autorelease();
return ptr;
}
CC_SAFE_DELETE(ptr);
return nullptr;
};
};
template<typename T>
class CCBLoaderTraits
{
public:
typedef T NodeType;
typedef typename cocos2d::extension::CCLayerLoader ParentLoader;
};
class CCBLoader;
template<typename NodeType, typename ParentLoader = typename CCBLoaderTraits<NodeType>::ParentLoader>
class TemplateLoader : public ParentLoader
{
public:
static TemplateLoader<NodeType>* loader() {
TemplateLoader<NodeType>* ptr = new TemplateLoader<NodeType>();
if(ptr != nullptr) {
ptr->autorelease();
return ptr;
}
CC_SAFE_DELETE(ptr);
return nullptr;
};
protected:
virtual NodeType* createCCNode(cocos2d::CCNode * pParent, cocos2d::extension::CCBReader* pCCBReader) {
return Template_creator<NodeType>::create();
}
};
#define REGISTER_CCB_LOADER(name) { \
node_loader_library->registerCCNodeLoader(#name, TemplateLoader<name>::loader()); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment