[fpc-pascal] Make TStringList.ExchangeItems virtual?
Michael Van Canneyt
michael at freepascal.org
Wed Feb 13 16:14:00 CET 2013
On Wed, 13 Feb 2013, Sven Barth wrote:
> On 13.02.2013 15:43, Bart wrote:
>> Is it at all feasible to make TStringList.ExchangeItems a virtual method?
>> 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.
> 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.
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()..
More information about the fpc-pascal