关于如何正确转换变体记录函数参数的老派 Pascal 问题

发布于 2024-09-18 15:09:36 字数 1479 浏览 3 评论 0原文

我正在尝试创建一个带有允许内联转换或赋值的变体记录类型参数的函数,如下所示:

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;

现在,只要您花时间手动设置变量,上面的“工作”就可以了,如下所示这样:

 r.t := VT_INT;
 r.i := 42;
 handler( r );

但是,我想稍微滥用一下打字系统,并尝试内联执行(我稍后会解释原因),大致如下:

 handler( rec( VT_INT, 42 ) );

辅助函数也很好,而且我'我尝试了几种不同的方法来执行此操作,可以在此处查看(为了本文的简洁性):

http: //pastie.org/private/glxhwbpsbbh5gtxju0uvxa

现在,原因是:我正在研究,并且确实发布了一个单元测试套件,旨在像 Pascal 本身一样可移植(在 FreePascal 和 Turbo Pascal 7 下构建) (是的,真的))。我已经公开发布了开源软件(还无法链接,没有足够的代表),其中包括针对不同类型的不同函数:isI()、isR()、isS()、isP()、isC()等等。这重复了很多代码,我知道有更好的方法来做到这一点。我相信 FPC 和 Delphi 支持一种变体类型,我可以在这些平台上通过 IFDEF 指令使用它,但真正的关键是 TP7,我仍然想出于淫秽的原因支持它。

每个函数调用 4 行来设置记录实际上并不可行,因为这是面向用户的 API,进行如此复杂的测试意味着没有人会这样做。使用当前 API 的测试集很简单,只需一个函数调用即可执行每个测试,我只是希望能够将所有几个特定于类型的函数变成类似这样的东西:

is( VT_INT, SomeIntFunc( v ), 42, 'Test Name' );

我愿意不遗余力地规避输入在 TP7 下,包括在汇编中自己操作堆栈等。但我希望使用我不熟悉的语法的修改版本能够完成这项工作。

那么,老派的 Pascal 程序员(我知道肯定有一些)有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2024-09-25 15:09:36

Free Pascal 没有问题,因为它支持 Delphi 构造,如(自定义)变体、const 接口数组和其他处理运行时类型的技巧(只要它限制在可监督的数量内)。最近的 Delphi 和 FPC 支持泛型,您可能可以用它来重复“提升”。

然而,在老派中做到这一点是困难的部分。它几乎没有无类型(或更好:运行时类型)功能。

我唯一能想到的是

 procedure rec(vartype:Ttypeofvar;var value):tvariantrecord;
 begin
   rec.t:=vartype
   case vartype of 
      vt_int: move (value,@rec.i,sizeof(integer));
      ..
      ..
 end;

一个可行的解决方法,但不是类型安全的,因为传递 rec(varint,"something double") 是可能的。除了 VAR 形式参数之外,TP 没有太多可以使用的东西。

此时,您确实必须问自己,为 TP7 破坏框架是否真的值得。到现在已经死了16年了。让它安静地腐烂。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文