Migrate to error constants in glacier
This commit is contained in:
parent
3ab9b4d818
commit
0b86a94f14
30 changed files with 171 additions and 114 deletions
41
lib/glacier/status/error_or.h
Normal file
41
lib/glacier/status/error_or.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class ErrorOr {
|
||||
public:
|
||||
ErrorOr() = delete;
|
||||
ErrorOr(const ErrorOr&) = delete;
|
||||
ErrorOr(ErrorOr&&) = delete;
|
||||
|
||||
ErrorOr(ErrorCode code) : error_(code), ok_(false) {}
|
||||
ErrorOr(T obj) : obj_(obj), ok_(true) {}
|
||||
ErrorOr(T&& obj) : obj_(obj), ok_(true) {}
|
||||
|
||||
bool ok() { return ok_; }
|
||||
operator bool() { return ok_; }
|
||||
|
||||
T& value() { return obj_; }
|
||||
|
||||
ErrorCode error() { return error_; }
|
||||
|
||||
private:
|
||||
union {
|
||||
ErrorCode error_;
|
||||
T obj_;
|
||||
};
|
||||
bool ok_;
|
||||
};
|
||||
|
||||
#define ASSIGN_OR_RETURN(lhs, rhs) \
|
||||
\
|
||||
auto e##__LINE__ = rhs; \
|
||||
if (!e##__LINE__.ok()) { \
|
||||
return e##__LINE__.error(); \
|
||||
} \
|
||||
lhs = rhs.value();
|
||||
|
||||
} // namespace glcr
|
||||
Loading…
Add table
Add a link
Reference in a new issue