Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef AAPT_MAYBE_H |
| 18 | #define AAPT_MAYBE_H |
| 19 | |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 20 | #include "util/TypeTraits.h" |
| 21 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <cassert> |
| 23 | #include <type_traits> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | /** |
| 29 | * Either holds a valid value of type T, or holds Nothing. |
| 30 | * The value is stored inline in this structure, so no |
| 31 | * heap memory is used when creating a Maybe<T> object. |
| 32 | */ |
| 33 | template <typename T> |
| 34 | class Maybe { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | public: |
| 36 | /** |
| 37 | * Construct Nothing. |
| 38 | */ |
| 39 | Maybe(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 41 | ~Maybe(); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 43 | Maybe(const Maybe& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 45 | template <typename U> |
| 46 | Maybe(const Maybe<U>& rhs); // NOLINT(implicit) |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 48 | Maybe(Maybe&& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 50 | template <typename U> |
| 51 | Maybe(Maybe<U>&& rhs); // NOLINT(implicit) |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | Maybe& operator=(const Maybe& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 55 | template <typename U> |
| 56 | Maybe& operator=(const Maybe<U>& rhs); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | Maybe& operator=(Maybe&& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 60 | template <typename U> |
| 61 | Maybe& operator=(Maybe<U>&& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 63 | /** |
| 64 | * Construct a Maybe holding a value. |
| 65 | */ |
| 66 | Maybe(const T& value); // NOLINT(implicit) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 68 | /** |
| 69 | * Construct a Maybe holding a value. |
| 70 | */ |
| 71 | Maybe(T&& value); // NOLINT(implicit) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 73 | /** |
| 74 | * True if this holds a value, false if |
| 75 | * it holds Nothing. |
| 76 | */ |
| 77 | explicit operator bool() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 79 | /** |
| 80 | * Gets the value if one exists, or else |
| 81 | * panics. |
| 82 | */ |
| 83 | T& value(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 85 | /** |
| 86 | * Gets the value if one exists, or else |
| 87 | * panics. |
| 88 | */ |
| 89 | const T& value() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 91 | T valueOrDefault(const T& def) const; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 93 | private: |
| 94 | template <typename U> |
| 95 | friend class Maybe; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 97 | template <typename U> |
| 98 | Maybe& copy(const Maybe<U>& rhs); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 100 | template <typename U> |
| 101 | Maybe& move(Maybe<U>&& rhs); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 103 | void destroy(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 105 | bool mNothing; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 107 | typename std::aligned_storage<sizeof(T), alignof(T)>::type mStorage; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | template <typename T> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 111 | Maybe<T>::Maybe() : mNothing(true) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
| 113 | template <typename T> |
| 114 | Maybe<T>::~Maybe() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 115 | if (!mNothing) { |
| 116 | destroy(); |
| 117 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | template <typename T> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 121 | Maybe<T>::Maybe(const Maybe& rhs) : mNothing(rhs.mNothing) { |
| 122 | if (!rhs.mNothing) { |
| 123 | new (&mStorage) T(reinterpret_cast<const T&>(rhs.mStorage)); |
| 124 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | template <typename T> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | template <typename U> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 129 | Maybe<T>::Maybe(const Maybe<U>& rhs) : mNothing(rhs.mNothing) { |
| 130 | if (!rhs.mNothing) { |
| 131 | new (&mStorage) T(reinterpret_cast<const U&>(rhs.mStorage)); |
| 132 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | template <typename T> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 136 | Maybe<T>::Maybe(Maybe&& rhs) : mNothing(rhs.mNothing) { |
| 137 | if (!rhs.mNothing) { |
| 138 | rhs.mNothing = true; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 140 | // Move the value from rhs. |
| 141 | new (&mStorage) T(std::move(reinterpret_cast<T&>(rhs.mStorage))); |
| 142 | rhs.destroy(); |
| 143 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | template <typename T> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | template <typename U> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 148 | Maybe<T>::Maybe(Maybe<U>&& rhs) : mNothing(rhs.mNothing) { |
| 149 | if (!rhs.mNothing) { |
| 150 | rhs.mNothing = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 152 | // Move the value from rhs. |
| 153 | new (&mStorage) T(std::move(reinterpret_cast<U&>(rhs.mStorage))); |
| 154 | rhs.destroy(); |
| 155 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <typename T> |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 159 | inline Maybe<T>& Maybe<T>::operator=(const Maybe& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 160 | // Delegate to the actual assignment. |
| 161 | return copy(rhs); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | template <typename T> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 165 | template <typename U> |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 166 | inline Maybe<T>& Maybe<T>::operator=(const Maybe<U>& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 167 | return copy(rhs); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | template <typename T> |
| 171 | template <typename U> |
| 172 | Maybe<T>& Maybe<T>::copy(const Maybe<U>& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 173 | if (mNothing && rhs.mNothing) { |
| 174 | // Both are nothing, nothing to do. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 175 | return *this; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 176 | } else if (!mNothing && !rhs.mNothing) { |
| 177 | // We both are something, so assign rhs to us. |
| 178 | reinterpret_cast<T&>(mStorage) = reinterpret_cast<const U&>(rhs.mStorage); |
| 179 | } else if (mNothing) { |
| 180 | // We are nothing but rhs is something. |
| 181 | mNothing = rhs.mNothing; |
| 182 | |
| 183 | // Copy the value from rhs. |
| 184 | new (&mStorage) T(reinterpret_cast<const U&>(rhs.mStorage)); |
| 185 | } else { |
| 186 | // We are something but rhs is nothing, so destroy our value. |
| 187 | mNothing = rhs.mNothing; |
| 188 | destroy(); |
| 189 | } |
| 190 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | template <typename T> |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 194 | inline Maybe<T>& Maybe<T>::operator=(Maybe&& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 195 | // Delegate to the actual assignment. |
| 196 | return move(std::forward<Maybe<T>>(rhs)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | template <typename T> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 200 | template <typename U> |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 201 | inline Maybe<T>& Maybe<T>::operator=(Maybe<U>&& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 202 | return move(std::forward<Maybe<U>>(rhs)); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | template <typename T> |
| 206 | template <typename U> |
| 207 | Maybe<T>& Maybe<T>::move(Maybe<U>&& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 208 | if (mNothing && rhs.mNothing) { |
| 209 | // Both are nothing, nothing to do. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 210 | return *this; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 211 | } else if (!mNothing && !rhs.mNothing) { |
| 212 | // We both are something, so move assign rhs to us. |
| 213 | rhs.mNothing = true; |
| 214 | reinterpret_cast<T&>(mStorage) = |
| 215 | std::move(reinterpret_cast<U&>(rhs.mStorage)); |
| 216 | rhs.destroy(); |
| 217 | } else if (mNothing) { |
| 218 | // We are nothing but rhs is something. |
| 219 | mNothing = false; |
| 220 | rhs.mNothing = true; |
| 221 | |
| 222 | // Move the value from rhs. |
| 223 | new (&mStorage) T(std::move(reinterpret_cast<U&>(rhs.mStorage))); |
| 224 | rhs.destroy(); |
| 225 | } else { |
| 226 | // We are something but rhs is nothing, so destroy our value. |
| 227 | mNothing = true; |
| 228 | destroy(); |
| 229 | } |
| 230 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | template <typename T> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 234 | Maybe<T>::Maybe(const T& value) : mNothing(false) { |
| 235 | new (&mStorage) T(value); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | template <typename T> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 239 | Maybe<T>::Maybe(T&& value) : mNothing(false) { |
| 240 | new (&mStorage) T(std::forward<T>(value)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | template <typename T> |
| 244 | Maybe<T>::operator bool() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 245 | return !mNothing; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | template <typename T> |
| 249 | T& Maybe<T>::value() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 250 | assert(!mNothing && "Maybe<T>::value() called on Nothing"); |
| 251 | return reinterpret_cast<T&>(mStorage); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | template <typename T> |
| 255 | const T& Maybe<T>::value() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 256 | assert(!mNothing && "Maybe<T>::value() called on Nothing"); |
| 257 | return reinterpret_cast<const T&>(mStorage); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | template <typename T> |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 261 | T Maybe<T>::valueOrDefault(const T& def) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 262 | if (mNothing) { |
| 263 | return def; |
| 264 | } |
| 265 | return reinterpret_cast<const T&>(mStorage); |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | template <typename T> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 269 | void Maybe<T>::destroy() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 270 | reinterpret_cast<T&>(mStorage).~T(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | template <typename T> |
| 274 | inline Maybe<typename std::remove_reference<T>::type> make_value(T&& value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 275 | return Maybe<typename std::remove_reference<T>::type>(std::forward<T>(value)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | template <typename T> |
| 279 | inline Maybe<T> make_nothing() { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 280 | return Maybe<T>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 283 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 284 | * Define the == operator between Maybe<T> and Maybe<U> only if the operator T |
| 285 | * == U is defined. |
| 286 | * That way the compiler will show an error at the callsite when comparing two |
| 287 | * Maybe<> objects |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 288 | * whose inner types can't be compared. |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 289 | */ |
| 290 | template <typename T, typename U> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 291 | typename std::enable_if<has_eq_op<T, U>::value, bool>::type operator==( |
| 292 | const Maybe<T>& a, const Maybe<U>& b) { |
| 293 | if (a && b) { |
| 294 | return a.value() == b.value(); |
| 295 | } else if (!a && !b) { |
| 296 | return true; |
| 297 | } |
| 298 | return false; |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Same as operator== but negated. |
| 303 | */ |
| 304 | template <typename T, typename U> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 305 | typename std::enable_if<has_eq_op<T, U>::value, bool>::type operator!=( |
| 306 | const Maybe<T>& a, const Maybe<U>& b) { |
| 307 | return !(a == b); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 310 | template <typename T, typename U> |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 311 | typename std::enable_if<has_lt_op<T, U>::value, bool>::type operator<( |
| 312 | const Maybe<T>& a, const Maybe<U>& b) { |
| 313 | if (a && b) { |
| 314 | return a.value() < b.value(); |
| 315 | } else if (!a && !b) { |
| 316 | return false; |
| 317 | } |
| 318 | return !a; |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 321 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 322 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 323 | #endif // AAPT_MAYBE_H |