| 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 | // maybe.c -- May contain a 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:22:03 2019
|
|---|
| 13 | // Update Count : 3
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <containers/maybe.hfa>
|
|---|
| 17 | #include <assert.h>
|
|---|
| 18 |
|
|---|
| 19 | #pragma GCC visibility push(default)
|
|---|
| 20 |
|
|---|
| 21 | forall(T)
|
|---|
| 22 | void ?{}(maybe(T) & this) {
|
|---|
| 23 | this.has_value = false;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | forall(T)
|
|---|
| 27 | void ?{}(maybe(T) & this, T value) {
|
|---|
| 28 | this.has_value = true;
|
|---|
| 29 | (this.value){value};
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | forall(T)
|
|---|
| 33 | void ?{}(maybe(T) & this, maybe(T) other) {
|
|---|
| 34 | this.has_value = other.has_value;
|
|---|
| 35 | if (other.has_value) {
|
|---|
| 36 | (this.value){other.value};
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | forall(T)
|
|---|
| 41 | maybe(T) ?=?(maybe(T) & this, maybe(T) that) {
|
|---|
| 42 | if (this.has_value && that.has_value) {
|
|---|
| 43 | this.value = that.value;
|
|---|
| 44 | } else if (this.has_value) {
|
|---|
| 45 | ^(this.value){};
|
|---|
| 46 | this.has_value = false;
|
|---|
| 47 | } else if (that.has_value) {
|
|---|
| 48 | this.has_value = true;
|
|---|
| 49 | (this.value){that.value};
|
|---|
| 50 | }
|
|---|
| 51 | return this;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | forall(T)
|
|---|
| 55 | void ^?{}(maybe(T) & this) {
|
|---|
| 56 | if (this.has_value) {
|
|---|
| 57 | ^(this.value){};
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | forall(T)
|
|---|
| 62 | bool ?!=?(maybe(T) this, zero_t) {
|
|---|
| 63 | return this.has_value;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | forall(T)
|
|---|
| 67 | maybe(T) maybe_value(T value) {
|
|---|
| 68 | return (maybe(T)){value};
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | forall(T)
|
|---|
| 72 | maybe(T) maybe_none() {
|
|---|
| 73 | return (maybe(T)){};
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | forall(T)
|
|---|
| 77 | bool has_value(maybe(T) * this) {
|
|---|
| 78 | return this->has_value;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | forall(T)
|
|---|
| 82 | T get(maybe(T) * this) {
|
|---|
| 83 | assertf(this->has_value, "attempt to get from maybe without value");
|
|---|
| 84 | return this->value;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | forall(T)
|
|---|
| 88 | void set(maybe(T) * this, T value) {
|
|---|
| 89 | if (this->has_value) {
|
|---|
| 90 | this->value = value;
|
|---|
| 91 | } else {
|
|---|
| 92 | this->has_value = true;
|
|---|
| 93 | (this->value){value};
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | forall(T)
|
|---|
| 98 | void set_none(maybe(T) * this) {
|
|---|
| 99 | if (this->has_value) {
|
|---|
| 100 | this->has_value = false;
|
|---|
| 101 | ^(this->value){};
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|