From 55080d713bd20c7874326689ef795091249aaab1 Mon Sep 17 00:00:00 2001 From: heliclei Date: Wed, 5 Mar 2014 17:03:25 +0800 Subject: [PATCH] refactor string tag to name --- cocos/2d/CCNode.cpp | 44 ++++++++++++++++++-------------------------- cocos/2d/CCNode.h | 8 ++++---- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index e78147b9d6..b7af7b9d98 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -141,8 +141,6 @@ Node::Node(void) kmMat4Identity(&_transform); kmMat4Identity(&_inverse); kmMat4Identity(&_additionalTransform); - - memset(_strTag, sizeof(_strTag), 0); } Node::~Node() @@ -596,17 +594,16 @@ void Node::setTag(int var) _tag = var; } -//StrTag getter -char* Node::getStrTag() +//name getter +std::string& Node::getName() { - return &(_strTag[0]); + return _name; } -//StrTag setter -void Node::setStrTag(const char* strTag) +//name setter +void Node::setName(const std::string& name) { - strncpy(_strTag, strTag, sizeof(_strTag)-1); - _strTag[sizeof(_strTag)-1] = 0; + _name = name; } /// userData setter void Node::setUserData(void *var) @@ -712,27 +709,22 @@ Node* Node::getChildByTag(int tag) return nullptr; } -Node* Node::getChildByStrTag(const char* label, bool recursive) +Node* Node::getChildByName(const std::string& name) { for (auto& child : _children) { - if(child) - { - if(strncmp(child->_strTag, label, sizeof(child->_strTag)-1) == 0) - { - return child; - } - if(recursive) - { - auto found = child->getChildByStrTag(label, true); - if(found != nullptr) - { - return found; - } - - } - } + + if(child->_name == name) + { + return child; + } + + auto found = child->getChildByName(name); + if(found != nullptr) + { + return found; + } } return nullptr; } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index d80f1ead73..33206425f2 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -678,7 +678,7 @@ public: * * @return the first Node object whose label equals to the input parameter */ - virtual Node* getChildByStrTag(const char* label, bool recursive); + virtual Node* getChildByName(const std::string& name); /** * Return an array of children * @@ -855,7 +855,7 @@ public: * * @return A C string that identifies the node. */ - virtual char* getStrTag(); + virtual std::string& getName(); /** * Changes the string TAG that is used to identify the node easily. * @@ -863,7 +863,7 @@ public: * * @param strTag A C String that indentifies the node. */ - virtual void setStrTag(const char* strTag); + virtual void setName(const std::string& name); /** * Returns a custom user data pointer * @@ -1513,7 +1513,7 @@ protected: int _tag; ///< a tag. Can be any number you assigned just to identify this node - char _strTag[64]; ///