Old-school Pascal question about how to cast variant record function parameters properly
I am trying to create a function with a variant record-type parameter that allows inline-casting or assignment, as such:
type rectype = ( VT_INT, VT_CHAR, VT_BOOL );
rec = record
case t : rectype of
VT_INT : ( i : integer );
VT_CHAR : ( c : char );
VT_BOOL : ( b : boolean );
end;
procedure handler( r : rec );
begin
case r.t of
VT_INT : { do something with r.i }
VT_CHAR : { do something with r.c }
VT_BOOL : { do something with r.b }
end;
end;
Now, the above "works" fine, as long as you take the time to do set up the variable manually, as such:
r.t := VT_INT;
r.i := 42;
handler( r );
But, I would like to abuse the typing system a bit, and attempt to do it inline (I'll explain why in a moment), along the lines of:
handler( rec( VT_INT, 42 ) );
A helper function would be fine, as well, and I've tried a couple of different methods to do this, which can be seen here (for brevity of this post):
http://pastie.org/private/glxhwbpsbbh5gtxju0uvxa
Now, for the reason: I am working on, and have indeed released a unit testing suite that is aiming to be as portable as Pascal itself (builds under FreePascal and Turbo Pascal 7 (yes, really)). I've already publicly released the open-source software (can't link yet, not enough rep), which include different functions for different types: isI(), isR(), isS(), isP(), isC(), etc. This repeats a lot of code, and I know there is a better way to do this. I believe there is a variant type supported by FPC and Delphi, which I can use via IFDEF directives on those platforms, but the real clincher is TP7, which I still want to support for obscene reasons.
The reason that 4 lines per function call to set up the record isn't really feasible is that as this is the user-facing API, and making testing that convoluted will just mean that no one will do it. A test set with the current API is straightforward with a single function call to perform each test, and I just hope it's possible to turn all of the several type-specific functions into something like:
is( VT_INT, SomeIntFunc( v ), 42, 'Test Name' );
开发者_运维百科
I am willing to go to great lengths to circumvent typing under TP7, including manipulating the stack myself in assembly, etc. But I'm hoping a modified version using syntax I'm just not familiar with will do the job.
So, old-school Pascal programmers (I know there must be some out there), any suggestions?
Free Pascal is no problem, since it supports Delphi constructs like (custom) variants, array of const interfaces and other tricks to deal with runtime typing (as long as it is limited to a overseeable number). Recent Delphi's and FPC support generics, and you could probably go a long way duplicate "boost" with it.
Doing it TP old school however is the hard part. It simply has nearly no untyped (or better: runtime typed) functionality.
The only thing I can think of is
procedure rec(vartype:Ttypeofvar;var value):tvariantrecord;
begin
rec.t:=vartype
case vartype of
vt_int: move (value,@rec.i,sizeof(integer));
..
..
end;
A working workaround, but not typesafe, since passing rec(varint,"something double") is possible. TP simply doesn't have much to work with, except the VAR formal parameter.
At this point you really have to ask yourself if crippling your frameworks for TP7 is really worth it. It has been dead for 16 years now. Let it rot in peace.
精彩评论