Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2011-06

Problem with {$BOOLEVAL ON/OFF} and it's default.

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Skybuck Flying

Hello,

I want to make sure that complete boolean evaluation for certain code sections is on.

However after the code section I want the "complete boolean evaluation" compiler switch state back to what it was/the default.

So I would like to write something like:

{$if BOOLEVAL = ON}
    {$define CompleteBooleanEvaluationWasOn}
{$ifend}
{$if BOOLEVAL = OFF}
    {$define CompleteBooleanEvaluationWasOff}
{$ifend}
{$BOOLEVAL ON}
     ... code section ...
{$if CompleteBooleanEvaluationWasOn}
    {BOOLEVAL ON}
{$ifend}
{$if CompleteBooleanEvaluationWasOff}
   {BOOLEVAL OFF}
{$ifend}

So I would like the rest of the code to be un-effected by my compiler switch change just to make sure that everything else compiles as normal/default.

However it either doesn't seem to be possible to write it like this or I am not good enough at writing conditional compiler directives.

Is it possible to solve it with compiler directives ?

If not then perhaps Delphi Compiler needs a "default" directive like so:

{$BOOLEVAL DEFAULT}

This would take the project options default value for this compiler directive to solve the problem ! ;) =D

I haven’t tried yet with Free Pascal, so what you guys think ? Possible or not ?! (I’m thinking not... so not even going to try it... but do surprise me if you can !)

To help you try it out here is a test program.
It's a little bit like an abortion... it's a little bit fucked up ! ;) =D But it's not to bad... if you do know how to solve it... then you can do it ! ;) =D

// *** Begin of Test Program ***
program TestProgram;
{$APPTYPE CONSOLE}

{

Test complete boolean evaluation compiler switches to see if they are unit/file width or per routine or per code section

version 0.01 created on 14 june 2011 by Skybuck Flying

Conclusion: it works per code section however there is a potential problem:

It might not be possible to return to the "default settings" ?!?

For example once B+ is done all code below it will be effected ?!? B- might then turn it off... but what if project default was on ?!?

Perhaps it can be solved with $if or so... and some kind of storage...

}

uses
  SysUtils;
function Function1 : boolean;
begin
writeln('function1 called');
result := true;
end;
function Function2 : boolean;
begin
writeln('function2 called');
result := false;
end;
function Function3 : boolean;
begin
writeln('function3 called');
result := false;
end;
// default result: function3 not called, pretty bad.
procedure TestRoutine1;
var
vResult : boolean;
begin
writeln('TestRoutine1 begin');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine1 vResult: ', vResult );
writeln('TestRoutine1 end');
end;
procedure TestRoutine2;
var
vResult : boolean;
begin
{$BOOLEVAL ON}
{$if (BOOLEVAL = ON)}
  {$define CompleteBooleanEvaluationWasOn }
  writeln('it''s on');
{$else}
  {$define CompleteBooleanEvaluationWasOff}
  writeln('it''s off');
{$ifend}
{$BOOLEVAL ON}
writeln('TestRoutine2 begin1');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 end1');
{$BOOLEVAL OFF}
writeln('TestRoutine2 begin2');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2; // off works... so it's code specific ok nice. however what will happen outside of code... will it go to default ?!?
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 end2');
{$if Defined(CompleteBooleanEvaluationWasOn)}
  writeln('turning it on');
  {$BOOLEVAL ON}
{$ifend}
{$if Defined(CompleteBooleanEvaluationWasOff)}
  writeln('turning it off');
  {$BOOLEVAL OFF}
{$ifend}
writeln('TestRoutine2 begin3');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2; // off works... so it's code specific ok nice. however what will happen outside of code... will it go to default ?!?
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 end3');
end;
// little problem... booleval remains on... and now we don't know what the default behaviour was ?!?
// it's desireable to fall back to default behaviour for the rest of the code ?!?
procedure TestRoutine3;
var
vResult : boolean;
begin
writeln('TestRoutine3 begin');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine3 vResult: ', vResult );
writeln('TestRoutine3 end');
end;
procedure Main;
begin
writeln('program started');
TestRoutine1;
TestRoutine2;
TestRoutine3;
writeln('program finished');
end;
begin
  try
Main;
  except
on E: Exception do
   Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.

// *** End of Test Program ***
Bye,
Skybuck.

Reply by email

Skybuck Flying
to Skybuck Flying

Solved:

The solution required one little line fixed:

{ifopt b+}

Anyway I have improved the test program somewhat, because it was a bit shitty, it's still a little bit shitty but not as shitty ! ;) =D

