user3764855
user3764855

Reputation: 297

How can I use Macros in Delphi?

How can I use Macros in Delphi? This feature is available in FPC for quite some time now. Could it be done with pre or post build events?

Example:

{$define sum:=a:=a+b;}  

Upvotes: 1

Views: 6625

Answers (3)

oOo
oOo

Reputation: 291

This is limited macro functionality.

It's an example of routines who's code is implemented just once and then re-used to implement different parameter types.

It can be done as follows:

uDynArrays.pas example:

unit uDynArrays;

interface

type
  TDynIntArray = array of Integer;
  TDynSingleArray = array of Single;
  TDynDoubleArray = array of Double;

{ TDynIntArray }
function AsDynArray(const AValues: array of Integer): TDynIntArray; overload;
function IsIn(AValue: Integer; const AValues: array of Integer): Boolean; overload;
function GetIndex(AValue: Integer; const AValues: array of Integer): Integer; overload;
function FindMinMaxValue(const AnArray: array of Integer; out AMin, AMax: Integer): Boolean; overload;
function FindMinValue(const AnArray: array of Integer; out AMin: Integer): Boolean; overload;
function FindMaxValue(const AnArray: array of Integer; out AMax: Integer): Boolean; overload;

{ TDynSingleArray }
function AsDynArray(const AValues: array of Single): TDynSingleArray; overload;
function IsIn(AValue: Single; const AValues: array of Single): Boolean; overload;
function GetIndex(AValue: Single; const AValues: array of Single): Integer; overload;
function FindMinMaxValue(const AnArray: array of Single; out AMin, AMax: Single): Boolean; overload;
function FindMinValue(const AnArray: array of Single; out AMin: Single): Boolean; overload;
function FindMaxValue(const AnArray: array of Single; out AMax: Single): Boolean; overload;

{ TDynDoubleArray }
function AsDynArray(const AValues: array of Double): TDynDoubleArray; overload;
function IsIn(AValue: Double; const AValues: array of Double): Boolean; overload;
function GetIndex(AValue: Double; const AValues: array of Double): Double; overload;
function FindMinMaxValue(const AnArray: array of Double; out AMin, AMax: Double): Boolean; overload;
function FindMinValue(const AnArray: array of Double; out AMin: Double): Boolean; overload;
function FindMaxValue(const AnArray: array of Double; out AMax: Double): Boolean; overload;

implementation

function AsDynArray(const AValues: array of Integer): TDynIntArray;
begin
  SetLength(Result, Length(AValues));
  if Length(AValues) > 0 then
    system.Move(AValues[Low(AValues)], Result[0], Length(AValues) * SizeOf(Integer));
end;

function AsDynArray(const AValues: array of Single): TDynSingleArray;
begin
  SetLength(Result, Length(AValues));
  if Length(AValues) > 0 then
    system.Move(AValues[Low(AValues)], Result[0], Length(AValues) * SizeOf(Single));
end;

function AsDynArray(const AValues: array of Double): TDynDoubleArray;
begin
  SetLength(Result, Length(AValues));
  if Length(AValues) > 0 then
    system.Move(AValues[Low(AValues)], Result[0], Length(AValues) * SizeOf(Double));
end;

{$DEFINE IsIn}
function IsIn(AValue: Integer; const AValues: array of Integer): Boolean; {$I uDynArrays.inc}
function IsIn(AValue: Single; const AValues: array of Single): Boolean; {$I uDynArrays.inc}
function IsIn(AValue: Double; const AValues: array of Double): Boolean; {$I uDynArrays.inc}
{$UNDEF IsIn}

{$DEFINE GetIndex}
function GetIndex(AValue: Integer; const AValues: array of Integer): Integer; {$I uDynArrays.inc}
function GetIndex(AValue: Single; const AValues: array of Single): Integer; {$I uDynArrays.inc}
function GetIndex(AValue: Double; const AValues: array of Double): Double; {$I uDynArrays.inc}
{$UNDEF GetIndex}

