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