source: libcfa/src/containers/maybe.cfa @ 12782a9

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 12782a9 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • 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// maybe.c -- May contain a value.
8//
9// Author           : Andrew Beach
10// Created On       : Wed May 24 15:40:00 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Feb 17 11:22:03 2019
13// Update Count     : 3
14//
15
16#include <containers/maybe.hfa>
17#include <assert.h>
18
19
20forall(T)
21void ?{}(maybe(T) & this) {
22        this.has_value = false;
23}
24
25forall(T)
26void ?{}(maybe(T) & this, T value) {
27        this.has_value = true;
28        (this.value){value};
29}
30
31forall(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(T)
40maybe(T) ?=?(maybe(T) & this, maybe(T) that) {
41        if (this.has_value && that.has_value) {
42                this.value = that.value;
43        } else if (this.has_value) {
44                ^(this.value){};
45                this.has_value = false;
46        } else if (that.has_value) {
47                this.has_value = true;
48                (this.value){that.value};
49        }
50        return this;
51}
52
53forall(T)
54void ^?{}(maybe(T) & this) {
55        if (this.has_value) {
56                ^(this.value){};
57        }
58}
59
60forall(T)
61bool ?!=?(maybe(T) this, zero_t) {
62        return this.has_value;
63}
64
65forall(T)
66maybe(T) maybe_value(T value) {
67        return (maybe(T)){value};
68}
69
70forall(T)
71maybe(T) maybe_none() {
72        return (maybe(T)){};
73}
74
75forall(T)
76bool has_value(maybe(T) * this) {
77        return this->has_value;
78}
79
80forall(T)
81T get(maybe(T) * this) {
82        assertf(this->has_value, "attempt to get from maybe without value");
83        return this->value;
84}
85
86forall(T)
87void set(maybe(T) * this, T value) {
88        if (this->has_value) {
89                this->value = value;
90        } else {
91                this->has_value = true;
92                (this->value){value};
93        }
94}
95
96forall(T)
97void set_none(maybe(T) * this) {
98        if (this->has_value) {
99                this->has_value = false;
100                ^(this->value){};
101        }
102}
Note: See TracBrowser for help on using the repository browser.