From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
Subject: Re: ASSERT in Delphi ?
Date: Wed, 10 May 1995 13:36:29 GMT

You can create your own symbol (called DEBUG, say) and have

{$ifdef DEBUG} {$D+} {$else} {$D-} {$endif}

and set both $D and your conditional simultaneously.  There's no really clean 
way to do Assert in Delphi; the one I prefer is

const
  debug = {$ifdef DEBUG} true {$else} false {$endif};

procedure Assert(condition:boolean);
begin
  if not condition then {give some error message} ;
end;

Now, to put a call to Assert into your program only while debugging, use

  if debug then Assert(X>1);

The call to Assert will be stripped out if debug is false; if you make no 
references to Assert that don't get stripped, then the whole routine will be 
stripped out of the executable.

>My next question is: is there any trick how to generate the line number
>like in MFC ? Probably not, because of unavailability of macro, am I right ?

No, there's no way I know of.  You can call Runerror() to generate a run-time 
error and print the address; you could write a program to search through a 
..map file to find that address, or just ask the IDE to do the search for you 
(with Find Error).

Duncan Murdoch

-------------------------------------------------------------------------------

From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
Subject: Re: ASSERT in Delphi/Pascal
Date: Sun, 14 May 1995 12:40:26 GMT

I guess I'll have to change my recommendation:  Don't use "if debug then 
Assert...", use "{$ifdef debug} Assert... {$endif}".  Sorry for the bad advice.

Duncan Murdoch

-------------------------------------------------------------------------------

From: ag623@FreeNet.Carleton.CA (Lester Kovac)
Subject: Re: ASSERT in Delphi/Pascal
Date: Wed, 17 May 1995 04:03:47 GMT

There is nothing to be sorry for. I'm thankfull to everybody who
contributed to this thread. I would just suggest the best would be the
condition according to Sean Palmer's advice:

{$IFOPT D+} Assert(condition, "Pointer not initialized"); {$ENDIF}

Thus we do not need to maintain DEBUG conditional define.
--
Lester Kovac
Nepean, Ontario, Canada

-------------------------------------------------------------------------------

From: vandewb@wkuvx1.wku.edu (Jay Cole)
Subject: Definitive Assert in Delphi
Date: 18 Jun 95 10:07:22 CDT

Well, here it is...

The definitive Assert.  Better than the line numbers supplied w/ the
C/C++ Assert, this assert gives you the "find|error" logical address
of the calling module that called assert.  It returns the ssss:oooo
seg offset pair so you can find where the assert was called.  Again,
you can use the ifopt D+ if you wish but I prefer a define _NDEBUG
instead.  Hopefully this is the end-all assert. Use freely, just drop
me an e-mail at vandewb@wku.wkuvx1.edu if you find it useful.

Here it is...Jay Cole

{ Call if you need to verify implied conditions }
procedure Assert(assertedCond : boolean; const msgStr : string);

implementation
{$ifdef WINDOWS}
   uses WinProcs, SysUtils, WinTypes;
{$else}
   uses sysUtils;
{$endif}

{ Error when the assumption made is not correct. }
procedure Assert(assertedCond : boolean; const msgStr : string);
var 
   msgOut : array[0..300] of char;
   hexStr : array[0..30] of char;
   progSeg, progOfs : word;
   tmpStr : string;
begin
   { Get the logical segment used by the Find|Error menu option of
delphi }
   asm
      mov ax, [bp+04]            { Load physical segm of the calling
proc on call stack }
      mov es, ax
      mov ax, word ptr es:0      { Logical segm stored in 0th position
of physical seg }
      mov progSeg, ax            { Logical, not physical segm used in
Find|Error menu item }
      mov ax, [bp+02]
      mov progOfs, ax            { Physical ofs is used by find|error,
no translation needed }
   end;

   {$ifndef _NDEBUG} { Are we allowed to assert? }
   if (not assertedCond) then begin
      { construct msg\nat location ssss:oooo using the logical address
}
      StrPCopy(msgOut, msgStr);
      tmpStr := chr(10)+'at location
'+IntToHex(progSeg,4)+':'+IntToHex(progOfs,4);
      StrPCopy(hexStr, tmpStr);
      StrCat(msgOut, hexStr);
      {$ifdef WINDOWS}
         MessageBox(0, msgOut, 'Assert Failed',
MB_ICONSTOP+MB_SYSTEMMODAL+MB_OK);
      {$else}
         WriteLn('Assert Failed', msgOut);
      {$endif}
      { Now, terminate the program because of assertion problem }
      halt; 
   end;
   {$endif}
end;
