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