source: libcfa/src/collections/result.cfa @ 1f11818

Last change on this file since 1f11818 was 55b060d, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

rename directories containers to collections

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