source:
libcfa/src/containers/maybe.cfa
@
edc6ea2
Last change on this file since edc6ea2 was fd54fef, checked in by , 4 years ago | |
---|---|
|
|
File size: 1.8 KB |
Rev | Line | |
---|---|---|
[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 | // maybe.c -- May contain a 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 |
[ee06e41b] | 12 | // Last Modified On : Sun Feb 17 11:22:03 2019 |
13 | // Update Count : 3 | |
[79308c8e] | 14 | // |
15 | ||
[58b6d1b] | 16 | #include <containers/maybe.hfa> |
[91c389a] | 17 | #include <assert.h> |
[79308c8e] | 18 | |
19 | ||
[fd54fef] | 20 | forall(T) |
[aca65621] | 21 | void ?{}(maybe(T) & this) { |
22 | this.has_value = false; | |
[79308c8e] | 23 | } |
24 | ||
[fd54fef] | 25 | forall(T) |
[aca65621] | 26 | void ?{}(maybe(T) & this, T value) { |
27 | this.has_value = true; | |
28 | (this.value){value}; | |
[79308c8e] | 29 | } |
30 | ||
[fd54fef] | 31 | forall(T) |
[aca65621] | 32 | void ?{}(maybe(T) & this, maybe(T) other) { |
33 | this.has_value = other.has_value; | |
[79308c8e] | 34 | if (other.has_value) { |
[aca65621] | 35 | (this.value){other.value}; |
[58daf53] | 36 | } |
37 | } | |
38 | ||
[fd54fef] | 39 | forall(T) |
[aca65621] | 40 | maybe(T) ?=?(maybe(T) & this, maybe(T) that) { |
[ee06e41b] | 41 | if (this.has_value && that.has_value) { |
[aca65621] | 42 | this.value = that.value; |
43 | } else if (this.has_value) { | |
44 | ^(this.value){}; | |
45 | this.has_value = false; | |
[58daf53] | 46 | } else if (that.has_value) { |
[aca65621] | 47 | this.has_value = true; |
48 | (this.value){that.value}; | |
[79308c8e] | 49 | } |
[aca65621] | 50 | return this; |
[79308c8e] | 51 | } |
52 | ||
[fd54fef] | 53 | forall(T) |
[aca65621] | 54 | void ^?{}(maybe(T) & this) { |
55 | if (this.has_value) { | |
56 | ^(this.value){}; | |
[79308c8e] | 57 | } |
58 | } | |
59 | ||
[fd54fef] | 60 | forall(T) |
[64fc0ba] | 61 | bool ?!=?(maybe(T) this, zero_t) { |
[58daf53] | 62 | return this.has_value; |
[79308c8e] | 63 | } |
64 | ||
[fd54fef] | 65 | forall(T) |
[79308c8e] | 66 | maybe(T) maybe_value(T value) { |
[64fc0ba] | 67 | return (maybe(T)){value}; |
[79308c8e] | 68 | } |
69 | ||
[fd54fef] | 70 | forall(T) |
[79308c8e] | 71 | maybe(T) maybe_none() { |
[64fc0ba] | 72 | return (maybe(T)){}; |
[79308c8e] | 73 | } |
74 | ||
[fd54fef] | 75 | forall(T) |
[64fc0ba] | 76 | bool has_value(maybe(T) * this) { |
[79308c8e] | 77 | return this->has_value; |
78 | } | |
79 | ||
[fd54fef] | 80 | forall(T) |
[79308c8e] | 81 | T get(maybe(T) * this) { |
82 | assertf(this->has_value, "attempt to get from maybe without value"); | |
83 | return this->value; | |
84 | } | |
[58daf53] | 85 | |
[fd54fef] | 86 | forall(T) |
[58daf53] | 87 | void set(maybe(T) * this, T value) { |
88 | if (this->has_value) { | |
89 | this->value = value; | |
90 | } else { | |
91 | this->has_value = true; | |
[aca65621] | 92 | (this->value){value}; |
[58daf53] | 93 | } |
94 | } | |
95 | ||
[fd54fef] | 96 | forall(T) |
[58daf53] | 97 | void set_none(maybe(T) * this) { |
98 | if (this->has_value) { | |
99 | this->has_value = false; | |
[aca65621] | 100 | ^(this->value){}; |
[58daf53] | 101 | } |
102 | } |
Note: See TracBrowser
for help on using the repository browser.