Signals and Slots
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Nov 3 09:49:33 PST 2006
Sean Kelly wrote:
> Frits van Bommel wrote:
>> Sean Kelly wrote:
>>> Frits van Bommel wrote:
>>>>
>>>> If we replace this line by:
>>>> { slots_idx--;
>>>> dg = slots[slots_index];
>>>> slots[slots_idx] = null;
>>>> then we don't need to check for empty slots in emit():
>>>> If slots.length == slots_idx, we can be sure the array is full.
>>>>
>>>> (Unless there's a requirement for the slots to be
>>>> called in any particular order?)
>>>
>>> No, but the sequence may need to be modified while emit is
>>> processing. Consider a slot that disconnects itself when it receives
>>> a signal. But
>>
>> Yeah, that can create some extra headaches, of course.
>
> By the way, I only mentioned this because of the foreach restrictions on
> modifying a sequence while it is being processed. If this restriction
> were not present, your example would be fine as-is. There's also
No it wouldn't (be fine). Even if that restriction was not in place,
it'd probably still skip the last slot if the current slot removed
itself (since it would be put into a place in the array the loop
considered to be done).
Of course, this could be fixed by checking whether the slot was modified
after it returns, and rerunning it if it was. Preferably by decrementing
the loop counter, so it'd still work if the next slot does the same or
the current slot _was_ the last slot.
You'd need to use a for-loop for that though, not foreach. (to have a
modifiable index, and because the end of the sequence can change)
More information about the Digitalmars-d
mailing list