source: libcfa/src/containers/maybe.cfa @ 26544f9

ADTast-experimental
Last change on this file since 26544f9 was accc9df9, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Visibility containers lib

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