[79308c8e] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // result.c -- Contains the expected value or an error value. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Wed May 25 15:40:00 2017 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed May 25 17:02:00 2017 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <containers/result> |
---|
| 17 | #include <assert> |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | forall(otype T, otype E) |
---|
| 21 | void ?{}(result(T, E) * this) { |
---|
| 22 | this->has_value = false; |
---|
[64fc0ba] | 23 | (&this->error){}; |
---|
[79308c8e] | 24 | } |
---|
| 25 | |
---|
| 26 | forall(otype T, otype E) |
---|
| 27 | void ?{}(result(T, E) * this, one_t, T value) { |
---|
| 28 | this->has_value = true; |
---|
| 29 | this->value = value; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | forall(otype T, otype E) |
---|
| 33 | void ?{}(result(T, E) * this, zero_t, E error) { |
---|
| 34 | this->has_value = false; |
---|
| 35 | this->error = error; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | forall(otype T, otype E) |
---|
| 39 | void ?{}(result(T, E) * this, result(T, E) other) { |
---|
| 40 | this->has_value = other.has_value; |
---|
| 41 | if (other.has_value) { |
---|
| 42 | this->value = other.value; |
---|
| 43 | } else { |
---|
| 44 | this->error = other.error; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | forall(otype T, otype E) |
---|
| 49 | void ^?{}(result(T, E) * this) { |
---|
| 50 | if (this->has_value) { |
---|
[64fc0ba] | 51 | ^(&this->value){}; |
---|
[79308c8e] | 52 | } else { |
---|
[64fc0ba] | 53 | ^(&this->error){}; |
---|
[79308c8e] | 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | forall(otype T, otype E) |
---|
[64fc0ba] | 58 | bool ?!=?(result(T, E) this, zero_t) { |
---|
| 59 | return !this.has_value; |
---|
[79308c8e] | 60 | } |
---|
| 61 | |
---|
| 62 | forall(otype T, otype E) |
---|
| 63 | result(T, E) result_value(T value) { |
---|
[64fc0ba] | 64 | return (result(T, E)){1, value}; |
---|
[79308c8e] | 65 | } |
---|
| 66 | |
---|
| 67 | forall(otype T, otype E) |
---|
| 68 | result(T, E) result_error(E error) { |
---|
[64fc0ba] | 69 | return (result(T, E)){0, error}; |
---|
[79308c8e] | 70 | } |
---|
| 71 | |
---|
| 72 | forall(otype T, otype E) |
---|
[64fc0ba] | 73 | bool has_value(result(T, E) * this) { |
---|
[79308c8e] | 74 | return this->has_value; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | forall(otype T, otype E) |
---|
| 78 | T get(result(T, E) * this) { |
---|
[64fc0ba] | 79 | assertf(this->has_value, "attempt to get from result without value"); |
---|
[79308c8e] | 80 | return this->value; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | forall(otype T, otype E) |
---|
| 84 | E get_error(result(T, E) * this) { |
---|
[64fc0ba] | 85 | assertf(this->has_value, "attempt to get from result without error"); |
---|
[79308c8e] | 86 | return this->error; |
---|
| 87 | } |
---|