ADT
        aaron-thesis
        arm-eh
        ast-experimental
        cleanup-dtors
        deferred_resn
        demangler
        enum
        forall-pointer-decay
        jacob/cs343-translation
        jenkins-sandbox
        new-ast
        new-ast-unique-expr
        new-env
        no_list
        persistent-indexer
        pthread-emulation
        qualifiedEnum
        resolv-new
        with_gc
      
      
        
          | 
            Last change
 on this file since 1eeab949 was             58daf53, checked in by Andrew Beach <ajbeach@…>, 8 years ago           | 
        
        
          | 
             
Made maybe and result mutable, they should now qualify as otypes. Also added a few details to cfa.nanorc. 
 
           | 
        
        
          
            
              - 
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 : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Thr May 25 15:24:00 2017
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <containers/maybe>
 | 
|---|
| 17 | #include <assert>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | 
 | 
|---|
| 20 | forall(otype T)
 | 
|---|
| 21 | void ?{}(maybe(T) * this) {
 | 
|---|
| 22 |         this->has_value = false;
 | 
|---|
| 23 | }
 | 
|---|
| 24 | 
 | 
|---|
| 25 | forall(otype T)
 | 
|---|
| 26 | void ?{}(maybe(T) * this, T value) {
 | 
|---|
| 27 |         this->has_value = true;
 | 
|---|
| 28 |         (&this->value){value};
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | forall(otype T)
 | 
|---|
| 32 | void ?{}(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 | 
 | 
|---|
| 39 | forall(otype T)
 | 
|---|
| 40 | maybe(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 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | forall(otype T)
 | 
|---|
| 53 | void ^?{}(maybe(T) * this) {
 | 
|---|
| 54 |         if (this->has_value) {
 | 
|---|
| 55 |                 ^(&this->value){};
 | 
|---|
| 56 |         }
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | forall(otype T)
 | 
|---|
| 60 | bool ?!=?(maybe(T) this, zero_t) {
 | 
|---|
| 61 |         return this.has_value;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | forall(otype T)
 | 
|---|
| 65 | maybe(T) maybe_value(T value) {
 | 
|---|
| 66 |         return (maybe(T)){value};
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | forall(otype T)
 | 
|---|
| 70 | maybe(T) maybe_none() {
 | 
|---|
| 71 |         return (maybe(T)){};
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | forall(otype T)
 | 
|---|
| 75 | bool has_value(maybe(T) * this) {
 | 
|---|
| 76 |         return this->has_value;
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | forall(otype T)
 | 
|---|
| 80 | T get(maybe(T) * this) {
 | 
|---|
| 81 |         assertf(this->has_value, "attempt to get from maybe without value");
 | 
|---|
| 82 |         return this->value;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | forall(otype T)
 | 
|---|
| 86 | void set(maybe(T) * this, T value) {
 | 
|---|
| 87 |         if (this->has_value) {
 | 
|---|
| 88 |                 this->value = value;
 | 
|---|
| 89 |         } else {
 | 
|---|
| 90 |                 this->has_value = true;
 | 
|---|
| 91 |                 (&this->value){value};
 | 
|---|
| 92 |         }
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | forall(otype T)
 | 
|---|
| 96 | void set_none(maybe(T) * this) {
 | 
|---|
| 97 |         if (this->has_value) {
 | 
|---|
| 98 |                 this->has_value = false;
 | 
|---|
| 99 |                 ^(&this->value){};
 | 
|---|
| 100 |         }
 | 
|---|
| 101 | }
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.