|
| 1 | +//===-- Either.h ------------------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The KLEE Symbolic Virtual Machine |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef KLEE_EITHER_H |
| 11 | +#define KLEE_EITHER_H |
| 12 | + |
| 13 | +#include "klee/ADT/Ref.h" |
| 14 | + |
| 15 | +#include "klee/Support/Casting.h" |
| 16 | + |
| 17 | +#include <cassert> |
| 18 | + |
| 19 | +namespace llvm { |
| 20 | +class raw_ostream; |
| 21 | +} // namespace llvm |
| 22 | + |
| 23 | +namespace klee { |
| 24 | + |
| 25 | +template <class T1, class T2> class either_left; |
| 26 | +template <class T1, class T2> class either_right; |
| 27 | + |
| 28 | +template <class T1, class T2> class either { |
| 29 | +protected: |
| 30 | + friend class ref<either<T1, T2>>; |
| 31 | + friend class ref<const either<T1, T2>>; |
| 32 | + |
| 33 | + /// @brief Required by klee::ref-managed objects |
| 34 | + class ReferenceCounter _refCount; |
| 35 | + |
| 36 | +public: |
| 37 | + using left = either_left<T1, T2>; |
| 38 | + using right = either_right<T1, T2>; |
| 39 | + |
| 40 | + enum class EitherKind { Left, Right }; |
| 41 | + |
| 42 | + virtual ~either() {} |
| 43 | + virtual EitherKind getKind() const = 0; |
| 44 | + |
| 45 | + static bool classof(const either<T1, T2> *) { return true; } |
| 46 | + // virtual unsigned hash() = 0; |
| 47 | + virtual int compare(const either<T1, T2> &b) = 0; |
| 48 | + virtual bool equals(const either<T1, T2> &b) = 0; |
| 49 | +}; |
| 50 | + |
| 51 | +template <class T1, class T2> class either_left : public either<T1, T2> { |
| 52 | +protected: |
| 53 | + friend class ref<either_left<T1, T2>>; |
| 54 | + friend class ref<const either_left<T1, T2>>; |
| 55 | + |
| 56 | +private: |
| 57 | + ref<T1> value_; |
| 58 | + |
| 59 | +public: |
| 60 | + either_left(ref<T1> leftValue) : value_(leftValue){}; |
| 61 | + |
| 62 | + ref<T1> value() const { return value_; } |
| 63 | + operator ref<T1> const() { return value_; } |
| 64 | + |
| 65 | + virtual typename either<T1, T2>::EitherKind getKind() const override { |
| 66 | + return either<T1, T2>::EitherKind::Left; |
| 67 | + } |
| 68 | + |
| 69 | + static bool classof(const either<T1, T2> *S) { |
| 70 | + return S->getKind() == either<T1, T2>::EitherKind::Left; |
| 71 | + } |
| 72 | + static bool classof(const either_left<T1, T2> *) { return true; } |
| 73 | + |
| 74 | + // virtual unsigned hash() override { return value_->hash(); }; |
| 75 | + virtual int compare(const either<T1, T2> &b) override { |
| 76 | + if (b.getKind() != getKind()) { |
| 77 | + return b.getKind() < getKind() ? -1 : 1; |
| 78 | + } |
| 79 | + const either_left<T1, T2> &el = static_cast<const either_left<T1, T2> &>(b); |
| 80 | + if (el.value() != value()) { |
| 81 | + return el.value() < value() ? -1 : 1; |
| 82 | + } |
| 83 | + return 0; |
| 84 | + } |
| 85 | + |
| 86 | + virtual bool equals(const either<T1, T2> &b) override { |
| 87 | + if (b.getKind() != getKind()) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + const either_left<T1, T2> &el = static_cast<const either_left<T1, T2> &>(b); |
| 91 | + return el.value() == value(); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +template <class T1, class T2> class either_right : public either<T1, T2> { |
| 96 | +protected: |
| 97 | + friend class ref<either_right<T1, T2>>; |
| 98 | + friend class ref<const either_right<T1, T2>>; |
| 99 | + |
| 100 | +private: |
| 101 | + ref<T2> value_; |
| 102 | + |
| 103 | +public: |
| 104 | + either_right(ref<T2> rightValue) : value_(rightValue){}; |
| 105 | + |
| 106 | + ref<T2> value() const { return value_; } |
| 107 | + operator ref<T2> const() { return value_; } |
| 108 | + |
| 109 | + virtual typename either<T1, T2>::EitherKind getKind() const override { |
| 110 | + return either<T1, T2>::EitherKind::Right; |
| 111 | + } |
| 112 | + |
| 113 | + static bool classof(const either<T1, T2> *S) { |
| 114 | + return S->getKind() == either<T1, T2>::EitherKind::Right; |
| 115 | + } |
| 116 | + static bool classof(const either_right<T1, T2> *) { return true; } |
| 117 | + |
| 118 | + // virtual unsigned hash() override { return value_->hash(); }; |
| 119 | + virtual int compare(const either<T1, T2> &b) override { |
| 120 | + if (b.getKind() != getKind()) { |
| 121 | + return b.getKind() < getKind() ? -1 : 1; |
| 122 | + } |
| 123 | + const either_right<T1, T2> &el = |
| 124 | + static_cast<const either_right<T1, T2> &>(b); |
| 125 | + if (el.value() != value()) { |
| 126 | + return el.value() < value() ? -1 : 1; |
| 127 | + } |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + virtual bool equals(const either<T1, T2> &b) override { |
| 132 | + if (b.getKind() != getKind()) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + const either_right<T1, T2> &el = |
| 136 | + static_cast<const either_right<T1, T2> &>(b); |
| 137 | + return el.value() == value(); |
| 138 | + } |
| 139 | +}; |
| 140 | +} // end namespace klee |
| 141 | + |
| 142 | +#endif /* KLEE_EITHER_H */ |
0 commit comments