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