| 1 |
/// The MIT License (MIT) |
| 2 |
/// Copyright (c) 2016 Peter Goldsborough |
| 3 |
/// |
| 4 |
/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
/// of this software and associated documentation files (the "Software"), to |
| 6 |
/// deal in the Software without restriction, including without limitation the |
| 7 |
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
/// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
/// furnished to do so, subject to the following conditions: |
| 10 |
/// |
| 11 |
/// The above copyright notice and this permission notice shall be included in |
| 12 |
/// all copies or substantial portions of the Software. |
| 13 |
/// |
| 14 |
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
/// IN THE SOFTWARE. |
| 21 |
|
| 22 |
#ifndef LRU_INTERNAL_OPTIONAL_HPP |
| 23 |
#define LRU_INTERNAL_OPTIONAL_HPP |
| 24 |
|
| 25 |
#ifndef __has_include |
| 26 |
#define USE_LRU_OPTIONAL |
| 27 |
#elif __has_include(<optional>) |
| 28 |
|
| 29 |
#include <optional> |
| 30 |
|
| 31 |
namespace LRU { |
| 32 |
namespace Internal { |
| 33 |
template <typename T> |
| 34 |
using Optional = std::optional<T>; |
| 35 |
} // namespace Internal |
| 36 |
} // namespace LRU |
| 37 |
|
| 38 |
#else |
| 39 |
#define USE_LRU_OPTIONAL |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef USE_LRU_OPTIONAL |
| 43 |
#include <memory> |
| 44 |
#include <stdexcept> |
| 45 |
|
| 46 |
namespace LRU { |
| 47 |
namespace Internal { |
| 48 |
|
| 49 |
// A roll-your-own replacement of `std::optional`. |
| 50 |
// |
| 51 |
// This class is only to be used if `std::optional` is unavailable. It |
| 52 |
// implements an optional type simply on top of a `unique_ptr`. It is |
| 53 |
// API-compatible with `std::optional`, as required for our purposes. |
| 54 |
template <typename T> |
| 55 |
class Optional { |
| 56 |
public: |
| 57 |
/// Constructor. |
| 58 |
Optional() = default; |
| 59 |
|
| 60 |
/// Copy constructor. |
| 61 |
/// |
| 62 |
/// \param other The other optional object to copy from. |
| 63 |
Optional(const Optional& other) { |
| 64 |
if (other) emplace(*other); |
| 65 |
} |
| 66 |
|
| 67 |
/// Generalized copy constructor. |
| 68 |
/// |
| 69 |
/// \param other The other optional object to copy from. |
| 70 |
template <typename U, |
| 71 |
typename = std::enable_if_t<std::is_convertible<T, U>::value>> |
| 72 |
Optional(const Optional<U>& other) { |
| 73 |
if (other) emplace(*other); |
| 74 |
} |
| 75 |
|
| 76 |
/// Move constructor. |
| 77 |
/// |
| 78 |
/// \param other The other optional object to move into this one. |
| 79 |
Optional(Optional&& other) noexcept { |
| 80 |
swap(other); |
| 81 |
} |
| 82 |
|
| 83 |
/// Generalized move constructor. |
| 84 |
/// |
| 85 |
/// \param other The other optional object to move into this one. |
| 86 |
template <typename U, |
| 87 |
typename = std::enable_if_t<std::is_convertible<T, U>::value>> |
| 88 |
Optional(Optional<U>&& other) noexcept { |
| 89 |
if (other) { |
| 90 |
_value = std::make_unique<T>(std::move(*other)); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/// Assignment operator. |
| 95 |
/// |
| 96 |
/// \param other The other object to assign from. |
| 97 |
/// \returns The resulting optional instance. |
| 98 |
Optional& operator=(Optional other) noexcept { |
| 99 |
swap(other); |
| 100 |
return *this; |
| 101 |
} |
| 102 |
|
| 103 |
/// Swaps the contents of this optional with another one. |
| 104 |
/// |
| 105 |
/// \param other The other optional to swap with. |
| 106 |
void swap(Optional& other) { |
| 107 |
_value.swap(other._value); |
| 108 |
} |
| 109 |
|
| 110 |
/// Swaps the contents of two optionals. |
| 111 |
/// |
| 112 |
/// \param first The first optional to swap. |
| 113 |
/// \param second The second optional to swap. |
| 114 |
friend void swap(Optional& first, Optional& second) /* NOLINT */ { |
| 115 |
first.swap(second); |
| 116 |
} |
| 117 |
|
| 118 |
/// \returns True if the `Optional` has a value, else false. |
| 119 |
bool has_value() const noexcept { |
| 120 |
return static_cast<bool>(_value); |
| 121 |
} |
| 122 |
|
| 123 |
/// \copydoc has_value() |
| 124 |
explicit operator bool() const noexcept { |
| 125 |
return has_value(); |
| 126 |
} |
| 127 |
|
| 128 |
/// \returns A pointer to the current value. Behavior is undefined if the |
| 129 |
/// optional has no value. |
| 130 |
T* operator->() { |
| 131 |
return _value.get(); |
| 132 |
} |
| 133 |
|
| 134 |
/// \returns A const pointer to the current value. Behavior is undefined if |
| 135 |
/// the `Optional` has no value. |
| 136 |
const T* operator->() const { |
| 137 |
return _value.get(); |
| 138 |
} |
| 139 |
|
| 140 |
/// \returns A const reference to the current value. Behavior is undefined if |
| 141 |
/// the `Optional` has no value. |
| 142 |
const T& operator*() const { |
| 143 |
return *_value; |
| 144 |
} |
| 145 |
|
| 146 |
/// \returns A reference to the current value. Behavior is undefined if |
| 147 |
/// the `Optional` has no value. |
| 148 |
T& operator*() { |
| 149 |
return *_value; |
| 150 |
} |
| 151 |
|
| 152 |
/// \returns A reference to the current value. |
| 153 |
/// \throws std::runtime_error If the `Optional` currently has no value. |
| 154 |
T& value() { |
| 155 |
if (!has_value()) { |
| 156 |
// Actually std::bad_optional_access |
| 157 |
throw std::runtime_error("optional has no value"); |
| 158 |
} |
| 159 |
|
| 160 |
return *_value; |
| 161 |
} |
| 162 |
|
| 163 |
/// \returns A const reference to the current value. |
| 164 |
/// \throws std::runtime_error If the `Optional` currently has no value. |
| 165 |
const T& value() const { |
| 166 |
if (!has_value()) { |
| 167 |
// Actually std::bad_optional_access |
| 168 |
throw std::runtime_error("optional has no value"); |
| 169 |
} |
| 170 |
|
| 171 |
return *_value; |
| 172 |
} |
| 173 |
|
| 174 |
/// \returns The current value, or the given argument if there is no value. |
| 175 |
/// \param default_value The value to return if this `Optional` currently has |
| 176 |
/// no value. |
| 177 |
template <class U> |
| 178 |
T value_or(U&& default_value) const { |
| 179 |
return *this ? **this : static_cast<T>(std::forward<U>(default_value)); |
| 180 |
} |
| 181 |
|
| 182 |
/// Resets the `Optional` to have no value. |
| 183 |
void reset() noexcept { |
| 184 |
_value.reset(); |
| 185 |
} |
| 186 |
|
| 187 |
/// Constructs the `Optional`'s value with the given arguments. |
| 188 |
/// |
| 189 |
/// \param args Arguments to perfeclty forward to the value's constructor. |
| 190 |
template <typename... Args> |
| 191 |
void emplace(Args&&... args) { |
| 192 |
_value = std::make_unique<T>(std::forward<Args>(args)...); |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
template <typename> |
| 197 |
friend class Optional; |
| 198 |
|
| 199 |
/// The value, as we implement it. |
| 200 |
std::unique_ptr<T> _value; |
| 201 |
}; |
| 202 |
} // namespace Internal |
| 203 |
} // namespace LRU |
| 204 |
|
| 205 |
#endif |
| 206 |
|
| 207 |
#endif // LRU_INTERNAL_OPTIONAL_HPP |