秋·风

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
一、mode模式
freepascal {$mode}模式有以下几种:
{$mode iso} 
{$mode objfpc}
{$mode delphi}
{$mode macpas}
{$mode extendedpascal} 
{$mode delphiunicode} 

fpc默认使用{$mode objfpc}
二、$modeswitch的使用
1、匿名函数:
在objfpc模式下使用delphi风格的匿名函数需要加上{$modeswitch anonymousfunctions}:

{$mode objfpc}{$modeswitch anonymousfunctions}

用{$mode delphi}模式,不需要使用{$modeswitch anonymousfunctions}就可以直接使用Delphi风格的匿名函数。

{ %OPT=-O3 }
program tw41155;

{$mode objfpc} {$modeswitch anonymousfunctions}
type
  pAdapter = ^Adapter;
  Adapter = record
    start: SizeUint;
    adaptee: procedure(p: pointer; a, b: SizeUint);
  end;

  procedure UseAdapter(ad: pAdapter; a, b: SizeUint);
  begin
    ad^.adaptee(nil, ad^.start + a, ad^.start + b);
  end;

var
  ad: Adapter;
  oops: boolean = false;

begin
  ad.start := 10;
  ad.adaptee :=
    procedure(p: pointer; a, b: SizeUint)
    begin
      if p <> nil then begin writeln('p = ', PtrUint(p), ', should be 0.'); oops := true; end;
      if a <> 110 then begin writeln('a = ', a, ', should be 110.'); oops := true; end;
      if b <> 1010 then begin writeln('b = ', b, ', should be 1010.'); oops := true; end;
    end;
  UseAdapter(@ad, 100, 1000);
  if oops then halt(1);
  WriteLn('ok');
end.

2、启用Delphi样式的函数引用:

program tw40031;{$mode objfpc}{$modeswitch functionreferences}{$modeswitch anonymousfunctions}
type Aoc0 = reference to procedure (aoc: array of const);
type Aoc1 = reference to procedure (var aoc: array of const);
type Aoc2 = reference to procedure (constref aoc: array of const);
type Aoc3 = reference to procedure (const aoc: array of const);
var
  t: aoc0;
  i: longint;
begin
  i := 0;
  t := procedure(aArgs: array of const) begin i:=length(aArgs); end;
  t([1, 'Hello']);
  if i <> 2 then
    halt(1);
end. 

 3、$modeswitch可用参数及简单说明

CLASS---delphi类模型
OBJPAS---加载objpas单元
RESULT---函数中的结果
PCHARTOSTRING---pchar 2字符串转换
CVAR---cvar变量指令
NESTEDCOMMENTS---嵌套注释
CLASSICPROCVARS---tp风格的procvar(无@需要)
MACPROCVARS---macpas风格程序
REPEATFORWARD---需要重复正向声明
POINTERTOPROCVAR---允许为过程变量分配指针
AUTODEREF---对struct进行自动解引用
INITFINAL---单元的初始化/最终化
ANSISTRINGS---默认情况下已打开分发
OUT---支持调用约定out
DEFAULTPARAMETERS---支持默认参数
HINTDIRECTIVE---支持提示指令
DUPLICATELOCALS---允许局部/段落具有重复的全局名称
PROPERTIES---允许属性
ALLOWINLINE---允许内联proc指令
EXCEPTIONS---允许与异常相关的关键字
OBJECTIVEC1---支持与Objective-C接口(1.0)
OBJECTIVEC2---支持与Objective-C(2.0)的接口
NESTEDPROCVARS---支持嵌套过程变量
NONLOCALGOTO---支持非本地gotos(如iso pascal)
ADVANCEDRECORDS---具有可见性部分、方法和属性的高级记录语法
ISOUNARYMINUS---类似于isopascal中的一元减号:与二进制减号/加号具有相同的优先级
SYSTEMCODEPAGE---默认情况下使用系统代码页作为编译器代码页,用系统代码页发出转发
FINALFIELDS---允许将字段声明为“final”,这意味着它们必须被初始化 在(class)构造函数中,从那时起是常量(与Java中的final字段相同)
UNICODESTRINGS---使$h+模式下的默认字符串类型为unicodestring而不是ansistring;同样,char变为unicodechar而不是ansichar
TYPEHELPERS---允许为所有支持的类型(基元类型、记录、类、接口)声明“类型助手”
CBLOCKS---支持http://en.wikipedia.org/wiki/Blocks_(C_语言_扩展名)
ISOIO---ISO兼容编译器要求的I/O
ISOPROGRAMPARAS---ISO兼容编译器要求的程序参数
ISOMOD---iso兼容编译器要求的mod操作
ARRAYOPERATORS---使用Delphi兼容的数组运算符,而不是自定义运算符(“+”)
MULTIHELPERS---助手可以同时出现在多个作用域中
ARRAYTODYNARRAY---常规数组可以隐式转换为动态数组
PREFIXEDATTRIBUTES---启用在它们所属的类型之前定义的属性
UNDERSCOREISSEPARATOR---可用作数字分组的分隔符
IMPLICITFUNCTIONSPECIALIZATION---试图通过从参数推断类型来专门化泛型函数
FUNCTIONREFERENCES---启用Delphi风格的函数引用
ANONYMOUSFUNCTIONS---启用Delphi风格的匿名函数
MULTILINESTRINGS---用“”表示的多行字符串已启用且有效

 三、H+模式:
{$H+}使$h+模式下的默认字符串类型为unicodestring而不是ansistring;同样,char变为unicodechar而不是ansichar。

posted on 2025-12-04 11:42  秋·风  阅读(28)  评论(0)    收藏  举报