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