Solved

Include some lines of code using compiler statement

Posted on 2009-05-17
15
191 Views
Last Modified: 2012-05-07
title say all

just

{$  do5Lines of code }    ??
0
Comment
Question by:BdLm
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 24405780
Is this what you are after?

{$DEFINE do5LinesOfCode} // Remove this line if you don't want 5 lines of code...

{$IFDEF do5LinesOfCode}
  // your lines of code...
{$ENDIF}
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405811
can I pass a Parameter to my lines of code ?
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405816
The waek point id idef ...  I need these point 2000 x in my code   (profiler)
therfore only want I line for a piease of code to insert ...
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 12

Expert Comment

by:Hypo
ID: 24405837
ok... so you want it like a macro then... I know one way to do that is using Include-files... You can then type  {$I 'C:\MyCode.pas'} and the compiler will replace all those statement with the lines that are in that file. I'll have a look if there is any other ways aswell that don't require you to put the lines in a separate file.

for instance, MyCode.Pas could contain the following line:
TButton(Sender).Caption := 'Test';

and then if you have a button event like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
  {$I 'C:\MyCode.pas'}
end;

The code will compile and the text on the button will change to Test when pressed.

regards
Hypo
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24405897
I've looked around some but didn't find any other way to do this, so to me it seems that the include files are the way to do what you want. Some extra info: You don't have to quote the filename if it doesn't contain spaces in it, and the compiler assumes that the default extension for includes are .pas, so {$I My5LinesOfCode} is equivalent to {$I 'My5LinesOfCode.pas'}.

Just by curiosity, may I ask why you want to include the lines using a compilerdirective? It isn't possible for you to put the code you want to execute in a simple function?

/Hypo
0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 100 total points
ID: 24406905
Trouble with using this technique is that you are going to end up with two versions of the program with different maps.  I think you are concerned about speed - putting the test code into a procedure is better if speed is not crucial.  

You can specify a parameter string when calling the program in "test" mode and use the paramstr(1) function to retrieve it.  If running in normal mode, leave the parameter blank.

http://www.delphibasics.co.uk/RTL.asp?Name=ParamStr

Doing this, you can setup Delphi to always use this "debug" string when you run the program from the IDE.  At the start of your prog have debugflag set depending on whether paramstr contains the correct keyword, and then test for this on entry to your test procedure.  If it is false, exit the test procedure immediately.
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407311
{ifdef PROFILER}

   {$I 'C:\InitProfiler.pas'}
{endif}

     testCode

{$ifdef PROFILER}
 
        {$I 'C:\StopProfiler.pas'}
 {endif}

any better / shorter way in the source code ?





0
 
LVL 8

Author Comment

by:BdLm
ID: 24407327
PS:  I know http://www.prodelphi.de/indexpd.htm   but not invented here  :-)  
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24407373
var
  debugflag:boolean;


procedure profiler(whocalledme:string);
begin
if debugflag then
  begin
 
  logging routine storing date time and whocalledme

  end;
end;


procedure main;
begin
debugflag:=(paramstr(1)='mysecretdebugswitch');
profiler('program started');
.
.
.

0
 
LVL 8

Author Comment

by:BdLm
ID: 24407387
here comes a sample with prodelphi , they include the lines of code in the $ifdef statement ...



procedure TForm1.Button3Click(Sender: TObject);
begin
{$IFDEF PROFILE}asm DW 310FH; call Profint.ProfStop; end; Try; asm mov edx,79 or $99EA0000; mov eax,self; call Profint.ProfEnter; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; {$ENDIF}
 
     ACheck.Filename := edit2.Text;
 
     ACheck.Name  := ' Bla Bla';
     
     ACheck.StoreResults;
{$IFDEF PROFILE}finally; asm DW 310FH; mov ecx,79; call Profint.ProfExit; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; end; {$ENDIF}
end;

Open in new window

0
 
LVL 12

Accepted Solution

by:
Hypo earned 400 total points
ID: 24407397
Yeah, you can put the defines in the inc-file as well...

procedure TForm1.Button1Click(Sender: TObject);
begin
  {$i C:\initProfiler}
  ...
  {$i C:\stopProfiler}
end;


And then in C:\InitProfiler.pas you have...
{$IFDEF PROFILER}
  // your code...
  TButton(Sender).Caption := 'Testing';
{$ENDIF}

And similar in StopProfiler.pas...
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407399
@ Hypo :  just working on that solution,   :-))
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24407423
Nice, I just tried it, putting those lines you have at the start and at the end in two seperate files, compiles... well I had to remove some of your asm-code since i don't have all the global variables you have defined... but I tried it with other code, so it should work with your code as well... :)
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407574
Btw:  did you see my new question , because now I need to init the profiler  var in the begin end block of my unit    http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24416082.html
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24416694
when you use the profiler, it parses all your units and adds the entry and exit procs to the beginning resp end of each function or procedure.
Why do you have to do this manually ?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 98
error 1.1 400 Bad request idhttp delphi 18 86
QRReport  TQrmemo vertical stretching 1 45
Delphi: barcode reading on android platform 1 26
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question