source: libcfa/src/collections/result.cfa @ 9c2ac95

Last change on this file since 9c2ac95 was 55b060d, checked in by Peter A. Buhr <pabuhr@…>, 14 months ago

rename directories containers to collections

  • Property mode set to 100644
File size: 2.5 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// result.c -- Contains the expected value or an error 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
[55b060d]12// Last Modified On : Wed Aug 30 21:27:22 2023
13// Update Count     : 4
[79308c8e]14//
15
[55b060d]16#include <collections/result.hfa>
[91c389a]17#include <assert.h>
[79308c8e]18
[accc9df9]19#pragma GCC visibility push(default)
[79308c8e]20
[fd54fef]21forall(T, E)
[242a902]22void ?{}(result(T, E) & this) {
23        this.has_value = false;
24        (this.error){};
[79308c8e]25}
26
[fd54fef]27forall(T, E)
[242a902]28void ?{}(result(T, E) & this, one_t, T value) {
29        this.has_value = true;
30        (this.value){value};
[79308c8e]31}
32
[fd54fef]33forall(T, E)
[242a902]34void ?{}(result(T, E) & this, zero_t, E error) {
35        this.has_value = false;
36        (this.error){error};
[79308c8e]37}
38
[fd54fef]39forall(T, E)
[242a902]40void ?{}(result(T, E) & this, result(T, E) other) {
41        this.has_value = other.has_value;
[79308c8e]42        if (other.has_value) {
[242a902]43                (this.value){other.value};
[79308c8e]44        } else {
[242a902]45                (this.error){other.error};
[58daf53]46        }
47}
48
[fd54fef]49forall(T, E)
[242a902]50result(T, E) ?=?(result(T, E) & this, result(T, E) that) {
[ee06e41b]51        if (this.has_value && that.has_value) {
[242a902]52                this.value = that.value;
53        } else if (this.has_value) {
54                ^(this.value){};
55                this.has_value = false;
56                (this.error){that.error};
[58daf53]57        } else if (that.has_value) {
[242a902]58                ^(this.error){};
59                this.has_value = true;
60                (this.value){that.value};
[58daf53]61        } else {
[242a902]62                this.error = that.error;
[79308c8e]63        }
64}
65
[fd54fef]66forall(T, E)
[242a902]67void ^?{}(result(T, E) & this) {
68        if (this.has_value) {
69                ^(this.value){};
[79308c8e]70        } else {
[242a902]71                ^(this.error){};
[79308c8e]72        }
73}
74
[fd54fef]75forall(T, E)
[64fc0ba]76bool ?!=?(result(T, E) this, zero_t) {
[20877d2]77        return this.has_value;
[79308c8e]78}
79
[fd54fef]80forall(T, E)
[79308c8e]81result(T, E) result_value(T value) {
[64fc0ba]82        return (result(T, E)){1, value};
[79308c8e]83}
84
[fd54fef]85forall(T, E)
[79308c8e]86result(T, E) result_error(E error) {
[64fc0ba]87        return (result(T, E)){0, error};
[79308c8e]88}
89
[fd54fef]90forall(T, E)
[64fc0ba]91bool has_value(result(T, E) * this) {
[79308c8e]92        return this->has_value;
93}
94
[fd54fef]95forall(T, E)
[79308c8e]96T get(result(T, E) * this) {
[64fc0ba]97        assertf(this->has_value, "attempt to get from result without value");
[79308c8e]98        return this->value;
99}
100
[fd54fef]101forall(T, E)
[79308c8e]102E get_error(result(T, E) * this) {
[20877d2]103        assertf(!this->has_value, "attempt to get from result without error");
[79308c8e]104        return this->error;
105}
[58daf53]106
[fd54fef]107forall(T, E)
[58daf53]108void set(result(T, E) * this, T value) {
109        if (this->has_value) {
110                this->value = value;
111        } else {
[242a902]112                ^(this->error){};
[58daf53]113                this->has_value = true;
[242a902]114                (this->value){value};
[58daf53]115        }
116}
117
[fd54fef]118forall(T, E)
[58daf53]119void set_error(result(T, E) * this, E error) {
120        if (this->has_value) {
[242a902]121                ^(this->value){};
[58daf53]122                this->has_value = false;
[242a902]123                (this->error){error};
[58daf53]124        } else {
125                this->error = error;
126        }
127}
Note: See TracBrowser for help on using the repository browser.