| 1 | #ifndef QT_MAYBE_H |
| 2 | #define QT_MAYBE_H |
| 3 | |
| 4 | #include "Either.h" |
| 5 | |
| 6 | /** A simple option type implementation which represents the result |
| 7 | * of an operation which returned either a valid result or nothing. |
| 8 | * |
| 9 | * Maybe<T> is built on top of Either<T1,T2>, using one type to |
| 10 | * hold the result (of type T) and the other to represent no result. |
| 11 | * |
| 12 | * Since Maybe<T> is built on Either<T1,T2>, it uses QVariant for the underlying |
| 13 | * value storage and has the same size. |
| 14 | * |
| 15 | * Maybe<T> instances can be constructed using the just(T) and |
| 16 | * nothing() helper functions. |
| 17 | */ |
| 18 | template <class T> |
| 19 | class Maybe |
| 20 | { |
| 21 | public: |
| 22 | /** Construct a Maybe<T> instance representing no |
| 23 | * value. |
| 24 | */ |
| 25 | Maybe() |
| 26 | : m_value(false) |
| 27 | {} |
| 28 | |
| 29 | /** Returns true if this Maybe<T> has a value. */ |
| 30 | operator bool() const { |
| 31 | return m_value.template is<T>(); |
| 32 | } |
| 33 | |
| 34 | /** Returns the value from this Maybe<T>. Asserts |
| 35 | * in debug builds if there is no value. |
| 36 | */ |
| 37 | T value() const { |
| 38 | return m_value.template as<T>(); |
| 39 | } |
| 40 | |
| 41 | // helper method for the non-member just() function, |
| 42 | // which external helpers should use instead |
| 43 | template <class U> |
| 44 | static Maybe<T> just(const U& value); |
| 45 | |
| 46 | private: |
| 47 | // use the just() function to create a new |
| 48 | // Maybe<T> instance with a value |
| 49 | Maybe(const T& value) |
| 50 | : m_value(value) |
| 51 | {} |
| 52 | |
| 53 | Either<bool,T> m_value; |
| 54 | }; |
| 55 | |
| 56 | template <class T> |
| 57 | template <class U> |
| 58 | Maybe<T> Maybe<T>::just(const U& value) |
| 59 | { |
| 60 | return Maybe<T>(value); |
| 61 | } |
| 62 | |
| 63 | // helper type used by nothing() |
| 64 | struct NothingType |
| 65 | { |
| 66 | template <typename T> |
| 67 | operator Maybe<T>() const { |
| 68 | return Maybe<T>(); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | // helper type used by just() |
| 73 | template <typename T> |
| 74 | struct JustType |
| 75 | { |
| 76 | JustType(const T& _value) |
| 77 | : value(_value) |
| 78 | {} |
| 79 | |
| 80 | template <typename U> |
| 81 | operator Maybe<U>() const { |
| 82 | return Maybe<U>::just(value); |
| 83 | } |
| 84 | |
| 85 | const T& value; |
| 86 | }; |
| 87 | |
| 88 | // helper type specialized for pointer types |
| 89 | template <typename T> |
| 90 | struct JustType<T*> |
| 91 | { |
| 92 | JustType(T* _value) |
| 93 | : value(_value) |
| 94 | {} |
| 95 | |
| 96 | operator Maybe<T*>() const { |
| 97 | // just(T*) returns a no-value Maybe if the pointer |
| 98 | // is null |
| 99 | return value ? Maybe<T*>::just(value) : Maybe<T*>(); |
| 100 | } |
| 101 | |
| 102 | T* value; |
| 103 | }; |
| 104 | |
| 105 | /** Returns a type which can be implicitly cast to any Maybe<T> type, |
| 106 | * representing a function which returned no value. |
| 107 | */ |
| 108 | inline NothingType nothing() |
| 109 | { |
| 110 | return NothingType(); |
| 111 | } |
| 112 | |
| 113 | /** Returns a type which can be implicitly cast to a Maybe<T> type |
| 114 | * representing a function which did return a value of @p value. |
| 115 | * |
| 116 | * just() is specialized for pointer types to return nothing() |
| 117 | * if the pointer is null. |
| 118 | */ |
| 119 | template <typename T> |
| 120 | inline JustType<T> just(const T& value) { |
| 121 | return JustType<T>(value); |
| 122 | } |
| 123 | |
| 124 | #endif |