[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 |
---|
[58daf53] | 10 | // Created On : Wed May 24 15:40:00 2017 |
---|
[91c389a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[55b060d] | 12 | // Last Modified On : Wed Aug 30 21:27:22 2023 |
---|
| 13 | // Update Count : 4 |
---|
[79308c8e] | 14 | // |
---|
| 15 | |
---|
[55b060d] | 16 | #include <collections/result.hfa> |
---|
[91c389a] | 17 | #include <assert.h> |
---|
[79308c8e] | 18 | |
---|
[accc9df9] | 19 | #pragma GCC visibility push(default) |
---|
[79308c8e] | 20 | |
---|
[fd54fef] | 21 | forall(T, E) |
---|
[242a902] | 22 | void ?{}(result(T, E) & this) { |
---|
| 23 | this.has_value = false; |
---|
| 24 | (this.error){}; |
---|
[79308c8e] | 25 | } |
---|
| 26 | |
---|
[fd54fef] | 27 | forall(T, E) |
---|
[242a902] | 28 | void ?{}(result(T, E) & this, one_t, T value) { |
---|
| 29 | this.has_value = true; |
---|
| 30 | (this.value){value}; |
---|
[79308c8e] | 31 | } |
---|
| 32 | |
---|
[fd54fef] | 33 | forall(T, E) |
---|
[242a902] | 34 | void ?{}(result(T, E) & this, zero_t, E error) { |
---|
| 35 | this.has_value = false; |
---|
| 36 | (this.error){error}; |
---|
[79308c8e] | 37 | } |
---|
| 38 | |
---|
[fd54fef] | 39 | forall(T, E) |
---|
[242a902] | 40 | void ?{}(result(T, E) & this, result(T, E) other) { |
---|
| 41 | this.has_value = other.has_value; |
---|
[79308c8e] | 42 | if (other.has_value) { |
---|
[242a902] | 43 | (this.value){other.value}; |
---|
[79308c8e] | 44 | } else { |
---|
[242a902] | 45 | (this.error){other.error}; |
---|
[58daf53] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
[fd54fef] | 49 | forall(T, E) |
---|
[242a902] | 50 | result(T, E) ?=?(result(T, E) & this, result(T, E) that) { |
---|
[ee06e41b] | 51 | if (this.has_value && that.has_value) { |
---|
[242a902] | 52 | this.value = that.value; |
---|
| 53 | } else if (this.has_value) { |
---|
| 54 | ^(this.value){}; |
---|
| 55 | this.has_value = false; |
---|
| 56 | (this.error){that.error}; |
---|
[58daf53] | 57 | } else if (that.has_value) { |
---|
[242a902] | 58 | ^(this.error){}; |
---|
| 59 | this.has_value = true; |
---|
| 60 | (this.value){that.value}; |
---|
[58daf53] | 61 | } else { |
---|
[242a902] | 62 | this.error = that.error; |
---|
[79308c8e] | 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
[fd54fef] | 66 | forall(T, E) |
---|
[242a902] | 67 | void ^?{}(result(T, E) & this) { |
---|
| 68 | if (this.has_value) { |
---|
| 69 | ^(this.value){}; |
---|
[79308c8e] | 70 | } else { |
---|
[242a902] | 71 | ^(this.error){}; |
---|
[79308c8e] | 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[fd54fef] | 75 | forall(T, E) |
---|
[64fc0ba] | 76 | bool ?!=?(result(T, E) this, zero_t) { |
---|
[20877d2] | 77 | return this.has_value; |
---|
[79308c8e] | 78 | } |
---|
| 79 | |
---|
[fd54fef] | 80 | forall(T, E) |
---|
[79308c8e] | 81 | result(T, E) result_value(T value) { |
---|
[64fc0ba] | 82 | return (result(T, E)){1, value}; |
---|
[79308c8e] | 83 | } |
---|
| 84 | |
---|
[fd54fef] | 85 | forall(T, E) |
---|
[79308c8e] | 86 | result(T, E) result_error(E error) { |
---|
[64fc0ba] | 87 | return (result(T, E)){0, error}; |
---|
[79308c8e] | 88 | } |
---|
| 89 | |
---|
[fd54fef] | 90 | forall(T, E) |
---|
[64fc0ba] | 91 | bool has_value(result(T, E) * this) { |
---|
[79308c8e] | 92 | return this->has_value; |
---|
| 93 | } |
---|
| 94 | |
---|
[fd54fef] | 95 | forall(T, E) |
---|
[79308c8e] | 96 | T get(result(T, E) * this) { |
---|
[64fc0ba] | 97 | assertf(this->has_value, "attempt to get from result without value"); |
---|
[79308c8e] | 98 | return this->value; |
---|
| 99 | } |
---|
| 100 | |
---|
[fd54fef] | 101 | forall(T, E) |
---|
[79308c8e] | 102 | E get_error(result(T, E) * this) { |
---|
[20877d2] | 103 | assertf(!this->has_value, "attempt to get from result without error"); |
---|
[79308c8e] | 104 | return this->error; |
---|
| 105 | } |
---|
[58daf53] | 106 | |
---|
[fd54fef] | 107 | forall(T, E) |
---|
[58daf53] | 108 | void set(result(T, E) * this, T value) { |
---|
| 109 | if (this->has_value) { |
---|
| 110 | this->value = value; |
---|
| 111 | } else { |
---|
[242a902] | 112 | ^(this->error){}; |
---|
[58daf53] | 113 | this->has_value = true; |
---|
[242a902] | 114 | (this->value){value}; |
---|
[58daf53] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[fd54fef] | 118 | forall(T, E) |
---|
[58daf53] | 119 | void set_error(result(T, E) * this, E error) { |
---|
| 120 | if (this->has_value) { |
---|
[242a902] | 121 | ^(this->value){}; |
---|
[58daf53] | 122 | this->has_value = false; |
---|
[242a902] | 123 | (this->error){error}; |
---|
[58daf53] | 124 | } else { |
---|
| 125 | this->error = error; |
---|
| 126 | } |
---|
| 127 | } |
---|