From da3e7946311df2d1c77d4934e36a5d0f8318ba2f Mon Sep 17 00:00:00 2001 From: andyque Date: Mon, 12 May 2014 14:59:00 +0800 Subject: [PATCH] issue #5057, convert const char* to std::string& --- cocos/ui/UIHelper.cpp | 4 ++-- cocos/ui/UIHelper.h | 2 +- cocos/ui/UILayout.cpp | 6 +++--- cocos/ui/UILayoutParameter.cpp | 16 ++++++++-------- cocos/ui/UILayoutParameter.h | 8 ++++---- cocos/ui/UIListView.h | 2 +- cocos/ui/UIPageView.h | 2 +- cocos/ui/UIRichText.cpp | 14 +++++++------- cocos/ui/UIRichText.h | 12 ++++++------ cocos/ui/UIScrollView.cpp | 2 +- cocos/ui/UIScrollView.h | 2 +- cocos/ui/UITextField.cpp | 14 +++++++------- cocos/ui/UITextField.h | 6 +++--- cocos/ui/UIWidget.cpp | 10 +++++----- cocos/ui/UIWidget.h | 10 +++++----- 15 files changed, 55 insertions(+), 55 deletions(-) diff --git a/cocos/ui/UIHelper.cpp b/cocos/ui/UIHelper.cpp index 140f9003b3..7e0e4ca169 100644 --- a/cocos/ui/UIHelper.cpp +++ b/cocos/ui/UIHelper.cpp @@ -55,13 +55,13 @@ Widget* Helper::seekWidgetByTag(Widget* root, int tag) return nullptr; } -Widget* Helper::seekWidgetByName(Widget* root, const char *name) +Widget* Helper::seekWidgetByName(Widget* root, const std::string& name) { if (!root) { return nullptr; } - if (strcmp(root->getName(), name) == 0) + if (root->getName() == name) { return root; } diff --git a/cocos/ui/UIHelper.h b/cocos/ui/UIHelper.h index e12fc0dfc7..c16f90e669 100644 --- a/cocos/ui/UIHelper.h +++ b/cocos/ui/UIHelper.h @@ -56,7 +56,7 @@ public: * * @return finded result. */ - static Widget* seekWidgetByName(Widget* root, const char* name); + static Widget* seekWidgetByName(Widget* root, const std::string& name); /*temp action*/ static Widget* seekActionWidgetByActionTag(Widget* root, int tag); diff --git a/cocos/ui/UILayout.cpp b/cocos/ui/UILayout.cpp index 6d01b074bd..d5401ebc34 100644 --- a/cocos/ui/UILayout.cpp +++ b/cocos/ui/UILayout.cpp @@ -234,19 +234,19 @@ void RelativeLayoutExecutant::doLayout(const cocos2d::Size &layoutSize, VectorgetAnchorPoint(); Size cs = child->getSize(); RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign(); - const char* relativeName = layoutParameter->getRelativeToWidgetName(); + const std::string relativeName = layoutParameter->getRelativeToWidgetName(); Widget* relativeWidget = nullptr; RelativeLayoutParameter* relativeWidgetLP = nullptr; float finalPosX = 0.0f; float finalPosY = 0.0f; - if (relativeName && strcmp(relativeName, "")) + if (!relativeName.empty()) { for (auto& sWidget : widgetChildren) { if (sWidget) { RelativeLayoutParameter* rlayoutParameter = dynamic_cast(sWidget->getLayoutParameter(LayoutParameter::Type::RELATIVE)); - if (rlayoutParameter && strcmp(rlayoutParameter->getRelativeName(), relativeName) == 0) + if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName) { relativeWidget = sWidget; relativeWidgetLP = rlayoutParameter; diff --git a/cocos/ui/UILayoutParameter.cpp b/cocos/ui/UILayoutParameter.cpp index 865542da67..71f78db93e 100644 --- a/cocos/ui/UILayoutParameter.cpp +++ b/cocos/ui/UILayoutParameter.cpp @@ -164,24 +164,24 @@ RelativeLayoutParameter::RelativeAlign RelativeLayoutParameter::getAlign() const return _relativeAlign; } -void RelativeLayoutParameter::setRelativeToWidgetName(const char *name) +void RelativeLayoutParameter::setRelativeToWidgetName(const std::string& name) { _relativeWidgetName = name; } -const char* RelativeLayoutParameter::getRelativeToWidgetName() const +const std::string& RelativeLayoutParameter::getRelativeToWidgetName() const { - return _relativeWidgetName.c_str(); + return _relativeWidgetName; } -void RelativeLayoutParameter::setRelativeName(const char* name) +void RelativeLayoutParameter::setRelativeName(const std::string& name) { _relativeLayoutName = name; } -const char* RelativeLayoutParameter::getRelativeName() const +const std::string& RelativeLayoutParameter::getRelativeName() const { - return _relativeLayoutName.c_str(); + return _relativeLayoutName; } LayoutParameter* RelativeLayoutParameter::createCloneInstance() @@ -196,8 +196,8 @@ void RelativeLayoutParameter::copyProperties(LayoutParameter *model) if (parameter) { setAlign(parameter->_relativeAlign); - setRelativeName(parameter->_relativeLayoutName.c_str()); - setRelativeToWidgetName(parameter->_relativeWidgetName.c_str()); + setRelativeName(parameter->_relativeLayoutName); + setRelativeToWidgetName(parameter->_relativeWidgetName); } } diff --git a/cocos/ui/UILayoutParameter.h b/cocos/ui/UILayoutParameter.h index 5dc3efedc1..388af3f442 100644 --- a/cocos/ui/UILayoutParameter.h +++ b/cocos/ui/UILayoutParameter.h @@ -268,28 +268,28 @@ public: * * @param name */ - void setRelativeToWidgetName(const char* name); + void setRelativeToWidgetName(const std::string& name); /** * Gets the key of LayoutParameter. Witch widget named this is relative to. * * @return name */ - const char* getRelativeToWidgetName() const; + const std::string& getRelativeToWidgetName() const; /** * Sets a name in Relative Layout for LayoutParameter. * * @param name */ - void setRelativeName(const char* name); + void setRelativeName(const std::string& name); /** * Gets a name in Relative Layout of LayoutParameter. * * @return name */ - const char* getRelativeName() const; + const std::string& getRelativeName() const; virtual LayoutParameter* createCloneInstance() override; virtual void copyProperties(LayoutParameter* model) override; diff --git a/cocos/ui/UIListView.h b/cocos/ui/UIListView.h index b16b4f1595..dc490026e5 100644 --- a/cocos/ui/UIListView.h +++ b/cocos/ui/UIListView.h @@ -197,7 +197,7 @@ protected: virtual const Vector& getChildren() const override{return ScrollView::getChildren();}; virtual ssize_t getChildrenCount() const override {return ScrollView::getChildrenCount();}; virtual Node * getChildByTag(int tag) override {return ScrollView::getChildByTag(tag);}; - virtual Widget* getChildByName(const char* name) override {return ScrollView::getChildByName(name);}; + virtual Widget* getChildByName(const std::string& name) override {return ScrollView::getChildByName(name);}; void updateInnerContainerSize(); void remedyLayoutParameter(Widget* item); virtual void onSizeChanged() override; diff --git a/cocos/ui/UIPageView.h b/cocos/ui/UIPageView.h index 1b1e62c283..188fae1aa2 100644 --- a/cocos/ui/UIPageView.h +++ b/cocos/ui/UIPageView.h @@ -184,7 +184,7 @@ protected: virtual const Vector& getChildren() const override{return Widget::getChildren();}; virtual ssize_t getChildrenCount() const override {return Widget::getChildrenCount();}; virtual Node * getChildByTag(int tag) override {return Widget::getChildByTag(tag);}; - virtual Widget* getChildByName(const char* name) override {return Widget::getChildByName(name);}; +virtual Widget* getChildByName(const std::string& name) override {return Widget::getChildByName(name);}; Layout* createPage(); float getPositionXByIndex(ssize_t idx); diff --git a/cocos/ui/UIRichText.cpp b/cocos/ui/UIRichText.cpp index c2abd53c56..1c96d9e50b 100644 --- a/cocos/ui/UIRichText.cpp +++ b/cocos/ui/UIRichText.cpp @@ -55,7 +55,7 @@ bool RichElement::init(int tag, const Color3B &color, GLubyte opacity) } -RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte opacity, const char *text, const char *fontName, float fontSize) +RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize) { RichElementText* element = new RichElementText(); if (element && element->init(tag, color, opacity, text, fontName, fontSize)) @@ -67,7 +67,7 @@ RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte return NULL; } -bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const char *text, const char *fontName, float fontSize) +bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize) { if (RichElement::init(tag, color, opacity)) { @@ -79,7 +79,7 @@ bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const return false; } -RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyte opacity, const char *filePath) +RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath) { RichElementImage* element = new RichElementImage(); if (element && element->init(tag, color, opacity, filePath)) @@ -91,7 +91,7 @@ RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyt return NULL; } -bool RichElementImage::init(int tag, const Color3B &color, GLubyte opacity, const char *filePath) +bool RichElementImage::init(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath) { if (RichElement::init(tag, color, opacity)) { @@ -275,7 +275,7 @@ void RichText::formatText() } } -void RichText::handleTextRenderer(const char *text, const char *fontName, float fontSize, const Color3B &color, GLubyte opacity) +void RichText::handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B &color, GLubyte opacity) { auto fileExist = FileUtils::getInstance()->isFileExist(fontName); Label* textRenderer = nullptr; @@ -293,7 +293,7 @@ void RichText::handleTextRenderer(const char *text, const char *fontName, float { float overstepPercent = (-_leftSpaceWidth) / textRendererWidth; std::string curText = text; - size_t stringLength = _calcCharCount(text); + size_t stringLength = _calcCharCount(text.c_str()); int leftLength = stringLength * (1.0f - overstepPercent); std::string leftWords = curText.substr(0, leftLength); std::string cutWords = curText.substr(leftLength, curText.length()-1); @@ -327,7 +327,7 @@ void RichText::handleTextRenderer(const char *text, const char *fontName, float } } -void RichText::handleImageRenderer(const char *fileParh, const Color3B &color, GLubyte opacity) +void RichText::handleImageRenderer(const std::string& fileParh, const Color3B &color, GLubyte opacity) { Sprite* imageRenderer = Sprite::create(fileParh); handleCustomRenderer(imageRenderer); diff --git a/cocos/ui/UIRichText.h b/cocos/ui/UIRichText.h index cc49e443df..b65ee42053 100644 --- a/cocos/ui/UIRichText.h +++ b/cocos/ui/UIRichText.h @@ -56,8 +56,8 @@ class RichElementText : public RichElement public: RichElementText(){_type = Type::TEXT;}; virtual ~RichElementText(){}; - bool init(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize); - static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const char* text, const char* fontName, float fontSize); + bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize); + static RichElementText* create(int tag, const Color3B& color, GLubyte opacity, const std::string& text, const std::string& fontName, float fontSize); protected: std::string _text; std::string _fontName; @@ -71,8 +71,8 @@ class RichElementImage : public RichElement public: RichElementImage(){_type = Type::IMAGE;}; virtual ~RichElementImage(){}; - bool init(int tag, const Color3B& color, GLubyte opacity, const char* filePath); - static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const char* filePath); + bool init(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath); + static RichElementImage* create(int tag, const Color3B& color, GLubyte opacity, const std::string& filePath); protected: std::string _filePath; Rect _textureRect; @@ -116,8 +116,8 @@ CC_CONSTRUCTOR_ACCESS: protected: virtual void initRenderer(); void pushToContainer(Node* renderer); - void handleTextRenderer(const char* text, const char* fontName, float fontSize, const Color3B& color, GLubyte opacity); - void handleImageRenderer(const char* fileParh, const Color3B& color, GLubyte opacity); + void handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B& color, GLubyte opacity); + void handleImageRenderer(const std::string& fileParh, const Color3B& color, GLubyte opacity); void handleCustomRenderer(Node* renderer); void formarRenderers(); void addNewLine(); diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index e8d34cbe5a..5d31d49e35 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -307,7 +307,7 @@ Node* ScrollView::getChildByTag(int tag) return _innerContainer->getChildByTag(tag); } -Widget* ScrollView::getChildByName(const char *name) +Widget* ScrollView::getChildByName(const std::string& name) { return _innerContainer->getChildByName(name); } diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index e4c54efd9b..bd59e99228 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -298,7 +298,7 @@ public: virtual Node * getChildByTag(int tag) override; - virtual Widget* getChildByName(const char* name) override; + virtual Widget* getChildByName(const std::string& name) override; virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override; virtual void onTouchMoved(Touch *touch, Event *unusedEvent) override; diff --git a/cocos/ui/UITextField.cpp b/cocos/ui/UITextField.cpp index d91673348f..fcffdd379c 100644 --- a/cocos/ui/UITextField.cpp +++ b/cocos/ui/UITextField.cpp @@ -61,14 +61,14 @@ UICCTextField::~UICCTextField() { } -UICCTextField * UICCTextField::create(const char *placeholder, const char *fontName, float fontSize) +UICCTextField * UICCTextField::create(const std::string& placeholder, const std::string& fontName, float fontSize) { UICCTextField *pRet = new UICCTextField(); if(pRet && pRet->initWithPlaceHolder("", fontName, fontSize)) { pRet->autorelease(); - if (placeholder) + if (!placeholder.empty()) { pRet->setPlaceHolder(placeholder); } @@ -121,7 +121,7 @@ bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF *pSender) return false; } -void UICCTextField::insertText(const char * text, size_t len) +void UICCTextField::insertText(const char* text, size_t len) { std::string input_text = text; @@ -276,9 +276,9 @@ bool UICCTextField::isPasswordEnabled() return _passwordEnabled; } -void UICCTextField::setPasswordStyleText(const char* styleText) +void UICCTextField::setPasswordStyleText(const std::string& styleText) { - if (strlen(styleText) > 1) + if (styleText.length() > 1) { return; } @@ -290,10 +290,10 @@ void UICCTextField::setPasswordStyleText(const char* styleText) _passwordStyleText = styleText; } -void UICCTextField::setPasswordText(const char *text) +void UICCTextField::setPasswordText(const std::string& text) { std::string tempStr = ""; - int text_count = _calcCharCount(text); + int text_count = _calcCharCount(text.c_str()); int max = text_count; if (_maxLengthEnabled) diff --git a/cocos/ui/UITextField.h b/cocos/ui/UITextField.h index 4fe9239ce9..58309551d4 100644 --- a/cocos/ui/UITextField.h +++ b/cocos/ui/UITextField.h @@ -44,7 +44,7 @@ public: virtual void onEnter() override; // static - static UICCTextField* create(const char *placeholder, const char *fontName, float fontSize); + static UICCTextField* create(const std::string& placeholder, const std::string& fontName, float fontSize); // CCTextFieldDelegate virtual bool onTextFieldAttachWithIME(TextFieldTTF *pSender) override; @@ -65,8 +65,8 @@ public: int getCharCount(); void setPasswordEnabled(bool enable); bool isPasswordEnabled(); - void setPasswordStyleText(const char* styleText); - void setPasswordText(const char* text); + void setPasswordStyleText(const std::string& styleText); + void setPasswordText(const std::string& text); void setAttachWithIME(bool attach); bool getAttachWithIME(); void setDetachWithIME(bool detach); diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index bd3d4c7e27..14d7f63597 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -162,7 +162,7 @@ void Widget::setEnabled(bool enabled) } } -Widget* Widget::getChildByName(const char *name) +Widget* Widget::getChildByName(const std::string& name) { for (auto& child : _children) { @@ -171,7 +171,7 @@ Widget* Widget::getChildByName(const char *name) Widget* widgetChild = dynamic_cast(child); if (widgetChild) { - if (strcmp(widgetChild->getName(), name) == 0) + if (widgetChild->getName() == name) { return widgetChild; } @@ -824,14 +824,14 @@ const Vector2& Widget::getTouchEndPos() return _touchEndPos; } -void Widget::setName(const char* name) +void Widget::setName(const std::string& name) { _name = name; } -const char* Widget::getName() const +const std::string& Widget::getName() const { - return _name.c_str(); + return _name; } Widget::Type Widget::getWidgetType() const diff --git a/cocos/ui/UIWidget.h b/cocos/ui/UIWidget.h index 1ae9142b9a..84566cf351 100644 --- a/cocos/ui/UIWidget.h +++ b/cocos/ui/UIWidget.h @@ -227,7 +227,7 @@ public: * * @return a Widget object whose name equals to the input parameter */ - virtual Widget* getChildByName(const char* name); + virtual Widget* getChildByName(const std::string& name); virtual void visit(cocos2d::Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated) override; @@ -384,18 +384,18 @@ public: /** * Changes the name that is used to identify the widget easily. * - * @param A const char* that indentifies the widget. + * @param A const std::string that indentifies the widget. */ - void setName(const char* name); + void setName(const std::string& name); /** * Returns a name that is used to identify the widget easily. * * You can set tags to widget then identify them easily. * - * @return A const char* that identifies the widget. + * @return A const std::string that identifies the widget. */ - const char* getName() const; + const std::string& getName() const; /** * Returns a type that is widget's type