Lightgraph
C++17 light-graph engine API reference
Loading...
Searching...
No Matches
status.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <utility>
5
6namespace lightgraph {
7
28
32class Status {
33 public:
34 Status() = default;
35
36 Status(ErrorCode code, std::string message) : code_(code), message_(std::move(message)) {}
37
41 static Status success() { return Status(); }
42
46 static Status error(ErrorCode code, std::string message) {
47 return Status(code, std::move(message));
48 }
49
53 bool ok() const { return code_ == ErrorCode::Ok; }
54
55 explicit operator bool() const { return ok(); }
56
60 ErrorCode code() const { return code_; }
61
65 const std::string& message() const { return message_; }
66
67 private:
69 std::string message_;
70};
71
72template <typename T> class Result {
73 public:
75 : value_(std::move(value)), status_(std::move(status)) {}
76
82 static Result<T> error(ErrorCode code, std::string message) {
83 return Result<T>(T{}, Status::error(code, std::move(message)));
84 }
85
89 bool ok() const { return status_.ok(); }
90
91 explicit operator bool() const { return ok(); }
92
98 const T& value() const { return value_; }
99
100 T& value() { return value_; }
101
105 const Status& status() const { return status_; }
106
107 private:
108 T value_;
109 Status status_;
110};
111
112} // namespace lightgraph
const T & value() const
Access the contained value.
Definition status.hpp:98
static Result< T > error(ErrorCode code, std::string message)
Construct an error result.
Definition status.hpp:82
Result(T value, Status status=Status::success())
Definition status.hpp:74
bool ok() const
True when result status is success.
Definition status.hpp:89
const Status & status() const
Access the operation status.
Definition status.hpp:105
Operation status object.
Definition status.hpp:32
static Status success()
Construct a success status.
Definition status.hpp:41
static Status error(ErrorCode code, std::string message)
Construct an error status with code and message.
Definition status.hpp:46
const std::string & message() const
Return the human-readable error message.
Definition status.hpp:65
Status(ErrorCode code, std::string message)
Definition status.hpp:36
ErrorCode code() const
Return the stable error code.
Definition status.hpp:60
bool ok() const
True when status code is ErrorCode::Ok.
Definition status.hpp:53
ErrorCode
Stable error codes returned by high-level API operations.
Definition status.hpp:16