26 #include "graphar/macros.h"
28 #define GAR_RETURN_IF_(condition, status, _) \
30 if (GAR_PREDICT_FALSE(condition)) { \
36 #define GAR_RETURN_NOT_OK(status) \
38 ::graphar::Status __s = ::graphar::internal::GenericToStatus(status); \
39 GAR_RETURN_IF_(!__s.ok(), __s, GAR_STRINGIFY(status)); \
43 #define RETURN_NOT_ARROW_OK(status) \
45 if (GAR_PREDICT_FALSE(!status.ok())) { \
46 return ::graphar::Status::ArrowError(status.ToString()); \
50 #define GAR_RAISE_ERROR_IF_(condition, status, _) \
52 if (GAR_PREDICT_FALSE(condition)) { \
53 throw std::runtime_error(status.message()); \
58 #define GAR_RAISE_ERROR_NOT_OK(status) \
60 ::graphar::Status __s = ::graphar::internal::GenericToStatus(status); \
61 GAR_RAISE_ERROR_IF_(!__s.ok(), __s, GAR_STRINGIFY(status)); \
64 namespace graphar::util {
65 template <
typename Head>
66 void StringBuilderRecursive(std::ostringstream& stream, Head&& head) {
70 template <
typename Head,
typename... Tail>
71 void StringBuilderRecursive(std::ostringstream& stream, Head&& head,
73 StringBuilderRecursive(stream, std::forward<Head>(head));
74 StringBuilderRecursive(stream, std::forward<Tail>(tail)...);
77 template <
typename... Args>
78 std::string StringBuilder(Args&&... args) {
79 std::ostringstream ss;
80 StringBuilderRecursive(ss, std::forward<Args>(args)...);
89 enum class StatusCode : unsigned char {
129 if (state_ !=
nullptr) {
145 : state_((s.state_ == nullptr) ? nullptr : new State(*s.state_)) {}
147 inline Status(
Status&& s) noexcept : state_(s.state_) { s.state_ =
nullptr; }
159 template <
typename... Args>
160 static Status FromArgs(StatusCode
code, Args... args) {
161 return Status(
code, util::StringBuilder(std::forward<Args>(args)...));
165 template <
typename... Args>
167 return Status::FromArgs(StatusCode::kIOError, std::forward<Args>(args)...);
171 template <
typename... Args>
173 return Status::FromArgs(StatusCode::kKeyError, std::forward<Args>(args)...);
177 template <
typename... Args>
179 return Status::FromArgs(StatusCode::kTypeError,
180 std::forward<Args>(args)...);
187 template <
typename... Args>
189 return Status::FromArgs(StatusCode::kInvalid, std::forward<Args>(args)...);
196 template <
typename... Args>
198 return Status::FromArgs(StatusCode::kIndexError,
199 std::forward<Args>(args)...);
203 template <
typename... Args>
205 return Status::FromArgs(StatusCode::kYamlError,
206 std::forward<Args>(args)...);
210 template <
typename... Args>
212 return Status::FromArgs(StatusCode::kArrowError,
213 std::forward<Args>(args)...);
217 template <
typename... Args>
219 return Status::FromArgs(StatusCode::kUnknownError,
220 std::forward<Args>(args)...);
224 constexpr
bool ok()
const {
return (state_ ==
nullptr); }
230 return code() == StatusCode::kTypeError;
236 return code() == StatusCode::kIndexError;
240 return code() == StatusCode::kYamlError;
244 return code() == StatusCode::kArrowError;
248 constexpr StatusCode
code()
const {
249 return ok() ? StatusCode::kOK : state_->code;
254 static const std::string no_message =
"";
255 return ok() ? no_message : state_->msg;
273 namespace graphar::internal {
277 inline const Status& GenericToStatus(
const Status& st) {
return st; }
278 inline Status GenericToStatus(Status&& st) {
return std::move(st); }
Status outcome object (success or error)
Status(StatusCode code, const std::string &msg)
Constructs a status with the specified error code and message.
const std::string & message() const
static Status IndexError(Args &&... args)
Status & operator=(Status &&s) noexcept
static Status TypeError(Args &&... args)
static Status YamlError(Args &&... args)
constexpr bool ok() const
static Status UnknownError(Args &&... args)
static Status KeyError(Args &&... args)
static Status ArrowError(Args &&... args)
constexpr bool IsTypeError() const
constexpr bool IsKeyError() const
constexpr bool IsArrowError() const
constexpr bool IsInvalid() const
static Status Invalid(Args &&... args)
constexpr bool IsYamlError() const
constexpr bool IsIndexError() const
constexpr StatusCode code() const
Status(Status &&s) noexcept
static Status IOError(Args &&... args)