Skip to content

Commit ab4b241

Browse files
JohnCoconutminggo
authored andcommitted
refactor CCIMEDispatcher (#19688)
* remove superfluous destructor * use range-based for loop * correct some documentation
1 parent edad341 commit ab4b241

File tree

3 files changed

+17
-58
lines changed

3 files changed

+17
-58
lines changed

cocos/base/CCIMEDelegate.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ extern const std::string CC_DLL STD_STRING_EMPTY;
4646
/**
4747
* Keyboard notification event type.
4848
*/
49-
typedef struct
49+
struct IMEKeyboardNotificationInfo
5050
{
5151
Rect begin; // the soft keyboard rectangle when animation begins
5252
Rect end; // the soft keyboard rectangle when animation ends
5353
float duration; // the soft keyboard animation duration
54-
} IMEKeyboardNotificationInfo;
54+
};
5555

5656
/**
5757
*@brief Input method editor delegate.
@@ -60,21 +60,21 @@ class CC_DLL IMEDelegate
6060
{
6161
public:
6262
/**
63-
* Default constructor.
63+
* Destructor.
6464
* @js NA
6565
* @lua NA
6666
*/
6767
virtual ~IMEDelegate();
6868

6969
/**
70-
* Default destructor.
70+
* Attach the delegate to IME. Return true if succesful.
7171
* @js NA
7272
* @lua NA
7373
*/
7474
virtual bool attachWithIME();
7575

7676
/**
77-
* Determine whether the IME is detached or not.
77+
* Detach the delegate from IME. Return true if succesful.
7878
* @js NA
7979
* @lua NA
8080
*/
@@ -166,6 +166,7 @@ class CC_DLL IMEDelegate
166166

167167
protected:
168168
/**
169+
* Default constructor.
169170
* @js NA
170171
* @lua NA
171172
*/

cocos/base/CCIMEDispatcher.cpp

+10-52
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,19 @@ typedef std::list< IMEDelegate * >::iterator DelegateIter;
6363
// Delegate List manage class
6464
//////////////////////////////////////////////////////////////////////////
6565

66-
class IMEDispatcher::Impl
66+
struct IMEDispatcher::Impl
6767
{
68-
public:
69-
Impl()
68+
Impl() : _delegateWithIme(nullptr)
7069
{
7170
}
7271

73-
~Impl()
74-
{
75-
76-
}
77-
78-
void init()
79-
{
80-
_delegateWithIme = 0;
81-
}
82-
8372
DelegateIter findDelegate(IMEDelegate* delegate)
8473
{
8574
DelegateIter end = _delegateList.end();
8675
for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter)
8776
{
8877
if (delegate == *iter)
89-
{
9078
return iter;
91-
}
9279
}
9380
return end;
9481
}
@@ -104,7 +91,6 @@ class IMEDispatcher::Impl
10491
IMEDispatcher::IMEDispatcher()
10592
: _impl(new IMEDispatcher::Impl)
10693
{
107-
_impl->init();
10894
}
10995

11096
IMEDispatcher::~IMEDispatcher()
@@ -137,11 +123,8 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
137123
{
138124
CC_BREAK_IF(! _impl || ! delegate);
139125

140-
DelegateIter end = _impl->_delegateList.end();
141-
DelegateIter iter = _impl->findDelegate(delegate);
142-
143126
// if pDelegate is not in delegate list, return
144-
CC_BREAK_IF(end == iter);
127+
CC_BREAK_IF(_impl->findDelegate(delegate) == _impl->_delegateList.end());
145128

146129
if (_impl->_delegateWithIme)
147130
{
@@ -155,10 +138,9 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
155138

156139
// detach first
157140
IMEDelegate * oldDelegate = _impl->_delegateWithIme;
158-
_impl->_delegateWithIme = 0;
159141
oldDelegate->didDetachWithIME();
160142

161-
_impl->_delegateWithIme = *iter;
143+
_impl->_delegateWithIme = delegate;
162144
delegate->didAttachWithIME();
163145
}
164146
ret = true;
@@ -168,7 +150,7 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
168150
// delegate hasn't attached to IME yet
169151
CC_BREAK_IF(! delegate->canAttachWithIME());
170152

171-
_impl->_delegateWithIme = *iter;
153+
_impl->_delegateWithIme = delegate;
172154
delegate->didAttachWithIME();
173155
ret = true;
174156
} while (0);
@@ -204,8 +186,6 @@ void IMEDispatcher::removeDelegate(IMEDelegate* delegate)
204186
DelegateIter end = _impl->_delegateList.end();
205187
CC_BREAK_IF(end == iter);
206188

