问题描述
我正在开发使用大量文件系统函数的 lib。
我想要的是我的函数返回各种错误代码(不仅仅是-1作为错误),这取决于errno,以防文件系统函数失败。
虽然我可以直接使用 errno 值,但我想在我的函数 错误代码 和 系统 errno 之间创建一些抽象层(例如我的错误值从 -1000 开始并且为负,而 errno 值为正。
我的问题是什么是实现它的最佳方式。
现在我看到两种可能的解决方案:
- 使用带有错误代码的枚举和切换大小写函数进行翻译,例如:
typedef enum {
MY_ERROR_EPERM = -1104,/* Operation not permitted */
MY_ERROR_ENOENT = -1105,/* No such file or directory */
// ...
} MyReturnCodes_t;
int ErrnoToErrCode(unsigned int sysErrno) {
int error = ENOSYS;
switch(sysErrno) {
case EPERM: error = MY_ERROR_EPERM; break;
case ENOENT: error = MY_ERROR_ENOENT; break;
// ...
}
return error;
}
- 直接在枚举中使用翻译:
#define ERR_OFFSET -1000
typedef enum {
MY_ERROR_EPERM = ERR_OFFSET - EPERM,/* Operation not permitted */
MY_ERROR_ENOENT = ERR_OFFSET - ENOENT,/* No such file or directory */
MY_ERROR_ESRCH = ERR_OFFSET - ESRCH,/* No such process */
// ...
} MyReturnCodes_t;
哪种方式更稳定?
还有一点:这个库应该在 QNX 和 Linux OS 上使用,对齐 errno 代码的正确方法是什么(在某些情况下会有所不同)?
解决方法
我会在专用函数中使用 std::map
。只要您使用提供的错误宏,您就不必关心间隙或任何东西:
#include <iostream>
#include <errno.h>
#include <map>
namespace MyError
{
enum MyReturnCode: int
{
MY_INVALID_VAL = 0,/* Invalid Mapping */
MY_ERROR_EPERM = -1104,/* Operation not permitted */
MY_ERROR_ENOENT = -1105,/* No such file or directory */
};
MyReturnCode fromErrno(int e)
{
static const std::map<int,MyReturnCode> mapping {
{ EPERM,MY_ERROR_EPERM},{ ENOENT,MY_ERROR_ENOENT}
};
if(mapping.count(e))
return mapping.at(e);
else
return MY_INVALID_VAL;
}
}
int main()
{
std::cout << MyError::fromErrno(ENOENT) << std::endl;
std::cout << MyError::fromErrno(42) << std::endl;
return 0;
}