Compiler Directives with code

Please discuss general Delphi programming topics here.

Compiler Directives with code

Postby lexdean » September 29th, 2012, 1:08 am

I have not used Compiler Directives that much

I have a function that returns a Boolean that I wish to turn on or off a directive

Can I do this in any way for compile time?

Conditional compilation (Delphi) link:-

http://docs.embarcadero.com/products/ra ... n_xml.html


can any one give me an example please
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Compiler Directives with code

Postby arhangelsoft » October 2nd, 2012, 4:01 pm

It's simple to use! For example:
Code: Select all
{$DEFINE RetStr}  //If defined our func will return string, else BOOL.

function GetResult:{$IFDEF RetStr} string; {$ELSEIF} boolean; {$ENDIF}
begin
 Result:={$IFDEF RetStr} 'It is amazing!1111'; {$ELSEIF} True; {$ENDIF}
end;



Delphi Compiler Pre-processor will ignore code if it included in definition that returns false.
Code above will be pre-processed to this:
Code: Select all
{
if {$DEFINE RetStr} //RetStr defined
}
//pre-processed code
function GetResult: string;
begin
 Result:= 'It is amazing!1111';
end;

{
if //{$DEFINE RetStr} //RetStr not defined
}
//pre-processed code
function GetResult:boolean;
begin
 Result:=True;
end;


Now we undef our RetStr.
That the result is:
Code: Select all
{
if {$DEFINE RetStr} //RetStr defined
}
//pre-processed code
function GetResult: string;
begin
 Result:= 'It is amazing!1111';
end;

{
 ///
}

{$UNDEF RetStr} //RetStr will be return false, it it defined

{
if //{$DEFINE RetStr} //RetStr not defined
}
//pre-processed code
function GetResult:boolean;
begin
 Result:=True;
end;


Definitions it's a compiller bool operations on pre-processing code, in delphi it's view like this:
Code: Select all
var
 VarBool:Boolean; //It's equal {$DEFINE VarBool}
begin
  if VarBool then //it's Equal {$IFDEF VarBool}
  begin
    {...}
  end else //it's Equal {$ELSEIF}
  begin
   {...}
  end; //it's Equal {$ENDIF}

  if not VarBool then //it's Equal {$IFNDEF VarBool}
  begin
    {...}
  end else //it's Equal {$ELSEIF}
  begin
   {...}
  end; //it's Equal {$ENDIF}
end';


With $UNDEF it's view like:
Code: Select all
[code]
var
 VarBool:Boolean; //It's equal {$DEFINE VarBool}
begin
  if VarBool then //it's Equal {$IFDEF VarBool}
  begin
    {...}
  end else //it's Equal {$ELSEIF}
  begin
   {...}
  end; //it's Equal {$ENDIF}

VarBool:=FALSE; //it's Equal {$UNDEF RetStr}

  if not VarBool then //it's Equal {$IFNDEF VarBool}
  begin
    {...}
  end else //it's Equal {$ELSEIF}
  begin
   {...}
  end; //it's Equal {$ENDIF}
end';
[/code]


Can I do this in any way for compile time?

Compiler automatically pre-processes defenitions in compile-time.
arhangelsoft
Junior Member
Junior Member
 
Posts: 39
Joined: February 16th, 2012, 9:56 pm
Location: Belarus, Minsk

Re: Compiler Directives with code

Postby lexdean » October 2nd, 2012, 11:24 pm

I will try these options out
Thanks

lex dean
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Compiler Directives with code

Postby lexdean » October 4th, 2012, 9:57 pm

This guy did not read my question, I need the oppersite
lexdean
Active Member
Active Member
 
Posts: 16
Joined: May 25th, 2011, 12:04 am

Re: Compiler Directives with code

Postby Kambiz » October 5th, 2012, 11:04 am

Ahell has given you a complete answer.
What part of the answer is not yet clear for you?
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2430
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron