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 25 15:40:00 2017 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed May 25 17:02:00 2017 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include <containers/result> |
---|
17 | #include <assert> |
---|
18 | |
---|
19 | |
---|
20 | forall(otype T, otype E) |
---|
21 | void ?{}(result(T, E) * this) { |
---|
22 | this->has_value = false; |
---|
23 | (this->error){}; |
---|
24 | } |
---|
25 | |
---|
26 | forall(otype T, otype E) |
---|
27 | void ?{}(result(T, E) * this, one_t, T value) { |
---|
28 | this->has_value = true; |
---|
29 | this->value = value; |
---|
30 | } |
---|
31 | |
---|
32 | forall(otype T, otype E) |
---|
33 | void ?{}(result(T, E) * this, zero_t, E error) { |
---|
34 | this->has_value = false; |
---|
35 | this->error = error; |
---|
36 | } |
---|
37 | |
---|
38 | forall(otype T, otype E) |
---|
39 | void ?{}(result(T, E) * this, result(T, E) other) { |
---|
40 | this->has_value = other.has_value; |
---|
41 | if (other.has_value) { |
---|
42 | this->value = other.value; |
---|
43 | } else { |
---|
44 | this->error = other.error; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | forall(otype T, otype E) |
---|
49 | void ^?{}(result(T, E) * this) { |
---|
50 | if (this->has_value) { |
---|
51 | ^(this->value){}; |
---|
52 | } else { |
---|
53 | ^(this->error){}; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | forall(otype T, otype E) |
---|
58 | _Bool ?!=?(result(T, E) this, zero_t) { |
---|
59 | return !this->has_value; |
---|
60 | } |
---|
61 | |
---|
62 | forall(otype T, otype E) |
---|
63 | result(T, E) result_value(T value) { |
---|
64 | return (Result(T, E)){1, value}; |
---|
65 | } |
---|
66 | |
---|
67 | forall(otype T, otype E) |
---|
68 | result(T, E) result_error(E error) { |
---|
69 | return (Result(T, E)){0, value}; |
---|
70 | } |
---|
71 | |
---|
72 | forall(otype T, otype E) |
---|
73 | _Bool has_value(result(T, E) * this) { |
---|
74 | return this->has_value; |
---|
75 | } |
---|
76 | |
---|
77 | forall(otype T, otype E) |
---|
78 | T get(result(T, E) * this) { |
---|
79 | assertf(this->has_value, "attempt to get from result without value"); |
---|
80 | return this->value; |
---|
81 | } |
---|
82 | |
---|
83 | forall(otype T, otype E) |
---|
84 | E get_error(result(T, E) * this) { |
---|
85 | assertf(this->has_value, "attempt to get from result without error"); |
---|
86 | return this->error; |
---|
87 | } |
---|