POV-Ray : Newsgroups : povray.general : #ifdef using a string expression? Server Time
2 May 2024 04:05:23 EDT (-0400)
  #ifdef using a string expression? (Message 34 to 38 of 38)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Kenneth
Subject: Re: #ifdef using a string expression?
Date: 11 Apr 2023 08:25:00
Message: <web.64354efe98df09c99b4924336e066e29@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> (v3.8.0 beta 1 in Windows 10)
>
> I was playing around with #ifdef recently; just experimenting.
>
Here's another example of an #ifdef feature that would be useful, IMO. I
recently had a need for it:
     #ifdef(AA & BB)

Apparently, it doe not (yet) work with 'tuples' like the newer v3.8 #declare
does, for example. Nor with boolean operators, so it seems.

I tried several workarounds for this, in various syntax forms. My first attempt
was with nested #ifdef's, possibly using an #elseif somewhere-- but I am not
very familiar with the #elseif construct and could not come up with a code
block that seemed 'reliable' or logically consistent:

#declare AA=3;
#declare BB=7;

#ifdef(AA)
     #ifdef(BB)
     ... do this...
    (or  #elseif   ...do this... ?)
#else (?)
... do that...
#end

My 2nd attempt was with an enclosing #if, again in various syntax forms-- but
this causes fatal errors, and I'm not sure that I understand why:

#if(#ifdef(AA) & #ifdef(BB)) // this line does not work, even though an #if can
                             // itself use boolean operators
...do this...
#else
...do that...
#end

For the first line, a simpler  #if(#ifdef(AA)) does not work either, nor does
#if(#ifdef(AA)=true))

Any suggestions or comments? It seems to me that at least one of these schemes
*should* work, unless I'm making a basic error or wrong assumption somewhere.

The current v3.8 operation of #ifdef is...ill-defined... so maybe that's part of
the problem(?)


Post a reply to this message

From: jr
Subject: Re: #ifdef using a string expression?
Date: 11 Apr 2023 08:50:00
Message: <web.643556bf98df09c94301edef6cde94f1@news.povray.org>
hi,

"Kenneth" <kdw### [at] gmailcom> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:
> > (v3.8.0 beta 1 in Windows 10)
> >
> > I was playing around with #ifdef recently; just experimenting.
> > ...
> For the first line, a simpler  #if(#ifdef(AA)) does not work either, nor does
> #if(#ifdef(AA)=true))
>
> Any suggestions or comments?

suggestion.  write '#if(defined(AA) & defined(BB))'  (ie both exist)


regards, jr.


Post a reply to this message

From: Kenneth
Subject: Re: #ifdef using a string expression?
Date: 11 Apr 2023 10:50:00
Message: <web.643572cd98df09c99b4924336e066e29@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:
>
> "Kenneth" <kdw### [at] gmailcom> wrote:
>
> >  ...
> > For the first line, a simpler  #if(#ifdef(AA)) does not work either, nor does
> > #if(#ifdef(AA)=true))
> >
> > Any suggestions or comments?
>
> suggestion.  write '#if(defined(AA) & defined(BB))'  (ie both exist)
>

Yes, that's a great workaround! Thanks. I had forgotten about the  'defined'
function, as I don't use it very often.


Post a reply to this message

From: Bald Eagle
Subject: Re: #ifdef using a string expression?
Date: 11 Apr 2023 13:30:00
Message: <web.6435984498df09c91f9dae3025979125@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> I recently had a need for it:
>      #ifdef(AA & BB)
>
> Apparently, it doe not (yet) work with 'tuples' like the newer v3.8 #declare
> does, for example. Nor with boolean operators, so it seems.

> Any suggestions or comments? It seems to me that at least one of these schemes
> *should* work, unless I'm making a basic error or wrong assumption somewhere.


Depending on how many variables you need to test, you lump them all together
into an array and cycle through them, using the product of the result as a sort
of Boolean AND result.

Not sure I knew about "defined" - that's a handy keyword.

#declare AA = 1;
#declare BB = 3;

#declare Variables = array {AA, BB}


#macro And (_Array)
 #declare Elements = dimension_size (_Array, 1)-1;
 #declare AndResult = 1;
 #for (Index, 0, Elements)
  #declare AndResult = AndResult * defined (_Array[Index]);
 #end

 AndResult
#end


#if (And (Variables))
 #debug "All variables are defined. \n"
#end

#error "No objects in scene"d


Post a reply to this message

From: Kenneth
Subject: Re: #ifdef using a string expression?
Date: 13 Apr 2023 13:25:00
Message: <web.64383a5198df09c99b4924336e066e29@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Kenneth" <kdw### [at] gmailcom> wrote:
>
> >   #ifdef(AA & BB)
> > Any suggestions or comments?
>
> Depending on how many variables you need to test, you lump them all together
> into an array and cycle through them, using the product of the result
> as a sort of Boolean AND result.
>
>
> #declare AA = 1;
> #declare BB = 3;
>
> #declare Variables = array {AA, BB}
>
> #macro And (_Array)
>  #declare Elements = dimension_size (_Array, 1)-1;
>  #declare AndResult = 1;
>  #for (Index, 0, Elements)
>   #declare AndResult = AndResult * defined (_Array[Index]);
>  #end
>  AndResult
> #end
>
>
> #if (And (Variables))
>  #debug "All variables are defined. \n"
> #end
>

I had to think about this one (and had to take a temporary break because of all
of the new Spring pollen in the air, here in the South. Sneezing uncontrollably!
Me no likey!)

This is an interesting way of attacking the problem, and works fine as-is. But
if I add an UN-declared variable to the array (i.e., wondering if I had
previously declared it or not), this line fails:
           #declare Variables = array {AA, BB, CC}

"Cannot pass uninitialized identifier to non-optional array initializer."
(What a mouthful!)

But I'm probably going outside the scope of my simpler  #ifdef(AA & BB)
question. JR's #if/#else code allows a more graceful exit though: I could add a
#debug like, "One or more of your variables is undefined."


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.