Page 1 of 1

Compiler Directives with code

PostPosted: September 29th, 2012, 1:08 am
by lexdean
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

Re: Compiler Directives with code

PostPosted: October 2nd, 2012, 4:01 pm
by arhangelsoft
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.

Re: Compiler Directives with code

PostPosted: October 2nd, 2012, 11:24 pm
by lexdean
I will try these options out
Thanks

lex dean

Re: Compiler Directives with code

PostPosted: October 4th, 2012, 9:57 pm
by lexdean
This guy did not read my question, I need the oppersite

Re: Compiler Directives with code

PostPosted: October 5th, 2012, 11:04 am
by Kambiz
Ahell has given you a complete answer.
What part of the answer is not yet clear for you?