Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何通过多个步骤跟踪状态_C++_Status - Fatal编程技术网

C++ 如何通过多个步骤跟踪状态

C++ 如何通过多个步骤跟踪状态,c++,status,C++,Status,我正在编写一些代码,由于其状态检查,很难重新考虑这些代码。我试图找出更好的方法来解决这个问题,这样我就可以保持代码的干净/可读性。下面是代码的一个片段: int status = FAILED; status = fn_action_one(); if (status != SUCCESS) { printf("ERROR: returned from fn_action_one()\n"); } else { status = fn_action_two(); } if

我正在编写一些代码,由于其状态检查,很难重新考虑这些代码。我试图找出更好的方法来解决这个问题,这样我就可以保持代码的干净/可读性。下面是代码的一个片段:

int status = FAILED;

status = fn_action_one();

if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_one()\n");
}
else
{
    status = fn_action_two();
}

if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_two()\n");
}
else
{
    status = fn_action_three();
}
对我来说,问题是我现在想要重构这段代码,并遍历其中的部分:

int status = FAILED;

status = fn_action_one();

if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_one()\n");
}
else
{
    // This task is now required to be done multiple times...
    for (int i = 0; i < numLoop; i++)
    {
        status = fn_action_two(i); // Keeping track of the status here is now an issue
    }
}

// If any of the looped action_two's had a fail this check should fail
if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_two()\n");
}
else
{
    status = fn_action_three();
}

“模式”是使用异常处理。如果无法修改fn_action_xxx(),请编写包装器

void my_action_one()
{
    int status = fn_action_one();
    if (status != SUCCESS)
         throw std::runtime_error("ERROR: returned from fn_action_one()");
}

...

try
{
    my_action_one();
    for (int i = 0; i < numLoop; i++)
    {
        my_action_two(i);
    }
    my_action_three();
}
catch (const std::exception& e)
{
    printf("%s\n", e.what());
}
void my_action_one()
{
int status=fn_action_one();
如果(状态!=成功)
throw std::runtime_error(“error:从fn_action_one()返回”);
}
...
尝试
{
我的行动;
对于(int i=0;i

状态检查的脆弱性是异常的动机之一。通过将错误处理与程序的主流程分离,您可以获得更清晰的代码。

模式是使用异常处理。如果无法修改fn_action_xxx(),请编写包装器

void my_action_one()
{
    int status = fn_action_one();
    if (status != SUCCESS)
         throw std::runtime_error("ERROR: returned from fn_action_one()");
}

...

try
{
    my_action_one();
    for (int i = 0; i < numLoop; i++)
    {
        my_action_two(i);
    }
    my_action_three();
}
catch (const std::exception& e)
{
    printf("%s\n", e.what());
}
void my_action_one()
{
int status=fn_action_one();
如果(状态!=成功)
throw std::runtime_error(“error:从fn_action_one()返回”);
}
...
尝试
{
我的行动;
对于(int i=0;i
状态检查的脆弱性是异常的动机之一。通过将错误处理与程序的主要流程分离,您可以获得更清晰的代码。

作为一种“C”方法,我将使用宏和goto的组合

#define CHECK(expr) \
  { \
    if( SUCCESS != (status = expr) ){ \
    printf("ERROR: returned from " #EXPR "\n"); \ 
    goto End
  } \
}


int status = FAILED;

CHECK(fn_action_one());

End:
return status;
而在循环中,我会这样做

  if (status != SUCCESS)
  {
     status = fn_action_two(i);
  }
作为一种“C”方法,我将使用宏和goto的组合

#define CHECK(expr) \
  { \
    if( SUCCESS != (status = expr) ){ \
    printf("ERROR: returned from " #EXPR "\n"); \ 
    goto End
  } \
}


int status = FAILED;

CHECK(fn_action_one());

End:
return status;
而在循环中,我会这样做

  if (status != SUCCESS)
  {
     status = fn_action_two(i);
  }

如果您希望尽可能地保持当前代码结构,并最小化实现所需功能所需的更改,您可能需要添加一个布尔值来跟踪循环中的任何调用是否失败,然后在此基础上设置
状态

int status = FAILED;

status = fn_action_one();

if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_one()\n");
}
else
{
    bool failed = false;
    // This task is now required to be done multiple times...
    for (int i = 0; i < numLoop; i++)
       failed |= (fn_action_two(i) == FAILED);
}
if (failed)
    status = FAILED;

// If any of the looped action_two's had a fail this check should fail
if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_two()\n");
}
else
{
    status = fn_action_three();
}
int status=失败;
状态=fn_action_one();
如果(状态!=成功)
{
printf(“错误:从fn_action_one()返回\n”);
}
其他的
{
bool失败=错误;
//此任务现在需要执行多次。。。
对于(int i=0;i
如果您希望尽可能保持当前的代码结构,并最小化实现所需功能所需的更改,您可能需要添加一个布尔值来跟踪循环中的任何调用是否失败,然后根据该值设置
状态

int status = FAILED;

status = fn_action_one();

if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_one()\n");
}
else
{
    bool failed = false;
    // This task is now required to be done multiple times...
    for (int i = 0; i < numLoop; i++)
       failed |= (fn_action_two(i) == FAILED);
}
if (failed)
    status = FAILED;

// If any of the looped action_two's had a fail this check should fail
if (status != SUCCESS)
{
    printf("ERROR: returned from fn_action_two()\n");
}
else
{
    status = fn_action_three();
}
int status=失败;
状态=fn_action_one();
如果(状态!=成功)
{
printf(“错误:从fn_action_one()返回\n”);
}
其他的
{
bool失败=错误;
//此任务现在需要执行多次。。。
对于(int i=0;i
FAILED
SUCCESS
的值是什么?我将使用真实代码中的值添加枚举什么是
FAILED
SUCCESS
的值?我将使用真实代码中的值添加枚举这可能是一个太大的更改,无法在我的代码库中的任何地方使用,即使它是正确的要做的事情:(,但我可以在某些地方使用它并获得新的代码位,谢谢!对于在我的代码库中的任何地方使用此
来说,这可能是一个太大的更改,即使这是正确的做法:(,但我可以在某些地方使用它并获得新的代码位,谢谢!非常有用,谢谢:),但我想尝试保持更多的c++定义(非常有用,谢谢:),但我想尝试保持更多的c++是的,我认为这可能是最好的解决方案,让我“重新适应”我的代码,直到我可以完成某种完整的过渡:o。谢谢:)是的,我认为这可能是最好的解决方案,我“复古适合”我的代码,直到我可以做一些全面的过渡:o。谢谢:)