如何编写Delphi编译时函数 [英] How to write Delphi compile-time functions

查看:303
本文介绍了如何编写Delphi编译时函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi - 可以为编译时使用可执行的const和var声明编写自己的编译时函数



标准Delphi lib包含用于常量初始化的例如Ord(),Chr(),Trunc(),Round(),High()等。 >

我可以自己编写,在编译时执行例程,并将结果用作常量吗?

解决方案

你不能写自己的内在功能。因为这需要编译器魔法。

但是可能还有其他选项来实现你的目标。



预处理器

唯一的方法是使用预处理器。

有几个: http://wiki.delphi-jedi.org/wiki/JEDI_Pre_Processor



Delphi预处理器
http://sourceforge.net/p/dpp32/wiki /家/历史



Andreas Hausladen刚刚开了自己的工作,在这方面。

这不是一个预处理器,但是一个语言扩展器。

https://github.com/ahausladen/DLangExtensions



预处理器的问题是它会杀死原始(预处理)源代码和Delphi编译的源代码之间的链接。

这意味着你不会有原始来源的调试信息。

(除非您重写地图文件)



内联

根据您想要的内容,您可以使用内联函数实现与内在函数几乎相同的效率。
请参阅: http://stackoverflow.com/a/6401833/650492



使用内在函数构造语句

如果您有一个由内在函数组成的代码块,则完整的结果将在编译时进行评估,总体构造工作就好像是内在函数一样。



请注意以下(愚蠢)示例:

 函数FitsInRegister< T>:布尔值一致; 
begin
如果[tkString,tkUString]中的GetTypeKind(T)结果为:= false
else
{$ IFDEF CPU32BITS}
结果:= SizeOf(T) < = 4;
{$ ELSEIF CPU64BITS}
结果:= SizeOf(T)<= 8;
{$ ENDIF}
end;

因为它是内联的,它只使用内在函数(和编译器指令),该函数将被解析在compiletime到一个常量,不生成任何代码。


Delphi - can I write my own compile-time functions for const and var declarations, executable at compiler time.

Standard Delphi lib contain routines like Ord(), Chr(), Trunc(), Round(), High() etc, used for constant initialization.

Can I write my own, to execute routine at compile-time and use the result as constant?

解决方案

You cannot write your own intrinsic functions. Because that requires compiler magic.
However there may be other options to achieve your goal.

Preprocessor
The only way is to use a preprocessor.
There are several: http://wiki.delphi-jedi.org/wiki/JEDI_Pre_Processor

The Delphi preprocessor http://sourceforge.net/p/dpp32/wiki/Home/history

Andreas Hausladen has just open sourced his own work in this respect.
It's not really a preprocessor, but a language extender.
https://github.com/ahausladen/DLangExtensions

The problem with preprocessors is that it kills the link between the original (prior to pre-processing) source code and the source code that Delphi compiles.
This means that you will not have debug info for your original source.
(unless you rewrite the map file).

Inlining
Depending on what you want to do you can use inlining to achieve almost the same efficiency as an intrinsic function. See: http://stackoverflow.com/a/6401833/650492

Construct your statements using intrinsic functions
If you have a code block consisting of instrinsic functions, the complete result will be evaluated at compile time, making the total construct work as if it was a intrinsic function.

Note the following (silly) example:

function FitsInRegister<T>: Boolean; inline;
begin
  if GetTypeKind(T) in [tkString, tkUString] then result:= false
  else 
  {$IFDEF CPU32BITS}
  Result:= SizeOf(T) <= 4;
  {$ELSEIF CPU64BITS}
  Result:= SizeOf(T) <= 8;
  {$ENDIF}
end;

Because it is inline and it only uses intrinsic functions (and compiler directives), the function will be resolved at compiletime to a constant and not generate any code.

这篇关于如何编写Delphi编译时函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