A Magical Gathering – Part 2

From Part 1:

take an enumerator, mix it with a class helper and pour it over an invokeable custom variant

There it is again. What is he just talking about?

Well, it took me a while to find that thing, as it isn’t mentioned very often. But it turned out to be exactly what I needed. So I dived into the sources and figured out what TInvokeableCustomVariant is all about and how to use it. And even the help was in this case, um,  helpful: RAD Studio Help

It turned out that you can use such variants just like classes with properties and methods. Realizing this it sprang directly to my mind: if our enumerator can return such a variant instead of that semi-usefull record index, we can access the record fields like properties:

That was the kind of code I was looking for!

As the class helper seemed to be able to cope with a little bit more, I made it responsible for returning the appropriate variant. The changes were made quickly: the enumerator’s Current property had to be a Variant and the class helper got an additional property CurrentRec also of type Variant, which is used by the enumerator’s GetCurrent method.

You need a couple of things to make your own TInvokeableVariantType descendant work. Obviously we need that descendant:

We will only have one instance of that class, so we need a record type for storing the variant data:

This is a simplified form of the TVarData record declared in system.pas and as we only have to store a reference to the dataset it can be kept as simple as possible.

At last we need a global variable holding that instance of TVarDataRecordType, a function returning the VarType of that instance and another function creating a Variant of that type.

The Clear and Copy methods of TVarDataRecordType are pretty simple and just call predefined methods from TCustomVariantType. GetProperty and SetProperty is where the work is done:

The only thing left to do is the implementation of GetCurrentRec from the class helper:

That’s it! Now we can write code like:


Magic, isn’t it?

You can download the complete sources from CodeCentral: 25386

Author: Uwe Raabe

Addicted to Pascal/Delphi since the late 70's