cgrapht 1.0.0
A modern C++20 header-only graph library
Loading...
Searching...
No Matches
models.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <stdexcept>
7#include <variant>
8
9namespace cgrapht {
10
18 template <typename SUCCESS, typename ERROR> class Result {
19 private:
20 std::variant<std::monostate, SUCCESS, ERROR> result {};
21 bool is_success {true};
22 Result() = default;
23
24 public:
30 static Result success(const SUCCESS& s) {
31 Result r;
32 r.result = s;
33 return r;
34 }
35
41 static Result success(SUCCESS&& s) {
42 Result r;
43 r.result = std::move(s);
44 return r;
45 }
46
52 static Result error(const ERROR& e) {
53 Result r;
54 r.result = e;
55 r.is_success = false;
56 return r;
57 }
58
64 static Result error(ERROR&& e) {
65 Result r;
66 r.result = std::move(e);
67 r.is_success = false;
68 return r;
69 }
70
74 bool operator==(const Result& other) const {
75 return is_success == other.is_success && result == other.result;
76 }
77
82 [[nodiscard]] bool is_ok() const {
83 return std::holds_alternative<SUCCESS>(result);
84 }
85
91 [[nodiscard]] const SUCCESS& get_ok() const & {
92 if (is_success) {
93 return std::get<SUCCESS>(result);
94 }
95 throw std::runtime_error("Cannot get ok value from error result");
96 }
97
103 [[nodiscard]] SUCCESS consume_ok() && {
104 if (is_ok()) {
105 return std::move(std::get<SUCCESS>(result));
106 }
107 throw std::runtime_error("Cannot consume ok value from error result");
108 }
109
115 [[nodiscard]] const ERROR& get_error() const & {
116 if (!is_ok()) {
117 return std::get<ERROR>(result);
118 }
119 throw std::runtime_error("Cannot get error value from success result");
120 }
121
127 [[nodiscard]] ERROR consume_error() && {
128 if (!is_ok()) {
129 return std::move(std::get<ERROR>(result));
130 }
131 throw std::runtime_error("Cannot consume error value from success result");
132 }
133 };
134
155
157}
Result wrapper for success or error values.
Definition models.hpp:18
SUCCESS consume_ok() &&
Move success value out of the result.
Definition models.hpp:103
const ERROR & get_error() const &
Access error value as const reference.
Definition models.hpp:115
static Result error(const ERROR &e)
Construct an error result from a const reference.
Definition models.hpp:52
ERROR consume_error() &&
Move the error value out of the result.
Definition models.hpp:127
bool is_ok() const
Check if the result represents success.
Definition models.hpp:82
static Result error(ERROR &&e)
Construct an error result from an rvalue.
Definition models.hpp:64
static Result success(SUCCESS &&s)
Construct a success result from an rvalue.
Definition models.hpp:41
static Result success(const SUCCESS &s)
Construct a success result from a const reference.
Definition models.hpp:30
bool operator==(const Result &other) const
Equality comparison based on status and payload.
Definition models.hpp:74
const SUCCESS & get_ok() const &
Access success value as const reference.
Definition models.hpp:91
ErrorType
Error codes returned by graph operations.
Definition models.hpp:147
@ UNKNOWN
An unexpected internal error occurred.
@ VERTEX_NOT_FREE
The vertex cannot be removed because it still has incident edges.
@ ABSENT_VERTEX
The referenced vertex ID does not exist in the graph.
@ EDGE_ALREADY_EXISTS
An edge between the two vertices already exists.
@ INVALID_ARGUMENT
A supplied argument is invalid.
@ ABSENT_EDGE
The referenced edge ID does not exist in the graph.