aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since 64fc0ba was
64fc0ba,
checked in by Thierry Delisle <tdelisle@…>, 6 years ago
|
Added maybe and result to libcfa makefiles and made some minor fixes
|
-
Property mode set to
100644
|
File size:
1.3 KB
|
Line | |
---|
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 25 15:40:00 2017 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed May 25 17:00:00 2017 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include <containers/maybe> |
---|
17 | #include <assert> |
---|
18 | |
---|
19 | |
---|
20 | forall(otype T) |
---|
21 | void ?{}(maybe(T) * this) { |
---|
22 | this->has_value = false; |
---|
23 | } |
---|
24 | |
---|
25 | forall(otype T) |
---|
26 | void ?{}(maybe(T) * this, T value) { |
---|
27 | this->has_value = true; |
---|
28 | this->value = value; |
---|
29 | } |
---|
30 | |
---|
31 | forall(otype T) |
---|
32 | void ?{}(maybe(T) * this, maybe(T) other) { |
---|
33 | this->has_value = other.has_value; |
---|
34 | if (other.has_value) { |
---|
35 | this->value = other.value; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | forall(otype T) |
---|
40 | void ^?{}(maybe(T) * this) { |
---|
41 | if (this->has_value) { |
---|
42 | ^(&this->value){}; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | forall(otype T) |
---|
47 | bool ?!=?(maybe(T) this, zero_t) { |
---|
48 | return !this.has_value; |
---|
49 | } |
---|
50 | |
---|
51 | forall(otype T) |
---|
52 | maybe(T) maybe_value(T value) { |
---|
53 | return (maybe(T)){value}; |
---|
54 | } |
---|
55 | |
---|
56 | forall(otype T) |
---|
57 | maybe(T) maybe_none() { |
---|
58 | return (maybe(T)){}; |
---|
59 | } |
---|
60 | |
---|
61 | forall(otype T) |
---|
62 | bool has_value(maybe(T) * this) { |
---|
63 | return this->has_value; |
---|
64 | } |
---|
65 | |
---|
66 | forall(otype T) |
---|
67 | T get(maybe(T) * this) { |
---|
68 | assertf(this->has_value, "attempt to get from maybe without value"); |
---|
69 | return this->value; |
---|
70 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.