Merge pull request #171 from halx99/feature-170

Add utils::createInstance to instead macro CREATE_FUNC
This commit is contained in:
halx99 2020-08-24 23:55:37 -07:00 committed by GitHub
parent feb4e1783a
commit 0e0a28e033
4 changed files with 41 additions and 17 deletions

View File

@ -23,8 +23,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __SUPPORT_CC_UTILS_H__
#define __SUPPORT_CC_UTILS_H__
#ifndef __CC_UTILS_H__
#define __CC_UTILS_H__
#include <vector>
#include <string>
@ -153,8 +153,8 @@ namespace utils
* @return Returns found node or nullptr with specified type 'T'
*/
template<typename T> inline
T findChild(Node* levelRoot, const std::string& name)
template<typename T>
inline T findChild(Node* levelRoot, const std::string& name)
{
return dynamic_cast<T>(findChild(levelRoot, name));
}
@ -164,12 +164,46 @@ namespace utils
* @return Returns found node or nullptr with specified type 'T'
*/
template<typename T> inline
T findChild(Node* levelRoot, int tag)
template<typename T>
inline T findChild(Node* levelRoot, int tag)
{
return dynamic_cast<T>(findChild(levelRoot, tag));
}
/**
* Create a Game Object instance, like CREATE_FUNC, but more powerful
* @return Returns a autorelease game object
* @limition: the init function finit must be public
*/
template<typename T, typename F, typename...Ts>
inline T* createInstance(F&& finit, Ts&&... args)
{
T* pRet = new(std::nothrow) T();
if (pRet && std::mem_fn(finit)(pRet, std::forward<Ts>(args)...)) {
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = nullptr;
return nullptr;
}
}
/**
* Create a Game Object instance with 'bool T::init()' function, like CREATE_FUNC, but more powerful
* @return Returns a autorelease game object
* @limition: the init function finit must be public
*/
template<typename T>
inline T* createInstance()
{
return ::cocos2d::utils::createInstance<T>(&T::init);
}
/**
* Gets the md5 hash for the given file.
* @param filename The file to calculate md5 hash.

View File

@ -108,7 +108,7 @@ bool AppDelegate::applicationDidFinishLaunching() {
register_all_packages();
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
auto scene = utils::createInstance<HelloWorld>();
// run
director->runWithScene(scene);

View File

@ -26,11 +26,6 @@
USING_NS_CC;
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{

View File

@ -30,15 +30,10 @@
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__