optional 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file Copyright.txt or https://cmake.org/licensing for details. */
  5. #ifndef cm_optional
  6. #define cm_optional
  7. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  8. # define CMake_HAVE_CXX_OPTIONAL
  9. #endif
  10. #if defined(CMake_HAVE_CXX_OPTIONAL)
  11. # include <optional> // IWYU pragma: export
  12. #else
  13. # include <memory>
  14. # include <cm/utility>
  15. #endif
  16. namespace cm {
  17. #if defined(CMake_HAVE_CXX_OPTIONAL)
  18. using std::nullopt_t;
  19. using std::nullopt;
  20. using std::optional;
  21. using std::bad_optional_access;
  22. using std::make_optional;
  23. #else
  24. class bad_optional_access : public std::exception
  25. {
  26. using std::exception::exception;
  27. };
  28. struct nullopt_t
  29. {
  30. explicit constexpr nullopt_t(int) {}
  31. };
  32. constexpr nullopt_t nullopt{ 0 };
  33. template <typename T>
  34. class optional
  35. {
  36. public:
  37. using value_type = T;
  38. optional() noexcept = default;
  39. optional(nullopt_t) noexcept;
  40. optional(const optional& other);
  41. optional(optional&& other) noexcept;
  42. template <typename... Args>
  43. explicit optional(cm::in_place_t, Args&&... args);
  44. template <
  45. typename U = T,
  46. typename = typename std::enable_if<
  47. std::is_constructible<T, U&&>::value &&
  48. !std::is_same<typename std::decay<U>::type, cm::in_place_t>::value &&
  49. !std::is_same<typename std::decay<U>::type,
  50. cm::optional<T>>::value>::type>
  51. optional(U&& v);
  52. ~optional();
  53. optional& operator=(nullopt_t) noexcept;
  54. optional& operator=(const optional& other);
  55. optional& operator=(optional&& other) noexcept;
  56. template <
  57. typename U = T,
  58. typename = typename std::enable_if<
  59. !std::is_same<typename std::decay<U>::type, cm::optional<T>>::value &&
  60. std::is_constructible<T, U>::value && std::is_assignable<T&, U>::value &&
  61. (!std::is_scalar<T>::value ||
  62. !std::is_same<typename std::decay<U>::type, T>::value)>::type>
  63. optional& operator=(U&& v);
  64. const T* operator->() const;
  65. T* operator->();
  66. const T& operator*() const&;
  67. T& operator*() &;
  68. const T&& operator*() const&&;
  69. T&& operator*() &&;
  70. explicit operator bool() const noexcept;
  71. bool has_value() const noexcept;
  72. T& value() &;
  73. const T& value() const&;
  74. T&& value() &&;
  75. const T&& value() const&&;
  76. template <typename U>
  77. T value_or(U&& default_value) const&;
  78. template <typename U>
  79. T value_or(U&& default_value) &&;
  80. void swap(optional& other) noexcept;
  81. void reset() noexcept;
  82. template <typename... Args>
  83. T& emplace(Args&&... args);
  84. private:
  85. bool _has_value = false;
  86. std::allocator<T> _allocator;
  87. union _mem_union
  88. {
  89. T value;
  90. // Explicit constructor and destructor is required to make this work
  91. _mem_union() noexcept {}
  92. ~_mem_union() noexcept {}
  93. } _mem;
  94. };
  95. template <typename T>
  96. optional<typename std::decay<T>::type> make_optional(T&& value)
  97. {
  98. return optional<typename std::decay<T>::type>(std::forward<T>(value));
  99. }
  100. template <typename T, class... Args>
  101. optional<T> make_optional(Args&&... args)
  102. {
  103. return optional<T>(in_place, std::forward<Args>(args)...);
  104. }
  105. template <typename T>
  106. optional<T>::optional(nullopt_t) noexcept
  107. {
  108. }
  109. template <typename T>
  110. optional<T>::optional(const optional& other)
  111. {
  112. *this = other;
  113. }
  114. template <typename T>
  115. optional<T>::optional(optional&& other) noexcept
  116. {
  117. *this = std::move(other);
  118. }
  119. template <typename T>
  120. template <typename... Args>
  121. optional<T>::optional(cm::in_place_t, Args&&... args)
  122. {
  123. this->emplace(std::forward<Args>(args)...);
  124. }
  125. template <typename T>
  126. template <typename U, typename>
  127. optional<T>::optional(U&& v)
  128. {
  129. this->emplace(std::forward<U>(v));
  130. }
  131. template <typename T>
  132. optional<T>::~optional()
  133. {
  134. this->reset();
  135. }
  136. template <typename T>
  137. optional<T>& optional<T>::operator=(nullopt_t) noexcept
  138. {
  139. this->reset();
  140. return *this;
  141. }
  142. template <typename T>
  143. optional<T>& optional<T>::operator=(const optional& other)
  144. {
  145. if (other.has_value()) {
  146. if (this->has_value()) {
  147. this->value() = *other;
  148. } else {
  149. this->emplace(*other);
  150. }
  151. } else {
  152. this->reset();
  153. }
  154. return *this;
  155. }
  156. template <typename T>
  157. optional<T>& optional<T>::operator=(optional&& other) noexcept
  158. {
  159. if (other.has_value()) {
  160. if (this->has_value()) {
  161. this->value() = std::move(*other);
  162. } else {
  163. this->emplace(std::move(*other));
  164. }
  165. } else {
  166. this->reset();
  167. }
  168. return *this;
  169. }
  170. template <typename T>
  171. template <typename U, typename>
  172. optional<T>& optional<T>::operator=(U&& v)
  173. {
  174. if (this->has_value()) {
  175. this->value() = v;
  176. } else {
  177. this->emplace(std::forward<U>(v));
  178. }
  179. return *this;
  180. }
  181. template <typename T>
  182. const T* optional<T>::operator->() const
  183. {
  184. return &**this;
  185. }
  186. template <typename T>
  187. T* optional<T>::operator->()
  188. {
  189. return &**this;
  190. }
  191. template <typename T>
  192. const T& optional<T>::operator*() const&
  193. {
  194. return this->_mem.value;
  195. }
  196. template <typename T>
  197. T& optional<T>::operator*() &
  198. {
  199. return this->_mem.value;
  200. }
  201. template <typename T>
  202. const T&& optional<T>::operator*() const&&
  203. {
  204. return std::move(**this);
  205. }
  206. template <typename T>
  207. T&& optional<T>::operator*() &&
  208. {
  209. return std::move(**this);
  210. }
  211. template <typename T>
  212. bool optional<T>::has_value() const noexcept
  213. {
  214. return this->_has_value;
  215. }
  216. template <typename T>
  217. optional<T>::operator bool() const noexcept
  218. {
  219. return this->has_value();
  220. }
  221. template <typename T>
  222. T& optional<T>::value() &
  223. {
  224. if (!this->has_value()) {
  225. throw cm::bad_optional_access{};
  226. }
  227. return **this;
  228. }
  229. template <typename T>
  230. const T& optional<T>::value() const&
  231. {
  232. if (!this->has_value()) {
  233. throw cm::bad_optional_access{};
  234. }
  235. return **this;
  236. }
  237. template <typename T>
  238. template <typename U>
  239. T optional<T>::value_or(U&& default_value) const&
  240. {
  241. return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
  242. }
  243. template <typename T>
  244. template <typename U>
  245. T optional<T>::value_or(U&& default_value) &&
  246. {
  247. return bool(*this) ? std::move(**this)
  248. : static_cast<T>(std::forward<U>(default_value));
  249. }
  250. template <typename T>
  251. void optional<T>::swap(optional& other) noexcept
  252. {
  253. if (this->has_value()) {
  254. if (other.has_value()) {
  255. using std::swap;
  256. swap(**this, *other);
  257. } else {
  258. other.emplace(std::move(**this));
  259. this->reset();
  260. }
  261. } else if (other.has_value()) {
  262. this->emplace(std::move(*other));
  263. other.reset();
  264. }
  265. }
  266. template <typename T>
  267. void optional<T>::reset() noexcept
  268. {
  269. if (this->has_value()) {
  270. this->_has_value = false;
  271. std::allocator_traits<std::allocator<T>>::destroy(this->_allocator,
  272. &**this);
  273. }
  274. }
  275. template <typename T>
  276. template <typename... Args>
  277. T& optional<T>::emplace(Args&&... args)
  278. {
  279. this->reset();
  280. std::allocator_traits<std::allocator<T>>::construct(
  281. this->_allocator, &**this, std::forward<Args>(args)...);
  282. this->_has_value = true;
  283. return this->value();
  284. }
  285. #endif
  286. }
  287. #endif