source: src/libcfa/containers/result.c@ 84d58c5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 84d58c5 was 64fc0ba, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

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

  • Property mode set to 100644
File size: 1.8 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)
58bool ?!=?(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, error};
70}
71
72forall(otype T, otype E)
73bool 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.