0

抱歉有点含糊,这是一个与编码相关的小问题,但我不确定如何尽可能简单地编码。

以下是 if 条件:

   if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD)) { /* do something */ }

我想OF_IS_FLAG_SET()根据另一个 X 条件包括另一个。基本上,

if(X) {
if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD)||
             OF_IS_FLAG_SET(pOfHwData, NEW_FLAG)) { /* do something */ }}
else {
if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
             OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD)) { /* do something */ }
}

但我不想重写完全相同的 /* 块做某事 */。如果 X 为真,我如何包含新标志OF_IS_FLAG_SET(pOfHwData, NEW_FLAG),否则。

4

4 回答 4

1
if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD))
{ 
    if ( X && OF_IS_FLAG_SET(pOfHwData, NEW_FLAG) )
    {
       /* X is true and all the checks are true, including the additional one depending on X */
    }
    else if (!X)
    {
       /* X is not true but all the checks are true */
    }
    else 
    {
       /* X is true but the additional check has failed */
    }

}
else
{
   /* Some check has failed. */
}
于 2014-05-27T12:56:09.617 回答
0

一种方法是合并常见条件:

if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
    OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
    OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
    OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD) {
  if(X) {
    if (OF_IS_FLAG_SET(pOfHwData, NEW_FLAG)) {
      /* do something */
    }
  } else {
   /* do something */
  }
}

这相当于您问题中的流程

于 2014-05-27T12:55:04.507 回答
0
if((X && (isFlagsUp() || OF_IS_FLAG_SET(pOfHwData, NEW_FLAG))) || isFlagsUp()) {
 /* do something */
}

int isFlagsUp() {
  if ((OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
       OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
       OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
       OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD)) {
    return 1;
  } else {
    return 0;
  }
}
于 2014-05-27T13:03:00.167 回答
0

感谢大家的回答,我没想到会产生更多基于OF_IS_FLAG_SET(pOfHwData, NEW_FLAG). 基本上,如果 X 为真,我只想OF_IS_FLAG_SET(pOfHwData, NEW_FLAG)包含在原始 if 条件中,否则不包含在内。

   bool XisTrue = ((getCompatibilityMode()) ? FALSE : TRUE);

   if (OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_PORT) ||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_MULTIPORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_TUNNEL_PORT)||
         OF_IS_FLAG_SET(pOfHwData, OF_HW_OUTPUT_FLOOD)||
         (XisTrue && OF_IS_FLAG_SET(pOfHwData, NEW_FLAG)))

getCompatibilityMode()决定 X 是否为真。这似乎正在解决问题。如果我错过了什么或者我不够清楚,请告诉我。

于 2014-05-27T13:28:05.783 回答