// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // maybe.c -- May contain a value. // // Author : Andrew Beach // Created On : Wed May 25 15:40:00 2017 // Last Modified By : Andrew Beach // Last Modified On : Wed May 25 17:00:00 2017 // Update Count : 1 // #include #include forall(otype T) void ?{}(maybe(T) * this) { this->has_value = false; } forall(otype T) void ?{}(maybe(T) * this, T value) { this->has_value = true; this->value = value; } forall(otype T) void ?{}(maybe(T) * this, maybe(T) other) { this->has_value = other.has_value; if (other.has_value) { this->value = other.value; } } forall(otype T) void ^?{}(maybe(T) * this) { if (this->has_value) { ^(this->value){}; } } forall(otype T) _Bool ?!=?(result(T, E) this, zero_t) { return !this->has_value; } forall(otype T) maybe(T) maybe_value(T value) { return (Maybe(T)){value}; } forall(otype T) maybe(T) maybe_none() { return (Maybe(T)){}; } forall(otype T) _Bool has_value(maybe(T) * this) { return this->has_value; } forall(otype T) T get(maybe(T) * this) { assertf(this->has_value, "attempt to get from maybe without value"); return this->value; }