207-
if (_impl->_delegateWithIme)
208-
209189
if (*iter == _impl->_delegateWithIme)
210190
{
211191
_impl->_delegateWithIme = 0;
@@ -268,9 +248,7 @@ const std::string& IMEDispatcher::getContentText()
268248

269249
bool IMEDispatcher::isAnyDelegateAttachedWithIME() const
270250
{
271-
if (!_impl)
272-
return false;
273-
return _impl->_delegateWithIme != nullptr;
251+
return _impl ? _impl->_delegateWithIme != nullptr : false;
274252
}
275253

276254
//////////////////////////////////////////////////////////////////////////
@@ -281,15 +259,10 @@ void IMEDispatcher::dispatchKeyboardWillShow(IMEKeyboardNotificationInfo& info)
281259
{
282260
if (_impl)
283261
{
284-
IMEDelegate * delegate = nullptr;
285-
DelegateIter last = _impl->_delegateList.end();
286-
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
262+
for (IMEDelegate *delegate : _impl->_delegateList)
287263
{
288-
delegate = *(first);
289264
if (delegate)
290-
{
291265
delegate->keyboardWillShow(info);
292-
}
293266
}
294267
}
295268
}
@@ -298,15 +271,10 @@ void IMEDispatcher::dispatchKeyboardDidShow(IMEKeyboardNotificationInfo& info)
298271
{
299272
if (_impl)
300273
{
301-
IMEDelegate * delegate = nullptr;
302-
DelegateIter last = _impl->_delegateList.end();
303-
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
274+
for (IMEDelegate *delegate : _impl->_delegateList)
304275
{
305-
delegate = *(first);
306276
if (delegate)
307-
{
308277
delegate->keyboardDidShow(info);
309-
}
310278
}
311279
}
312280
}
@@ -315,15 +283,10 @@ void IMEDispatcher::dispatchKeyboardWillHide(IMEKeyboardNotificationInfo& info)
315283
{
316284
if (_impl)
317285
{
318-
IMEDelegate * delegate = nullptr;
319-
DelegateIter last = _impl->_delegateList.end();
320-
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
286+
for (IMEDelegate *delegate : _impl->_delegateList)
321287
{
322-
delegate = *(first);
323288
if (delegate)
324-
{
325289
delegate->keyboardWillHide(info);
326-
}
327290
}
328291
}
329292
}
@@ -332,15 +295,10 @@ void IMEDispatcher::dispatchKeyboardDidHide(IMEKeyboardNotificationInfo& info)
332295
{
333296
if (_impl)
334297
{
335-
IMEDelegate * delegate = nullptr;
336-
DelegateIter last = _impl->_delegateList.end();
337-
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
298+
for (IMEDelegate *delegate : _impl->_delegateList)
338299
{
339-
delegate = *(first);
340300
if (delegate)
341-
{
342301
delegate->keyboardDidHide(info);
343-
}
344302
}
345303
}
346304
}

cocos/base/CCIMEDispatcher.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class CC_DLL IMEDispatcher
138138
private:
139139
IMEDispatcher();
140140

141-
class Impl;
141+
struct Impl;
142142
Impl * _impl;
143143
};
144144

0 commit comments

Comments
 (0)