Ignore:
Timestamp:
May 25, 2017, 11:38:36 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
e883a4b
Parents:
58ed882
Message:

Added maybe and result to libcfa makefiles and made some minor fixes

Location:
src/libcfa/containers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/libcfa/containers/maybe

    r58ed882 r64fc0ba  
    1818#define MAYBE_H
    1919
     20#include <stdbool.h>
    2021
    2122// DO NOT USE DIRECTLY!
    2223forall(otype T)
    2324struct maybe {
    24     _Bool has_value;
     25    bool has_value;
    2526    T value;
    2627};
     
    4041
    4142forall(otype T)
    42 _Bool ?!=?(result(T, E) this, zero_t);
     43bool ?!=?(maybe(T) this, zero_t);
    4344
    4445forall(otype T)
     
    4950
    5051forall(otype T)
    51 _Bool has_value(maybe(T) * this);
     52bool has_value(maybe(T) * this);
    5253
    5354forall(otype T)
  • TabularUnified src/libcfa/containers/maybe.c

    r58ed882 r64fc0ba  
    4040void ^?{}(maybe(T) * this) {
    4141        if (this->has_value) {
    42                 ^(this->value){};
     42                ^(&this->value){};
    4343        }
    4444}
    4545
    4646forall(otype T)
    47 _Bool ?!=?(result(T, E) this, zero_t) {
    48         return !this->has_value;
     47bool ?!=?(maybe(T) this, zero_t) {
     48        return !this.has_value;
    4949}
    5050
    5151forall(otype T)
    5252maybe(T) maybe_value(T value) {
    53         return (Maybe(T)){value};
     53        return (maybe(T)){value};
    5454}
    5555
    5656forall(otype T)
    5757maybe(T) maybe_none() {
    58         return (Maybe(T)){};
     58        return (maybe(T)){};
    5959}
    6060
    6161forall(otype T)
    62 _Bool has_value(maybe(T) * this) {
     62bool has_value(maybe(T) * this) {
    6363        return this->has_value;
    6464}
  • TabularUnified src/libcfa/containers/result

    r58ed882 r64fc0ba  
    1818#define RESULT_H
    1919
     20#include <stdbool.h>
    2021
    2122// DO NOT USE DIRECTLY!
    2223forall(otype T, otype E)
     24union inner_result{
     25        T value;
     26        E error;
     27};
     28
     29forall(otype T, otype E)
    2330struct result {
    24         _Bool has_value;
    25         union {
    26                 T value;
    27                 E error;
    28         };
     31        bool has_value;
     32        inner_result(T, E);
    2933};
    3034
     
    4650
    4751forall(otype T, otype E)
    48 _Bool ?!=?(result(T, E) this, zero_t);
     52bool ?!=?(result(T, E) this, zero_t);
    4953
    5054forall(otype T, otype E)
     
    5559
    5660forall(otype T, otype E)
    57 _Bool has_value(result(T, E) * this);
     61bool has_value(result(T, E) * this);
    5862
    5963forall(otype T, otype E)
  • TabularUnified src/libcfa/containers/result.c

    r58ed882 r64fc0ba  
    2121void ?{}(result(T, E) * this) {
    2222        this->has_value = false;
    23         (this->error){};
     23        (&this->error){};
    2424}
    2525
     
    4949void ^?{}(result(T, E) * this) {
    5050        if (this->has_value) {
    51                 ^(this->value){};
     51                ^(&this->value){};
    5252        } else {
    53                 ^(this->error){};
     53                ^(&this->error){};
    5454        }
    5555}
    5656
    5757forall(otype T, otype E)
    58 _Bool ?!=?(result(T, E) this, zero_t) {
    59         return !this->has_value;
     58bool ?!=?(result(T, E) this, zero_t) {
     59        return !this.has_value;
    6060}
    6161
    6262forall(otype T, otype E)
    6363result(T, E) result_value(T value) {
    64         return (Result(T, E)){1, value};
     64        return (result(T, E)){1, value};
    6565}
    6666
    6767forall(otype T, otype E)
    6868result(T, E) result_error(E error) {
    69         return (Result(T, E)){0, value};
     69        return (result(T, E)){0, error};
    7070}
    7171
    7272forall(otype T, otype E)
    73 _Bool has_value(result(T, E) * this) {
     73bool has_value(result(T, E) * this) {
    7474        return this->has_value;
    7575}
     
    7777forall(otype T, otype E)
    7878T get(result(T, E) * this) {
    79     assertf(this->has_value, "attempt to get from result without value");
     79        assertf(this->has_value, "attempt to get from result without value");
    8080        return this->value;
    8181}
     
    8383forall(otype T, otype E)
    8484E get_error(result(T, E) * this) {
    85     assertf(this->has_value, "attempt to get from result without error");
     85        assertf(this->has_value, "attempt to get from result without error");
    8686        return this->error;
    8787}
Note: See TracChangeset for help on using the changeset viewer.