Regarding gr_squelch_base_cc.cc

Hi list,
I was looking at the gr_pwr_squelch_cc- code, and there is a thing I
just want to check the meaning of. It is not as much that I don’t
believe the code works correctly, it’s just that some lines of code
confuse and scare me. :wink:

The lines are 59 and 64,
57: case ST_MUTED:
58: if (!mute())
58: d_state = d_ramp ? ST_ATTACK : ST_UNMUTED; // If not ramping, go
straight to unmuted
60: break;
61:
62: case ST_UNMUTED:
63: if (mute())
64: d_state = d_ramp ? ST_DECAY : ST_MUTED; // If not ramping, go
straight to muted
65: break;

It might be that I don’t fully understand the “?”- construct, but if I
try to translate line 64 into an if- statement, I would do it (with my
understanding) to something like:

if(d_state = d_ramp){
d_state = ST_DECAY;
}
else{
d_state = ST_MUTED;
}

But this can’t be the way since this will always set d_state = ST_DECAY
(?) Or it could be the way, but in that case… Why the crazy ?-
construct?

As I said earlier, I’m scared and confused, since the code does it’s job
but it looks very… Suspicious

BR
Mattias

On Thu, Apr 29, 2010 at 01:55, Mattias K. [email protected] wrote:

57: case ST_MUTED:
58: if (!mute())
58: d_state = d_ramp ? ST_ATTACK : ST_UNMUTED; // If not ramping, go straight to unmuted
60: break;

The ‘condition ? expr1 : expr2’ construct is an expression that
evaluations to ‘expr1’ if ‘condition’ is true, otherwise it evaluates
to ‘expr2’.

Here, ‘condition’ is d_ramp, ‘expr1’ is ST_ATTACK, and ‘expr2’ is
ST_UNMUTED.

Thus, line 58 says:

“Assign to d_state the value of ‘ST_ATTACK’ if d_ramp is true,
otherwise assign to d_state the value of ‘ST_UNMUTED’.”

In other words, the variable d_state is the left side of an
assignment, and the right side of the assignment evaluates to either
ST_ATTACK or ST_UNMUTED, depending on whether d_ramp is true.

It may help to mentally parse line 58 with parenthesis just before
‘d_ramp’ and after ‘ST_UNMUTED’.

Specifically, this is not a test of the value of d_state.

Hope this helps with your fear and confusion :slight_smile:

Johnathan

On 04/29/2010 04:55 AM, Mattias K. wrote:

understanding) to something like:
?- construct?

As I said earlier, I’m scared and confused, since the code does it’s
job but it looks very… Suspicious

BR
Mattias

Ah, conditional expressions.

Let’s deal with line 64:

d_state = d_ramp ? ST_DECAY : ST_MUTED; // If not ramping, go
straight to muted

Translated into “conventional” conditionals:

if (d_ramp != 0)
{
d_state = ST_DECAY;
}
else
{
d_state = ST_MUTED;
}

In C, you can have a condition expression that just mentions the
variable name, without any explicit
comparision operator:

if (foo)
{

}

Which in another language would be:

if (foo not-equal 0)
{
}

So, the “?” operator isn’t all that crazy after all:

(condition) ? (clause-to-execute-if-true) : 

(clause-to-execute-if-false)


Marcus L.
Principal Investigator
Shirleys Bay Radio Astronomy Consortium