-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Different source bases have different error conventions; libc has 0/-1+errno, while the rest os the source base uses 0/error. Wrap errors in a class to prevent confusion between the two.
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#ifndef ERROR_H_ | ||
#define ERROR_H_ | ||
|
||
#include <errno.h> | ||
|
||
struct error { | ||
#ifdef __cplusplus | ||
public: | ||
explicit error() = default; | ||
explicit error(int _errno) : _errno(_errno) {} | ||
error(const error& e) = default; | ||
bool bad() const { return _errno != 0; } | ||
int get() const { return _errno; } | ||
int to_libc() const; | ||
private: | ||
#endif | ||
int _errno = 0; | ||
}; | ||
|
||
typedef struct error error; | ||
|
||
static inline error no_error() | ||
{ | ||
return (error) { 0 }; | ||
} | ||
|
||
static inline struct error make_error(int _errno) | ||
{ | ||
return (error) { _errno }; | ||
} | ||
|
||
static inline bool error_bad(error e) | ||
{ | ||
#ifdef __cplusplus | ||
return e.bad(); | ||
#else | ||
return e._errno != 0; | ||
#endif | ||
} | ||
|
||
static inline int error_get(error e) | ||
{ | ||
#ifdef __cplusplus | ||
return e.get(); | ||
#else | ||
return e._errno; | ||
#endif | ||
} | ||
|
||
inline static int error_to_libc(error e) | ||
{ | ||
if (error_bad(e)) { | ||
return 0; | ||
} else { | ||
errno = error_get(e); | ||
return -1; | ||
} | ||
} | ||
|
||
#ifdef __cplusplus | ||
|
||
inline int error::to_libc() const | ||
{ | ||
return error_to_libc(*this); | ||
} | ||
|
||
#endif | ||
|
||
#endif /* ERROR_H_ */ |