Last active
August 29, 2015 14:06
-
-
Save rubentorresbonet/51e5a99c41fadaaa58a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Applies a function to every object of the array which will receive the casted object. | |
* | |
* Usage: for_each<MyCustomObject*>(myArray, [] (MyCustomObject *obj) { ... }); | |
* | |
* @author ruben.torres | |
*/ | |
template<typename T> | |
void for_each(cocos2d::CCArray *arr, std::function<void(T)> function) | |
{ | |
cocos2d::CCObject *obj = nullptr; | |
CCARRAY_FOREACH(arr, obj) | |
{ | |
T castedObj = dynamic_cast<T>(obj); | |
if (castedObj) { | |
function(castedObj); | |
} | |
} | |
} | |
/** | |
* Applies a function to every object of the dictionary which will receive its (string) key and the casted object. | |
* | |
* Usage: for_each<MyCustomObject*>(myArray, [] (std::string key, MyCustomObject *obj) { ... }); | |
* | |
* @author ruben.torres | |
*/ | |
template<typename T> | |
void for_each(cocos2d::CCDictionary *dict, std::function<void(std::string, T)> function) | |
{ | |
cocos2d::CCDictElement *obj = nullptr, *pTmpDictObj = nullptr; | |
HASH_ITER(hh, dict->m_pElements,obj, pTmpDictObj) | |
{ | |
T castedObj = dynamic_cast<T>(obj->getObject()); | |
if (castedObj) { | |
function(obj->getStrKey(), castedObj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment