source: libcfa/src/containers/maybe.cfa

Last change on this file was accc9df9, checked in by Thierry Delisle <tdelisle@…>, 6 days ago

Visibility containers lib

  • 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// 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#pragma GCC visibility push(default)
20
21forall(T)
22void ?{}(maybe(T) & this) {
23        this.has_value = false;
24}
25
26forall(T)
27void ?{}(maybe(T) & this, T value) {
28        this.has_value = true;
29        (this.value){value};
30}
31
32forall(T)
33void ?{}(maybe(T) & this, maybe(T) other) {
34        this.has_value = other.has_value;
35        if (other.has_value) {
36                (this.value){other.value};
37        }
38}
39
40forall(T)
41maybe(T) ?=?(maybe(T) & this, maybe(T) that) {
42        if (this.has_value && that.has_value) {
43                this.value = that.value;
44        } else if (this.has_value) {
45                ^(this.value){};
46                this.has_value = false;
47        } else if (that.has_value) {
48                this.has_value = true;
49                (this.value){that.value};
50        }
51        return this;
52}
53
54forall(T)
55void ^?{}(maybe(T) & this) {
56        if (this.has_value) {
57                ^(this.value){};
58        }
59}
60
61forall(T)
62bool ?!=?(maybe(T) this, zero_t) {
63        return this.has_value;
64}
65
66forall(T)
67maybe(T) maybe_value(T value) {
68        return (maybe(T)){value};
69}
70
71forall(T)
72maybe(T) maybe_none() {
73        return (maybe(T)){};
74}
75
76forall(T)
77bool has_value(maybe(T) * this) {
78        return this->has_value;
79}
80
81forall(T)
82T get(maybe(T) * this) {
83        assertf(this->has_value, "attempt to get from maybe without value");
84        return this->value;
85}
86
87forall(T)
88void set(maybe(T) * this, T value) {
89        if (this->has_value) {
90                this->value = value;
91        } else {
92                this->has_value = true;
93                (this->value){value};
94        }
95}
96
97forall(T)
98void set_none(maybe(T) * this) {
99        if (this->has_value) {
100                this->has_value = false;
101                ^(this->value){};
102        }
103}
Note: See TracBrowser for help on using the repository browser.