source: src/libcfa/containers/maybe.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.3 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// 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) {
[64fc0ba]42                ^(&this->value){};
[79308c8e]43        }
44}
45
46forall(otype T)
[64fc0ba]47bool ?!=?(maybe(T) this, zero_t) {
48        return !this.has_value;
[79308c8e]49}
50
51forall(otype T)
52maybe(T) maybe_value(T value) {
[64fc0ba]53        return (maybe(T)){value};
[79308c8e]54}
55
56forall(otype T)
57maybe(T) maybe_none() {
[64fc0ba]58        return (maybe(T)){};
[79308c8e]59}
60
61forall(otype T)
[64fc0ba]62bool has_value(maybe(T) * this) {
[79308c8e]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.