source: libcfa/src/containers/result.cfa @ c3ee5f3

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c3ee5f3 was ee06e41b, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add char, signed char, unsigned char to basic types in prologue

  • Property mode set to 100644
File size: 2.6 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// result.c -- Contains the expected value or an error 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:24:04 2019
13// Update Count     : 3
14//
15
16#include <containers/result.hfa>
17#include <assert.h>
18
19
20forall(otype T, otype E)
21void ?{}(result(T, E) & this) {
22        this.has_value = false;
23        (this.error){};
24}
25
26forall(otype T, otype E)
27void ?{}(result(T, E) & this, one_t, T value) {
28        this.has_value = true;
29        (this.value){value};
30}
31
32forall(otype T, otype E)
33void ?{}(result(T, E) & this, zero_t, E error) {
34        this.has_value = false;
35        (this.error){error};
36}
37
38forall(otype T, otype E)
39void ?{}(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
48forall(otype T, otype E)
49result(T, E) ?=?(result(T, E) & this, result(T, E) that) {
50        if (this.has_value && that.has_value) {
51                this.value = that.value;
52        } else if (this.has_value) {
53                ^(this.value){};
54                this.has_value = false;
55                (this.error){that.error};
56        } else if (that.has_value) {
57                ^(this.error){};
58                this.has_value = true;
59                (this.value){that.value};
60        } else {
61                this.error = that.error;
62        }
63}
64
65forall(otype T, otype E)
66void ^?{}(result(T, E) & this) {
67        if (this.has_value) {
68                ^(this.value){};
69        } else {
70                ^(this.error){};
71        }
72}
73
74forall(otype T, otype E)
75bool ?!=?(result(T, E) this, zero_t) {
76        return this.has_value;
77}
78
79forall(otype T, otype E)
80result(T, E) result_value(T value) {
81        return (result(T, E)){1, value};
82}
83
84forall(otype T, otype E)
85result(T, E) result_error(E error) {
86        return (result(T, E)){0, error};
87}
88
89forall(otype T, otype E)
90bool has_value(result(T, E) * this) {
91        return this->has_value;
92}
93
94forall(otype T, otype E)
95T get(result(T, E) * this) {
96        assertf(this->has_value, "attempt to get from result without value");
97        return this->value;
98}
99
100forall(otype T, otype E)
101E get_error(result(T, E) * this) {
102        assertf(!this->has_value, "attempt to get from result without error");
103        return this->error;
104}
105
106forall(otype T, otype E)
107void set(result(T, E) * this, T value) {
108        if (this->has_value) {
109                this->value = value;
110        } else {
111                ^(this->error){};
112                this->has_value = true;
113                (this->value){value};
114        }
115}
116
117forall(otype T, otype E)
118void set_error(result(T, E) * this, E error) {
119        if (this->has_value) {
120                ^(this->value){};
121                this->has_value = false;
122                (this->error){error};
123        } else {
124                this->error = error;
125        }
126}
Note: See TracBrowser for help on using the repository browser.