Solved

Include some lines of code using compiler statement

Posted on 2009-05-17
15
187 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 36

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now