Skip to content

Commit ce633b8

Browse files
committed
refactor: use const iterators for QVector in signal connector
The `invalidate` method in `QwSignalConnector` iterates through a temporary `QVector` of `qw_signal_listener*` pointers. Previously, it used non-const iterators (`begin()` and `end()`). This change updates the iteration to use `constBegin()` and `constEnd()`. This is a best practice when the loop only reads from the container and does not modify its structure (e.g., adding or removing elements from `tmpList`). The elements pointed to by the iterators (`qw_signal_listener*`) are still modified (specifically, their `wl_list_remove` operation), but the `QVector` itself remains unchanged during the iteration. This refactoring improves code clarity and expresses the intent that the vector's contents are not being structurally altered during traversal. Influence: 1. Verify that `QwSignalConnector` instances can still be invalidated correctly. 2. Ensure that signals connected via `QwSignalConnector` are properly disconnected when the connector is invalidated. 3. Check for any memory leaks or crashes related to signal listener management. refactor: 在信号连接器中为 QVector 使用 const 迭代器 `QwSignalConnector` 的 `invalidate` 方法会遍历一个临时的 `QVector`, 其中存储着 `qw_signal_listener*` 指针。此前,它使用了非 const 迭 代器 (`begin()` 和 `end()`)。此更改将迭代更新为使用 `constBegin()` 和 `constEnd()`。当循环只从容器读取数据,而不修改其结构(例如,不 向 `tmpList` 添加或删除元素)时,这是一种最佳实践。迭代器指向的元素 (`qw_signal_listener*`) 仍然会被修改(特别是它们的 `wl_list_remove` 操 作),但在迭代过程中 `QVector` 本身不会被结构性改变。此重构提高了代码清 晰度,并表达了在遍历过程中不会对向量内容进行结构性更改的意图。 Influence: 1. 验证 `QwSignalConnector` 实例是否仍能正确失效。 2. 确保通过 `QwSignalConnector` 连接的信号在连接器失效时能正确断开。 3. 检查是否存在与信号监听器管理相关的内存泄漏或崩溃。
1 parent bf3be27 commit ce633b8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

qwlroots/src/util/qwsignalconnector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ class qw_signal_connector
117117
}
118118
void disconnect(wl_signal *signal) {
119119
auto tmpList = listenerList;
120-
auto begin = tmpList.begin();
121-
while (begin != tmpList.end()) {
120+
auto begin = tmpList.constBegin();
121+
while (begin != tmpList.constEnd()) {
122122
qw_signal_listener *l = *begin;
123123
++begin;
124124

@@ -129,8 +129,8 @@ class qw_signal_connector
129129
void invalidate() {
130130
QVector<qw_signal_listener*> tmpList;
131131
tmpList.swap(listenerList);
132-
auto begin = tmpList.begin();
133-
while (begin != tmpList.end()) {
132+
auto begin = tmpList.constBegin();
133+
while (begin != tmpList.constEnd()) {
134134
qw_signal_listener *l = *begin;
135135
wl_list_remove(&l->l.link);
136136
++begin;

0 commit comments

Comments
 (0)