// *** Begin of Test Program ***
program TestProgram;
{$APPTYPE CONSOLE}

{

Test complete boolean evaluation compiler switches to see if they are unit/file width or per routine or per code section

version 0.01 created on 14 june 2011 and fixed/improved on 15 june 2011 by Skybuck Flying

Conclusion: it works per code section however there is a potential problem:

It might not be possible to return to the "default settings" ?!?

For example once B+ is done all code below it will be effected ?!? B- might then turn it off... but what if project default was on ?!?

Perhaps it can be solved with $if or so... and some kind of storage...

It's fucked up/abortion-ed ;) I don't think it's possible so for now I will have to stick with B+ down below the code sections ;)

Ok solved,

Solution is to use:

$ifopt b+

Thanks to "Bart" on usenet for providing this solution, he usefull after all ! ;) =D

}

uses
  SysUtils;
function Function1 : boolean;
begin
writeln('function1 called, result: true');
result := true;
end;
function Function2 : boolean;
begin
writeln('function2 called, result: false');
result := false;
end;
function Function3 : boolean;
begin
writeln('function3 called, result: true');
result := false;
end;
// default result: function3 not called, pretty bad.
procedure TestRoutine1;
var
vResult : boolean;
begin
writeln('TestRoutine1 - DEFAULT - BEGIN');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine1 vResult: ', vResult );
writeln('TestRoutine1 - DEFAULT - END');
writeln;
end;
procedure TestRoutine2;
var
vResult : boolean;
begin
{$ifopt b+}
  {$define CompleteBooleanEvaluationWasOn }
  writeln('Default for Complete Boolean Evaluation is On.');
{$else}
  {$define CompleteBooleanEvaluationWasOff}
  writeln('Default for Complete Boolean Evaluation is Off.');
{$endif}
writeln;
{$BOOLEVAL ON}
writeln('TestRoutine2 - BOOLEVAL ON - BEGIN');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 - BOOLEVAL OFF - END');
writeln;
{$BOOLEVAL OFF}
writeln('TestRoutine2 - CONTINUATION - BEGIN');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2; // off works... so it's code specific ok nice. however what will happen outside of code... will it go to default ?!?
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 - CONTINUATION - END');
writeln;
{$if Defined(CompleteBooleanEvaluationWasOn)}
  writeln('Falling back to Default Complete Boolean Evaluation which was On.');
  {$BOOLEVAL ON}
{$ifend}
{$if Defined(CompleteBooleanEvaluationWasOff)}
  writeln('Falling back to Default Complete Boolean Evaluation which was Off.');
  {$BOOLEVAL OFF}
{$ifend}
writeln;
writeln('TestRoutine2 - BOOLEVAL DEFAULT');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2; // off works... so it's code specific ok nice. however what will happen outside of code... will it go to default ?!?
vResult := vResult and Function3;
writeln('TestRoutine2 vResult: ', vResult );
writeln('TestRoutine2 - BOOLEVAL DEFAULT');
writeln;
end;
// little problem... booleval remains on... and now we don't know what the default behaviour was ?!?
// it's desireable to fall back to default behaviour for the rest of the code ?!?
// problem solved.
procedure TestRoutine3;
var
vResult : boolean;
begin
writeln('TestRoutine3 - OUTSIDE - BEGIN');
vResult := true;
vResult := vResult and Function1;
vResult := vResult and Function2;
vResult := vResult and Function3;
writeln('TestRoutine3 vResult: ', vResult );
writeln('TestRoutine3 - OUTSIDE - END');
writeln;
end;
procedure Main;
begin
writeln('program started');
TestRoutine1;
TestRoutine2;
TestRoutine3;
writeln('program finished');
end;
begin
  try
Main;
  except
on E: Exception do
   Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.
// *** End of Test Program ***

Bye,
Skybuck.

Reply by email

Martin Schreiber
to Skybuck Flying
Skybuck Flying wrote 6 lines
Solved: The solution required one little line fixed: {ifopt b+}

[...]

The CHECKPOINTER directive can be used with DEFAULT: "
{$CHECKPOINTER ON}
[...]
{$CHECKPOINTER DEFAULT}
"
Maybe that method can be implemented for other directives too?

Martin

Reply by email

Jonas Maebe
to Martin Schreiber
Martin Schreiber wrote 7 lines
The CHECKPOINTER directive can be used with DEFAULT: " {$CHECKPOINTER ON} [...] {$CHECKPOINTER DEFAULT} " Maybe that method can be implemented for other directives too?

The generic way to temporarily change local switches (and verbosity) is

{$push}

<change any switches you want>

...

{$pop}

Jonas

Reply by email