Skip to content

Commit

Permalink
osv: add error class
Browse files Browse the repository at this point in the history
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
avikivity committed Sep 2, 2013
1 parent e8c8a90 commit c0f7f0f
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions include/osv/error.h
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_ */

0 comments on commit c0f7f0f

Please sign in to comment.