fpc和delphi的record一些新语法

fpc和delphi的record一些新语法

注意:delphi7等老版本是不支持的,所以称之为新语法。

新法语可能始于delphi2006吧,fpc的支持不知始于哪个版本。

1)常量

type
  TConstVar = record
  const
    AddServer = 'addserver';
    RemoveServer = 'removeserver';
    ServerList = 'serverlist';

Success = 'success'; Msg = 'message'; IpHash = 'iphash'; Random = 'random'; RoundRobin = 'roundrobin'; end;

2)类方法

  TServer = record // 一个服务
  var
    Protocol: string; // 通讯协议:http赋值http或https; websocket赋值url
    Name: string; // 服务名
    Prefix: string; // 前缀:http://,https://...
    Ip: string; // ip或域名
    Port: Integer; // 端口
  public
    class procedure ToJson(AServer: TServer; var AJsonObject: TJsonO); static;
    // 序列TServer
    class procedure FromJson(var AJsonObject: TJsonO; var AServer: TServer);
      static; // 还原TServer
    class function ServerList: string; static; // 代理服务列表
    class function Add(const AServer: TServer): string; static; // 新增一个代理服务
    class function Remove(const AServer: TServer): string; static; // 移除一个代理服务
    class function IpHash(const AIp: string): TServer; static; // ip_hash算法
    class function RoundRobin: TServer; static; // 轮转算法
    class function Random: TServer; static; // 随机算法
    class procedure GenerateDict; static; // 生成代理服务字典
    class function GetServer(const AContext: TContext): TServer; static;
    // 通过负载均衡算法,获取一个服务
    class function Forwarding(const AContext: TContext; AContentType: string)
      : Cardinal; static; // 转发
  end;

 

3)嵌套record

TConfig = record
  public
    ApiServer: record
      Port: string;
      HTTPQueueLength:  Integer;
      ChildThreadCount: Integer;
    end;
    WsServer: record
      Port: string;
      HTTPQueueLength:  Integer;
      ChildThreadCount: Integer;
    end;
    Redis: record
      Host:         string;
      Port:         Integer;
      PassWord:     string;
      InitSize:     Integer;
      TimeOut:      Integer;
      ReadTimeOut:  Integer;
    end;

 

posted @ 2025-04-25 19:51  delphi中间件  阅读(69)  评论(0)    收藏  举报