source: src/libcfa/containers/maybe.c @ 58ed882

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 58ed882 was 79308c8e, checked in by Andrew Beach <ajbeach@…>, 7 years ago

First draft of maybe and result.

  • Property mode set to 100644
File size: 1.3 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// maybe.c -- May contain a 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:00:00 2017
13// Update Count     : 1
14//
15
16#include <containers/maybe>
17#include <assert>
18
19
20forall(otype T)
21void ?{}(maybe(T) * this) {
22        this->has_value = false;
23}
24
25forall(otype T)
26void ?{}(maybe(T) * this, T value) {
27        this->has_value = true;
28        this->value = value;
29}
30
31forall(otype T)
32void ?{}(maybe(T) * this, maybe(T) other) {
33        this->has_value = other.has_value;
34        if (other.has_value) {
35                this->value = other.value;
36        }
37}
38
39forall(otype T)
40void ^?{}(maybe(T) * this) {
41        if (this->has_value) {
42                ^(this->value){};
43        }
44}
45
46forall(otype T)
47_Bool ?!=?(result(T, E) this, zero_t) {
48        return !this->has_value;
49}
50
51forall(otype T)
52maybe(T) maybe_value(T value) {
53        return (Maybe(T)){value};
54}
55
56forall(otype T)
57maybe(T) maybe_none() {
58        return (Maybe(T)){};
59}
60
61forall(otype T)
62_Bool has_value(maybe(T) * this) {
63        return this->has_value;
64}
65
66forall(otype T)
67T get(maybe(T) * this) {
68        assertf(this->has_value, "attempt to get from maybe without value");
69        return this->value;
70}
Note: See TracBrowser for help on using the repository browser.