From 71304b302f567b968ec60484122d3860f41a020a Mon Sep 17 00:00:00 2001 From: John Zhang Date: Fri, 10 May 2019 15:13:11 +0800 Subject: [PATCH] refactor CCIMEDispatcher * remove superfluous destructor * use range-based for loop * correct some documentation --- cocos/base/CCIMEDelegate.h | 11 +++--- cocos/base/CCIMEDispatcher.cpp | 62 ++++++---------------------------- cocos/base/CCIMEDispatcher.h | 2 +- 3 files changed, 17 insertions(+), 58 deletions(-) diff --git a/cocos/base/CCIMEDelegate.h b/cocos/base/CCIMEDelegate.h index 3c73ba2d96f6..8c10edcb527a 100644 --- a/cocos/base/CCIMEDelegate.h +++ b/cocos/base/CCIMEDelegate.h @@ -46,12 +46,12 @@ extern const std::string CC_DLL STD_STRING_EMPTY; /** * Keyboard notification event type. */ -typedef struct +struct IMEKeyboardNotificationInfo { Rect begin; // the soft keyboard rectangle when animation begins Rect end; // the soft keyboard rectangle when animation ends float duration; // the soft keyboard animation duration -} IMEKeyboardNotificationInfo; +}; /** *@brief Input method editor delegate. @@ -60,21 +60,21 @@ class CC_DLL IMEDelegate { public: /** - * Default constructor. + * Destructor. * @js NA * @lua NA */ virtual ~IMEDelegate(); /** - * Default destructor. + * Attach the delegate to IME. Return true if succesful. * @js NA * @lua NA */ virtual bool attachWithIME(); /** - * Determine whether the IME is detached or not. + * Detach the delegate from IME. Return true if succesful. * @js NA * @lua NA */ @@ -166,6 +166,7 @@ class CC_DLL IMEDelegate protected: /** + * Default constructor. * @js NA * @lua NA */ diff --git a/cocos/base/CCIMEDispatcher.cpp b/cocos/base/CCIMEDispatcher.cpp index 85cafb1a3cef..8afd81a6e166 100644 --- a/cocos/base/CCIMEDispatcher.cpp +++ b/cocos/base/CCIMEDispatcher.cpp @@ -63,32 +63,19 @@ typedef std::list< IMEDelegate * >::iterator DelegateIter; // Delegate List manage class ////////////////////////////////////////////////////////////////////////// -class IMEDispatcher::Impl +struct IMEDispatcher::Impl { -public: - Impl() + Impl() : _delegateWithIme(nullptr) { } - ~Impl() - { - - } - - void init() - { - _delegateWithIme = 0; - } - DelegateIter findDelegate(IMEDelegate* delegate) { DelegateIter end = _delegateList.end(); for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter) { if (delegate == *iter) - { return iter; - } } return end; } @@ -104,7 +91,6 @@ class IMEDispatcher::Impl IMEDispatcher::IMEDispatcher() : _impl(new IMEDispatcher::Impl) { - _impl->init(); } IMEDispatcher::~IMEDispatcher() @@ -137,11 +123,8 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate) { CC_BREAK_IF(! _impl || ! delegate); - DelegateIter end = _impl->_delegateList.end(); - DelegateIter iter = _impl->findDelegate(delegate); - // if pDelegate is not in delegate list, return - CC_BREAK_IF(end == iter); + CC_BREAK_IF(_impl->findDelegate(delegate) == _impl->_delegateList.end()); if (_impl->_delegateWithIme) { @@ -155,10 +138,9 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate) // detach first IMEDelegate * oldDelegate = _impl->_delegateWithIme; - _impl->_delegateWithIme = 0; oldDelegate->didDetachWithIME(); - _impl->_delegateWithIme = *iter; + _impl->_delegateWithIme = delegate; delegate->didAttachWithIME(); } ret = true; @@ -168,7 +150,7 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate) // delegate hasn't attached to IME yet CC_BREAK_IF(! delegate->canAttachWithIME()); - _impl->_delegateWithIme = *iter; + _impl->_delegateWithIme = delegate; delegate->didAttachWithIME(); ret = true; } while (0); @@ -204,8 +186,6 @@ void IMEDispatcher::removeDelegate(IMEDelegate* delegate) DelegateIter end = _impl->_delegateList.end(); CC_BREAK_IF(end == iter); - if (_impl->_delegateWithIme) - if (*iter == _impl->_delegateWithIme) { _impl->_delegateWithIme = 0; @@ -268,9 +248,7 @@ const std::string& IMEDispatcher::getContentText() bool IMEDispatcher::isAnyDelegateAttachedWithIME() const { - if (!_impl) - return false; - return _impl->_delegateWithIme != nullptr; + return _impl ? _impl->_delegateWithIme != nullptr : false; } ////////////////////////////////////////////////////////////////////////// @@ -281,15 +259,10 @@ void IMEDispatcher::dispatchKeyboardWillShow(IMEKeyboardNotificationInfo& info) { if (_impl) { - IMEDelegate * delegate = nullptr; - DelegateIter last = _impl->_delegateList.end(); - for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first) + for (IMEDelegate *delegate : _impl->_delegateList) { - delegate = *(first); if (delegate) - { delegate->keyboardWillShow(info); - } } } } @@ -298,15 +271,10 @@ void IMEDispatcher::dispatchKeyboardDidShow(IMEKeyboardNotificationInfo& info) { if (_impl) { - IMEDelegate * delegate = nullptr; - DelegateIter last = _impl->_delegateList.end(); - for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first) + for (IMEDelegate *delegate : _impl->_delegateList) { - delegate = *(first); if (delegate) - { delegate->keyboardDidShow(info); - } } } } @@ -315,15 +283,10 @@ void IMEDispatcher::dispatchKeyboardWillHide(IMEKeyboardNotificationInfo& info) { if (_impl) { - IMEDelegate * delegate = nullptr; - DelegateIter last = _impl->_delegateList.end(); - for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first) + for (IMEDelegate *delegate : _impl->_delegateList) { - delegate = *(first); if (delegate) - { delegate->keyboardWillHide(info); - } } } } @@ -332,15 +295,10 @@ void IMEDispatcher::dispatchKeyboardDidHide(IMEKeyboardNotificationInfo& info) { if (_impl) { - IMEDelegate * delegate = nullptr; - DelegateIter last = _impl->_delegateList.end(); - for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first) + for (IMEDelegate *delegate : _impl->_delegateList) { - delegate = *(first); if (delegate) - { delegate->keyboardDidHide(info); - } } } } diff --git a/cocos/base/CCIMEDispatcher.h b/cocos/base/CCIMEDispatcher.h index 6b48ea9d31d2..bb6aa10fa89c 100644 --- a/cocos/base/CCIMEDispatcher.h +++ b/cocos/base/CCIMEDispatcher.h @@ -138,7 +138,7 @@ class CC_DLL IMEDispatcher private: IMEDispatcher(); - class Impl; + struct Impl; Impl * _impl; };