{$DEFINE FindMinMaxValue}
function FindMinMaxValue(const AnArray: array of Integer; out AMin, AMax: Integer): Boolean; {$I uDynArrays.inc}
function FindMinMaxValue(const AnArray: array of Single; out AMin, AMax: Single): Boolean; {$I uDynArrays.inc}
function FindMinMaxValue(const AnArray: array of Double; out AMin, AMax: Double): Boolean; {$I uDynArrays.inc}
{$UNDEF FindMinMaxValue}

{$DEFINE FindMinValue}
function FindMinValue(const AnArray: array of Integer; out AMin: Integer): Boolean; {$I uDynArrays.inc}
function FindMinValue(const AnArray: array of Single; out AMin: Single): Boolean; {$I uDynArrays.inc}
function FindMinValue(const AnArray: array of Double; out AMin: Double): Boolean; {$I uDynArrays.inc}
{$UNDEF FindMinValue}

{$DEFINE FindMaxValue}
function FindMaxValue(const AnArray: array of Integer; out AMax: Integer): Boolean; {$I uDynArrays.inc}
function FindMaxValue(const AnArray: array of Single; out AMax: Single): Boolean; {$I uDynArrays.inc}
function FindMaxValue(const AnArray: array of Double; out AMax: Double): Boolean; {$I uDynArrays.inc}
{$UNDEF FindMaxValue}

end.

uDynArrays.inc example:

{$IFDEF IsIn}
var
  I: Integer;
begin
  Result := False;
  for I := Low(AValues) to High(AValues) do
    if AValue = AValues[I] then
      Exit(True);
end;
{$ENDIF}

{$IFDEF GetIndex}
var
  I: Integer;
begin
  Result := -1;
  for I := Low(AValues) to High(AValues) do
    if AValue = AValues[I] then
      Exit(I);
end;
{$ENDIF}

{$IFDEF FindMinMaxValue}
var
  I: Integer;
begin
  AMax := 0;
  AMin := 0;
  Result := Length(AnArray) > 0;
  if not Result then
    Exit;

  AMax := AnArray[Low(AnArray)];
  AMin := AMax;
  for I := Low(AnArray) to High(AnArray) do
    if AMax < AnArray[I] then
      AMax := AnArray[I]
    else if AMin > AnArray[I] then
      AMin := AnArray[I];
end;
{$ENDIF}

{$IFDEF FindMinValue}
var
  I: Integer;
begin
  AMin := 0;
  Result := Length(AnArray) > 0;
  if not Result then
    Exit;

  AMin := AnArray[Low(AnArray)];
  for I := Low(AnArray) to High(AnArray) do
    if AMin > AnArray[I] then
      AMin := AnArray[I];
end;
{$ENDIF}

{$IFDEF FindMaxValue}
var
  I: Integer;
begin
  AMax := 0;
  Result := Length(AnArray) > 0;
  if not Result then
    Exit;

  AMax := AnArray[Low(AnArray)];
  for I := Low(AnArray) to High(AnArray) do
    if AMax < AnArray[I] then
      AMax := AnArray[I];
end;
{$ENDIF}

Upvotes: -4

stanleyxu2005
stanleyxu2005

Reputation: 8241

Delphi does not provide built-in functionality for macros. Maybe DWScript is what you are looking for.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613582

You cannot use macros in Delphi. No such functionality exists. The closest built in functionality would be an inline function.


You could certainly write your own pre-processor that expands macros. However, you won't be able to make that integrate well with the IDE. At least, not in a way that I would consider acceptable.

The problem is where to expand the macro to. You can hardly expand it to the original file, because you cannot realistically modify that. So you'd need to expand to a temporary file. And the compiler doesn't have any mechanism to build from a temporary files. I suppose you might expand to an include file. That may be the best option but it's going to be horrible to work with. For instance, consider the fun when debugging.

Upvotes: 4

Related Questions