首页 > 解决方案 > 了解以硬编码值作为参数的 NiFpga_MergeStatus() 函数

问题描述

我正在学习其他人的代码(该代码有效并且目前正在硬件应用程序中使用)。给定的代码是在 Qt 中实现的,因为我们需要生成一个应用程序,并创建一个允许用户使用 gui 设置某些参数的应用程序,同时代码处理通过 NI PXI 将此命令传输到 FPGA 等。

在我理解这段代码的过程中,我发现下面的代码中显示了一个函数调用NiFpga_MergeStatus()。作为第一个参数传递的参数已被硬编码并设置为NiFpga_Status_Success(如果您遵循路径,则该static const NiFpga_Status类型设置为 value 0

在查看NiFpga_MergeStatus()函数实现时,我相信这个值是硬编码的,我们永远不会到达第二个 if 语句,我们的返回值将是 Invalid Parameter 值。

为什么有人要实现这样的代码?尤其是自从发送了第二个参数以来,似乎对此进行了一些思考。在将状态参数作为参数传递之前,我们将始终执行第一个 if 语句,我在分析它时是否错了?让我知道我是否应该提供更多详细信息。头文件由 Ni ( NiFpga.h) 提供。谢谢

NiFpga 描述了这个函数的目的:

 * Conditionally sets the status to a new value. The previous status is
 * preserved unless the new status is more of an error, which means that
 * warnings and errors overwrite successes, and errors overwrite warnings. New
 * errors do not overwrite older errors, and new warnings do not overwrite
 * older warnings.
 *
 * @param status status to conditionally set
 * @param newStatus new status value that may be set
 * @return the resulting status

static NiFpga_Inline NiFpga_Status NiFpga_MergeStatus(                                           
    NiFpga_Status* const status,                                               
    const NiFpga_Status  newStatus)
{
    if (!status) //
        return NiFpga_Status_InvalidParameter;
    if(NiFpga_IsNotError(*status)
    &&  (*status == NiFpga_Status_Success || NiFpga_IsError(newStatus)))
        *status = newStatus;
    return *status;
}

标签: c++

解决方案


状态是一个指针参数。在第一个 if 语句 ( if (!status)) 中,它只检查指针是否实际上指向内存中的某个东西。因此,在您的情况下,它始终评估为 false 并且return NiFpga_Status_InvalidParameter;永远不会被调用。

但是第二个 if 语句正在将 status ( ) 的实际值(注意星号 * *status)与 NiFpga_Status_Success.


推荐阅读