Skip to content

Commit 79128d8

Browse files
committed
Add note on removing array items
1 parent cea2146 commit 79128d8

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

README.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ Simple arrays are accessed via the :cpp:class:`ConfigDB::Array` class. All eleme
191191

192192
The :cpp:class:`ConfigDB::ObjectArray` type can be used for arrays of objects or unions. Default values are not currently supported for these.
193193

194+
.. important::
195+
196+
Be very careful when removing items from an array (via :cpp:func:`Array::removeItem`).
197+
Don't hold Object or Property references to items in the array during the operation as they may become invalid if the array ordering changes.
198+
Do not rely on the item index for removing multiple items since indices change when removeItem is called.
199+
200+
Do not rely on the item index for removal, but instead use the content of the object or item as criteria. For example:
201+
202+
.. code-block:: c++
203+
204+
for(unsigned index=0; index < array.getItemCount();) {
205+
auto item = array[index];
206+
if (want_to_delete(item)) {
207+
// This causes subsquent items to shift down one, so leave index where it is
208+
array.removeItem(index);
209+
} else {
210+
++index;
211+
}
212+
}
213+
194214

195215
Unions
196216
~~~~~~

src/include/ConfigDB/ArrayBase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class ArrayBase : public Object
3737
return getId() ? getArray().getCount() : 0;
3838
}
3939

40+
/*
41+
* @brief Remove an item from the array.
42+
* @note Be very careful dealing with `ObjectArray` item removal as all
43+
* items after this one will be shifted and their references no longer valid.
44+
*/
4045
bool removeItem(unsigned index)
4146
{
4247
return getArray().remove(index);

0 commit comments

Comments
 (0)