| 1 | #ifndef QT_EITHER_H |
| 2 | #define QT_EITHER_H |
| 3 | |
| 4 | #include <QtCore/QMetaType> |
| 5 | #include <QtCore/QVariant> |
| 6 | #include <QtCore/QList> |
| 7 | |
| 8 | template <typename T1, typename T2, typename T> |
| 9 | struct EitherHelper |
| 10 | { |
| 11 | }; |
| 12 | |
| 13 | template <typename T1, typename T2> |
| 14 | struct EitherHelper<T1,T2,T1> |
| 15 | { |
| 16 | EitherHelper(const QVariant& _value) |
| 17 | : value(_value) |
| 18 | {} |
| 19 | |
| 20 | bool is() const |
| 21 | { |
| 22 | return value.userType() == qMetaTypeId<T1>(); |
| 23 | } |
| 24 | |
| 25 | T1 as() const { |
| 26 | return value.value<T1>(); |
| 27 | } |
| 28 | |
| 29 | const QVariant& value; |
| 30 | }; |
| 31 | |
| 32 | template <typename T1, typename T2> |
| 33 | struct EitherHelper<T1,T2,T2> |
| 34 | { |
| 35 | EitherHelper(const QVariant& _value) |
| 36 | : value(_value) |
| 37 | {} |
| 38 | |
| 39 | bool is() const { |
| 40 | return value.userType() == qMetaTypeId<T2>(); |
| 41 | } |
| 42 | |
| 43 | T2 as() const { |
| 44 | return value.value<T2>(); |
| 45 | } |
| 46 | |
| 47 | const QVariant& value; |
| 48 | }; |
| 49 | |
| 50 | /** A sum type which represents either an instance of T1 or |
| 51 | * an instance of T2. |
| 52 | * |
| 53 | * Either uses QVariant for the underlying storage, so the |
| 54 | * same performance considerations apply. An Either instance |
| 55 | * has the same size as a QVariant (12 bytes) regardless of the |
| 56 | * types used. |
| 57 | * |
| 58 | * A convenient way to create an Either<T1,T2> instance is |
| 59 | * the some(T) function which returns a type that can be implicitly |
| 60 | * cast to any Either<> type where T1 or T2 is T. |
| 61 | */ |
| 62 | template <typename T1, typename T2> |
| 63 | class Either |
| 64 | { |
| 65 | public: |
| 66 | Either(const T1& t1) |
| 67 | : m_value(QVariant::fromValue(t1)) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | Either(const T2& t2) |
| 72 | : m_value(QVariant::fromValue(t2)) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | template <typename T> |
| 77 | bool is() const; |
| 78 | |
| 79 | template <typename T> |
| 80 | T as() const; |
| 81 | |
| 82 | bool is1st() const; |
| 83 | bool is2nd() const; |
| 84 | |
| 85 | T1 as1st() const; |
| 86 | T2 as2nd() const; |
| 87 | |
| 88 | private: |
| 89 | template <typename T> |
| 90 | EitherHelper<T1,T2,T> is_t() const; |
| 91 | |
| 92 | QVariant m_value; |
| 93 | }; |
| 94 | |
| 95 | template <typename T1, typename T2> |
| 96 | template <typename T> |
| 97 | EitherHelper<T1,T2,T> Either<T1,T2>::is_t() const |
| 98 | { |
| 99 | return EitherHelper<T1,T2,T>(m_value); |
| 100 | } |
| 101 | |
| 102 | template <typename T1, typename T2> |
| 103 | template <typename T> |
| 104 | bool Either<T1, T2>::is() const |
| 105 | { |
| 106 | return is_t<T>().is(); |
| 107 | } |
| 108 | |
| 109 | template <typename T1, typename T2> |
| 110 | template <typename T> |
| 111 | T Either<T1, T2>::as() const |
| 112 | { |
| 113 | Q_ASSERT_X(is<T>(),"Either::as()","Tried to extract the wrong type from an Either instance"); |
| 114 | return is_t<T>().as(); |
| 115 | } |
| 116 | |
| 117 | template <typename T1, typename T2> |
| 118 | bool Either<T1,T2>::is1st() const |
| 119 | { |
| 120 | return is<T1>(); |
| 121 | } |
| 122 | |
| 123 | template <typename T1, typename T2> |
| 124 | bool Either<T1,T2>::is2nd() const |
| 125 | { |
| 126 | return is<T2>(); |
| 127 | } |
| 128 | |
| 129 | template <typename T1, typename T2> |
| 130 | T1 Either<T1,T2>::as1st() const |
| 131 | { |
| 132 | return as<T1>(); |
| 133 | } |
| 134 | |
| 135 | template <typename T1, typename T2> |
| 136 | T2 Either<T1,T2>::as2nd() const |
| 137 | { |
| 138 | return as<T2>(); |
| 139 | } |
| 140 | |
| 141 | template <class T1> |
| 142 | struct MakeEither |
| 143 | { |
| 144 | MakeEither(const T1& _value) |
| 145 | : value(_value) |
| 146 | {} |
| 147 | |
| 148 | template <typename T2> |
| 149 | operator Either<T1,T2>() |
| 150 | { |
| 151 | return Either<T1,T2>(value); |
| 152 | } |
| 153 | |
| 154 | template <typename T2> |
| 155 | operator Either<T2,T1>() |
| 156 | { |
| 157 | return Either<T2,T1>(value); |
| 158 | } |
| 159 | |
| 160 | const T1& value; |
| 161 | }; |
| 162 | |
| 163 | template <class T1> |
| 164 | inline MakeEither<T1> some(const T1& value) |
| 165 | { |
| 166 | return MakeEither<T1>(value); |
| 167 | } |
| 168 | |
| 169 | #endif |