cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
status.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <string_view>
5#include <type_traits>
6#include <utility>
7#include <variant>
8
9namespace cppfig {
10
12enum class StatusCode : std::uint8_t {
13 kOk = 0,
17};
18
23class Status {
24public:
26 Status() = default;
27
30 : code_(code)
31 , message_(std::move(message))
32 {
33 }
34
36 [[nodiscard]] auto ok() const noexcept -> bool { return code_ == StatusCode::kOk; }
37
39 [[nodiscard]] auto code() const noexcept -> StatusCode { return code_; }
40
42 [[nodiscard]] auto message() const noexcept -> std::string_view { return message_; }
43
44private:
46 std::string message_;
47};
48
49// Factory functions
50
52[[nodiscard]] inline auto OkStatus() -> Status { return {}; }
53
55[[nodiscard]] inline auto NotFoundError(std::string message) -> Status
56{
57 return { StatusCode::kNotFound, std::move(message) };
58}
59
61[[nodiscard]] inline auto InvalidArgumentError(std::string message) -> Status
62{
63 return { StatusCode::kInvalidArgument, std::move(message) };
64}
65
67[[nodiscard]] inline auto InternalError(std::string message) -> Status
68{
69 return { StatusCode::kInternal, std::move(message) };
70}
71
72// Status-code checkers
73
75[[nodiscard]] inline auto IsNotFound(const Status& status) noexcept -> bool
76{
77 return status.code() == StatusCode::kNotFound;
78}
79
81[[nodiscard]] inline auto IsInvalidArgument(const Status& status) noexcept -> bool
82{
83 return status.code() == StatusCode::kInvalidArgument;
84}
85
87[[nodiscard]] inline auto IsInternal(const Status& status) noexcept -> bool
88{
89 return status.code() == StatusCode::kInternal;
90}
91
99template <typename T>
100class StatusOr {
101 static_assert(!std::is_same_v<T, Status>, "StatusOr<Status> is not allowed");
102
103public:
105 StatusOr(T value) // NOLINT(google-explicit-constructor)
106 : storage_(std::move(value))
107 {
108 }
109
113 StatusOr(Status status) // NOLINT(google-explicit-constructor)
114 : storage_(std::move(status))
115 {
116 }
117
119 [[nodiscard]] auto ok() const noexcept -> bool { return std::holds_alternative<T>(storage_); }
120
124 [[nodiscard]] auto status() const& -> Status
125 {
126 if (ok()) {
127 return OkStatus();
128 }
129 return std::get<Status>(storage_);
130 }
131
133 [[nodiscard]] auto status() && -> Status
134 {
135 if (ok()) {
136 return OkStatus();
137 }
138 return std::get<Status>(std::move(storage_));
139 }
140
144 [[nodiscard]] auto value() const& -> const T& { return std::get<T>(storage_); }
145
149 [[nodiscard]] auto value() && -> T&& { return std::get<T>(std::move(storage_)); }
150
152 [[nodiscard]] auto operator*() const& -> const T& { return value(); }
153
155 [[nodiscard]] auto operator*() && -> T&& { return std::move(*this).value(); }
156
158 [[nodiscard]] auto operator->() const -> const T* { return &value(); }
159
160private:
161 std::variant<Status, T> storage_;
162};
163
164} // namespace cppfig
A value-or-error type, similar to std::expected (C++23).
Definition status.h:100
auto status() &&-> Status
Returns the error status (move).
Definition status.h:133
StatusOr(T value)
Constructs a StatusOr holding a value (implicit conversion).
Definition status.h:105
auto operator->() const -> const T *
Arrow operator for member access on the stored value.
Definition status.h:158
auto operator*() const &-> const T &
Dereferences to the stored value (const).
Definition status.h:152
auto value() &&-> T &&
Returns an rvalue reference to the value.
Definition status.h:149
auto value() const &-> const T &
Returns a const reference to the value.
Definition status.h:144
auto status() const &-> Status
Returns the error status.
Definition status.h:124
StatusOr(Status status)
Constructs a StatusOr holding an error status.
Definition status.h:113
auto operator*() &&-> T &&
Dereferences to the stored value (move).
Definition status.h:155
auto ok() const noexcept -> bool
Returns true if a value is present (no error).
Definition status.h:119
A lightweight status object carrying an error code and message.
Definition status.h:23
Status()=default
Constructs an OK status.
auto code() const noexcept -> StatusCode
Returns the error code.
Definition status.h:39
auto ok() const noexcept -> bool
Returns true if the status represents success.
Definition status.h:36
auto message() const noexcept -> std::string_view
Returns the error message (empty for OK status).
Definition status.h:42
Status(StatusCode code, std::string message)
Constructs a status with the given code and message.
Definition status.h:29
C++20 compile-time type-safe configuration library.
Definition conf.h:13
auto IsInternal(const Status &status) noexcept -> bool
Returns true if the status has code kInternal.
Definition status.h:87
StatusCode
Error codes used by cppfig operations.
Definition status.h:12
auto NotFoundError(std::string message) -> Status
Returns a NotFound error status.
Definition status.h:55
auto IsInvalidArgument(const Status &status) noexcept -> bool
Returns true if the status has code kInvalidArgument.
Definition status.h:81
auto IsNotFound(const Status &status) noexcept -> bool
Returns true if the status has code kNotFound.
Definition status.h:75
auto InternalError(std::string message) -> Status
Returns an Internal error status.
Definition status.h:67
auto OkStatus() -> Status
Returns an OK status.
Definition status.h:52
auto InvalidArgumentError(std::string message) -> Status
Returns an InvalidArgument error status.
Definition status.h:61