[fpc-pascal] Make TStringList.ExchangeItems virtual?

Sven Barth pascaldragon at googlemail.com
Wed Feb 13 16:26:56 CET 2013

On 13.02.2013 16:14, Michael Van Canneyt wrote:
> On Wed, 13 Feb 2013, Sven Barth wrote:
>> On 13.02.2013 15:43, Bart wrote:
>>> Hi,
>>> Is it at all feasible to make TStringList.ExchangeItems a virtual
>>> method?
>>> Reason:
>>> In Lazarus we have a component (TValueListEditor) that has a
>>> TStringList descendant as one of it's properties.
>>> This particular list needs te bee in sync with another list of objects.
>>> For Insert, Delete, Exchange and Clear that is easily done.
>>> However sorting is a problem, because CustomSort calls QuickSort which
>>> internally uses ExchangeItem() and not Exchange (which is virtual).
>>> For now I just duplicated the QuickSort and replaced ExchangeItem with
>>> (the overridden) Exchange.
>>> This however has the distinct disadvantage of having duplicate code.
>> And it calls OnChanging and OnChange and notifies the observers each
>> time an item is swapped... (at least if the Sort is not done inside a
>> BeginUpdate/EndUpdate pair...)
> Currently it is not, in fact this should be done automatically.

Than this could be adjusted at least...

>> So in that case I'd prefer the "virtualisation" of ExchangeItems as
>> well...
> We can do that, but it wouldn't do much good, since it is private.
> So I guess we'd have to make it protected too.

I have silently assumed this :)

> The disadvantage is that making it virtual will make it slower, since an
> additional VMT lookup must be done. The reason that it is a separate
> routine is exactly to make it faster, otherwise quicksort could just
> have used Exchange()..

Exchange additionally contains checks and two further method calls that 
do complex stuff if BeginUpdate was not called, so one can argue what is 
faster here :)

We could maybe adjust the string list in so far that at the beginning of 
QuickSort we determine whether ExchangeItem was overridden, if it was we 
call the now virtual, protected ExchangeItem and if it was not overriden 
we call a private non virtual version (which could be called by the 
default implementation of ExchangeItem as well).

Just a stupid idea to make the code more complex :P


More information about the fpc-pascal mailing list