source: src/libcfa/containers/result.c @ 79308c8e

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 79308c8e was 79308c8e, checked in by Andrew Beach <ajbeach@…>, 4 years ago

First draft of maybe and result.

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