您可以使用宏和包含的组合来实现类似于令牌粘贴的功能...
ACME_Integer.inc
const ACME_1 = ACME_3;
const ACME_2 = pred(1 shl ACME_1);
type ACME_0 = 0..ACME_2;
//clear the passed in parameters so they can be reused in the next inclusion
{$undef ACME_0}{$undef ACME_1}{$undef ACME_2}{$undef ACME_3}
somefile.pas
{$macro on}
{$define ACME_0:=TAllocationPatchIndex}
{$define ACME_1:=TAllocationPatchIndex_bits}
{$define ACME_2:=TAllocationPatchIndex_high}
{$define ACME_3:=16}
{$Include ACME_Integer.inc}
{$define ACME_0:=TAllocationId}
{$define ACME_1:=TAllocationId_bits}
{$define ACME_2:=TAllocationId_high}
{$define ACME_3:=28}
{$Include ACME_Integer.inc}
扩展到
const TAllocationPatchIndex_bits = 16;
const TAllocationPatchIndex_high = pred(1 shl TAllocationPatchIndex_bits);
type TAllocationPatchIndex= 0..TAllocationPatchIndex_high;
const TAllocationId_bits = 28;
const TAllocationId_high = pred(1 shl TAllocationId_bits);
type TAllocationId= 0..TAllocationId_high;
虽然此示例使用的代码多于节省的代码,但您显然可以看到包含文件可能是相当复杂的重复代码,需要多次自定义。
更新 - 使用命名/可选参数的更复杂的宏
ACME_Integer.inc
{$ifdef ACME_packed_array}
{$undef ACME_array}
{$define ACME_array:=ACME_packed_array}
{$undef ACME_array_decl}
{$define ACME_array_decl:=packed array}
{$else}
{$ifdef ACME_bitpacked_array}
{$undef ACME_array}
{$define ACME_array:=ACME_bitpacked_array}
{$undef ACME_array_decl}
{$define ACME_array_decl:=bitpacked array}
{$else}
{$undef ACME_array_decl}
{$define ACME_array_decl:=array}
{$endif}
{$endif}
{$ifndef ACME_array_type}
{$define ACME_array_type:=byte}
{$endif}
{$ifdef ACME_bitconst}
// we want to keep the X_bits constant
const ACME_bitconst = ACME_bits; //X_bits
{$ifdef ACME_high}
// we want to keep the X_high constant
const ACME_high = pred(1 shl ACME_bitconst); //X_high
type ACME_type = 0..ACME_high; //X
{$else}
// we don't care about keeping the X_high constant
type ACME_type = 0..pred(1 shl ACME_bitconst); //X
{$endif}
{$else}
{$ifdef ACME_high}
// we don't care about keeping the X_bits constant
const ACME_high = pred(1 shl ACME_bits); //X_high
type ACME_type = 0..ACME_high; //X
{$else}
// we don't care about keeping the X_high or X_bits constants
type ACME_type = 0..pred(1 shl ACME_bits); //X
{$endif}
{$endif}
{$ifdef ACME_array}
type ACME_array = ACME_array_decl [ACME_type] of ACME_array_type;
{$ifdef ACME_array_length}
const ACME_array_length = sizeof(ACME_array);
{$endif}
{$ifdef ACME_array_pointer}
type ACME_array_pointer=^ACME_array;
{$endif}
{$undef ACME_array}
{$endif}
{$undef ACME_type}
{$undef ACME_bitconst}
{$undef ACME_high}
{$undef ACME_bits}
{$undef ACME_packed_array}
{$undef ACME_bitpacked_array}
{$undef ACME_array}
{$undef ACME_array_pointer}
{$undef ACME_array_type}
{$undef ACME_array_decl}
{$undef ACME_array_length}
somefile.pas
{$macro on}
{$define ACME_type:=TSha256IndexSliceMagic}
{$define ACME_bitconst:=TSha256IndexSliceMagic_bits}
{$define ACME_high:=TSha256IndexSliceMagic_high}
{$define ACME_bits:=17}
{$Include ACME_Integer.inc}
{$define ACME_type:=TSha256DataBufferIndex}
{$define ACME_bitconst:=TSha256DataBufferIndex_bits}
{$define ACME_high:=TSha256DataBufferIndex_high}
{$define ACME_bits:=9}
{$define ACME_packed_array:=TSha256DataBuffer}
{$define ACME_array_pointer:=PSha256DataBuffer}
{$Include ACME_Integer.inc}
扩展到
const TSha256IndexSliceMagic_bits = 17;
const TSha256IndexSliceMagic_high = pred(1 shl TSha256IndexSliceMagic_bits);
type TSha256IndexSliceMagic= 0..TSha256IndexSliceMagic_high;
const TSha256DataBufferIndex_bits = 9;
const TSha256DataBufferIndex_high = pred(1 shl TSha256DataBufferIndex_bits);
type TSha256DataBufferIndex= 0..TSha256DataBufferIndex_high;
type TSha256DataBuffer = packed array [TSha256DataBufferIndex] of byte;
type PSha256DataBuffer = ^TSha256DataBuffer;