source: src/libcfa/containers/result.c @ 64fc0ba

ADTaaron-thesisarm-ehast-experimentalcleanup-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@…>, 7 years ago

Added maybe and result to libcfa makefiles and made some minor fixes

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[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// 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
20forall(otype T, otype E)
21void ?{}(result(T, E) * this) {
22        this->has_value = false;
[64fc0ba]23        (&this->error){};
[79308c8e]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)
49void ^?{}(result(T, E) * this) {
50        if (this->has_value) {
[64fc0ba]51                ^(&this->value){};
[79308c8e]52        } else {
[64fc0ba]53                ^(&this->error){};
[79308c8e]54        }
55}
56
57forall(otype T, otype E)
[64fc0ba]58bool ?!=?(result(T, E) this, zero_t) {
59        return !this.has_value;
[79308c8e]60}
61
62forall(otype T, otype E)
63result(T, E) result_value(T value) {
[64fc0ba]64        return (result(T, E)){1, value};
[79308c8e]65}
66
67forall(otype T, otype E)
68result(T, E) result_error(E error) {
[64fc0ba]69        return (result(T, E)){0, error};
[79308c8e]70}
71
72forall(otype T, otype E)
[64fc0ba]73bool has_value(result(T, E) * this) {
[79308c8e]74        return this->has_value;
75}
76
77forall(otype T, otype E)
78T get(result(T, E) * this) {
[64fc0ba]79        assertf(this->has_value, "attempt to get from result without value");
[79308c8e]80        return this->value;
81}
82
83forall(otype T, otype E)
84E get_error(result(T, E) * this) {
[64fc0ba]85        assertf(this->has_value, "attempt to get from result without error");
[79308c8e]86        return this->error;
87}
Note: See TracBrowser for help on using the repository browser.