diff options
author | Tim King <taking@cs.nyu.edu> | 2013-04-26 17:10:21 -0400 |
---|---|---|
committer | Morgan Deters <mdeters@cs.nyu.edu> | 2013-04-26 17:10:21 -0400 |
commit | 9098391fe334d829ec4101f190b8f1fa21c30752 (patch) | |
tree | b134fc1fe1c767a50013e1449330ca6a7ee18a3d /src/util/maybe.h | |
parent | a9174ce4dc3939bbe14c9aa1fd11c79c7877eb16 (diff) |
FCSimplex branch merge
Diffstat (limited to 'src/util/maybe.h')
-rw-r--r-- | src/util/maybe.h | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/util/maybe.h b/src/util/maybe.h new file mode 100644 index 000000000..795eac759 --- /dev/null +++ b/src/util/maybe.h @@ -0,0 +1,84 @@ +/********************* */ +/*! \file maybe.h + ** \verbatim + ** Original author: taking + ** Major contributors: none + ** Minor contributors (to current version): mdeters + ** This file is part of the CVC4 prototype. + ** Copyright (c) 2009-2012 New York University and The University of Iowa + ** See the file COPYING in the top-level source directory for licensing + ** information.\endverbatim + ** + ** \brief This provides a templated Maybe construct. + ** + ** This class provides a templated Maybe<T> construct. + ** This follows the rough pattern of the Maybe monad in haskell. + ** A Maybe is an algebraic type that is either Nothing | Just T + ** + ** T must support T() and operator=. + ** + ** This has a couple of uses: + ** - There is no reasonable value or particularly clean way to represent + ** Nothing using a value of T + ** - High level of assurance that a value is not used before it is set. + **/ +#include "cvc4_private.h" + +#pragma once + +#include <ostream> +#include "util/exception.h" + +namespace CVC4 { + +template <class T> +class Maybe { +private: + bool d_just; + T d_value; + +public: + Maybe() : d_just(false), d_value(){} + Maybe(const T& val): d_just(true), d_value(val){} + + Maybe& operator=(const T& v){ + d_just = true; + d_value = v; + return *this; + } + + inline bool nothing() const { return !d_just; } + inline bool just() const { return d_just; } + + void clear() { + if(just()){ + d_just = false; + d_value = T(); + } + } + + T& value() { + Assert(just(), "Maybe::value() requires the maybe to be set."); + return d_value; + } + const T& constValue() const { + Assert(just(), "Maybe::constValue() requires the maybe to be set."); + return d_value; + } + + operator const T&() const { return constValue(); } +}; + +template <class T> +inline std::ostream& operator<<(std::ostream& out, const Maybe<T>& m){ + out << "{"; + if(m.nothing()){ + out << "Nothing"; + }else{ + out << m.constValue(); + } + out << "}"; + return out; +} + +}/* CVC4 namespace */ |