optional 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file LICENSE.rst or https://cmake.org/licensing for details. */
  5. #pragma once
  6. #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
  7. # define CMake_HAVE_CXX_OPTIONAL
  8. #endif
  9. #if defined(CMake_HAVE_CXX_OPTIONAL)
  10. # include <optional> // IWYU pragma: export
  11. #else
  12. # if defined(__GNUC__) && !defined(__clang__)
  13. # include <cstring>
  14. # endif
  15. # include <memory>
  16. # include <cm/utility>
  17. #endif
  18. namespace cm {
  19. #if defined(CMake_HAVE_CXX_OPTIONAL)
  20. using std::nullopt_t;
  21. using std::nullopt;
  22. using std::optional;
  23. using std::bad_optional_access;
  24. using std::make_optional;
  25. #else
  26. class bad_optional_access : public std::exception
  27. {
  28. using std::exception::exception;
  29. };
  30. struct nullopt_t
  31. {
  32. explicit constexpr nullopt_t(int) {}
  33. };
  34. constexpr nullopt_t nullopt{ 0 };
  35. template <typename T>
  36. class optional
  37. {
  38. public:
  39. using value_type = T;
  40. optional() noexcept = default;
  41. optional(nullopt_t) noexcept;
  42. optional(optional const& other);
  43. optional(optional&& other) noexcept;
  44. template <typename... Args>
  45. explicit optional(cm::in_place_t, Args&&... args);
  46. template <
  47. typename U = T,
  48. typename = typename std::enable_if<
  49. std::is_constructible<T, U&&>::value &&
  50. !std::is_same<typename std::decay<U>::type, cm::in_place_t>::value &&
  51. !std::is_same<typename std::decay<U>::type,
  52. cm::optional<T>>::value>::type>
  53. optional(U&& v);
  54. ~optional();
  55. optional& operator=(nullopt_t) noexcept;
  56. optional& operator=(optional const& other);
  57. template <typename U = T>
  58. typename std::enable_if<std::is_constructible<T, U&&>::value &&
  59. std::is_assignable<T&, U&&>::value,
  60. optional&>::type
  61. operator=(optional<U>&& other) noexcept;
  62. template <typename U = T>
  63. typename std::enable_if<
  64. !std::is_same<typename std::decay<U>::type, cm::optional<T>>::value &&
  65. std::is_constructible<T, U&&>::value &&
  66. std::is_assignable<T&, U&&>::value &&
  67. (!std::is_scalar<T>::value ||
  68. !std::is_same<typename std::decay<U>::type, T>::value),
  69. optional&>::type
  70. operator=(U&& v);
  71. T const* operator->() const;
  72. T* operator->();
  73. T const& operator*() const&;
  74. T& operator*() &;
  75. T const&& operator*() const&&;
  76. T&& operator*() &&;
  77. explicit operator bool() const noexcept;
  78. bool has_value() const noexcept;
  79. T& value() &;
  80. T const& value() const&;
  81. T&& value() &&;
  82. T const&& value() const&&;
  83. template <typename U>
  84. T value_or(U&& default_value) const&;
  85. template <typename U>
  86. T value_or(U&& default_value) &&;
  87. void swap(optional& other) noexcept;
  88. void reset() noexcept;
  89. template <typename... Args>
  90. T& emplace(Args&&... args);
  91. private:
  92. bool _has_value = false;
  93. std::allocator<T> _allocator;
  94. union _mem_union
  95. {
  96. T value;
  97. // Explicit constructor and destructor is required to make this work
  98. _mem_union() noexcept
  99. {
  100. # if defined(__GNUC__) && !defined(__clang__)
  101. // Some versions of GCC complain about uninitialized memory
  102. std::memset(this, 0, sizeof(*this));
  103. # endif
  104. }
  105. ~_mem_union() noexcept {}
  106. } _mem;
  107. };
  108. template <typename T>
  109. optional<typename std::decay<T>::type> make_optional(T&& value)
  110. {
  111. return optional<typename std::decay<T>::type>(std::forward<T>(value));
  112. }
  113. template <typename T, class... Args>
  114. optional<T> make_optional(Args&&... args)
  115. {
  116. return optional<T>(in_place, std::forward<Args>(args)...);
  117. }
  118. template <typename T>
  119. optional<T>::optional(nullopt_t) noexcept
  120. : optional()
  121. {
  122. }
  123. template <typename T>
  124. optional<T>::optional(optional const& other)
  125. {
  126. if (other.has_value()) {
  127. this->emplace(*other);
  128. }
  129. }
  130. template <typename T>
  131. optional<T>::optional(optional&& other) noexcept
  132. {
  133. if (other.has_value()) {
  134. this->emplace(std::move(*other));
  135. }
  136. }
  137. template <typename T>
  138. template <typename... Args>
  139. optional<T>::optional(cm::in_place_t, Args&&... args)
  140. {
  141. this->emplace(std::forward<Args>(args)...);
  142. }
  143. template <typename T>
  144. template <typename U, typename>
  145. optional<T>::optional(U&& v)
  146. {
  147. this->emplace(std::forward<U>(v));
  148. }
  149. template <typename T>
  150. optional<T>::~optional()
  151. {
  152. this->reset();
  153. }
  154. template <typename T>
  155. optional<T>& optional<T>::operator=(nullopt_t) noexcept
  156. {
  157. this->reset();
  158. return *this;
  159. }
  160. template <typename T>
  161. optional<T>& optional<T>::operator=(optional const& other)
  162. {
  163. if (other.has_value()) {
  164. if (this->has_value()) {
  165. this->value() = *other;
  166. } else {
  167. this->emplace(*other);
  168. }
  169. } else {
  170. this->reset();
  171. }
  172. return *this;
  173. }
  174. template <typename T>
  175. template <typename U>
  176. typename std::enable_if<std::is_constructible<T, U&&>::value &&
  177. std::is_assignable<T&, U&&>::value,
  178. optional<T>&>::type
  179. optional<T>::operator=(optional<U>&& other) noexcept
  180. {
  181. if (other.has_value()) {
  182. if (this->has_value()) {
  183. this->value() = std::move(*other);
  184. } else {
  185. this->emplace(std::move(*other));
  186. }
  187. } else {
  188. this->reset();
  189. }
  190. return *this;
  191. }
  192. template <typename T>
  193. template <typename U>
  194. typename std::enable_if<
  195. !std::is_same<typename std::decay<U>::type, cm::optional<T>>::value &&
  196. std::is_constructible<T, U&&>::value &&
  197. std::is_assignable<T&, U&&>::value &&
  198. (!std::is_scalar<T>::value ||
  199. !std::is_same<typename std::decay<U>::type, T>::value),
  200. optional<T>&>::type
  201. optional<T>::operator=(U&& v)
  202. {
  203. if (this->has_value()) {
  204. this->value() = v;
  205. } else {
  206. this->emplace(std::forward<U>(v));
  207. }
  208. return *this;
  209. }
  210. template <typename T, typename U>
  211. bool operator==(optional<T> const& lhs, optional<U> const& rhs)
  212. {
  213. if (lhs.has_value()) {
  214. return rhs.has_value() && *lhs == *rhs;
  215. }
  216. return !rhs.has_value();
  217. }
  218. template <typename T, typename U>
  219. bool operator!=(optional<T> const& lhs, optional<U> const& rhs)
  220. {
  221. if (lhs.has_value()) {
  222. return !rhs.has_value() || *lhs != *rhs;
  223. }
  224. return rhs.has_value();
  225. }
  226. template <typename T, typename U>
  227. bool operator<(optional<T> const& lhs, optional<U> const& rhs)
  228. {
  229. if (rhs.has_value()) {
  230. return !lhs.has_value() || *lhs < *rhs;
  231. }
  232. return false;
  233. }
  234. template <typename T, typename U>
  235. bool operator<=(optional<T> const& lhs, optional<U> const& rhs)
  236. {
  237. if (!lhs.has_value()) {
  238. return true;
  239. }
  240. if (rhs.has_value()) {
  241. return *lhs <= *rhs;
  242. }
  243. return false;
  244. }
  245. template <typename T, typename U>
  246. bool operator>(optional<T> const& lhs, optional<U> const& rhs)
  247. {
  248. if (lhs.has_value()) {
  249. return !rhs.has_value() || *lhs > *rhs;
  250. }
  251. return false;
  252. }
  253. template <typename T, typename U>
  254. bool operator>=(optional<T> const& lhs, optional<U> const& rhs)
  255. {
  256. if (!rhs.has_value()) {
  257. return true;
  258. }
  259. if (lhs.has_value()) {
  260. return *lhs >= *rhs;
  261. }
  262. return false;
  263. }
  264. template <typename T>
  265. bool operator==(optional<T> const& opt, nullopt_t) noexcept
  266. {
  267. return !opt.has_value();
  268. }
  269. template <typename T>
  270. bool operator!=(optional<T> const& opt, nullopt_t) noexcept
  271. {
  272. return opt.has_value();
  273. }
  274. template <typename T>
  275. bool operator<(optional<T> const& /*opt*/, nullopt_t) noexcept
  276. {
  277. return false;
  278. }
  279. template <typename T>
  280. bool operator<=(optional<T> const& opt, nullopt_t) noexcept
  281. {
  282. return !opt.has_value();
  283. }
  284. template <typename T>
  285. bool operator>(optional<T> const& opt, nullopt_t) noexcept
  286. {
  287. return opt.has_value();
  288. }
  289. template <typename T>
  290. bool operator>=(optional<T> const& /*opt*/, nullopt_t) noexcept
  291. {
  292. return true;
  293. }
  294. template <typename T>
  295. bool operator==(nullopt_t, optional<T> const& opt) noexcept
  296. {
  297. return !opt.has_value();
  298. }
  299. template <typename T>
  300. bool operator!=(nullopt_t, optional<T> const& opt) noexcept
  301. {
  302. return opt.has_value();
  303. }
  304. template <typename T>
  305. bool operator<(nullopt_t, optional<T> const& opt) noexcept
  306. {
  307. return opt.has_value();
  308. }
  309. template <typename T>
  310. bool operator<=(nullopt_t, optional<T> const& /*opt*/) noexcept
  311. {
  312. return true;
  313. }
  314. template <typename T>
  315. bool operator>(nullopt_t, optional<T> const& /*opt*/) noexcept
  316. {
  317. return false;
  318. }
  319. template <typename T>
  320. bool operator>=(nullopt_t, optional<T> const& opt) noexcept
  321. {
  322. return !opt.has_value();
  323. }
  324. template <typename T, typename U>
  325. bool operator==(optional<T> const& opt, U const& value)
  326. {
  327. return opt.has_value() && *opt == value;
  328. }
  329. template <typename T, typename U>
  330. bool operator!=(optional<T> const& opt, U const& value)
  331. {
  332. return !opt.has_value() || *opt != value;
  333. }
  334. template <typename T, typename U>
  335. bool operator<(optional<T> const& opt, U const& value)
  336. {
  337. return !opt.has_value() || *opt < value;
  338. }
  339. template <typename T, typename U>
  340. bool operator<=(optional<T> const& opt, U const& value)
  341. {
  342. return !opt.has_value() || *opt <= value;
  343. }
  344. template <typename T, typename U>
  345. bool operator>(optional<T> const& opt, U const& value)
  346. {
  347. return opt.has_value() && *opt > value;
  348. }
  349. template <typename T, typename U>
  350. bool operator>=(optional<T> const& opt, U const& value)
  351. {
  352. return opt.has_value() && *opt >= value;
  353. }
  354. template <typename T, typename U>
  355. bool operator==(T const& value, optional<U> const& opt)
  356. {
  357. return opt.has_value() && value == *opt;
  358. }
  359. template <typename T, typename U>
  360. bool operator!=(T const& value, optional<U> const& opt)
  361. {
  362. return !opt.has_value() || value != *opt;
  363. }
  364. template <typename T, typename U>
  365. bool operator<(T const& value, optional<U> const& opt)
  366. {
  367. return opt.has_value() && value < *opt;
  368. }
  369. template <typename T, typename U>
  370. bool operator<=(T const& value, optional<U> const& opt)
  371. {
  372. return opt.has_value() && value <= *opt;
  373. }
  374. template <typename T, typename U>
  375. bool operator>(T const& value, optional<U> const& opt)
  376. {
  377. return !opt.has_value() || value > *opt;
  378. }
  379. template <typename T, typename U>
  380. bool operator>=(T const& value, optional<U> const& opt)
  381. {
  382. return !opt.has_value() || value >= *opt;
  383. }
  384. template <typename T>
  385. T const* optional<T>::operator->() const
  386. {
  387. return &**this;
  388. }
  389. template <typename T>
  390. T* optional<T>::operator->()
  391. {
  392. return &**this;
  393. }
  394. template <typename T>
  395. T const& optional<T>::operator*() const&
  396. {
  397. return this->_mem.value;
  398. }
  399. template <typename T>
  400. T& optional<T>::operator*() &
  401. {
  402. return this->_mem.value;
  403. }
  404. template <typename T>
  405. T const&& optional<T>::operator*() const&&
  406. {
  407. return std::move(**this);
  408. }
  409. template <typename T>
  410. T&& optional<T>::operator*() &&
  411. {
  412. return std::move(**this);
  413. }
  414. template <typename T>
  415. bool optional<T>::has_value() const noexcept
  416. {
  417. return this->_has_value;
  418. }
  419. template <typename T>
  420. optional<T>::operator bool() const noexcept
  421. {
  422. return this->has_value();
  423. }
  424. template <typename T>
  425. T& optional<T>::value() &
  426. {
  427. if (!this->has_value()) {
  428. throw cm::bad_optional_access{};
  429. }
  430. return **this;
  431. }
  432. template <typename T>
  433. T const& optional<T>::value() const&
  434. {
  435. if (!this->has_value()) {
  436. throw cm::bad_optional_access{};
  437. }
  438. return **this;
  439. }
  440. template <typename T>
  441. template <typename U>
  442. T optional<T>::value_or(U&& default_value) const&
  443. {
  444. return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
  445. }
  446. template <typename T>
  447. template <typename U>
  448. T optional<T>::value_or(U&& default_value) &&
  449. {
  450. return bool(*this) ? std::move(**this)
  451. : static_cast<T>(std::forward<U>(default_value));
  452. }
  453. template <typename T>
  454. void optional<T>::swap(optional& other) noexcept
  455. {
  456. if (this->has_value()) {
  457. if (other.has_value()) {
  458. using std::swap;
  459. swap(**this, *other);
  460. } else {
  461. other.emplace(std::move(**this));
  462. this->reset();
  463. }
  464. } else if (other.has_value()) {
  465. this->emplace(std::move(*other));
  466. other.reset();
  467. }
  468. }
  469. template <typename T>
  470. void optional<T>::reset() noexcept
  471. {
  472. if (this->has_value()) {
  473. this->_has_value = false;
  474. std::allocator_traits<std::allocator<T>>::destroy(this->_allocator,
  475. &**this);
  476. }
  477. }
  478. template <typename T>
  479. template <typename... Args>
  480. T& optional<T>::emplace(Args&&... args)
  481. {
  482. this->reset();
  483. std::allocator_traits<std::allocator<T>>::construct(
  484. this->_allocator, &**this, std::forward<Args>(args)...);
  485. this->_has_value = true;
  486. return this->value();
  487. }
  488. #endif
  489. }