// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // result.c -- Contains the expected value or an error value. // // Author : Andrew Beach // Created On : Wed May 25 15:40:00 2017 // Last Modified By : Andrew Beach // Last Modified On : Wed May 25 17:02:00 2017 // Update Count : 1 // #include #include forall(otype T, otype E) void ?{}(result(T, E) * this) { this->has_value = false; (&this->error){}; } forall(otype T, otype E) void ?{}(result(T, E) * this, one_t, T value) { this->has_value = true; this->value = value; } forall(otype T, otype E) void ?{}(result(T, E) * this, zero_t, E error) { this->has_value = false; this->error = error; } forall(otype T, otype E) void ?{}(result(T, E) * this, result(T, E) other) { this->has_value = other.has_value; if (other.has_value) { this->value = other.value; } else { this->error = other.error; } } forall(otype T, otype E) void ^?{}(result(T, E) * this) { if (this->has_value) { ^(&this->value){}; } else { ^(&this->error){}; } } forall(otype T, otype E) bool ?!=?(result(T, E) this, zero_t) { return !this.has_value; } forall(otype T, otype E) result(T, E) result_value(T value) { return (result(T, E)){1, value}; } forall(otype T, otype E) result(T, E) result_error(E error) { return (result(T, E)){0, error}; } forall(otype T, otype E) bool has_value(result(T, E) * this) { return this->has_value; } forall(otype T, otype E) T get(result(T, E) * this) { assertf(this->has_value, "attempt to get from result without value"); return this->value; } forall(otype T, otype E) E get_error(result(T, E) * this) { assertf(this->has_value, "attempt to get from result without error"); return this->error; }