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
26
30class Status {
31 public:
32 Status() = default;
33
34 Status(ErrorCode code, std::string message) : code_(code), message_(std::move(message)) {}
35
39 static Status success() { return Status(); }
40
44 static Status error(ErrorCode code, std::string message) {
45 return Status(code, std::move(message));
46 }
47
51 bool ok() const { return code_ == ErrorCode::Ok; }
52
53 explicit operator bool() const { return ok(); }
54
58 ErrorCode code() const { return code_; }
59
63 const std::string& message() const { return message_; }
64
65 private:
67 std::string message_;
68};
69
70template <typename T> class Result {
71 public:
73 : value_(std::move(value)), status_(std::move(status)) {}
74
80 static Result<T> error(ErrorCode code, std::string message) {
81 return Result<T>(T{}, Status::error(code, std::move(message)));
82 }
83
87 bool ok() const { return status_.ok(); }
88
89 explicit operator bool() const { return ok(); }
90
96 const T& value() const { return value_; }
97
98 T& value() { return value_; }
99
103 const Status& status() const { return status_; }
104
105 private:
106 T value_;
107 Status status_;
108};
109
110} // namespace lightgraph
const T & value() const
Access the contained value.
Definition status.hpp:96
static Result< T > error(ErrorCode code, std::string message)
Construct an error result.
Definition status.hpp:80
Result(T value, Status status=Status::success())
Definition status.hpp:72
bool ok() const
True when result status is success.
Definition status.hpp:87
const Status & status() const
Access the operation status.
Definition status.hpp:103
Operation status object.
Definition status.hpp:30
static Status success()
Construct a success status.
Definition status.hpp:39
static Status error(ErrorCode code, std::string message)
Construct an error status with code and message.
Definition status.hpp:44
const std::string & message() const
Return the human-readable error message.
Definition status.hpp:63
Status(ErrorCode code, std::string message)
Definition status.hpp:34
ErrorCode code() const
Return the stable error code.
Definition status.hpp:58
bool ok() const
True when status code is ErrorCode::Ok.
Definition status.hpp:51
ErrorCode
Stable error codes returned by high-level API operations.
Definition status.hpp:16