mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-17 08:57:26 +00:00
tint: Implement const eval of binary multiply
Bug: tint:1581 Change-Id: I70ff40ed4d8faf0a665824fef936ffbafb3f0948 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99362 Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Antonio Maiorano <amaiorano@google.com> Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
ae6f76fe3a
commit
c20c5dfb4a
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <ostream>
|
||||
#include <variant>
|
||||
#include "src/tint/debug.h"
|
||||
|
||||
namespace tint::utils {
|
||||
|
||||
@@ -36,6 +37,9 @@ struct [[nodiscard]] Result {
|
||||
static_assert(!std::is_same_v<SUCCESS_TYPE, FAILURE_TYPE>,
|
||||
"Result must not have the same type for SUCCESS_TYPE and FAILURE_TYPE");
|
||||
|
||||
/// Default constructor initializes to invalid state
|
||||
Result() : value(std::monostate{}) {}
|
||||
|
||||
/// Constructor
|
||||
/// @param success the success result
|
||||
Result(const SUCCESS_TYPE& success) // NOLINT(runtime/explicit):
|
||||
@@ -47,27 +51,43 @@ struct [[nodiscard]] Result {
|
||||
: value{failure} {}
|
||||
|
||||
/// @returns true if the result was a success
|
||||
operator bool() const { return std::holds_alternative<SUCCESS_TYPE>(value); }
|
||||
operator bool() const {
|
||||
Validate();
|
||||
return std::holds_alternative<SUCCESS_TYPE>(value);
|
||||
}
|
||||
|
||||
/// @returns true if the result was a failure
|
||||
bool operator!() const { return std::holds_alternative<FAILURE_TYPE>(value); }
|
||||
bool operator!() const {
|
||||
Validate();
|
||||
return std::holds_alternative<FAILURE_TYPE>(value);
|
||||
}
|
||||
|
||||
/// @returns the success value
|
||||
/// @warning attempting to call this when the Result holds an failure will result in UB.
|
||||
const SUCCESS_TYPE* operator->() const { return &std::get<SUCCESS_TYPE>(value); }
|
||||
const SUCCESS_TYPE* operator->() const {
|
||||
Validate();
|
||||
return &(Get());
|
||||
}
|
||||
|
||||
/// @returns the success value
|
||||
/// @warning attempting to call this when the Result holds an failure value will result in UB.
|
||||
const SUCCESS_TYPE& Get() const { return std::get<SUCCESS_TYPE>(value); }
|
||||
const SUCCESS_TYPE& Get() const {
|
||||
Validate();
|
||||
return std::get<SUCCESS_TYPE>(value);
|
||||
}
|
||||
|
||||
/// @returns the failure value
|
||||
/// @warning attempting to call this when the Result holds a success value will result in UB.
|
||||
const FAILURE_TYPE& Failure() const { return std::get<FAILURE_TYPE>(value); }
|
||||
const FAILURE_TYPE& Failure() const {
|
||||
Validate();
|
||||
return std::get<FAILURE_TYPE>(value);
|
||||
}
|
||||
|
||||
/// Equality operator
|
||||
/// @param val the value to compare this Result to
|
||||
/// @returns true if this result holds a success value equal to `value`
|
||||
bool operator==(SUCCESS_TYPE val) const {
|
||||
Validate();
|
||||
if (auto* v = std::get_if<SUCCESS_TYPE>(&value)) {
|
||||
return *v == val;
|
||||
}
|
||||
@@ -78,14 +98,18 @@ struct [[nodiscard]] Result {
|
||||
/// @param val the value to compare this Result to
|
||||
/// @returns true if this result holds a failure value equal to `value`
|
||||
bool operator==(FAILURE_TYPE val) const {
|
||||
Validate();
|
||||
if (auto* v = std::get_if<FAILURE_TYPE>(&value)) {
|
||||
return *v == val;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
void Validate() const { TINT_ASSERT(Utils, !std::holds_alternative<std::monostate>(value)); }
|
||||
|
||||
/// The result. Either a success of failure value.
|
||||
std::variant<SUCCESS_TYPE, FAILURE_TYPE> value;
|
||||
std::variant<std::monostate, SUCCESS_TYPE, FAILURE_TYPE> value;
|
||||
};
|
||||
|
||||
/// Writes the result to the ostream.
|
||||
|
||||
Reference in New Issue
Block a user