Skip to content

Commit

Permalink
remove CppReflection, add CCBCustomClass and CCBCustomClassFactory to…
Browse files Browse the repository at this point in the history
… implement reflection, leave some jobs to class HelloCocosBuilder: public CCBCustomClass. The approach is not so tricky, much easier to understand now.
  • Loading branch information
walzer committed May 7, 2012
1 parent 1901fa6 commit 69a90b4
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 445 deletions.
@@ -1,6 +1,5 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2012 XiaoLong Zhang, Chukong Inc.
http://www.cocos2d-x.org
Expand All @@ -23,37 +22,57 @@
THE SOFTWARE.
****************************************************************************/

#include "CClassFactory.h"
#include "CObject.h"
#include "CGenDynamic.h"
#include "cocos2d.h"
using namespace cocos2d ;
#include "CCBCustomClass.h"

CClassFactory& CClassFactory::sharedClassFactory() {
static CClassFactory _shared;
return _shared;
USING_NS_CC_EXT;

static CCBCustomClassFactory g_FactoryInstance;

// CCBCustomClassFactory
CCBCustomClassFactory::CCBCustomClassFactory()
{
m_pCustomCreatorsMap = new CUSTOM_CLASS_MAP;
}

CClassFactory::CClassFactory()
CCBCustomClassFactory::~CCBCustomClassFactory()
{
CClassFactory::m_classMap = std::map<std::string, createClass>();
CC_SAFE_DELETE(m_pCustomCreatorsMap);
}

void* CClassFactory::getClassByName(std::string className)
CCBCustomClassFactory* CCBCustomClassFactory::sharedFactory()
{
std::map<std::string, createClass>::const_iterator iter ;

iter = m_classMap.find(className) ;

if ( iter == m_classMap.end() )
return NULL ;
// TBD: don't use static global variable, so ugly
return &g_FactoryInstance;
}

bool CCBCustomClassFactory::registCustomClass(const char* name, FUNC_CUSTON_CLASS_CREATOR pfnCreator)
{
bool bRetVal = false;

if (! (*m_pCustomCreatorsMap)[name] )
{
(*m_pCustomCreatorsMap)[name] = pfnCreator;
bRetVal = true;
}
else
return iter->second() ;
{
CCLOG("CCB: key = [%s] in m_pCustomCreatorsMap is already registed", name);
}

return bRetVal;
}

void CClassFactory::registClass(std::string name, createClass method)
CCBCustomClass* CCBCustomClassFactory::createCustomClassWithName(const char* name)
{
CCLog(name.c_str()) ;
m_classMap.insert(std::pair<std::string, createClass>(name, method)) ;
CCBCustomClass* pRetVal = NULL;
FUNC_CUSTON_CLASS_CREATOR pfnCreator = (*m_pCustomCreatorsMap)[name];

if (pfnCreator)
{
CCLOG("CCB: creating [%s] object", name);
pRetVal = pfnCreator();
}

return pRetVal;
}

@@ -1,6 +1,5 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2012 XiaoLong Zhang, Chukong Inc.
http://www.cocos2d-x.org
Expand All @@ -23,30 +22,50 @@
THE SOFTWARE.
****************************************************************************/

#ifndef cocos2dXReflection_CClassFactory_h
#define cocos2dXReflection_CClassFactory_h
#ifndef _CC_CUSTOM_CLASS_H_
#define _CC_CUSTOM_CLASS_H_

#include <string>
#include <map>
#include <iostream>
#include "cocos2d.h"
#include <map>

typedef void* (*createClass)(void);
NS_CC_EXT_BEGIN

class CCBCustomClass;
typedef CCBCustomClass* (*FUNC_CUSTON_CLASS_CREATOR)(void);

This comment has been minimized.

Copy link
@nicolasgramlich

nicolasgramlich May 21, 2012

Contributor

Typo:
...FUNC_CUSTON_CLASS_CREATOR...

Should be:
...FUNC_CUSTOM_CLASS_CREATOR...

This comment has been minimized.

Copy link
@walzer

walzer May 22, 2012

Author Contributor

Thanks for your code review!
CppReflection is removed in the edge version, coz the design and marcos are so tricky.
Now we should use CCBCustomClass.

typedef std::map<std::string, FUNC_CUSTON_CLASS_CREATOR> CUSTOM_CLASS_MAP;

class CClassFactory
// This is a simple reflection implement for custom classes in CocosBuilder
class CCBCustomClass : public cocos2d::CCNode
{
public:
CClassFactory() ;
public:
static CCBCustomClass* createInstance() { return NULL; }; // cannot create virual class here

CCBCustomClass() {};
virtual ~CCBCustomClass() {};

void* getClassByName(std::string className);
// implement 3 pure virtual methods inherited from CCBCustomClass
virtual bool callbackSetChildren(const char* name, cocos2d::CCObject* node) = 0;
virtual void callbackInvokeMethods(cocos2d::CCNode *sender) = 0;
virtual void callbackAfterCCBLoaded() = 0;
};

void registClass(std::string name, createClass method) ;


class CCBCustomClassFactory
{
public:
CCBCustomClassFactory();
virtual ~CCBCustomClassFactory();

static CClassFactory& sharedClassFactory() ;
static CCBCustomClassFactory* sharedFactory();

bool registCustomClass(const char* name, FUNC_CUSTON_CLASS_CREATOR pfnCreator);
CCBCustomClass* createCustomClassWithName(const char* name);

private:
std::map<std::string, createClass> m_classMap ;
} ;
protected:
CUSTOM_CLASS_MAP* m_pCustomCreatorsMap;
};

NS_CC_EXT_END;

#endif
#endif // _CC_CUSTOM_CLASS_H_

0 comments on commit 69a90b4

Please sign in to comment.