From 2cda34d59a8f9f1a3452a2361f3cc9b988f22b47 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 14:39:21 +0800 Subject: [PATCH 01/27] issue #2790: Deprecates CCDictionary, uses ValueMap for pure base data. Adds StringUtils class . --- cocos/2d/CCConfiguration.cpp | 164 +++++++++++------------------------ cocos/2d/CCConfiguration.h | 21 +---- cocos/2d/CCDirector.cpp | 22 ++--- cocos/2d/CCNode.h | 1 - cocos/2d/CCTileMapAtlas.cpp | 24 +++-- cocos/2d/CCTileMapAtlas.h | 5 +- cocos/base/CCDataVisitor.h | 8 +- cocos/base/CCDictionary.cpp | 74 ++++++++-------- cocos/base/CCDictionary.h | 23 ++--- cocos/base/CCString.h | 15 ++++ cocos/base/CCValue.cpp | 93 +++++++++++++++++++- cocos/base/CCValue.h | 5 +- 12 files changed, 244 insertions(+), 211 deletions(-) diff --git a/cocos/2d/CCConfiguration.cpp b/cocos/2d/CCConfiguration.cpp index 9aa44b5295..73f27e7270 100644 --- a/cocos/2d/CCConfiguration.cpp +++ b/cocos/2d/CCConfiguration.cpp @@ -54,34 +54,30 @@ Configuration::Configuration() , _maxSamplesAllowed(0) , _maxTextureUnits(0) , _glExtensions(nullptr) -, _valueDict(nullptr) { } bool Configuration::init() { - _valueDict = Dictionary::create(); - _valueDict->retain(); - - _valueDict->setObject(String::create( cocos2dVersion() ), "cocos2d.x.version"); + _valueDict["cocos2d.x.version"] = Value(cocos2dVersion()); #if CC_ENABLE_PROFILERS - _valueDict->setObject(Bool::create(true), "cocos2d.x.compiled_with_profiler"); + _valueDict["cocos2d.x.compiled_with_profiler"] = Value(true); #else - _valueDict->setObject(Bool::create(false), "cocos2d.x.compiled_with_profiler"); + _valueDict["cocos2d.x.compiled_with_profiler"] = Value(false); #endif #if CC_ENABLE_GL_STATE_CACHE == 0 - _valueDict->setObject(Bool::create(false), "cocos2d.x.compiled_with_gl_state_cache"); + _valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(false); #else - _valueDict->setObject(Bool::create(true), "cocos2d.x.compiled_with_gl_state_cache"); + _valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(true); #endif #ifdef DEBUG - _valueDict->setObject(String::create("DEBUG"), "cocos2d.x.build_type"); + _valueDict["cocos2d.x.build_type"] = Value("DEBUG"); #else - _valueDict->setObject(String::create("RELEASE"), "cocos2d.x.build_type"); + _valueDict["cocos2d.x.build_type"] = Value("RELEASE"); #endif return true; @@ -89,17 +85,13 @@ bool Configuration::init() Configuration::~Configuration() { - _valueDict->release(); } void Configuration::dumpInfo() const { // Dump - PrettyPrinter visitor(0); - _valueDict->acceptVisitor(visitor); - - CCLOG("%s", visitor.getResult().c_str()); - + Value forDump = Value(_valueDict); + log("%s", forDump.getDescription().c_str()); // And Dump some warnings as well #if CC_ENABLE_PROFILERS @@ -117,46 +109,46 @@ void Configuration::dumpInfo() const void Configuration::gatherGPUInfo() { - _valueDict->setObject(String::create((const char*)glGetString(GL_VENDOR)), "gl.vendor"); - _valueDict->setObject(String::create((const char*)glGetString(GL_RENDERER)), "gl.renderer"); - _valueDict->setObject(String::create((const char*)glGetString(GL_VERSION)), "gl.version"); + _valueDict["gl.vendor"] = Value((const char*)glGetString(GL_VENDOR)); + _valueDict["gl.renderer"] = Value((const char*)glGetString(GL_RENDERER)); + _valueDict["gl.version"] = Value((const char*)glGetString(GL_VERSION)); _glExtensions = (char *)glGetString(GL_EXTENSIONS); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize); - _valueDict->setObject(Integer::create((int)_maxTextureSize), "gl.max_texture_size"); + _valueDict["gl.max_texture_size"] = Value((int)_maxTextureSize); glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits); - _valueDict->setObject(Integer::create((int)_maxTextureUnits), "gl.max_texture_units"); + _valueDict["gl.max_texture_units"] = Value((int)_maxTextureUnits); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) glGetIntegerv(GL_MAX_SAMPLES_APPLE, &_maxSamplesAllowed); - _valueDict->setObject(Integer::create((int)_maxSamplesAllowed), "gl.max_samples_allowed"); + _valueDict["gl.max_samples_allowed"] = Value((int)_maxSamplesAllowed); #endif _supportsETC1 = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture"); - _valueDict->setObject(Bool::create(_supportsETC1), "gl.supports_ETC1"); + _valueDict["gl.supports_ETC1"] = Value(_supportsETC1); _supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc"); - _valueDict->setObject(Bool::create(_supportsS3TC), "gl.supports_S3TC"); + _valueDict["gl.supports_S3TC"] = Value(_supportsS3TC); _supportsATITC = checkForGLExtension("GL_AMD_compressed_ATC_texture"); - _valueDict->setObject(Bool::create(_supportsATITC), "gl.supports_ATITC"); + _valueDict["gl.supports_ATITC"] = Value(_supportsATITC); _supportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc"); - _valueDict->setObject(Bool::create(_supportsPVRTC), "gl.supports_PVRTC"); + _valueDict["gl.supports_PVRTC"] = Value(_supportsPVRTC); _supportsNPOT = true; - _valueDict->setObject(Bool::create(_supportsNPOT), "gl.supports_NPOT"); + _valueDict["gl.supports_NPOT"] = Value(_supportsNPOT); _supportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA888"); - _valueDict->setObject(Bool::create(_supportsBGRA8888), "gl.supports_BGRA8888"); + _valueDict["gl.supports_BGRA8888"] = Value(_supportsBGRA8888); _supportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer"); - _valueDict->setObject(Bool::create(_supportsDiscardFramebuffer), "gl.supports_discard_framebuffer"); + _valueDict["gl.supports_discard_framebuffer"] = Value(_supportsDiscardFramebuffer); _supportsShareableVAO = checkForGLExtension("vertex_array_object"); - _valueDict->setObject(Bool::create(_supportsShareableVAO), "gl.supports_vertex_array_object"); + _valueDict["gl.supports_vertex_array_object"] = Value(_supportsShareableVAO); CHECK_GL_ERROR_DEBUG(); } @@ -265,78 +257,27 @@ bool Configuration::supportsDiscardFramebuffer() const bool Configuration::supportsShareableVAO() const { - #if CC_TEXTURE_ATLAS_USE_VAO - return _supportsShareableVAO; - #else - return false; - #endif +#if CC_TEXTURE_ATLAS_USE_VAO + return _supportsShareableVAO; +#else + return false; +#endif } // // generic getters for properties // -const char *Configuration::getCString(const char *key, const char *defaultValue) const +const Value& Configuration::getValue(const std::string& key, const Value& defaultValue) const { - Object *ret = _valueDict->objectForKey(key); - if (ret) - { - if (String *str=dynamic_cast(ret)) - return str->getCString(); - - CCASSERT(false, "Key found, but from different type"); - } - - // XXX: Should it throw an exception ? + auto iter = _valueDict.find(key); + if (iter != _valueDict.end()) + return _valueDict.at(key); return defaultValue; } -/** returns the value of a given key as a boolean */ -bool Configuration::getBool(const char *key, bool defaultValue) const +void Configuration::setValue(const std::string& key, const Value& value) { - Object *ret = _valueDict->objectForKey(key); - if (ret) - { - if (Bool *boolobj=dynamic_cast(ret)) - return boolobj->getValue(); - if (String *strobj=dynamic_cast(ret)) - return strobj->boolValue(); - CCASSERT(false, "Key found, but from different type"); - } - - // XXX: Should it throw an exception ? - return defaultValue; -} - -/** returns the value of a given key as a double */ -double Configuration::getNumber( const char *key, double defaultValue ) const -{ - Object *ret = _valueDict->objectForKey(key); - if( ret ) - { - if (Double *obj=dynamic_cast(ret)) - return obj->getValue(); - - if (Integer *obj=dynamic_cast(ret)) - return obj->getValue(); - - if (String *strobj=dynamic_cast(ret)) - return strobj->doubleValue(); - - CCASSERT(false, "Key found, but from different type"); - } - - // XXX: Should it throw an exception ? - return defaultValue; -} - -Object * Configuration::getObject(const char *key) const -{ - return _valueDict->objectForKey(key); -} - -void Configuration::setObject(const char *key, Object *value) -{ - _valueDict->setObject(value, key); + _valueDict[key] = value; } @@ -345,20 +286,21 @@ void Configuration::setObject(const char *key, Object *value) // void Configuration::loadConfigFile(const char *filename) { - Dictionary *dict = Dictionary::createWithContentsOfFile(filename); - CCASSERT(dict, "cannot create dictionary"); + ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename); + CCASSERT(!dict.empty(), "cannot create dictionary"); // search for metadata bool validMetadata = false; - Object *metadata = dict->objectForKey("metadata"); - if (metadata && dynamic_cast(metadata)) + auto metadataIter = dict.find("metadata"); + if (metadataIter != dict.end() && metadataIter->second.getType() == Value::Type::MAP) { - Object *format_o = static_cast(metadata)->objectForKey("format"); - - // XXX: cocos2d-x returns Strings when importing from .plist. This bug will be addressed in cocos2d-x v3.x - if (format_o && dynamic_cast(format_o)) + + const auto& metadata = metadataIter->second.asValueMap(); + auto formatIter = metadata.find("format"); + + if (formatIter != metadata.end()) { - int format = static_cast(format_o)->intValue(); + int format = formatIter->second.asInt(); // Support format: 1 if (format == 1) @@ -374,22 +316,22 @@ void Configuration::loadConfigFile(const char *filename) return; } - Object *data = dict->objectForKey("data"); - if (!data || !dynamic_cast(data)) + auto dataIter = dict.find("data"); + if (dataIter == dict.end() || dataIter->second.getType() != Value::Type::MAP) { CCLOG("Expected 'data' dict, but not found. Config file: %s", filename); return; } // Add all keys in the existing dictionary - Dictionary *data_dict = static_cast(data); - DictElement* element; - CCDICT_FOREACH(data_dict, element) + + const auto& dataMap = dataIter->second.asValueMap(); + for (auto dataMapIter = dataMap.begin(); dataMapIter != dataMap.end(); ++dataMapIter) { - if(! _valueDict->objectForKey( element->getStrKey() )) - _valueDict->setObject(element->getObject(), element->getStrKey()); - else - CCLOG("Key already present. Ignoring '%s'", element->getStrKey()); + if (_valueDict.find(dataMapIter->first) == _valueDict.end()) + _valueDict[dataMapIter->first] = dataMapIter->second; + else + CCLOG("Key already present. Ignoring '%s'",dataMapIter->first.c_str()); } } diff --git a/cocos/2d/CCConfiguration.h b/cocos/2d/CCConfiguration.h index eb78275ef9..a1bebb24c7 100644 --- a/cocos/2d/CCConfiguration.h +++ b/cocos/2d/CCConfiguration.h @@ -29,10 +29,9 @@ THE SOFTWARE. #include "CCObject.h" #include "CCGL.h" #include "CCString.h" +#include "CCValue.h" #include - - NS_CC_BEGIN /** @@ -116,23 +115,11 @@ public: bool init(); - /** returns the value of a given key as a string. - If the key is not found, it will return the default value */ - const char* getCString(const char *key, const char *defaultValue = nullptr) const; - - /** returns the value of a given key as a boolean. - If the key is not found, it will return the default value */ - bool getBool(const char *key, bool defaultValue = false) const; - - /** returns the value of a given key as a double. - If the key is not found, it will return the default value */ - double getNumber(const char *key, double defaultValue = 0.0) const; - /** returns the value of a given key as a double */ - Object * getObject(const char *key) const; + const Value& getValue(const std::string& key, const Value& defaultValue) const; /** sets a new key/value pair in the configuration dictionary */ - void setObject(const char *key, Object *value); + void setValue(const std::string& key, const Value& value); /** dumps the current configuration on the console */ void dumpInfo() const; @@ -163,7 +150,7 @@ protected: GLint _maxTextureUnits; char * _glExtensions; - Dictionary *_valueDict; + ValueMap _valueDict; }; // end of global group diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index cd029d737f..93ed8b8932 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -184,34 +184,34 @@ void Director::setDefaultValues(void) Configuration *conf = Configuration::getInstance(); // default FPS - double fps = conf->getNumber("cocos2d.x.fps", kDefaultFPS); + double fps = conf->getValue("cocos2d.x.fps", Value(kDefaultFPS)).asDouble(); _oldAnimationInterval = _animationInterval = 1.0 / fps; // Display FPS - _displayStats = conf->getBool("cocos2d.x.display_fps", false); + _displayStats = conf->getValue("cocos2d.x.display_fps", Value(false)).asBool(); // GL projection - const char *projection = conf->getCString("cocos2d.x.gl.projection", "3d"); - if (strcmp(projection, "3d") == 0) + std::string projection = conf->getValue("cocos2d.x.gl.projection", Value("3d")).asString(); + if (projection == "3d") _projection = Projection::_3D; - else if (strcmp(projection, "2d") == 0) + else if (projection == "2d") _projection = Projection::_2D; - else if (strcmp(projection, "custom") == 0) + else if (projection == "custom") _projection = Projection::CUSTOM; else CCASSERT(false, "Invalid projection value"); // Default pixel format for PNG images with alpha - const char *pixel_format = conf->getCString("cocos2d.x.texture.pixel_format_for_png", "rgba8888"); - if (strcmp(pixel_format, "rgba8888") == 0) + std::string pixel_format = conf->getValue("cocos2d.x.texture.pixel_format_for_png", Value("rgba8888")).asString(); + if (pixel_format == "rgba8888") Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA8888); - else if(strcmp(pixel_format, "rgba4444") == 0) + else if(pixel_format == "rgba4444") Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444); - else if(strcmp(pixel_format, "rgba5551") == 0) + else if(pixel_format == "rgba5551") Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB5A1); // PVR v2 has alpha premultiplied ? - bool pvr_alpha_premultipled = conf->getBool("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", false); + bool pvr_alpha_premultipled = conf->getValue("cocos2d.x.texture.pvrv2_has_alpha_premultiplied", Value(false)).asBool(); Texture2D::PVRImagesHavePremultipliedAlpha(pvr_alpha_premultipled); } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index c9de3a916c..3916765e9b 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -52,7 +52,6 @@ class LabelProtocol; class Scheduler; class ActionManager; class Component; -class Dictionary; class ComponentContainer; class EventDispatcher; #ifdef CC_USE_PHYSICS diff --git a/cocos/2d/CCTileMapAtlas.cpp b/cocos/2d/CCTileMapAtlas.cpp index 8dc22d8244..d4a7ddd944 100644 --- a/cocos/2d/CCTileMapAtlas.cpp +++ b/cocos/2d/CCTileMapAtlas.cpp @@ -28,9 +28,10 @@ THE SOFTWARE. #include "CCTextureAtlas.h" #include "TGAlib.h" #include "ccConfig.h" -#include "CCDictionary.h" #include "CCInteger.h" #include "CCDirector.h" +#include "CCString.h" +#include NS_CC_BEGIN @@ -55,8 +56,6 @@ bool TileMapAtlas::initWithTileFile(const std::string& tile, const std::string& if( AtlasNode::initWithTileFile(tile, tileWidth, tileHeight, _itemsToRender) ) { - _posToAtlasIndex = new Dictionary(); - _posToAtlasIndex->init(); this->updateAtlasValues(); this->setContentSize(Size((float)(_TGAInfo->width*_itemWidth), (float)(_TGAInfo->height*_itemHeight))); @@ -78,7 +77,6 @@ TileMapAtlas::~TileMapAtlas() { tgaDestroy(_TGAInfo); } - CC_SAFE_RELEASE(_posToAtlasIndex); } void TileMapAtlas::releaseMap() @@ -88,8 +86,6 @@ void TileMapAtlas::releaseMap() tgaDestroy(_TGAInfo); } _TGAInfo = NULL; - - CC_SAFE_RELEASE_NULL(_posToAtlasIndex); } void TileMapAtlas::calculateItemsToRender() @@ -133,7 +129,6 @@ void TileMapAtlas::loadTGAfile(const std::string& file) void TileMapAtlas::setTile(const Color3B& tile, const Point& position) { CCASSERT(_TGAInfo != NULL, "tgaInfo must not be nil"); - CCASSERT(_posToAtlasIndex != NULL, "posToAtlasIndex must not be nil"); CCASSERT(position.x < _TGAInfo->width, "Invalid position.x"); CCASSERT(position.y < _TGAInfo->height, "Invalid position.x"); CCASSERT(tile.r != 0, "R component must be non 0"); @@ -150,10 +145,10 @@ void TileMapAtlas::setTile(const Color3B& tile, const Point& position) // XXX: this method consumes a lot of memory // XXX: a tree of something like that shall be implemented - Integer *num = (Integer*)_posToAtlasIndex->objectForKey(String::createWithFormat("%ld,%ld", - (long)position.x, - (long)position.y)->getCString()); - this->updateAtlasValueAt(position, tile, num->getValue()); + std::string key = StringUtils::toString(position.x) + "," + StringUtils::toString(position.y); + int num = _posToAtlasIndex[key].asInt(); + + this->updateAtlasValueAt(position, tile, num); } } @@ -252,10 +247,11 @@ void TileMapAtlas::updateAtlasValues() { this->updateAtlasValueAt(Point(x,y), value, total); - String *key = String::createWithFormat("%d,%d", x,y); - Integer *num = Integer::create(total); - _posToAtlasIndex->setObject(num, key->getCString()); + std::string key = StringUtils::toString(x) + "," + StringUtils::toString(y); + _posToAtlasIndex[key] = total; + StringUtils::toString(String::create("hello")); + total++; } } diff --git a/cocos/2d/CCTileMapAtlas.h b/cocos/2d/CCTileMapAtlas.h index e9899366d4..169be3fc88 100644 --- a/cocos/2d/CCTileMapAtlas.h +++ b/cocos/2d/CCTileMapAtlas.h @@ -26,13 +26,12 @@ THE SOFTWARE. #ifndef __CCTILE_MAP_ATLAS__ #define __CCTILE_MAP_ATLAS__ - #include "CCAtlasNode.h" +#include "CCValue.h" NS_CC_BEGIN struct sImageTGA; -class Dictionary; /** * @addtogroup tilemap_parallax_nodes @@ -98,7 +97,7 @@ protected: //! x,y to atlas dictionary - Dictionary* _posToAtlasIndex; + ValueMap _posToAtlasIndex; //! numbers of tiles to render int _itemsToRender; /** TileMap info */ diff --git a/cocos/base/CCDataVisitor.h b/cocos/base/CCDataVisitor.h index 9abb1f1424..7824e30c95 100644 --- a/cocos/base/CCDataVisitor.h +++ b/cocos/base/CCDataVisitor.h @@ -37,7 +37,7 @@ class Float; class Double; class String; class __Array; -class Dictionary; +class __Dictionary; class __Set; class Data; @@ -52,7 +52,7 @@ class Data; * Use cases: * - data serialization, * - pretty printing of Object * - * - safe value reading from Array, Dictionary, Set + * - safe value reading from Array, __Dictionary, Set * * Usage: * 1. subclass DataVisitor @@ -78,7 +78,7 @@ public: virtual void visit(const Double *p); virtual void visit(const String *p); virtual void visit(const __Array *p); - virtual void visit(const Dictionary *p); + virtual void visit(const __Dictionary *p); virtual void visit(const __Set *p); virtual void visit(const Data *p); }; @@ -99,7 +99,7 @@ public: virtual void visit(const Double *p); virtual void visit(const String *p); virtual void visit(const __Array *p); - virtual void visit(const Dictionary *p); + virtual void visit(const __Dictionary *p); virtual void visit(const __Set *p); virtual void visit(const Data *p); private: diff --git a/cocos/base/CCDictionary.cpp b/cocos/base/CCDictionary.cpp index ceb0333823..626b24a702 100644 --- a/cocos/base/CCDictionary.cpp +++ b/cocos/base/CCDictionary.cpp @@ -68,27 +68,27 @@ DictElement::~DictElement() } // ----------------------------------------------------------------------- -// Dictionary +// __Dictionary -Dictionary::Dictionary() +__Dictionary::__Dictionary() : _elements(NULL) , _dictType(kDictUnknown) { } -Dictionary::~Dictionary() +__Dictionary::~__Dictionary() { - CCLOGINFO("deallocing Dictionary: %p", this); + CCLOGINFO("deallocing __Dictionary: %p", this); removeAllObjects(); } -unsigned int Dictionary::count() +unsigned int __Dictionary::count() { return HASH_COUNT(_elements); } -__Array* Dictionary::allKeys() +__Array* __Dictionary::allKeys() { int iKeyCount = this->count(); if (iKeyCount <= 0) return NULL; @@ -118,7 +118,7 @@ __Array* Dictionary::allKeys() return array; } -__Array* Dictionary::allKeysForObject(Object* object) +__Array* __Dictionary::allKeysForObject(Object* object) { int iKeyCount = this->count(); if (iKeyCount <= 0) return NULL; @@ -153,12 +153,12 @@ __Array* Dictionary::allKeysForObject(Object* object) return array; } -Object* Dictionary::objectForKey(const std::string& key) +Object* __Dictionary::objectForKey(const std::string& key) { // if dictionary wasn't initialized, return NULL directly. if (_dictType == kDictUnknown) return NULL; - // Dictionary only supports one kind of key, string or integer. - // This method uses string as key, therefore we should make sure that the key type of this Dictionary is string. + // __Dictionary only supports one kind of key, string or integer. + // This method uses string as key, therefore we should make sure that the key type of this __Dictionary is string. CCASSERT(_dictType == kDictStr, "this dictionary does not use string as key."); Object* pRetObject = NULL; @@ -171,12 +171,12 @@ Object* Dictionary::objectForKey(const std::string& key) return pRetObject; } -Object* Dictionary::objectForKey(intptr_t key) +Object* __Dictionary::objectForKey(intptr_t key) { // if dictionary wasn't initialized, return NULL directly. if (_dictType == kDictUnknown) return NULL; - // Dictionary only supports one kind of key, string or integer. - // This method uses integer as key, therefore we should make sure that the key type of this Dictionary is integer. + // __Dictionary only supports one kind of key, string or integer. + // This method uses integer as key, therefore we should make sure that the key type of this __Dictionary is integer. CCASSERT(_dictType == kDictInt, "this dictionary does not use integer as key."); Object* pRetObject = NULL; @@ -189,7 +189,7 @@ Object* Dictionary::objectForKey(intptr_t key) return pRetObject; } -const String* Dictionary::valueForKey(const std::string& key) +const String* __Dictionary::valueForKey(const std::string& key) { String* pStr = dynamic_cast(objectForKey(key)); if (pStr == NULL) @@ -199,7 +199,7 @@ const String* Dictionary::valueForKey(const std::string& key) return pStr; } -const String* Dictionary::valueForKey(intptr_t key) +const String* __Dictionary::valueForKey(intptr_t key) { String* pStr = dynamic_cast(objectForKey(key)); if (pStr == NULL) @@ -209,7 +209,7 @@ const String* Dictionary::valueForKey(intptr_t key) return pStr; } -void Dictionary::setObject(Object* pObject, const std::string& key) +void __Dictionary::setObject(Object* pObject, const std::string& key) { CCASSERT(key.length() > 0 && pObject != NULL, "Invalid Argument!"); if (_dictType == kDictUnknown) @@ -235,7 +235,7 @@ void Dictionary::setObject(Object* pObject, const std::string& key) } } -void Dictionary::setObject(Object* pObject, intptr_t key) +void __Dictionary::setObject(Object* pObject, intptr_t key) { CCASSERT(pObject != NULL, "Invalid Argument!"); if (_dictType == kDictUnknown) @@ -262,7 +262,7 @@ void Dictionary::setObject(Object* pObject, intptr_t key) } -void Dictionary::removeObjectForKey(const std::string& key) +void __Dictionary::removeObjectForKey(const std::string& key) { if (_dictType == kDictUnknown) { @@ -276,7 +276,7 @@ void Dictionary::removeObjectForKey(const std::string& key) removeObjectForElememt(pElement); } -void Dictionary::removeObjectForKey(intptr_t key) +void __Dictionary::removeObjectForKey(intptr_t key) { if (_dictType == kDictUnknown) { @@ -289,21 +289,21 @@ void Dictionary::removeObjectForKey(intptr_t key) removeObjectForElememt(pElement); } -void Dictionary::setObjectUnSafe(Object* pObject, const std::string& key) +void __Dictionary::setObjectUnSafe(Object* pObject, const std::string& key) { pObject->retain(); DictElement* pElement = new DictElement(key.c_str(), pObject); HASH_ADD_STR(_elements, _strKey, pElement); } -void Dictionary::setObjectUnSafe(Object* pObject, const intptr_t key) +void __Dictionary::setObjectUnSafe(Object* pObject, const intptr_t key) { pObject->retain(); DictElement* pElement = new DictElement(key, pObject); HASH_ADD_PTR(_elements, _intKey, pElement); } -void Dictionary::removeObjectsForKeys(__Array* pKey__Array) +void __Dictionary::removeObjectsForKeys(__Array* pKey__Array) { Object* pObj = NULL; CCARRAY_FOREACH(pKey__Array, pObj) @@ -313,7 +313,7 @@ void Dictionary::removeObjectsForKeys(__Array* pKey__Array) } } -void Dictionary::removeObjectForElememt(DictElement* pElement) +void __Dictionary::removeObjectForElememt(DictElement* pElement) { if (pElement != NULL) { @@ -323,7 +323,7 @@ void Dictionary::removeObjectForElememt(DictElement* pElement) } } -void Dictionary::removeAllObjects() +void __Dictionary::removeAllObjects() { DictElement *pElement, *tmp; HASH_ITER(hh, _elements, pElement, tmp) @@ -335,7 +335,7 @@ void Dictionary::removeAllObjects() } } -Object* Dictionary::randomObject() +Object* __Dictionary::randomObject() { if (_dictType == kDictUnknown) { @@ -358,9 +358,9 @@ Object* Dictionary::randomObject() } } -Dictionary* Dictionary::create() +__Dictionary* __Dictionary::create() { - Dictionary* ret = new Dictionary(); + __Dictionary* ret = new __Dictionary(); if (ret && ret->init() ) { ret->autorelease(); @@ -368,21 +368,21 @@ Dictionary* Dictionary::create() return ret; } -bool Dictionary::init() +bool __Dictionary::init() { return true; } -Dictionary* Dictionary::createWithDictionary(Dictionary* srcDict) +__Dictionary* __Dictionary::createWithDictionary(__Dictionary* srcDict) { return srcDict->clone(); } static __Array* visitArray(const ValueVector& array); -static Dictionary* visitDict(const ValueMap& dict) +static __Dictionary* visitDict(const ValueMap& dict) { - Dictionary* ret = new Dictionary(); + __Dictionary* ret = new __Dictionary(); ret->init(); for (auto iter = dict.begin(); iter != dict.end(); ++iter) @@ -442,17 +442,17 @@ static __Array* visitArray(const ValueVector& array) return ret; } -Dictionary* Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName) +__Dictionary* __Dictionary::createWithContentsOfFileThreadSafe(const char *pFileName) { return visitDict(FileUtils::getInstance()->getValueMapFromFile(pFileName)); } -void Dictionary::acceptVisitor(DataVisitor &visitor) +void __Dictionary::acceptVisitor(DataVisitor &visitor) { return visitor.visit(this); } -Dictionary* Dictionary::createWithContentsOfFile(const char *pFileName) +__Dictionary* __Dictionary::createWithContentsOfFile(const char *pFileName) { auto ret = createWithContentsOfFileThreadSafe(pFileName); if (ret != nullptr) @@ -462,7 +462,7 @@ Dictionary* Dictionary::createWithContentsOfFile(const char *pFileName) return ret; } -bool Dictionary::writeToFile(const char *fullPath) +bool __Dictionary::writeToFile(const char *fullPath) { ValueMap dict; DictElement* element = nullptr; @@ -474,9 +474,9 @@ bool Dictionary::writeToFile(const char *fullPath) return FileUtils::getInstance()->writeToFile(dict, fullPath); } -Dictionary* Dictionary::clone() const +__Dictionary* __Dictionary::clone() const { - Dictionary* newDict = Dictionary::create(); + __Dictionary* newDict = __Dictionary::create(); DictElement* element = NULL; Object* tmpObj = NULL; diff --git a/cocos/base/CCDictionary.h b/cocos/base/CCDictionary.h index 323241a19c..65bf856237 100644 --- a/cocos/base/CCDictionary.h +++ b/cocos/base/CCDictionary.h @@ -32,7 +32,7 @@ THE SOFTWARE. NS_CC_BEGIN -class Dictionary; +class __Dictionary; /** * @addtogroup data_structures @@ -131,7 +131,7 @@ private: Object* _object; // hash value public: UT_hash_handle hh; // makes this class hashable - friend class Dictionary; // declare Dictionary as friend class + friend class __Dictionary; // declare Dictionary as friend class }; /** The macro for traversing dictionary @@ -171,7 +171,7 @@ public: * */ -class CC_DLL Dictionary : public Object, public Clonable +class CC_DLL __Dictionary : public Object, public Clonable { public: /** @@ -179,14 +179,14 @@ public: * @js NA * @lua NA */ - Dictionary(); + __Dictionary(); /** * The destructor of Dictionary * @js NA * @lua NA */ - ~Dictionary(); + ~__Dictionary(); /** Initializes the dictionary. It returns true if the initializations was successful. * @js NA @@ -364,7 +364,7 @@ public: * @see createWithDictionary(Dictionary*), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*). * @js NA */ - static Dictionary* create(); + static __Dictionary* create(); /** * Create a dictionary with an existing dictionary. @@ -374,7 +374,7 @@ public: * @see create(), createWithContentsOfFile(const char*), createWithContentsOfFileThreadSafe(const char*). * @js NA */ - static Dictionary* createWithDictionary(Dictionary* srcDict); + static __Dictionary* createWithDictionary(__Dictionary* srcDict); /** * Create a dictionary with a plist file. @@ -383,7 +383,7 @@ public: * @see create(), createWithDictionary(Dictionary*), createWithContentsOfFileThreadSafe(const char*). * @js NA */ - static Dictionary* createWithContentsOfFile(const char *pFileName); + static __Dictionary* createWithContentsOfFile(const char *pFileName); /** * Write a dictionary to a plist file. @@ -407,7 +407,7 @@ public: * @js NA * @lua NA */ - static Dictionary* createWithContentsOfFileThreadSafe(const char *pFileName); + static __Dictionary* createWithContentsOfFileThreadSafe(const char *pFileName); /* override functions * @js NA @@ -418,7 +418,7 @@ public: * @js NA * @lua NA */ - virtual Dictionary* clone() const; + virtual __Dictionary* clone() const; private: /** @@ -453,6 +453,9 @@ private: // end of data_structure group /// @} +typedef __Dictionary Dictionary; + + NS_CC_END #endif /* __CCDICTIONARY_H__ */ diff --git a/cocos/base/CCString.h b/cocos/base/CCString.h index 19af0302e2..2906f9acef 100644 --- a/cocos/base/CCString.h +++ b/cocos/base/CCString.h @@ -31,6 +31,8 @@ THE SOFTWARE. #include #include #include +#include + #include "CCObject.h" NS_CC_BEGIN @@ -202,6 +204,19 @@ struct StringCompare : public std::binary_function { #define StringMake(str) String::create(str) #define ccs StringMake +class StringUtils +{ +public: + + template + static std::string toString(T arg) + { + std::stringstream ss; + ss << arg; + return ss.str(); + } +}; + // end of data_structure group /// @} diff --git a/cocos/base/CCValue.cpp b/cocos/base/CCValue.cpp index de5e8a3bb5..207f82260c 100644 --- a/cocos/base/CCValue.cpp +++ b/cocos/base/CCValue.cpp @@ -580,7 +580,7 @@ std::string Value::asString() const ret << _baseData.doubleVal; break; case Type::BOOLEAN: - ret << _baseData.boolVal; + ret << (_baseData.boolVal ? "true" : "false"); break; default: break; @@ -588,6 +588,97 @@ std::string Value::asString() const return ret.str(); } +static std::string getTabs(int depth) +{ + std::string tabWidth; + + for (int i = 0; i < depth; ++i) + { + tabWidth += "\t"; + } + + return tabWidth; +} + +static std::string visit(const Value& v, int depth); + +static std::string visitVector(const ValueVector& v, int depth) +{ + std::stringstream ret; + + if (depth > 0) + ret << "\n"; + + ret << getTabs(depth) << "[\n"; + + int i = 0; + for (const auto& child : v) + { + ret << getTabs(depth+1) << i << ": " << visit(child, depth + 1); + ++i; + } + + ret << getTabs(depth) << "]\n"; + + return ret.str(); +} + +static std::string visitMap(const ValueMap& v, int depth) +{ + std::stringstream ret; + + if (depth > 0) + ret << "\n"; + + ret << getTabs(depth) << "{\n"; + + for (auto iter = v.begin(); iter != v.end(); ++iter) + { + ret << getTabs(depth + 1) << iter->first << ": "; + ret << visit(iter->second, depth + 1); + } + + ret << getTabs(depth) << "}\n"; + + return ret.str(); +} + +static std::string visit(const Value& v, int depth) +{ + std::stringstream ret; + + switch (v.getType()) + { + case Value::Type::BYTE: + case Value::Type::INTEGER: + case Value::Type::FLOAT: + case Value::Type::DOUBLE: + case Value::Type::BOOLEAN: + case Value::Type::STRING: + ret << v.asString() << "\n"; + break; + case Value::Type::VECTOR: + ret << visitVector(v.asValueVector(), depth); + break; + case Value::Type::MAP: + ret << visitMap(v.asValueMap(), depth); + break; + case Value::Type::INT_KEY_MAP: + break; + default: + break; + } + + return ret.str(); +} + +std::string Value::getDescription() +{ + std::string ret("\n"); + ret += visit(*this, 0); + return ret; +} + void Value::clear() { _type = Type::NONE; diff --git a/cocos/base/CCValue.h b/cocos/base/CCValue.h index 8328c2b3a7..c6c05979e0 100644 --- a/cocos/base/CCValue.h +++ b/cocos/base/CCValue.h @@ -119,10 +119,11 @@ public: inline Type getType() const { return _type; }; -protected: - void clear(); + std::string getDescription(); private: + void clear(); + union { unsigned char byteVal; From 022df4c0144edc4eb222e26f6f4a9031d8fd28df Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 14:41:55 +0800 Subject: [PATCH 02/27] issue #2790: Removes test codes. --- cocos/2d/CCTileMapAtlas.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/cocos/2d/CCTileMapAtlas.cpp b/cocos/2d/CCTileMapAtlas.cpp index d4a7ddd944..001a2aca6d 100644 --- a/cocos/2d/CCTileMapAtlas.cpp +++ b/cocos/2d/CCTileMapAtlas.cpp @@ -250,8 +250,6 @@ void TileMapAtlas::updateAtlasValues() std::string key = StringUtils::toString(x) + "," + StringUtils::toString(y); _posToAtlasIndex[key] = total; - StringUtils::toString(String::create("hello")); - total++; } } From 1a1cef9342bcaed4c16988b3af22399f68fca794 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 14:59:18 +0800 Subject: [PATCH 03/27] issue #2790: Addes Value::Null variable. --- cocos/base/CCValue.cpp | 2 ++ cocos/base/CCValue.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/cocos/base/CCValue.cpp b/cocos/base/CCValue.cpp index 207f82260c..f6507c5a51 100644 --- a/cocos/base/CCValue.cpp +++ b/cocos/base/CCValue.cpp @@ -27,6 +27,8 @@ NS_CC_BEGIN +const Value Value::Null; + Value::Value() : _vectorData(new ValueVector()) , _mapData(new ValueMap()) diff --git a/cocos/base/CCValue.h b/cocos/base/CCValue.h index c6c05979e0..189c9236e0 100644 --- a/cocos/base/CCValue.h +++ b/cocos/base/CCValue.h @@ -42,6 +42,8 @@ typedef std::unordered_map IntValueMap; class Value { public: + static const Value Null; + Value(); explicit Value(unsigned char v); explicit Value(int v); From 40bc797b1ff54075d98bc1dd34b76be4909f3205 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 14:59:46 +0800 Subject: [PATCH 04/27] issue #2790: Configuration::getValue has default value now. --- cocos/2d/CCConfiguration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCConfiguration.h b/cocos/2d/CCConfiguration.h index a1bebb24c7..f0296080ec 100644 --- a/cocos/2d/CCConfiguration.h +++ b/cocos/2d/CCConfiguration.h @@ -116,7 +116,7 @@ public: bool init(); /** returns the value of a given key as a double */ - const Value& getValue(const std::string& key, const Value& defaultValue) const; + const Value& getValue(const std::string& key, const Value& defaultValue = Value::Null) const; /** sets a new key/value pair in the configuration dictionary */ void setValue(const std::string& key, const Value& value); From 624340140493ab92ee606e591500dc576c1e4ac5 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 15:08:00 +0800 Subject: [PATCH 05/27] issue #2790: ConfigurationTest fix. --- .../ConfigurationTest/ConfigurationTest.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp index 7df99cf60e..feea02ad1d 100644 --- a/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp @@ -121,13 +121,13 @@ void ConfigurationQuery::onEnter() { ConfigurationBase::onEnter(); - CCLOG("cocos2d version: %s", Configuration::getInstance()->getCString("cocos2d.version") ); - CCLOG("OpenGL version: %s", Configuration::getInstance()->getCString("gl.version") ); + CCLOG("cocos2d version: %s", Configuration::getInstance()->getValue("cocos2d.x.version").asString().c_str() ); + CCLOG("OpenGL version: %s", Configuration::getInstance()->getValue("gl.version").asString().c_str() ); } std::string ConfigurationQuery::subtitle() { - return "Using getCString(). Check the console"; + return "Check the console"; } //------------------------------------------------------------------ @@ -156,19 +156,19 @@ void ConfigurationDefault::onEnter() { ConfigurationBase::onEnter(); - const char *c_value = Configuration::getInstance()->getCString("invalid.key", "no key"); - if( strcmp(c_value, "no key") != 0 ) + std::string c_value = Configuration::getInstance()->getValue("invalid.key", Value("no key")).asString(); + if( c_value != "no key" ) CCLOG("1. Test failed!"); else CCLOG("1. Test OK!"); - bool b_value = Configuration::getInstance()->getBool("invalid.key", true); + bool b_value = Configuration::getInstance()->getValue("invalid.key", Value(true)).asBool(); if( ! b_value ) CCLOG("2. Test failed!"); else CCLOG("2. Test OK!"); - double d_value = Configuration::getInstance()->getNumber("invalid.key", 42.42); + double d_value = Configuration::getInstance()->getValue("invalid.key", Value(42.42)).asDouble(); if( d_value != 42.42 ) CCLOG("3. Test failed!"); else @@ -192,9 +192,9 @@ void ConfigurationSet::onEnter() Configuration *conf = Configuration::getInstance(); - conf->setObject("this.is.an.int.value", Integer::create(10) ); - conf->setObject("this.is.a.bool.value", Bool::create(true) ); - conf->setObject("this.is.a.string.value", String::create("hello world") ); + conf->setValue("this.is.an.int.value", Value(10) ); + conf->setValue("this.is.a.bool.value", Value(true) ); + conf->setValue("this.is.a.string.value", Value("hello world") ); conf->dumpInfo(); } From 49cc9719a6f643f1b27a14f48545415fdfa30dd3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 15:08:23 +0800 Subject: [PATCH 06/27] =?UTF-8?q?issue=20#2790:=20log=20=E2=80=94>=20CCLOG?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/2d/CCConfiguration.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCConfiguration.cpp b/cocos/2d/CCConfiguration.cpp index 73f27e7270..ea3e64d362 100644 --- a/cocos/2d/CCConfiguration.cpp +++ b/cocos/2d/CCConfiguration.cpp @@ -91,7 +91,7 @@ void Configuration::dumpInfo() const { // Dump Value forDump = Value(_valueDict); - log("%s", forDump.getDescription().c_str()); + CCLOG("%s", forDump.getDescription().c_str()); // And Dump some warnings as well #if CC_ENABLE_PROFILERS From be9bb6998b004fa5a1fb1cb85e184d57d8342e35 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 17:15:27 +0800 Subject: [PATCH 07/27] issue #2790: Deprecates Dictionary, Array, String, Integer, Bool, Float, Double classes. Also renames Map::remove to Map::erase. --- cocos/2d/CCAction.cpp | 4 +- cocos/2d/CCAction.h | 2 +- cocos/2d/CCAnimationCache.cpp | 2 +- cocos/2d/CCComponentContainer.cpp | 63 +++++++++++------------- cocos/2d/CCComponentContainer.h | 4 +- cocos/2d/CCDeprecated.h | 24 +++++++--- cocos/2d/CCLabelAtlas.cpp | 17 +++---- cocos/2d/CCLabelBMFont.cpp | 25 +++++----- cocos/2d/CCLabelBMFont.h | 2 +- cocos/2d/CCLabelTTF.cpp | 4 +- cocos/2d/CCLabelTTF.h | 2 +- cocos/2d/CCNode.cpp | 6 ++- cocos/2d/CCNode.h | 2 +- cocos/2d/CCNotificationCenter.cpp | 6 +-- cocos/2d/CCNotificationCenter.h | 2 +- cocos/2d/CCProfiling.cpp | 20 ++++---- cocos/2d/CCProfiling.h | 4 +- cocos/2d/CCSpriteFrameCache.cpp | 10 ++-- cocos/base/CCArray.cpp | 4 +- cocos/base/CCArray.h | 3 -- cocos/base/CCBool.h | 12 ++--- cocos/base/CCConsole.cpp | 2 +- cocos/base/CCDataVisitor.cpp | 24 +++++----- cocos/base/CCDataVisitor.h | 30 ++++++------ cocos/base/CCDictionary.cpp | 32 ++++++------- cocos/base/CCDictionary.h | 33 ++++++------- cocos/base/CCDouble.h | 12 ++--- cocos/base/CCFloat.h | 12 ++--- cocos/base/CCInteger.h | 16 +++---- cocos/base/CCMap.h | 80 +++++++++++++++++++++---------- cocos/base/CCString.cpp | 74 ++++++++++++++-------------- cocos/base/CCString.h | 65 +++++++++++++++++-------- 32 files changed, 326 insertions(+), 272 deletions(-) diff --git a/cocos/2d/CCAction.cpp b/cocos/2d/CCAction.cpp index 5e1a080c4e..79dcbe4f18 100644 --- a/cocos/2d/CCAction.cpp +++ b/cocos/2d/CCAction.cpp @@ -47,9 +47,9 @@ Action::~Action() CCLOGINFO("deallocing Action: %p - tag: %i", this, _tag); } -const char* Action::description() const +std::string Action::description() const { - return String::createWithFormat("", _tag)->getCString(); + return StringUtils::toStringWithFormat("(_components->objectForKey(pName)); + pRet = _components->at(pName); } while (0); return pRet; @@ -60,18 +60,17 @@ bool ComponentContainer::add(Component *pCom) CCASSERT(pCom->getOwner() == NULL, "Component already added. It can't be added again"); do { - if (_components == NULL) + if (_components == nullptr) { - _components = Dictionary::create(); - _components->retain(); + _components = new Map(); _owner->scheduleUpdate(); } - Component *pComponent = dynamic_cast(_components->objectForKey(pCom->getName())); + Component *pComponent = _components->at(pCom->getName()); CCASSERT(pComponent == NULL, "Component already added. It can't be added again"); CC_BREAK_IF(pComponent); pCom->setOwner(_owner); - _components->setObject(pCom, pCom->getName()); + _components->insert(pCom->getName(), pCom); pCom->onEnter(); bRet = true; } while(0); @@ -85,20 +84,16 @@ bool ComponentContainer::remove(const char *pName) do { CC_BREAK_IF(!_components); - Object* pRetObject = NULL; - DictElement *pElement = NULL; - HASH_FIND_PTR(_components->_elements, pName, pElement); - if (pElement != NULL) - { - pRetObject = pElement->getObject(); - } - Component *com = dynamic_cast(pRetObject); - CC_BREAK_IF(!com); + + auto iter = _components->find(pName); + CC_BREAK_IF(iter == _components->end()); + + auto com = iter->second; com->onExit(); com->setOwner(NULL); - HASH_DEL(_components->_elements, pElement); - pElement->getObject()->release(); - CC_SAFE_DELETE(pElement); + + _components->erase(iter); + bRet = true; } while(0); return bRet; @@ -106,42 +101,40 @@ bool ComponentContainer::remove(const char *pName) void ComponentContainer::removeAll() { - if (_components != NULL) + if (_components != nullptr) { - DictElement *pElement, *tmp; - HASH_ITER(hh, _components->_elements, pElement, tmp) + for (auto iter = _components->begin(); iter != _components->end(); ++iter) { - HASH_DEL(_components->_elements, pElement); - ((Component*)pElement->getObject())->onExit(); - ((Component*)pElement->getObject())->setOwner(NULL); - pElement->getObject()->release(); - CC_SAFE_DELETE(pElement); + iter->second->onExit(); + iter->second->setOwner(NULL); } + + _components->clear(); + CC_SAFE_DELETE(_components); + _owner->unscheduleUpdate(); } } void ComponentContainer::alloc(void) { - _components = Dictionary::create(); - _components->retain(); + _components = new Map(); } void ComponentContainer::visit(float fDelta) { - if (_components != NULL) + if (_components != nullptr) { - DictElement *pElement, *tmp; - HASH_ITER(hh, _components->_elements, pElement, tmp) + for (auto iter = _components->begin(); iter != _components->end(); ++iter) { - ((Component*)pElement->getObject())->update(fDelta); + iter->second->update(fDelta); } } } bool ComponentContainer::isEmpty() const { - return (bool)(!(_components && _components->count())); + return (_components == nullptr || _components->empty()); } NS_CC_END diff --git a/cocos/2d/CCComponentContainer.h b/cocos/2d/CCComponentContainer.h index 57622a7d1b..894fcc635a 100644 --- a/cocos/2d/CCComponentContainer.h +++ b/cocos/2d/CCComponentContainer.h @@ -25,7 +25,7 @@ THE SOFTWARE. #ifndef __CC_FRAMEWORK_COMCONTAINER_H__ #define __CC_FRAMEWORK_COMCONTAINER_H__ -#include "CCDictionary.h" +#include "CCMap.h" NS_CC_BEGIN @@ -58,7 +58,7 @@ private: void alloc(void); private: - Dictionary *_components; ///< Dictionary of components + Map* _components; Node *_owner; friend class Node; diff --git a/cocos/2d/CCDeprecated.h b/cocos/2d/CCDeprecated.h index bae29435be..b8a3dd87a8 100644 --- a/cocos/2d/CCDeprecated.h +++ b/cocos/2d/CCDeprecated.h @@ -534,13 +534,6 @@ CC_DEPRECATED_ATTRIBUTE static inline AffineTransform CCAffineTransformIdentity( // CC prefix compatibility CC_DEPRECATED_ATTRIBUTE typedef Object CCObject; CC_DEPRECATED_ATTRIBUTE typedef Event CCEvent; -CC_DEPRECATED_ATTRIBUTE typedef Integer CCInteger; -CC_DEPRECATED_ATTRIBUTE typedef String CCString; -CC_DEPRECATED_ATTRIBUTE typedef Bool CCBool; -CC_DEPRECATED_ATTRIBUTE typedef Float CCFloat; -CC_DEPRECATED_ATTRIBUTE typedef Double CCDouble; -CC_DEPRECATED_ATTRIBUTE typedef Data CCData; -CC_DEPRECATED_ATTRIBUTE typedef Dictionary CCDictionary; CC_DEPRECATED_ATTRIBUTE typedef DataVisitor CCDataVisitor; CC_DEPRECATED_ATTRIBUTE typedef PrettyPrinter CCPrettyPrinter; CC_DEPRECATED_ATTRIBUTE typedef Acceleration CCAcceleration; @@ -1024,11 +1017,28 @@ CC_DEPRECATED_ATTRIBUTE inline void CC_DLL ccGLBindVAO(GLuint vaoId) { GL::bindV CC_DEPRECATED_ATTRIBUTE inline void CC_DLL ccGLEnable( int flags ) { /* ignore */ }; CC_DEPRECATED_ATTRIBUTE typedef int ccGLServerState; +CC_DEPRECATED_ATTRIBUTE typedef Data CCData; CC_DEPRECATED_ATTRIBUTE typedef __Set CCSet; CC_DEPRECATED_ATTRIBUTE typedef __SetIterator CCSetIterator; CC_DEPRECATED_ATTRIBUTE typedef __Set Set; CC_DEPRECATED_ATTRIBUTE typedef __SetIterator SetIterator; +CC_DEPRECATED_ATTRIBUTE typedef __Array CCArray; +CC_DEPRECATED_ATTRIBUTE typedef __Array Array; + +CC_DEPRECATED_ATTRIBUTE typedef __Dictionary Dictionary; +CC_DEPRECATED_ATTRIBUTE typedef __Dictionary CCDictionary; + +CC_DEPRECATED_ATTRIBUTE typedef __Double Double; +CC_DEPRECATED_ATTRIBUTE typedef __Double CCDouble; +CC_DEPRECATED_ATTRIBUTE typedef __Float Float; +CC_DEPRECATED_ATTRIBUTE typedef __Float CCFloat; +CC_DEPRECATED_ATTRIBUTE typedef __Integer Integer; +CC_DEPRECATED_ATTRIBUTE typedef __Integer CCInteger; +CC_DEPRECATED_ATTRIBUTE typedef __Bool Bool; +CC_DEPRECATED_ATTRIBUTE typedef __Bool CCBool; +CC_DEPRECATED_ATTRIBUTE typedef __String CCString; +//CC_DEPRECATED_ATTRIBUTE typedef __String String; NS_CC_END diff --git a/cocos/2d/CCLabelAtlas.cpp b/cocos/2d/CCLabelAtlas.cpp index 8ec15a3c1c..7230eae21a 100644 --- a/cocos/2d/CCLabelAtlas.cpp +++ b/cocos/2d/CCLabelAtlas.cpp @@ -93,18 +93,19 @@ bool LabelAtlas::initWithString(const std::string& theString, const std::string& { std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile); std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/"; - Dictionary *dict = Dictionary::createWithContentsOfFile(pathStr.c_str()); + + ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr.c_str()); - CCASSERT(((String*)dict->objectForKey("version"))->intValue() == 1, "Unsupported version. Upgrade cocos2d version"); + CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version"); - std::string texturePathStr = relPathStr + ((String*)dict->objectForKey("textureFilename"))->getCString(); - String *textureFilename = String::create(texturePathStr); - unsigned int width = ((String*)dict->objectForKey("itemWidth"))->intValue() / CC_CONTENT_SCALE_FACTOR(); - unsigned int height = ((String*)dict->objectForKey("itemHeight"))->intValue() / CC_CONTENT_SCALE_FACTOR(); - unsigned int startChar = ((String*)dict->objectForKey("firstChar"))->intValue(); + std::string textureFilename = relPathStr + dict["textureFilename"].asString(); + + unsigned int width = dict["itemWidth"].asInt() / CC_CONTENT_SCALE_FACTOR(); + unsigned int height = dict["itemHeight"].asInt() / CC_CONTENT_SCALE_FACTOR(); + unsigned int startChar = dict["firstChar"].asInt(); - this->initWithString(theString, textureFilename->getCString(), width, height, startChar); + this->initWithString(theString, textureFilename.c_str(), width, height, startChar); return true; } diff --git a/cocos/2d/CCLabelBMFont.cpp b/cocos/2d/CCLabelBMFont.cpp index 05e85eb95b..cba6abe0c3 100644 --- a/cocos/2d/CCLabelBMFont.cpp +++ b/cocos/2d/CCLabelBMFont.cpp @@ -40,6 +40,7 @@ http://www.angelcode.com/products/bmfont/ (Free, Windows only) #include "CCDirector.h" #include "CCTextureCache.h" #include "ccUTF8.h" +#include "CCMap.h" using namespace std; @@ -60,25 +61,24 @@ static unsigned short* copyUTF16StringN(unsigned short* str) // //FNTConfig Cache - free functions // -static Dictionary* s_pConfigurations = NULL; +static Map* s_configurations = nullptr; CCBMFontConfiguration* FNTConfigLoadFile(const std::string& fntFile) { CCBMFontConfiguration* ret = NULL; - if( s_pConfigurations == NULL ) + if( s_configurations == nullptr ) { - s_pConfigurations = new Dictionary(); - s_pConfigurations->init(); + s_configurations = new Map(); } - ret = static_cast( s_pConfigurations->objectForKey(fntFile) ); + ret = s_configurations->at(fntFile); if( ret == NULL ) { ret = CCBMFontConfiguration::create(fntFile.c_str()); if (ret) { - s_pConfigurations->setObject(ret, fntFile); + s_configurations->insert(fntFile, ret); } } @@ -87,10 +87,10 @@ CCBMFontConfiguration* FNTConfigLoadFile(const std::string& fntFile) void FNTConfigRemoveCache( void ) { - if (s_pConfigurations) + if (s_configurations) { - s_pConfigurations->removeAllObjects(); - CC_SAFE_RELEASE_NULL(s_pConfigurations); + s_configurations->clear(); + CC_SAFE_DELETE(s_configurations); } } @@ -148,15 +148,15 @@ CCBMFontConfiguration::~CCBMFontConfiguration() CC_SAFE_DELETE(_characterSet); } -const char* CCBMFontConfiguration::description(void) const +std::string CCBMFontConfiguration::description(void) const { - return String::createWithFormat( + return StringUtils::toStringWithFormat( "", (size_t)this, HASH_COUNT(_fontDefDictionary), HASH_COUNT(_kerningDictionary), _atlasName.c_str() - )->getCString(); + ); } void CCBMFontConfiguration::purgeKerningDictionary() @@ -183,6 +183,7 @@ void CCBMFontConfiguration::purgeFontDefDictionary() std::set* CCBMFontConfiguration::parseConfigFile(const std::string& controlFile) { std::string fullpath = FileUtils::getInstance()->fullPathForFilename(controlFile); + String *contents = String::createWithContentsOfFile(fullpath.c_str()); CCASSERT(contents, "CCBMFontConfiguration::parseConfigFile | Open file error."); diff --git a/cocos/2d/CCLabelBMFont.h b/cocos/2d/CCLabelBMFont.h index 41bd2c46bc..9bda1aeaa8 100644 --- a/cocos/2d/CCLabelBMFont.h +++ b/cocos/2d/CCLabelBMFont.h @@ -137,7 +137,7 @@ public: * @js NA * @lua NA */ - const char * description() const; + std::string description() const; /** allocates a CCBMFontConfiguration with a FNT file */ static CCBMFontConfiguration * create(const std::string& FNTfile); diff --git a/cocos/2d/CCLabelTTF.cpp b/cocos/2d/CCLabelTTF.cpp index 179f079e50..edcae3152d 100644 --- a/cocos/2d/CCLabelTTF.cpp +++ b/cocos/2d/CCLabelTTF.cpp @@ -185,9 +185,9 @@ const std::string& LabelTTF::getString() const return _string; } -const char* LabelTTF::description() const +std::string LabelTTF::description() const { - return String::createWithFormat("", _fontName.c_str(), _fontSize)->getCString(); + return StringUtils::toStringWithFormat("", _fontName.c_str(), _fontSize); } TextHAlignment LabelTTF::getHorizontalAlignment() const diff --git a/cocos/2d/CCLabelTTF.h b/cocos/2d/CCLabelTTF.h index 8c5689e3d0..224b099c80 100644 --- a/cocos/2d/CCLabelTTF.h +++ b/cocos/2d/CCLabelTTF.h @@ -70,7 +70,7 @@ public: * @js NA * @lua NA */ - const char* description() const; + std::string description() const; /** creates a LabelTTF with a font name and font size in points @since v2.0.1 diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 3a31d7efdf..5f5d315bfe 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -581,9 +581,11 @@ void Node::cleanup() } -const char* Node::description() const +std::string Node::description() const { - return String::createWithFormat("", _tag)->getCString(); + std::stringstream ss; + ss << "description()); } } @@ -143,7 +139,7 @@ void ProfilingTimer::reset() void ProfilingBeginTimingBlock(const char *timerName) { Profiler* p = Profiler::getInstance(); - ProfilingTimer* timer = static_cast( p->_activeTimers->objectForKey(timerName) ); + ProfilingTimer* timer = p->_activeTimers.at(timerName); if( ! timer ) { timer = p->createAndAddTimerWithName(timerName); @@ -161,7 +157,7 @@ void ProfilingEndTimingBlock(const char *timerName) auto now = chrono::high_resolution_clock::now(); Profiler* p = Profiler::getInstance(); - ProfilingTimer* timer = (ProfilingTimer*)p->_activeTimers->objectForKey(timerName); + ProfilingTimer* timer = p->_activeTimers.at(timerName); CCASSERT(timer, "CCProfilingTimer not found"); @@ -178,7 +174,7 @@ void ProfilingEndTimingBlock(const char *timerName) void ProfilingResetTimingBlock(const char *timerName) { Profiler* p = Profiler::getInstance(); - ProfilingTimer *timer = (ProfilingTimer*)p->_activeTimers->objectForKey(timerName); + ProfilingTimer *timer = p->_activeTimers.at(timerName); CCASSERT(timer, "CCProfilingTimer not found"); diff --git a/cocos/2d/CCProfiling.h b/cocos/2d/CCProfiling.h index b79c08c991..68c7a3f079 100644 --- a/cocos/2d/CCProfiling.h +++ b/cocos/2d/CCProfiling.h @@ -29,7 +29,7 @@ THE SOFTWARE. #include #include "ccConfig.h" #include "CCObject.h" -#include "CCDictionary.h" +#include "CCMap.h" NS_CC_BEGIN @@ -94,7 +94,7 @@ public: */ void releaseAllTimers(); - Dictionary* _activeTimers; + Map _activeTimers; }; class ProfilingTimer : public Object diff --git a/cocos/2d/CCSpriteFrameCache.cpp b/cocos/2d/CCSpriteFrameCache.cpp index 23c702bb00..471be8dbbd 100644 --- a/cocos/2d/CCSpriteFrameCache.cpp +++ b/cocos/2d/CCSpriteFrameCache.cpp @@ -300,7 +300,7 @@ void SpriteFrameCache::removeUnusedSpriteFrames() } } - _spriteFrames.remove(toRemoveFrames); + _spriteFrames.erase(toRemoveFrames); // XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache if( bRemoved ) @@ -321,12 +321,12 @@ void SpriteFrameCache::removeSpriteFrameByName(const std::string& name) if (!key.empty()) { - _spriteFrames.remove(key); + _spriteFrames.erase(key); _spriteFramesAliases.erase(key); } else { - _spriteFrames.remove(name); + _spriteFrames.erase(name); } // XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache @@ -365,7 +365,7 @@ void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary) } } - _spriteFrames.remove(keysToRemove); + _spriteFrames.erase(keysToRemove); } void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture) @@ -382,7 +382,7 @@ void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture) } } - _spriteFrames.remove(keysToRemove); + _spriteFrames.erase(keysToRemove); } SpriteFrame* SpriteFrameCache::getSpriteFrameByName(const std::string& name) diff --git a/cocos/base/CCArray.cpp b/cocos/base/CCArray.cpp index 333183a822..0a9bc7953b 100644 --- a/cocos/base/CCArray.cpp +++ b/cocos/base/CCArray.cpp @@ -36,7 +36,7 @@ NS_CC_BEGIN // std::vector implementation // ---------------------------------------------------------------------------------- -__Array::Array() +__Array::__Array() : data(NULL) { init(); @@ -483,7 +483,7 @@ __Array* __Array::createWithContentsOfFileThreadSafe(const char* fileName) __Array* ret = __Array::createWithCapacity(static_cast(arr.size())); std::for_each(arr.cbegin(), arr.cend(), [&ret](const Value& value){ - ret->addObject(String::create(value.asString())); + ret->addObject(__String::create(value.asString())); }); return ret; diff --git a/cocos/base/CCArray.h b/cocos/base/CCArray.h index d5df307745..2ecd9e5fe7 100644 --- a/cocos/base/CCArray.h +++ b/cocos/base/CCArray.h @@ -552,9 +552,6 @@ public: // end of data_structure group /// @} -CC_DEPRECATED_ATTRIBUTE typedef __Array CCArray; -CC_DEPRECATED_ATTRIBUTE typedef __Array Array; - NS_CC_END #endif // __CCARRAY_H__ diff --git a/cocos/base/CCBool.h b/cocos/base/CCBool.h index 32b7b276ee..c07c7a1e9a 100644 --- a/cocos/base/CCBool.h +++ b/cocos/base/CCBool.h @@ -34,16 +34,16 @@ NS_CC_BEGIN * @{ */ -class CC_DLL Bool : public Object, public Clonable +class CC_DLL __Bool : public Object, public Clonable { public: - Bool(bool v) + __Bool(bool v) : _value(v) {} bool getValue() const {return _value;} - static Bool* create(bool v) + static __Bool* create(bool v) { - Bool* pRet = new Bool(v); + __Bool* pRet = new __Bool(v); if (pRet) { pRet->autorelease(); @@ -54,9 +54,9 @@ public: /* override functions */ virtual void acceptVisitor(DataVisitor &visitor) { visitor.visit(this); } - Bool* clone() const + __Bool* clone() const { - return Bool::create(_value); + return __Bool::create(_value); } private: bool _value; diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index c15ba3383b..36aa67d13e 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -74,7 +74,7 @@ static void printSceneGraph(int fd, Node* node, int level) for(int i=0; idescription(), node->getZOrder(), node->getTag()); + mydprintf(fd, " %s: z=%d, tag=%d\n", node->description().c_str(), node->getZOrder(), node->getTag()); for(const auto& child: node->getChildren()) printSceneGraph(fd, child, level+1); diff --git a/cocos/base/CCDataVisitor.cpp b/cocos/base/CCDataVisitor.cpp index 5f7d95203c..6da64524e4 100644 --- a/cocos/base/CCDataVisitor.cpp +++ b/cocos/base/CCDataVisitor.cpp @@ -35,27 +35,27 @@ NS_CC_BEGIN -void DataVisitor::visit(const Bool *value) +void DataVisitor::visit(const __Bool *value) { visitObject(value); } -void DataVisitor::visit(const Integer *value) +void DataVisitor::visit(const __Integer *value) { visitObject(value); } -void DataVisitor::visit(const Float *value) +void DataVisitor::visit(const __Float *value) { visitObject(value); } -void DataVisitor::visit(const Double *value) +void DataVisitor::visit(const __Double *value) { visitObject(value); } -void DataVisitor::visit(const String *value) +void DataVisitor::visit(const __String *value) { visitObject(value); } @@ -65,7 +65,7 @@ void DataVisitor::visit(const __Array *value) visitObject(value); } -void DataVisitor::visit(const Dictionary *value) +void DataVisitor::visit(const __Dictionary *value) { visitObject(value); } @@ -103,35 +103,35 @@ void PrettyPrinter::visitObject(const Object *p) _result += buf; } -void PrettyPrinter::visit(const Bool * p) +void PrettyPrinter::visit(const __Bool * p) { char buf[50] = {0}; sprintf(buf, "%s", p->getValue() ? "true" : "false"); _result += buf; } -void PrettyPrinter::visit(const Integer *p) +void PrettyPrinter::visit(const __Integer *p) { char buf[50] = {0}; sprintf(buf, "%d", p->getValue()); _result += buf; } -void PrettyPrinter::visit(const Float *p) +void PrettyPrinter::visit(const __Float *p) { char buf[50] = {0}; sprintf(buf, "%f", p->getValue()); _result += buf; } -void PrettyPrinter::visit(const Double *p) +void PrettyPrinter::visit(const __Double *p) { char buf[50] = {0}; sprintf(buf, "%lf", p->getValue()); _result += buf; } -void PrettyPrinter::visit(const String *p) +void PrettyPrinter::visit(const __String *p) { _result += p->getCString(); } @@ -165,7 +165,7 @@ void PrettyPrinter::visit(const __Array *p) _result += ""; } -void PrettyPrinter::visit(const Dictionary *p) +void PrettyPrinter::visit(const __Dictionary *p) { _result += "\n"; _result += _indentStr; diff --git a/cocos/base/CCDataVisitor.h b/cocos/base/CCDataVisitor.h index 7824e30c95..37e08fc0d4 100644 --- a/cocos/base/CCDataVisitor.h +++ b/cocos/base/CCDataVisitor.h @@ -31,11 +31,11 @@ NS_CC_BEGIN class Object; -class Bool; -class Integer; -class Float; -class Double; -class String; +class __Bool; +class __Integer; +class __Float; +class __Double; +class __String; class __Array; class __Dictionary; class __Set; @@ -72,11 +72,11 @@ public: /** default method, called from non-overloaded methods and for unrecognized objects */ virtual void visitObject(const Object *p) = 0; - virtual void visit(const Bool *p); - virtual void visit(const Integer *p); - virtual void visit(const Float *p); - virtual void visit(const Double *p); - virtual void visit(const String *p); + virtual void visit(const __Bool *p); + virtual void visit(const __Integer *p); + virtual void visit(const __Float *p); + virtual void visit(const __Double *p); + virtual void visit(const __String *p); virtual void visit(const __Array *p); virtual void visit(const __Dictionary *p); virtual void visit(const __Set *p); @@ -93,11 +93,11 @@ public: virtual std::string getResult(); virtual void visitObject(const Object *p); - virtual void visit(const Bool * p); - virtual void visit(const Integer *p); - virtual void visit(const Float *p); - virtual void visit(const Double *p); - virtual void visit(const String *p); + virtual void visit(const __Bool * p); + virtual void visit(const __Integer *p); + virtual void visit(const __Float *p); + virtual void visit(const __Double *p); + virtual void visit(const __String *p); virtual void visit(const __Array *p); virtual void visit(const __Dictionary *p); virtual void visit(const __Set *p); diff --git a/cocos/base/CCDictionary.cpp b/cocos/base/CCDictionary.cpp index 626b24a702..a8beaf9bf2 100644 --- a/cocos/base/CCDictionary.cpp +++ b/cocos/base/CCDictionary.cpp @@ -100,7 +100,7 @@ __Array* __Dictionary::allKeys() { HASH_ITER(hh, _elements, pElement, tmp) { - String* pOneKey = new String(pElement->_strKey); + __String* pOneKey = new __String(pElement->_strKey); array->addObject(pOneKey); CC_SAFE_RELEASE(pOneKey); } @@ -109,7 +109,7 @@ __Array* __Dictionary::allKeys() { HASH_ITER(hh, _elements, pElement, tmp) { - Integer* pOneKey = new Integer(static_cast(pElement->_intKey)); + __Integer* pOneKey = new __Integer(static_cast(pElement->_intKey)); array->addObject(pOneKey); CC_SAFE_RELEASE(pOneKey); } @@ -132,7 +132,7 @@ __Array* __Dictionary::allKeysForObject(Object* object) { if (object == pElement->_object) { - String* pOneKey = new String(pElement->_strKey); + __String* pOneKey = new __String(pElement->_strKey); array->addObject(pOneKey); CC_SAFE_RELEASE(pOneKey); } @@ -144,7 +144,7 @@ __Array* __Dictionary::allKeysForObject(Object* object) { if (object == pElement->_object) { - Integer* pOneKey = new Integer(static_cast(pElement->_intKey)); + __Integer* pOneKey = new __Integer(static_cast(pElement->_intKey)); array->addObject(pOneKey); CC_SAFE_RELEASE(pOneKey); } @@ -189,22 +189,22 @@ Object* __Dictionary::objectForKey(intptr_t key) return pRetObject; } -const String* __Dictionary::valueForKey(const std::string& key) +const __String* __Dictionary::valueForKey(const std::string& key) { - String* pStr = dynamic_cast(objectForKey(key)); + __String* pStr = dynamic_cast<__String*>(objectForKey(key)); if (pStr == NULL) { - pStr = String::create(""); + pStr = __String::create(""); } return pStr; } -const String* __Dictionary::valueForKey(intptr_t key) +const __String* __Dictionary::valueForKey(intptr_t key) { - String* pStr = dynamic_cast(objectForKey(key)); + __String* pStr = dynamic_cast<__String*>(objectForKey(key)); if (pStr == NULL) { - pStr = String::create(""); + pStr = __String::create(""); } return pStr; } @@ -308,7 +308,7 @@ void __Dictionary::removeObjectsForKeys(__Array* pKey__Array) Object* pObj = NULL; CCARRAY_FOREACH(pKey__Array, pObj) { - String* pStr = static_cast(pObj); + __String* pStr = static_cast<__String*>(pObj); removeObjectForKey(pStr->getCString()); } } @@ -346,11 +346,11 @@ Object* __Dictionary::randomObject() if (_dictType == kDictInt) { - return objectForKey( static_cast(key)->getValue()); + return objectForKey( static_cast<__Integer*>(key)->getValue()); } else if (_dictType == kDictStr) { - return objectForKey( static_cast(key)->getCString()); + return objectForKey( static_cast<__String*>(key)->getCString()); } else { @@ -403,7 +403,7 @@ static __Dictionary* visitDict(const ValueMap& dict) } else { - auto str = new String(iter->second.asString()); + auto str = new __String(iter->second.asString()); ret->setObject(str, iter->first); str->release(); } @@ -433,7 +433,7 @@ static __Array* visitArray(const ValueVector& array) } else { - auto str = new String(value.asString()); + auto str = new __String(value.asString()); ret->addObject(str); str->release(); } @@ -468,7 +468,7 @@ bool __Dictionary::writeToFile(const char *fullPath) DictElement* element = nullptr; CCDICT_FOREACH(this, element) { - dict[element->getStrKey()] = Value(static_cast(element->getObject())->getCString()); + dict[element->getStrKey()] = Value(static_cast<__String*>(element->getObject())->getCString()); } return FileUtils::getInstance()->writeToFile(dict, fullPath); diff --git a/cocos/base/CCDictionary.h b/cocos/base/CCDictionary.h index 65bf856237..f43f32a12d 100644 --- a/cocos/base/CCDictionary.h +++ b/cocos/base/CCDictionary.h @@ -155,16 +155,16 @@ public: * Dictionary* pDict = Dictionary::create(); * * // Insert objects to dictionary - * String* pValue1 = String::create("100"); - * String* pValue2 = String::create("120"); + * __String* pValue1 = __String::create("100"); + * __String* pValue2 = __String::create("120"); * Integer* pValue3 = Integer::create(200); * pDict->setObject(pValue1, "key1"); * pDict->setObject(pValue2, "key2"); * pDict->setObject(pValue3, "key3"); * * // Get the object for key - * String* pStr1 = (String*)pDict->objectForKey("key1"); - * log("{ key1: %s }", pStr1->getCString()); + * __String* pStr1 = (__String*)pDict->objectForKey("key1"); + * log("{ key1: %s }", pStr1->getC__String()); * Integer* pInteger = (Integer*)pDict->objectForKey("key3"); * log("{ key3: %d }", pInteger->getValue()); * @endcode @@ -224,11 +224,11 @@ public: * @param key The string key for searching. * @return The object matches the key. You need to force convert it to the type you know. * @code - * // Assume that the elements are String* pointers. Convert it by following code. - * String* pStr = (String*)pDict->objectForKey("key1"); + * // Assume that the elements are __String* pointers. Convert it by following code. + * __String* pStr = (__String*)pDict->objectForKey("key1"); * // Do something about pStr. * // If you don't know the object type, properly you need to use dynamic_cast to check it. - * String* pStr2 = dynamic_cast(pDict->objectForKey("key1")); + * __String* pStr2 = dynamic_cast<__String*>(pDict->objectForKey("key1")); * if (pStr2 != NULL) { * // Do something about pStr2 * } @@ -251,25 +251,25 @@ public: /** Get the value according to the specified string key. * - * @note Be careful to use this function since it assumes the objects in the dictionary are String pointer. + * @note Be careful to use this function since it assumes the objects in the dictionary are __String pointer. * @param key The string key for searching - * @return An instance of String. - * It will return an empty string if the objects aren't String pointer or the key wasn't found. + * @return An instance of __String. + * It will return an empty string if the objects aren't __String pointer or the key wasn't found. * @see valueForKey(intptr_t) * @js NA */ - const String* valueForKey(const std::string& key); + const __String* valueForKey(const std::string& key); /** Get the value according to the specified integer key. * - * @note Be careful to use this function since it assumes the objects in the dictionary are String pointer. + * @note Be careful to use this function since it assumes the objects in the dictionary are __String pointer. * @param key The string key for searching. - * @return An instance of String. - * It will return an empty string if the objects aren't String pointer or the key wasn't found. + * @return An instance of __String. + * It will return an empty string if the objects aren't __String pointer or the key wasn't found. * @see valueForKey(intptr_t) * @js NA */ - const String* valueForKey(intptr_t key); + const __String* valueForKey(intptr_t key); /** Insert an object to dictionary, and match it with the specified string key. * @@ -453,9 +453,6 @@ private: // end of data_structure group /// @} -typedef __Dictionary Dictionary; - - NS_CC_END #endif /* __CCDICTIONARY_H__ */ diff --git a/cocos/base/CCDouble.h b/cocos/base/CCDouble.h index 5b45b1d6cf..6ef8ae28c8 100644 --- a/cocos/base/CCDouble.h +++ b/cocos/base/CCDouble.h @@ -34,16 +34,16 @@ NS_CC_BEGIN * @{ */ -class CC_DLL Double : public Object, public Clonable +class CC_DLL __Double : public Object, public Clonable { public: - Double(double v) + __Double(double v) : _value(v) {} double getValue() const {return _value;} - static Double* create(double v) + static __Double* create(double v) { - Double* pRet = new Double(v); + __Double* pRet = new __Double(v); if (pRet) { pRet->autorelease(); @@ -54,9 +54,9 @@ public: /* override functions */ virtual void acceptVisitor(DataVisitor &visitor) { visitor.visit(this); } - Double* clone() const + __Double* clone() const { - return Double::create(_value); + return __Double::create(_value); } private: double _value; diff --git a/cocos/base/CCFloat.h b/cocos/base/CCFloat.h index c85a2e1e70..6591638cd2 100644 --- a/cocos/base/CCFloat.h +++ b/cocos/base/CCFloat.h @@ -34,16 +34,16 @@ NS_CC_BEGIN * @{ */ -class CC_DLL Float : public Object, public Clonable +class CC_DLL __Float : public Object, public Clonable { public: - Float(float v) + __Float(float v) : _value(v) {} float getValue() const {return _value;} - static Float* create(float v) + static __Float* create(float v) { - Float* pRet = new Float(v); + __Float* pRet = new __Float(v); if (pRet) { pRet->autorelease(); @@ -54,9 +54,9 @@ public: /* override functions */ virtual void acceptVisitor(DataVisitor &visitor) { visitor.visit(this); } - Float* clone() const + __Float* clone() const { - return Float::create(_value); + return __Float::create(_value); } private: diff --git a/cocos/base/CCInteger.h b/cocos/base/CCInteger.h index 23735627dd..56b6468dd4 100644 --- a/cocos/base/CCInteger.h +++ b/cocos/base/CCInteger.h @@ -35,36 +35,36 @@ NS_CC_BEGIN * @{ */ -class CC_DLL Integer : public Object, public Clonable +class CC_DLL __Integer : public Object, public Clonable { public: - static Integer* create(int v) + static __Integer* create(int v) { - Integer* pRet = new Integer(v); + __Integer* pRet = new __Integer(v); pRet->autorelease(); return pRet; } /** * @js NA */ - Integer(int v) + __Integer(int v) : _value(v) {} int getValue() const {return _value;} /** * @js NA * @lua NA */ - virtual ~Integer() { - CCLOGINFO("deallocing ~Integer: %p", this); + virtual ~__Integer() { + CCLOGINFO("deallocing ~__Integer: %p", this); } /* override functions */ virtual void acceptVisitor(DataVisitor &visitor) { visitor.visit(this); } // overrides - virtual Integer* clone() const override + virtual __Integer* clone() const override { - return Integer::create(_value); + return __Integer::create(_value); } private: diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index 00324867d6..5a95cd70c5 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -25,6 +25,8 @@ THE SOFTWARE. #ifndef __CCMAP_H__ #define __CCMAP_H__ +#include "ccMacros.h" + #include #include #include // std::for_each @@ -40,6 +42,23 @@ template class CC_DLL Map { public: + // ------------------------------------------ + // Iterators + // ------------------------------------------ + typedef std::unordered_map RefMap; + + typedef typename RefMap::iterator iterator; + typedef typename RefMap::const_iterator const_iterator; + + iterator begin() { return _data.begin(); } + const_iterator begin() const { return _data.begin(); } + + iterator end() { return _data.end(); } + const_iterator end() const { return _data.end(); } + + const_iterator cbegin() const { return _data.cbegin(); } + const_iterator cend() const { return _data.cend(); } + Map() : _data() { @@ -123,37 +142,67 @@ public: return keys; } - V at(const K& key) const + const V at(const K& key) const { auto iter = _data.find(key); if (iter != _data.end()) return iter->second; - return nullptr; } + V at(const K& key) + { + auto iter = _data.find(key); + if (iter != _data.end()) + return iter->second; + return nullptr; + } + + const_iterator find(const K& key) const + { + return _data.find(key); + } + + iterator find(const K& key) + { + return _data.find(key); + } + void insert(const K& key, V object) { CCASSERT(object != nullptr, "Object is nullptr!"); - remove(key); + erase(key); _data.insert(std::make_pair(key, object)); object->retain(); } - void remove(const K& key) + iterator erase(const_iterator position) { - auto iter = _data.find(key); + return _data.erase(position); + } + + size_t erase(const K& k) + { + auto iter = _data.find(k); if (iter != _data.end()) { iter->second->release(); _data.erase(iter); + return 1; } + + return 0; } - void remove(const std::vector& keys) + iterator erase(const_iterator first, const_iterator last) + { + return _data.erase(first, last); + } + + void erase(const std::vector& keys) { std::for_each(keys.cbegin(), keys.cend(), [this](const K& key){ - this->remove(key); + this->erase(key); }); } @@ -177,23 +226,6 @@ public: return nullptr; } - // ------------------------------------------ - // Iterators - // ------------------------------------------ - typedef std::unordered_map RefMap; - - typedef typename RefMap::iterator iterator; - typedef typename RefMap::const_iterator const_iterator; - - iterator begin() { return _data.begin(); } - const_iterator begin() const { return _data.begin(); } - - iterator end() { return _data.end(); } - const_iterator end() const { return _data.end(); } - - const_iterator cbegin() const { return _data.cbegin(); } - const_iterator cend() const { return _data.cend(); } - // Don't uses operator since we could not decide whether it needs 'retain'/'release'. // V& operator[] ( const K& key ) // { diff --git a/cocos/base/CCString.cpp b/cocos/base/CCString.cpp index 97e1261f48..0441dfed65 100644 --- a/cocos/base/CCString.cpp +++ b/cocos/base/CCString.cpp @@ -33,36 +33,36 @@ NS_CC_BEGIN #define kMaxStringLen (1024*100) -String::String() +__String::__String() :_string("") {} -String::String(const char * str) +__String::__String(const char * str) :_string(str) {} -String::String(const std::string& str) +__String::__String(const std::string& str) :_string(str) {} -String::String(const String& str) +__String::__String(const __String& str) :_string(str.getCString()) {} -String::~String() +__String::~__String() { - CCLOGINFO("deallocing String: %p", this); + CCLOGINFO("deallocing __String: %p", this); _string.clear(); } -String& String::operator= (const String& other) +__String& __String::operator= (const __String& other) { _string = other._string; return *this; } -bool String::initWithFormatAndValist(const char* format, va_list ap) +bool __String::initWithFormatAndValist(const char* format, va_list ap) { bool bRet = false; char* pBuf = (char*)malloc(kMaxStringLen); @@ -76,7 +76,7 @@ bool String::initWithFormatAndValist(const char* format, va_list ap) return bRet; } -bool String::initWithFormat(const char* format, ...) +bool __String::initWithFormat(const char* format, ...) { bool bRet = false; _string.clear(); @@ -91,7 +91,7 @@ bool String::initWithFormat(const char* format, ...) return bRet; } -int String::intValue() const +int __String::intValue() const { if (length() == 0) { @@ -100,7 +100,7 @@ int String::intValue() const return atoi(_string.c_str()); } -unsigned int String::uintValue() const +unsigned int __String::uintValue() const { if (length() == 0) { @@ -109,7 +109,7 @@ unsigned int String::uintValue() const return (unsigned int)atoi(_string.c_str()); } -float String::floatValue() const +float __String::floatValue() const { if (length() == 0) { @@ -118,7 +118,7 @@ float String::floatValue() const return (float)atof(_string.c_str()); } -double String::doubleValue() const +double __String::doubleValue() const { if (length() == 0) { @@ -127,7 +127,7 @@ double String::doubleValue() const return atof(_string.c_str()); } -bool String::boolValue() const +bool __String::boolValue() const { if (length() == 0) { @@ -141,27 +141,27 @@ bool String::boolValue() const return true; } -const char* String::getCString() const +const char* __String::getCString() const { return _string.c_str(); } -int String::length() const +int __String::length() const { return static_cast(_string.length()); } -int String::compare(const char * pStr) const +int __String::compare(const char * pStr) const { return strcmp(getCString(), pStr); } -void String::append(const std::string& str) +void __String::append(const std::string& str) { _string.append(str); } -void String::appendWithFormat(const char* format, ...) +void __String::appendWithFormat(const char* format, ...) { va_list ap; va_start(ap, format); @@ -178,7 +178,7 @@ void String::appendWithFormat(const char* format, ...) } -__Array* String::componentsSeparatedByString(const char *delimiter) +__Array* __String::componentsSeparatedByString(const char *delimiter) { __Array* result = __Array::create(); @@ -187,23 +187,23 @@ __Array* String::componentsSeparatedByString(const char *delimiter) { if(cutAt > 0) { - result->addObject(String::create(_string.substr(0, cutAt))); + result->addObject(__String::create(_string.substr(0, cutAt))); } _string = _string.substr(cutAt + 1); } if(_string.length() > 0) { - result->addObject(String::create(_string)); + result->addObject(__String::create(_string)); } return result; } -bool String::isEqual(const Object* pObject) +bool __String::isEqual(const Object* pObject) { bool bRet = false; - const String* pStr = dynamic_cast(pObject); + const __String* pStr = dynamic_cast(pObject); if (pStr != NULL) { if (0 == _string.compare(pStr->_string)) @@ -214,16 +214,16 @@ bool String::isEqual(const Object* pObject) return bRet; } -String* String::create(const std::string& str) +__String* __String::create(const std::string& str) { - String* ret = new String(str); + __String* ret = new __String(str); ret->autorelease(); return ret; } -String* String::createWithData(const unsigned char* data, int nLen) +__String* __String::createWithData(const unsigned char* data, int nLen) { - String* ret = NULL; + __String* ret = NULL; if (data != NULL) { char* pStr = (char*)malloc(nLen+1); @@ -235,16 +235,16 @@ String* String::createWithData(const unsigned char* data, int nLen) memcpy(pStr, data, nLen); } - ret = String::create(pStr); + ret = __String::create(pStr); free(pStr); } } return ret; } -String* String::createWithFormat(const char* format, ...) +__String* __String::createWithFormat(const char* format, ...) { - String* ret = String::create(""); + __String* ret = __String::create(""); va_list ap; va_start(ap, format); ret->initWithFormatAndValist(format, ap); @@ -253,25 +253,25 @@ String* String::createWithFormat(const char* format, ...) return ret; } -String* String::createWithContentsOfFile(const char* filename) +__String* __String::createWithContentsOfFile(const char* filename) { ssize_t size = 0; unsigned char* data = 0; - String* ret = NULL; + __String* ret = NULL; data = FileUtils::getInstance()->getFileData(filename, "rb", &size); - ret = String::createWithData(data, static_cast(size)); + ret = __String::createWithData(data, static_cast(size)); free(data); return ret; } -void String::acceptVisitor(DataVisitor &visitor) +void __String::acceptVisitor(DataVisitor &visitor) { visitor.visit(this); } -String* String::clone() const +__String* __String::clone() const { - return String::create(_string); + return __String::create(_string); } NS_CC_END diff --git a/cocos/base/CCString.h b/cocos/base/CCString.h index 2906f9acef..7bf853ff78 100644 --- a/cocos/base/CCString.h +++ b/cocos/base/CCString.h @@ -42,40 +42,40 @@ NS_CC_BEGIN * @{ */ -class CC_DLL String : public Object, public Clonable +class CC_DLL __String : public Object, public Clonable { public: /** * @js NA * @lua NA */ - String(); + __String(); /** * @js NA * @lua NA */ - String(const char* str); + __String(const char* str); /** * @js NA * @lua NA */ - String(const std::string& str); + __String(const std::string& str); /** * @js NA * @lua NA */ - String(const String& str); + __String(const __String& str); /** * @js NA * @lua NA */ - virtual ~String(); + virtual ~__String(); /* override assignment operator * @js NA * @lua NA */ - String& operator= (const String& other); + __String& operator= (const __String& other); /** init a string with format, it's similar with the c function 'sprintf' * @js NA @@ -147,33 +147,33 @@ public: virtual bool isEqual(const Object* pObject); /** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer. - * @return A String pointer which is an autorelease object pointer, + * @return A __String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ - static String* create(const std::string& str); + static __String* create(const std::string& str); /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes, - * if you want to change it, you should modify the kMaxStringLen macro in String.cpp file. - * @return A String pointer which is an autorelease object pointer, + * if you want to change it, you should modify the kMax__StringLen macro in __String.cpp file. + * @return A __String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ - static String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2); + static __String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2); /** create a string with binary data - * @return A String pointer which is an autorelease object pointer, + * @return A __String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ - static String* createWithData(const unsigned char* pData, int nLen); + static __String* createWithData(const unsigned char* pData, int nLen); /** create a string with a file, - * @return A String pointer which is an autorelease object pointer, + * @return A __String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ - static String* createWithContentsOfFile(const char* filename); + static __String* createWithContentsOfFile(const char* filename); /** * @js NA * @lua NA @@ -183,7 +183,7 @@ public: * @js NA * @lua NA */ - virtual String* clone() const; + virtual __String* clone() const; private: @@ -194,15 +194,15 @@ public: std::string _string; }; -struct StringCompare : public std::binary_function { +struct StringCompare : public std::binary_function<__String *, __String *, bool> { public: - bool operator() (String * a, String * b) const { + bool operator() (__String * a, __String * b) const { return strcmp(a->getCString(), b->getCString()) < 0; } }; #define StringMake(str) String::create(str) -#define ccs StringMake +#define ccs StringMake class StringUtils { @@ -215,8 +215,33 @@ public: ss << arg; return ss.str(); } + + static std::string toStringWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2) + { + #define CC_MAX_STRING_LENGTH (1024*100) + + std::string ret; + + va_list ap; + va_start(ap, format); + + char* buf = (char*)malloc(CC_MAX_STRING_LENGTH); + if (buf != nullptr) + { + vsnprintf(buf, CC_MAX_STRING_LENGTH, format, ap); + ret = buf; + free(buf); + } + va_end(ap); + + return ret; + } + }; + +CC_DEPRECATED_ATTRIBUTE typedef __String String; + // end of data_structure group /// @} From b66fccb5abe8c4eb5712c4fe8dff539e17867e4a Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 17:49:44 +0800 Subject: [PATCH 08/27] issue #2790: Removes Map::erase(first, last). --- cocos/base/CCMap.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index 5a95cd70c5..3c5566b196 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -194,11 +194,6 @@ public: return 0; } - iterator erase(const_iterator first, const_iterator last) - { - return _data.erase(first, last); - } - void erase(const std::vector& keys) { std::for_each(keys.cbegin(), keys.cend(), [this](const K& key){ From 093e310cab6865937aabbc30d340ddbb164a15fd Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 17:50:30 +0800 Subject: [PATCH 09/27] =?UTF-8?q?issue=20#2790:=20Vector::remove=20?= =?UTF-8?q?=E2=80=94>=20Vector::erase.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/base/CCVector.h | 78 ++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index cddc02c25d..7d1bb80435 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -37,6 +37,34 @@ template class CC_DLL Vector { public: + // ------------------------------------------ + // Iterators + // ------------------------------------------ + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; + + typedef typename std::vector::reverse_iterator reverse_iterator; + typedef typename std::vector::const_reverse_iterator const_reverse_iterator; + + iterator begin() { return _data.begin(); } + const_iterator begin() const { return _data.begin(); } + + iterator end() { return _data.end(); } + const_iterator end() const { return _data.end(); } + + const_iterator cbegin() const { return _data.cbegin(); } + const_iterator cend() const { return _data.cend(); } + + reverse_iterator rbegin() { return _data.rbegin(); } + const_reverse_iterator rbegin() const { return _data.rbegin(); } + + reverse_iterator rend() { return _data.rend(); } + const_reverse_iterator rend() const { return _data.rend(); } + + const_reverse_iterator crbegin() const { return _data.crbegin(); } + const_reverse_iterator crend() const { return _data.crend(); } + + /** Constructor */ Vector() : _data() { @@ -228,9 +256,9 @@ public: _data.pop_back(); last->release(); } - + /** Remove a certain object */ - void removeObject(T object, bool toRelease = true) + void erase(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); auto iter = std::find(_data.begin(), _data.end(), object); @@ -241,7 +269,24 @@ public: } /** Removes an element with a certain index */ - void remove(int index) + iterator erase(const_iterator position) + { + CCASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!"); + (*position)->release(); + return _data.erase(position); + } + + iterator erase(const_iterator first, const_iterator last) + { + for (auto iter = first; iter != last; ++iter) + { + (*iter)->release(); + } + + return _data.erase(first, last); + } + + void erase(int index) { CCASSERT(!_data.empty() && index >=0 && index < size(), "Invalid index!"); auto it = std::next( begin(), index ); @@ -352,33 +397,6 @@ public: }); } - // ------------------------------------------ - // Iterators - // ------------------------------------------ - typedef typename std::vector::iterator iterator; - typedef typename std::vector::const_iterator const_iterator; - - typedef typename std::vector::reverse_iterator reverse_iterator; - typedef typename std::vector::const_reverse_iterator const_reverse_iterator; - - iterator begin() { return _data.begin(); } - const_iterator begin() const { return _data.begin(); } - - iterator end() { return _data.end(); } - const_iterator end() const { return _data.end(); } - - const_iterator cbegin() const { return _data.cbegin(); } - const_iterator cend() const { return _data.cend(); } - - reverse_iterator rbegin() { return _data.rbegin(); } - const_reverse_iterator rbegin() const { return _data.rbegin(); } - - reverse_iterator rend() { return _data.rend(); } - const_reverse_iterator rend() const { return _data.rend(); } - - const_reverse_iterator crbegin() const { return _data.crbegin(); } - const_reverse_iterator crend() const { return _data.crend(); } - protected: void addRefForAllObjects() From cc567a30779d9ae2098c621636f0fb546c5b6788 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 17:53:45 +0800 Subject: [PATCH 10/27] =?UTF-8?q?issue=20#2790:=20Vector::remove=20?= =?UTF-8?q?=E2=80=94>=20Vector::erase.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/2d/CCNode.cpp | 2 +- cocos/2d/CCParticleBatchNode.cpp | 2 +- cocos/2d/CCScheduler.cpp | 2 +- cocos/base/CCAutoreleasePool.cpp | 6 +++--- cocos/editor-support/cocostudio/CCArmature.cpp | 2 +- cocos/editor-support/cocostudio/CCBone.cpp | 2 +- cocos/network/HttpClient.cpp | 4 ++-- cocos/physics/CCPhysicsBody.cpp | 2 +- cocos/physics/CCPhysicsWorld.cpp | 6 +++--- extensions/GUI/CCControlExtension/CCControl.cpp | 2 +- extensions/GUI/CCScrollView/CCTableView.cpp | 4 ++-- 11 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 5f5d315bfe..476f69cfb3 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -772,7 +772,7 @@ void Node::detachChild(Node *child, int childIndex, bool doCleanup) // set parent nil at the end child->setParent(nullptr); - _children.remove(childIndex); + _children.erase(childIndex); } diff --git a/cocos/2d/CCParticleBatchNode.cpp b/cocos/2d/CCParticleBatchNode.cpp index 74df4b4ad0..3f1b3779bd 100644 --- a/cocos/2d/CCParticleBatchNode.cpp +++ b/cocos/2d/CCParticleBatchNode.cpp @@ -253,7 +253,7 @@ void ParticleBatchNode::reorderChild(Node * aChild, int zOrder) // reorder _children->array child->retain(); - _children.remove(oldIndex); + _children.erase(oldIndex); _children.insert(newIndex, child); child->release(); diff --git a/cocos/2d/CCScheduler.cpp b/cocos/2d/CCScheduler.cpp index 9ad8a2b308..5566c3f292 100644 --- a/cocos/2d/CCScheduler.cpp +++ b/cocos/2d/CCScheduler.cpp @@ -949,7 +949,7 @@ void Scheduler::update(float dt) SchedulerScriptHandlerEntry* eachEntry = _scriptHandlerEntries.at(i); if (eachEntry->isMarkedForDeletion()) { - _scriptHandlerEntries.remove(i); + _scriptHandlerEntries.erase(i); } else if (!eachEntry->isPaused()) { diff --git a/cocos/base/CCAutoreleasePool.cpp b/cocos/base/CCAutoreleasePool.cpp index 8c32f4c42b..01f65a7d3a 100644 --- a/cocos/base/CCAutoreleasePool.cpp +++ b/cocos/base/CCAutoreleasePool.cpp @@ -51,7 +51,7 @@ void AutoreleasePool::removeObject(Object* object) { for (unsigned int i = 0; i < object->_autoReleaseCount; ++i) { - _managedObjectArray.removeObject(object, false); + _managedObjectArray.erase(object, false); } } @@ -111,7 +111,7 @@ PoolManager::~PoolManager() // we only release the last autorelease pool here _curReleasePool = 0; - _releasePoolStack.remove(0); + _releasePoolStack.erase(0); } void PoolManager::finalize() @@ -147,7 +147,7 @@ void PoolManager::pop() if (count > 1) { - _releasePoolStack.remove(count-1); + _releasePoolStack.erase(count-1); // if(nCount > 1) // { diff --git a/cocos/editor-support/cocostudio/CCArmature.cpp b/cocos/editor-support/cocostudio/CCArmature.cpp index 2585f00bfb..8cecf76105 100644 --- a/cocos/editor-support/cocostudio/CCArmature.cpp +++ b/cocos/editor-support/cocostudio/CCArmature.cpp @@ -312,7 +312,7 @@ void Armature::changeBoneParent(Bone *bone, const char *parentName) if(bone->getParentBone()) { - bone->getParentBone()->getChildren().removeObject(bone); + bone->getParentBone()->getChildren().erase(bone); bone->setParentBone(nullptr); } diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index 18b14e40b2..06340bc422 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -334,7 +334,7 @@ void Bone::removeChildBone(Bone *bone, bool recursion) bone->getDisplayManager()->setCurrentDecorativeDisplay(nullptr); - _children.removeObject(bone); + _children.erase(bone); } } diff --git a/cocos/network/HttpClient.cpp b/cocos/network/HttpClient.cpp index 58efaaa836..3fc6f2bb90 100644 --- a/cocos/network/HttpClient.cpp +++ b/cocos/network/HttpClient.cpp @@ -118,7 +118,7 @@ void HttpClient::networkThread() if (!s_requestQueue->empty()) { request = s_requestQueue->at(0); - s_requestQueue->remove(0); + s_requestQueue->erase(0); } s_requestQueueMutex.unlock(); @@ -480,7 +480,7 @@ void HttpClient::dispatchResponseCallbacks() if (!s_responseQueue->empty()) { response = s_responseQueue->at(0); - s_responseQueue->remove(0); + s_responseQueue->erase(0); } s_responseQueueMutex.unlock(); diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 69a197b9b7..7fd340b043 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -672,7 +672,7 @@ void PhysicsBody::removeShape(PhysicsShape* shape, bool reduceMassAndMoment/* = // set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call. shape->_body = nullptr; shape->setBody(nullptr); - _shapes.removeObject(shape); + _shapes.erase(shape); } } diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 4d3f5066ac..fe2fccec36 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -630,7 +630,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body) { if (_delayRemoveBodies.getIndex(body) != CC_INVALID_INDEX) { - _delayRemoveBodies.removeObject(body); + _delayRemoveBodies.erase(body); return; } @@ -712,7 +712,7 @@ void PhysicsWorld::removeBody(PhysicsBody* body) body->_joints.clear(); removeBodyOrDelay(body); - _bodies.removeObject(body); + _bodies.erase(body); body->_world = nullptr; } @@ -721,7 +721,7 @@ void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body) { if (_delayAddBodies.getIndex(body) != CC_INVALID_INDEX) { - _delayAddBodies.removeObject(body); + _delayAddBodies.erase(body); return; } diff --git a/extensions/GUI/CCControlExtension/CCControl.cpp b/extensions/GUI/CCControlExtension/CCControl.cpp index 184b8125b5..1b7adde712 100644 --- a/extensions/GUI/CCControlExtension/CCControl.cpp +++ b/extensions/GUI/CCControlExtension/CCControl.cpp @@ -207,7 +207,7 @@ void Control::removeTargetWithActionForControlEvent(Object* target, Handler acti // Remove the corresponding invocation object if (shouldBeRemoved) { - eventInvocationList.removeObject(invocation, bDeleteObjects); + eventInvocationList.erase(invocation, bDeleteObjects); } }); } diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp index 16d53aa68a..b74026cd01 100644 --- a/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -242,7 +242,7 @@ TableViewCell *TableView::dequeueCell() } else { cell = _cellsFreed.at(0); cell->retain(); - _cellsFreed.remove(0); + _cellsFreed.erase(0); cell->autorelease(); } return cell; @@ -397,7 +397,7 @@ void TableView::_moveCellOutOfSight(TableViewCell *cell) } _cellsFreed.pushBack(cell); - _cellsUsed.removeObject(cell); + _cellsUsed.erase(cell); _isUsedCellsDirty = true; _indices->erase(cell->getIdx()); From 36fd873257c1d7bd0d22d08e6a29b21358250ecf Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 18:06:10 +0800 Subject: [PATCH 11/27] issue #2790: Adds Vector::find. --- cocos/base/CCVector.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 7d1bb80435..903144cce9 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -154,18 +154,23 @@ public: /** Returns index of a certain object, return UINT_MAX if doesn't contain the object */ int getIndex(T object) const { - int i = 0; - for (auto it = _data.begin(); it != _data.end(); ++it, ++i) - { - if (*it == object) - { - return i; - } - } - + auto iter = std::find(_data.begin(), _data.end(), object); + if (iter != _data.end()) + return iter - _data.begin(); + return -1; } + const_iterator find(T object) const + { + return std::find(_data.begin(), _data.end(), object); + } + + iterator find(T object) + { + return std::find(_data.begin(), _data.end(), object); + } + /** Returns an element with a certain index */ T at(int index) const { From 45dabe305f6c53705e05c42516c1447455019cff Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 18:07:14 +0800 Subject: [PATCH 12/27] issue #2790: performance improved for PhysicsWorld. --- cocos/physics/CCPhysicsWorld.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index fe2fccec36..785411c113 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -628,20 +628,22 @@ void PhysicsWorld::doAddBody(PhysicsBody* body) void PhysicsWorld::addBodyOrDelay(PhysicsBody* body) { - if (_delayRemoveBodies.getIndex(body) != CC_INVALID_INDEX) + auto removeBodyIter = _delayRemoveBodies.find(body); + if (removeBodyIter != _delayRemoveBodies.end()) { - _delayRemoveBodies.erase(body); + _delayRemoveBodies.erase(removeBodyIter); return; } if (_info->isLocked()) { - if (_delayAddBodies.getIndex(body) == CC_INVALID_INDEX) + if (_delayAddBodies.find(body) == _delayAddBodies.end()) { _delayAddBodies.pushBack(body); _delayDirty = true; } - }else + } + else { doAddBody(body); } From ce61fa4d9b8aa2996f8a4f454c283c4e7d8013eb Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 22:09:22 +0800 Subject: [PATCH 13/27] =?UTF-8?q?issue=20#2790:=20const=5Fiterator=20?= =?UTF-8?q?=E2=80=94>=20iterator=20to=20fix=20android=20build=20errors.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/base/CCVector.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 903144cce9..f049e4dd42 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -274,7 +274,7 @@ public: } /** Removes an element with a certain index */ - iterator erase(const_iterator position) + iterator erase(iterator position) { CCASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!"); (*position)->release(); From c2bccbaf989b50469aa19923e8d9c63f0e99aeb4 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 22:09:59 +0800 Subject: [PATCH 14/27] issue #2790: Removes unneeded dynamic_cast in CCPhyicsWorld.cpp. --- cocos/physics/CCPhysicsWorld.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 785411c113..f6b7722fb5 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -937,7 +937,7 @@ void PhysicsWorld::doRemoveBody(PhysicsBody* body) // remove shaps for (auto& shape : body->getShapes()) { - removeShape(dynamic_cast(shape)); + removeShape(shape); } // remove body @@ -951,9 +951,8 @@ void PhysicsWorld::doRemoveJoint(PhysicsJoint* joint) void PhysicsWorld::removeAllBodies() { - for (auto& obj : _bodies) + for (auto& child : _bodies) { - PhysicsBody* child = dynamic_cast(obj); removeBodyOrDelay(child); child->_world = nullptr; } From 9e637a3a3b0f4eee8a83c405098d8efd115f476a Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 11 Dec 2013 22:35:37 +0800 Subject: [PATCH 15/27] issue #2790: GLProgram::getXXXLog return `std::string` to prevent using CCString which was deprecated. --- cocos/2d/CCGLProgram.cpp | 19 ++++++++++--------- cocos/2d/CCGLProgram.h | 25 ++++++------------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/cocos/2d/CCGLProgram.cpp b/cocos/2d/CCGLProgram.cpp index 81afbdc09f..4dde356977 100644 --- a/cocos/2d/CCGLProgram.cpp +++ b/cocos/2d/CCGLProgram.cpp @@ -202,11 +202,11 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source if (type == GL_VERTEX_SHADER) { - CCLOG("cocos2d: %s", getVertexShaderLog()); + CCLOG("cocos2d: %s", getVertexShaderLog().c_str()); } else { - CCLOG("cocos2d: %s", getFragmentShaderLog()); + CCLOG("cocos2d: %s", getFragmentShaderLog().c_str()); } free(src); @@ -288,34 +288,35 @@ void GLProgram::use() GL::useProgram(_program); } -const char* GLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const +std::string GLProgram::logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const { + std::string ret; GLint logLength = 0, charsWritten = 0; infoFunc(object, GL_INFO_LOG_LENGTH, &logLength); if (logLength < 1) - return 0; + return ""; char *logBytes = (char*)malloc(logLength); logFunc(object, logLength, &charsWritten, logBytes); - String* log = String::create(logBytes); + ret = logBytes; free(logBytes); - return log->getCString(); + return ret; } -const char* GLProgram::getVertexShaderLog() const +std::string GLProgram::getVertexShaderLog() const { return this->logForOpenGLObject(_vertShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog); } -const char* GLProgram::getFragmentShaderLog() const +std::string GLProgram::getFragmentShaderLog() const { return this->logForOpenGLObject(_fragShader, (GLInfoFunction)&glGetShaderiv, (GLLogFunction)&glGetShaderInfoLog); } -const char* GLProgram::getProgramLog() const +std::string GLProgram::getProgramLog() const { return this->logForOpenGLObject(_program, (GLInfoFunction)&glGetProgramiv, (GLLogFunction)&glGetProgramInfoLog); } diff --git a/cocos/2d/CCGLProgram.h b/cocos/2d/CCGLProgram.h index 2d66baf3bb..803d7c7ca4 100644 --- a/cocos/2d/CCGLProgram.h +++ b/cocos/2d/CCGLProgram.h @@ -211,26 +211,13 @@ public: void setUniformsForBuiltins(); /** returns the vertexShader error log */ - const char* getVertexShaderLog() const; - /** @deprecated. Use getVertexShaderLog() instead - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE const char* vertexShaderLog() const { return getVertexShaderLog(); } + std::string getVertexShaderLog() const; + /** returns the fragmentShader error log */ - const char* getFragmentShaderLog() const; - /** @deprecated. Use getFragmentShaderLog() instead - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE const char* fragmentShaderLog() const{ return getFragmentShaderLog();} + std::string getFragmentShaderLog() const; + /** returns the program error log */ - const char* getProgramLog() const; - /** @deprecated. Use getProgramLog() instead - * @js NA - * @lua NA - */ - CC_DEPRECATED_ATTRIBUTE const char* programLog() const { return getProgramLog(); } + std::string getProgramLog() const; // reload all shaders, this function is designed for android // when opengl context lost, so don't call it. @@ -242,7 +229,7 @@ private: bool updateUniformLocation(GLint location, GLvoid* data, unsigned int bytes); const char* description() const; bool compileShader(GLuint * shader, GLenum type, const GLchar* source); - const char* logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const; + std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const; private: GLuint _program; From 976bef8b68a7bfc817c7e804157f270bd11aab6e Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 09:37:56 +0800 Subject: [PATCH 16/27] =?UTF-8?q?issue=20#2790:=20StringUtils::toStringWit?= =?UTF-8?q?hFormat=20=E2=80=94>=20StringUtils::format.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos/2d/CCAction.cpp | 2 +- cocos/2d/CCLabelBMFont.cpp | 2 +- cocos/2d/CCLabelTTF.cpp | 2 +- cocos/base/CCString.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos/2d/CCAction.cpp b/cocos/2d/CCAction.cpp index 79dcbe4f18..3556b6e413 100644 --- a/cocos/2d/CCAction.cpp +++ b/cocos/2d/CCAction.cpp @@ -49,7 +49,7 @@ Action::~Action() std::string Action::description() const { - return StringUtils::toStringWithFormat("", (size_t)this, HASH_COUNT(_fontDefDictionary), diff --git a/cocos/2d/CCLabelTTF.cpp b/cocos/2d/CCLabelTTF.cpp index edcae3152d..28b62d0b7c 100644 --- a/cocos/2d/CCLabelTTF.cpp +++ b/cocos/2d/CCLabelTTF.cpp @@ -187,7 +187,7 @@ const std::string& LabelTTF::getString() const std::string LabelTTF::description() const { - return StringUtils::toStringWithFormat("", _fontName.c_str(), _fontSize); + return StringUtils::format("", _fontName.c_str(), _fontSize); } TextHAlignment LabelTTF::getHorizontalAlignment() const diff --git a/cocos/base/CCString.h b/cocos/base/CCString.h index 7bf853ff78..044016185a 100644 --- a/cocos/base/CCString.h +++ b/cocos/base/CCString.h @@ -216,7 +216,7 @@ public: return ss.str(); } - static std::string toStringWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2) + static std::string format(const char* format, ...) CC_FORMAT_PRINTF(1, 2) { #define CC_MAX_STRING_LENGTH (1024*100) From b2e8bdce1efdc84fbbf1325a0996f81cf22bce47 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 09:47:35 +0800 Subject: [PATCH 17/27] issue #2790: Uses StringUtils::format for Node::description(). --- cocos/2d/CCNode.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 476f69cfb3..aafec6fd47 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -583,9 +583,7 @@ void Node::cleanup() std::string Node::description() const { - std::stringstream ss; - ss << " Date: Thu, 12 Dec 2013 09:53:50 +0800 Subject: [PATCH 18/27] issue #2790: Reverts comments in CCDictionary.h --- cocos/base/CCDictionary.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cocos/base/CCDictionary.h b/cocos/base/CCDictionary.h index f43f32a12d..ef20d76e05 100644 --- a/cocos/base/CCDictionary.h +++ b/cocos/base/CCDictionary.h @@ -155,17 +155,17 @@ public: * Dictionary* pDict = Dictionary::create(); * * // Insert objects to dictionary - * __String* pValue1 = __String::create("100"); - * __String* pValue2 = __String::create("120"); + * String* pValue1 = String::create("100"); + * String* pValue2 = String::create("120"); * Integer* pValue3 = Integer::create(200); * pDict->setObject(pValue1, "key1"); * pDict->setObject(pValue2, "key2"); * pDict->setObject(pValue3, "key3"); * * // Get the object for key - * __String* pStr1 = (__String*)pDict->objectForKey("key1"); - * log("{ key1: %s }", pStr1->getC__String()); - * Integer* pInteger = (Integer*)pDict->objectForKey("key3"); + * String* pStr1 = static_cast(pDict->objectForKey("key1")); + * log("{ key1: %s }", pStr1->getCString()); + * Integer* pInteger = static_cast(pDict->objectForKey("key3")); * log("{ key3: %d }", pInteger->getValue()); * @endcode * @@ -224,11 +224,11 @@ public: * @param key The string key for searching. * @return The object matches the key. You need to force convert it to the type you know. * @code - * // Assume that the elements are __String* pointers. Convert it by following code. - * __String* pStr = (__String*)pDict->objectForKey("key1"); + * // Assume that the elements are String* pointers. Convert it by following code. + * String* pStr = static_cast(pDict->objectForKey("key1")); * // Do something about pStr. * // If you don't know the object type, properly you need to use dynamic_cast to check it. - * __String* pStr2 = dynamic_cast<__String*>(pDict->objectForKey("key1")); + * String* pStr2 = dynamic_cast(pDict->objectForKey("key1")); * if (pStr2 != NULL) { * // Do something about pStr2 * } @@ -253,7 +253,7 @@ public: * * @note Be careful to use this function since it assumes the objects in the dictionary are __String pointer. * @param key The string key for searching - * @return An instance of __String. + * @return An instance of String. * It will return an empty string if the objects aren't __String pointer or the key wasn't found. * @see valueForKey(intptr_t) * @js NA @@ -264,7 +264,7 @@ public: * * @note Be careful to use this function since it assumes the objects in the dictionary are __String pointer. * @param key The string key for searching. - * @return An instance of __String. + * @return An instance of String. * It will return an empty string if the objects aren't __String pointer or the key wasn't found. * @see valueForKey(intptr_t) * @js NA @@ -321,7 +321,7 @@ public: /** * Remove objects by an array of keys. * - * @param pKey__Array The array contains keys to be removed. + * @param pKeyArray The array contains keys to be removed. * @see removeObjectForKey(const std::string&), removeObjectForKey(intptr_t), * removeObjectForElememt(DictElement*), removeAllObjects(). * @js NA From 5c62fe8f3f26750e4a1f60124ce1460e81b0c2b6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 10:34:41 +0800 Subject: [PATCH 19/27] issue #2790: Reverts comments in CCString.h --- cocos/base/CCString.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos/base/CCString.h b/cocos/base/CCString.h index 044016185a..ba5b773b84 100644 --- a/cocos/base/CCString.h +++ b/cocos/base/CCString.h @@ -147,7 +147,7 @@ public: virtual bool isEqual(const Object* pObject); /** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer. - * @return A __String pointer which is an autorelease object pointer, + * @return A String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ @@ -155,21 +155,21 @@ public: /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes, * if you want to change it, you should modify the kMax__StringLen macro in __String.cpp file. - * @return A __String pointer which is an autorelease object pointer, + * @return A String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ static __String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2); /** create a string with binary data - * @return A __String pointer which is an autorelease object pointer, + * @return A String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ static __String* createWithData(const unsigned char* pData, int nLen); /** create a string with a file, - * @return A __String pointer which is an autorelease object pointer, + * @return A String pointer which is an autorelease object pointer, * it means that you needn't do a release operation unless you retain it. * @js NA */ From b5bab598b39f1769a4d503612df2de49e416d36f Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 10:35:13 +0800 Subject: [PATCH 20/27] issue #2790: Disables deprecated warning for android to make Travis-CI happy. --- cocos/2d/Android.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/2d/Android.mk b/cocos/2d/Android.mk index 8f06c536bb..789cae2d91 100644 --- a/cocos/2d/Android.mk +++ b/cocos/2d/Android.mk @@ -196,9 +196,9 @@ LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static # define the macro to compile through support/zip_support/ioapi.c LOCAL_CFLAGS := -Wno-psabi -DUSE_FILE32API -LOCAL_CPPFLAGS := -Wno-literal-suffix +LOCAL_CPPFLAGS := -Wno-literal-suffix -Wno-deprecated-declarations LOCAL_EXPORT_CFLAGS := -Wno-psabi -DUSE_FILE32API -LOCAL_EXPORT_CPPFLAGS := -Wno-literal-suffix +LOCAL_EXPORT_CPPFLAGS := -Wno-literal-suffix -Wno-deprecated-declarations include $(BUILD_STATIC_LIBRARY) From 320ae15db4804f7c452e0ee2b29f62471cfb9226 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 10:54:41 +0800 Subject: [PATCH 21/27] issue #2790: Lua compilation fix. --- cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id index d93e528fb1..5a40e20077 100644 --- a/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id @@ -1 +1 @@ -19467d03b8f48f5f1933d53c756dfda6ab9e7126 \ No newline at end of file +cdda759b3fa681ecf395a93e2e45fe2e2fac9ec9 \ No newline at end of file From f12895a020e75206e29bb4d38118bcb993766365 Mon Sep 17 00:00:00 2001 From: CocosRobot Date: Thu, 12 Dec 2013 03:32:44 +0000 Subject: [PATCH 22/27] [AUTO] : updating submodule reference to latest autogenerated bindings --- cocos/scripting/auto-generated | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scripting/auto-generated b/cocos/scripting/auto-generated index d8bd5f3e89..dc780f0ca8 160000 --- a/cocos/scripting/auto-generated +++ b/cocos/scripting/auto-generated @@ -1 +1 @@ -Subproject commit d8bd5f3e890e578deeeeb39889359935da7dc55c +Subproject commit dc780f0ca828c53d31c3daddf10606e187b197d6 From 6264336809a2cb630af74a42dbb094421de80dea Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 11:54:03 +0800 Subject: [PATCH 23/27] issue #2790: Updates comments for Map container. Signed-off-by: James Chen --- cocos/base/CCMap.h | 59 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index 3c5566b196..5f2de4e9db 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -59,12 +59,14 @@ public: const_iterator cbegin() const { return _data.cbegin(); } const_iterator cend() const { return _data.cend(); } + /** Default constructor */ Map() : _data() { CCLOGINFO("In the default constructor of Map!"); } + /** Contructor with capacity */ explicit Map(int capacity) : _data() { @@ -72,6 +74,7 @@ public: _data.reserve(capacity); } + /** Copy constructor */ Map(const Map& other) { CCLOGINFO("In the copy constructor of Map!"); @@ -79,40 +82,50 @@ public: addRefForAllObjects(); } + /** Move constructor */ Map(Map&& other) { CCLOGINFO("In the move constructor of Map!"); _data = std::move(other._data); } + /** Destructor + * It will release all objects in map. + */ ~Map() { CCLOGINFO("In the destructor of Map!"); clear(); } - /** Sets capacity of current array */ + /** Sets capacity of the map */ void reserve(int capacity) { _data.reserve(capacity); } - /** Returns capacity of the array */ + /** Returns capacity of the map */ size_t capacity() const { return _data.capacity(); } + /** The number of elements in the map. */ size_t size() const { return _data.size(); } + /** Returns a bool value indicating whether the map container is empty, i.e. whether its size is 0. + * @note This function does not modify the content of the container in any way. + * To clear the content of an array object, member function unordered_map::clear exists. + */ bool empty() const { return _data.empty(); } + /** Returns all keys in the map */ std::vector keys() const { std::vector keys; @@ -127,6 +140,7 @@ public: return keys; } + /** Returns all keys that matches the object */ std::vector keys(V object) const { std::vector keys; @@ -142,6 +156,11 @@ public: return keys; } + /** @brief Returns a reference to the mapped value of the element with key k in the map. + * @note If key does not match the key of any element in the container, the function return nullptr. + * @param key Key value of the element whose mapped value is accessed. + * Member type K is the keys for the elements in the container. defined in Map as an alias of its first template parameter (Key). + */ const V at(const K& key) const { auto iter = _data.find(key); @@ -158,6 +177,13 @@ public: return nullptr; } + /** @brief Searches the container for an element with 'key' as key and returns an iterator to it if found, + * otherwise it returns an iterator to Map::end (the element past the end of the container). + * @param key Key to be searched for. + * Member type 'K' is the type of the keys for the elements in the container, + * defined in Map as an alias of its first template parameter (Key). + * + */ const_iterator find(const K& key) const { return _data.find(key); @@ -168,6 +194,11 @@ public: return _data.find(key); } + /** @brief Inserts new elements in the map. + * @note If the container has already contained the key, this function will erase the old pair(key, object) and insert the new pair. + * @param key The key to be inserted. + * @param object The object to be inserted. + */ void insert(const K& key, V object) { CCASSERT(object != nullptr, "Object is nullptr!"); @@ -176,11 +207,22 @@ public: object->retain(); } + /** @brief Removes an element with an iterator from the Map container. + * @param position Iterator pointing to a single element to be removed from the Map. + * Member type const_iterator is a forward iterator type. + */ iterator erase(const_iterator position) { + CCASSERT(position >= _data.begin() && position < _data.end(), ""); + position->second->release(); return _data.erase(position); } + /** @brief Removes an element with an iterator from the Map container. + * @param k Key of the element to be erased. + * Member type 'K' is the type of the keys for the elements in the container, + * defined in Map as an alias of its first template parameter (Key). + */ size_t erase(const K& k) { auto iter = _data.find(k); @@ -194,6 +236,9 @@ public: return 0; } + /** @brief Removes some elements with a vector which contains keys in the map. + * @param keys Keys of elements to be erased. + */ void erase(const std::vector& keys) { std::for_each(keys.cbegin(), keys.cend(), [this](const K& key){ @@ -201,6 +246,10 @@ public: }); } + /** All the elements in the Map container are dropped: + * their reference count will be decreased, and they are removed from the container, + * leaving it with a size of 0. Al + */ void clear() { for (auto iter = _data.cbegin(); iter != _data.cend(); ++iter) @@ -211,6 +260,9 @@ public: _data.clear(); } + /** @brief Gets a random object in the map + * @return Returns the random object if the map isn't empty, otherwise it returns nullptr. + */ V getRandomObject() const { if (!_data.empty()) @@ -246,6 +298,7 @@ public: // return _data.at(key); // } + /** Copy assignment operator */ Map& operator= ( const Map& other ) { CCLOGINFO("In the copy assignment operator of Map!"); @@ -255,6 +308,7 @@ public: return *this; } + /** Move assignment operator */ Map& operator= ( Map&& other ) { CCLOGINFO("In the move assignment operator of Map!"); @@ -264,6 +318,7 @@ public: protected: + /** Retains all the objects in the map */ void addRefForAllObjects() { for (auto iter = _data.begin(); iter != _data.end(); ++iter) From 3fc9c93102c5f16adbfa325d7ce07b13c9335890 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 12:58:01 +0800 Subject: [PATCH 24/27] issue #2790: Comment fix for Map::clear. Signed-off-by: James Chen --- cocos/base/CCMap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index 5f2de4e9db..e48ef19854 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -248,7 +248,7 @@ public: /** All the elements in the Map container are dropped: * their reference count will be decreased, and they are removed from the container, - * leaving it with a size of 0. Al + * leaving it with a size of 0. */ void clear() { From 93bd45cefd7d4d4818cda72f0bb01e267fbb0dc6 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 14:22:49 +0800 Subject: [PATCH 25/27] issue #2790: Adds comments for Vector. Signed-off-by: James Chen --- cocos/base/CCVector.h | 122 ++++++++++++++++++++++++++++++++---------- 1 file changed, 95 insertions(+), 27 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index f049e4dd42..984e0a8314 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -71,7 +71,7 @@ public: } - /** creates an emptry Vector */ + /** Constructor with a capacity */ explicit Vector(int capacity) : _data() { @@ -79,12 +79,14 @@ public: reserve(capacity); } + /** Destructor */ ~Vector() { CCLOGINFO("In the destructor of Vector."); clear(); } + /** Copy constructor */ Vector(const Vector& other) { CCLOGINFO("In the copy constructor!"); @@ -99,6 +101,7 @@ public: _data = std::move(other._data); } + /** Copy assignment operator */ Vector& operator=(const Vector& other) { CCLOGINFO("In the copy assignment operator!"); @@ -108,6 +111,7 @@ public: return *this; } + /** Move assignment operator */ Vector& operator=(Vector&& other) { CCLOGINFO("In the move assignment operator!"); @@ -126,31 +130,49 @@ public: // return _data[index]; // } - /** Sets capacity of current array */ - void reserve(int capacity) + /** @brief Request a change in capacity + * @param capacity Minimum capacity for the vector. + * If n is greater than the current vector capacity, + * the function causes the container to reallocate its storage increasing its capacity to n (or greater). + */ + void reserve(int n) { - _data.reserve(capacity); + _data.reserve(n); } - /** Returns capacity of the array */ + /** @brief Returns the size of the storage space currently allocated for the vector, expressed in terms of elements. + * @note This capacity is not necessarily equal to the vector size. + * It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. + * @return The size of the currently allocated storage capacity in the vector, measured in terms of the number elements it can hold. + */ int capacity() const { return _data.capacity(); } - // Querying an Array - - /** Returns element count of the array */ + /** @brief Returns the number of elements in the vector. + * @note This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity. + * @return The number of elements in the container. + */ int size() const { return static_cast(_data.size()); } + /** @brief Returns whether the vector is empty (i.e. whether its size is 0). + * @note This function does not modify the container in any way. To clear the content of a vector, see Vector::clear. + */ bool empty() const { return _data.empty(); } + /** Returns the maximum number of elements that the vector can hold. */ + size_t max_size() const + { + return _data.max_size(); + } + /** Returns index of a certain object, return UINT_MAX if doesn't contain the object */ int getIndex(T object) const { @@ -161,6 +183,10 @@ public: return -1; } + /** @brief Find the object in the vector. + * @return Returns an iterator to the first element in the range [first,last) that compares equal to val. + * If no such element is found, the function returns last. + */ const_iterator find(T object) const { return std::find(_data.begin(), _data.end(), object); @@ -171,25 +197,26 @@ public: return std::find(_data.begin(), _data.end(), object); } - /** Returns an element with a certain index */ + /** Returns the element at position 'index' in the vector. */ T at(int index) const { CCASSERT( index >= 0 && index < size(), "index out of range in getObjectAtIndex()"); return _data[index]; } + /** Returns the first element in the vector. */ T front() const { return _data.front(); } - /** Returns the last element of the array */ + /** Returns the last element of the vector. */ T back() const { return _data.back(); } - /** Returns a random element */ + /** Returns a random element of the vector. */ T getRandomObject() const { if (!_data.empty()) @@ -200,13 +227,13 @@ public: return nullptr; } - /** Returns a Boolean value that indicates whether object is present in array. */ + /** Returns a Boolean value that indicates whether object is present in vector. */ bool contains(T object) const { return( std::find(_data.begin(), _data.end(), object) != _data.end() ); } - /** returns true if the the arrays are equal */ + /** Returns true if the two vectors are equal */ bool equals(const Vector &other) { size_t s = this->size(); @@ -223,9 +250,13 @@ public: return true; } - // Adding Objects - - /** Add a certain object */ + // Adds objects + + /** @brief Adds a new element at the end of the vector, after its current last element. + * @note This effectively increases the container size by one, + * which causes an automatic reallocation of the allocated storage space + * if -and only if- the new vector size surpasses the current vector capacity. + */ void pushBack(T object) { CCASSERT(object != nullptr, "The object should not be nullptr"); @@ -233,7 +264,7 @@ public: object->retain(); } - /** Add all elements of an existing array */ + /** Inserts all elements of an existing vector */ void insert(const Vector& other) { for( auto it = other.begin(); it != other.end(); ++it ) { @@ -242,7 +273,12 @@ public: } } - /** Insert a certain object at a certain index */ + /** @brief Insert a certain object at a certain index + * @note The vector is extended by inserting new elements before the element at the specified 'index', + * effectively increasing the container size by the number of elements inserted. + * This causes an automatic reallocation of the allocated storage space + * if -and only if- the new vector size surpasses the current vector capacity. + */ void insert(int index, T object) { CCASSERT(index >= 0 && index <= size(), "Invalid index!"); @@ -251,9 +287,11 @@ public: object->retain(); } - // Removing Objects + // Removes Objects - /** Remove last object */ + /** Removes the last element in the vector, + * effectively reducing the container size by one, decrease the referece count of the deleted object. + */ void popBack() { CCASSERT(!_data.empty(), "no objects added"); @@ -262,7 +300,10 @@ public: last->release(); } - /** Remove a certain object */ + /** @brief Remove a certain object. + * @param object The object to be removed. + * @param toRelease Whether to decrease the referece count of the deleted object. + */ void erase(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); @@ -273,7 +314,11 @@ public: object->release(); } - /** Removes an element with a certain index */ + /** @brief Removes from the vector with an iterator. + * @param position Iterator pointing to a single element to be removed from the vector. + * @return An iterator pointing to the new location of the element that followed the last element erased by the function call. + * This is the container end if the operation erased the last element in the sequence. + */ iterator erase(iterator position) { CCASSERT(position >= _data.begin() && position < _data.end(), "Invalid position!"); @@ -281,6 +326,12 @@ public: return _data.erase(position); } + /** @brief Removes from the vector with a range of elements ( [first, last) ). + * @param first The beginning of the range + * @param last The end of the range, the 'last' will not used, it's only for indicating the end of range. + * @return An iterator pointing to the new location of the element that followed the last element erased by the function call. + * This is the container end if the operation erased the last element in the sequence. + */ iterator erase(const_iterator first, const_iterator last) { for (auto iter = first; iter != last; ++iter) @@ -291,15 +342,22 @@ public: return _data.erase(first, last); } - void erase(int index) + /** @brief Removes from the vector with an index. + * @param index The index of the element to be removed from the vector. + * @return An iterator pointing to the new location of the element that followed the last element erased by the function call. + * This is the container end if the operation erased the last element in the sequence. + */ + iterator erase(int index) { CCASSERT(!_data.empty() && index >=0 && index < size(), "Invalid index!"); auto it = std::next( begin(), index ); (*it)->release(); - _data.erase(it); + return _data.erase(it); } - /** Removes all objects */ + /** @brief Removes all elements from the vector (which are destroyed), leaving the container with a size of 0. + * @note All the elements in the vector will be released (referece count will be decreased). + */ void clear() { for( auto it = std::begin(_data); it != std::end(_data); ++it ) { @@ -340,18 +398,19 @@ public: object->retain(); } - /** reverses the array */ + /** reverses the vector */ void reverse() { std::reverse( std::begin(_data), std::end(_data) ); } - /** Shrinks the array so the memory footprint corresponds with the number of items */ + /** Shrinks the vector so the memory footprint corresponds with the number of items */ void shrinkToFit() { _data.shrink_to_fit(); } + /** Traverses through the vector and applys the callback function for each element */ void forEach(const std::function& callback) { if (empty()) @@ -362,6 +421,7 @@ public: }); } + /** Traverses through the vector and applys the callback function for each element */ void forEach(const std::function& callback) const { if (empty()) @@ -372,6 +432,7 @@ public: }); } + /** Traverses through the vector in reversed order and applys the callback function for each element */ void forEachReverse(const std::function& callback) { if (empty()) @@ -382,6 +443,7 @@ public: }); } + /** Traverses through the vector in reversed order and applys the callback function for each element */ void forEachReverse(const std::function& callback) const { if (empty()) @@ -392,6 +454,11 @@ public: }); } + /** @brief Sorts all elements in the vector according the binary callback function. + * @param callback Binary function that accepts two elements in the vector as arguments, + * and returns a value convertible to bool. + * The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. + */ void sort(const std::function& callback) { if (empty()) @@ -404,6 +471,7 @@ public: protected: + /** Retains all the objects in the vector */ void addRefForAllObjects() { std::for_each(_data.begin(), _data.end(), [](T obj){ From 3649c12d3734802d2430c50d58cf5f898734fbde Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 12 Dec 2013 14:36:59 +0800 Subject: [PATCH 26/27] issue #2790: Compilation error fix in CCMap.h. --- cocos/base/CCMap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/base/CCMap.h b/cocos/base/CCMap.h index e48ef19854..67184cf20f 100644 --- a/cocos/base/CCMap.h +++ b/cocos/base/CCMap.h @@ -213,7 +213,7 @@ public: */ iterator erase(const_iterator position) { - CCASSERT(position >= _data.begin() && position < _data.end(), ""); + CCASSERT(position != _data.cend(), "Invalid iterator!"); position->second->release(); return _data.erase(position); } From 4ccceea23090038d547e1055d05116c5dc9455ca Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Thu, 12 Dec 2013 14:47:35 +0800 Subject: [PATCH 27/27] Fix:Remove some needless lua binding functions and resolve a bug --- cocos/scripting/lua/bindings/LuaBasicConversions.h | 2 +- cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id | 2 +- cocos/scripting/lua/bindings/LuaOpengl.h | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cocos/scripting/lua/bindings/LuaBasicConversions.h b/cocos/scripting/lua/bindings/LuaBasicConversions.h index 21350574fc..d8b2cd45e8 100644 --- a/cocos/scripting/lua/bindings/LuaBasicConversions.h +++ b/cocos/scripting/lua/bindings/LuaBasicConversions.h @@ -149,7 +149,7 @@ void ccvector_to_luaval(lua_State* L,const cocos2d::Vector& inValue) { long typeId = typeid(*obj).hash_code(); auto iter = g_luaType.find(typeId); - if (g_luaType.end() == iter) + if (g_luaType.end() != iter) { lua_pushnumber(L, (lua_Number)indexTable); int ID = (obj) ? (int)obj->_ID : -1; diff --git a/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id index 5a40e20077..2381b20f37 100644 --- a/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/LuaOpengl.cpp.REMOVED.git-id @@ -1 +1 @@ -cdda759b3fa681ecf395a93e2e45fe2e2fac9ec9 \ No newline at end of file +4604aa76ce1cd72165190c09b5eba4faf2efca40 \ No newline at end of file diff --git a/cocos/scripting/lua/bindings/LuaOpengl.h b/cocos/scripting/lua/bindings/LuaOpengl.h index 3ab56c56fe..fa8f3780f6 100644 --- a/cocos/scripting/lua/bindings/LuaOpengl.h +++ b/cocos/scripting/lua/bindings/LuaOpengl.h @@ -16,8 +16,6 @@ class GLNode:public cocos2d::Node virtual void draw(); }; -TOLUA_API int tolua_Cocos2d_CCDrawNode_drawPolygon00(lua_State* tolua_S); - TOLUA_API int tolua_opengl_open(lua_State* tolua_S); TOLUA_API int register_glnode_manual(lua_State* tolua_S);