Created
June 27, 2021 09:53
-
-
Save ShaneEverittM/255e272eab753d96f00381d1880a9246 to your computer and use it in GitHub Desktop.
Simple Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DB { | |
class Result { | |
public: | |
enum Value : uint8_t { | |
Success, | |
Failure | |
}; | |
constexpr Result() = default; | |
// Coerce from these type if necessary, useful for writing returning Result::Success as a Result not a Value. | |
// Do not want explicit constructions. Also will not show as used ever. | |
[[maybe_unused]] constexpr Result(Value aResult) : value(aResult) {} | |
[[maybe_unused]] constexpr Result(bool aBool) : value(aBool ? Success : Failure) {} | |
constexpr operator Value() const { return value; } // Allow switch and comparisons. | |
constexpr bool operator==(Result a) const { return value == a.value; } | |
constexpr bool operator!=(Result a) const { return value != a.value; } | |
[[nodiscard]] constexpr bool succeeded() const { return value == Success; } | |
[[nodiscard]] constexpr bool failed() const { return value == Failure; } | |
private: | |
Value value; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment