optional_implementation.hpp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. // The MIT License (MIT)
  2. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. // this software and associated documentation files (the "Software"), to deal in
  5. // the Software without restriction, including without limitation the rights to
  6. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. // the Software, and to permit persons to whom the Software is furnished to do so,
  8. // subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. // Taken from: TartanLlama/optional on Github, because
  18. // holy shit am I done dealing with C++11 constexpr
  19. ///
  20. // optional - An implementation of std::optional with extensions
  21. // Written in 2017 by Simon Brand (@TartanLlama)
  22. //
  23. // To the extent possible under law, the author(s) have dedicated all
  24. // copyright and related and neighboring rights to this software to the
  25. // public domain worldwide. This software is distributed without any warranty.
  26. //
  27. // You should have received a copy of the CC0 Public Domain Dedication
  28. // along with this software. If not, see
  29. // <http://creativecommons.org/publicdomain/zero/1.0/>.
  30. ///
  31. #ifndef SOL_TL_OPTIONAL_HPP
  32. #define SOL_TL_OPTIONAL_HPP
  33. #include <sol/version.hpp>
  34. #include <sol/in_place.hpp>
  35. #define SOL_TL_OPTIONAL_VERSION_MAJOR 0
  36. #define SOL_TL_OPTIONAL_VERSION_MINOR 5
  37. #include <exception>
  38. #include <functional>
  39. #include <new>
  40. #include <type_traits>
  41. #include <utility>
  42. #include <cstdlib>
  43. #include <optional>
  44. #if (defined(_MSC_VER) && _MSC_VER == 1900)
  45. #define SOL_TL_OPTIONAL_MSVC2015
  46. #endif
  47. #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
  48. #define SOL_TL_OPTIONAL_GCC49
  49. #endif
  50. #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && !defined(__clang__))
  51. #define SOL_TL_OPTIONAL_GCC54
  52. #endif
  53. #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && !defined(__clang__))
  54. #define SOL_TL_OPTIONAL_GCC55
  55. #endif
  56. #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
  57. // GCC < 5 doesn't support overloading on const&& for member functions
  58. #define SOL_TL_OPTIONAL_NO_CONSTRR
  59. // GCC < 5 doesn't support some standard C++11 type traits
  60. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::has_trivial_copy_constructor<T>::value
  61. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign<T>::value
  62. // This one will be different for GCC 5.7 if it's ever supported
  63. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  64. // GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector
  65. // for non-copyable types
  66. #elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
  67. #ifndef SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
  68. #define SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
  69. namespace sol { namespace detail {
  70. template <class T>
  71. struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T> { };
  72. #ifdef _GLIBCXX_VECTOR
  73. template <class T, class A>
  74. struct is_trivially_copy_constructible<std::vector<T, A>> : std::is_trivially_copy_constructible<T> { };
  75. #endif
  76. }} // namespace sol::detail
  77. #endif
  78. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) sol::detail::is_trivially_copy_constructible<T>::value
  79. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
  80. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  81. #else
  82. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::is_trivially_copy_constructible<T>::value
  83. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
  84. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  85. #endif
  86. #if __cplusplus > 201103L
  87. #define SOL_TL_OPTIONAL_CXX14
  88. #endif
  89. // constexpr implies const in C++11, not C++14
  90. #if (__cplusplus == 201103L || defined(SOL_TL_OPTIONAL_MSVC2015) || defined(SOL_TL_OPTIONAL_GCC49))
  91. /// \exclude
  92. #define SOL_TL_OPTIONAL_11_CONSTEXPR
  93. #else
  94. /// \exclude
  95. #define SOL_TL_OPTIONAL_11_CONSTEXPR constexpr
  96. #endif
  97. namespace sol {
  98. #ifndef SOL_TL_MONOSTATE_INPLACE_MUTEX
  99. #define SOL_TL_MONOSTATE_INPLACE_MUTEX
  100. /// \brief Used to represent an optional with no data; essentially a bool
  101. class monostate { };
  102. #endif
  103. template <class T>
  104. class optional;
  105. /// \exclude
  106. namespace detail {
  107. #ifndef SOL_TL_TRAITS_MUTEX
  108. #define SOL_TL_TRAITS_MUTEX
  109. // C++14-style aliases for brevity
  110. template <class T>
  111. using remove_const_t = typename std::remove_const<T>::type;
  112. template <class T>
  113. using remove_reference_t = typename std::remove_reference<T>::type;
  114. template <class T>
  115. using decay_t = typename std::decay<T>::type;
  116. template <bool E, class T = void>
  117. using enable_if_t = typename std::enable_if<E, T>::type;
  118. template <bool B, class T, class F>
  119. using conditional_t = typename std::conditional<B, T, F>::type;
  120. // std::conjunction from C++17
  121. template <class...>
  122. struct conjunction : std::true_type { };
  123. template <class B>
  124. struct conjunction<B> : B { };
  125. template <class B, class... Bs>
  126. struct conjunction<B, Bs...> : std::conditional<bool(B::value), conjunction<Bs...>, B>::type { };
  127. #if defined(_LIBCPP_VERSION) && __cplusplus == 201103L
  128. #define SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  129. #endif
  130. // In C++11 mode, there's an issue in libc++'s std::mem_fn
  131. // which results in a hard-error when using it in a noexcept expression
  132. // in some cases. This is a check to workaround the common failing case.
  133. #ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  134. template <class T>
  135. struct is_pointer_to_non_const_member_func : std::false_type { };
  136. template <class T, class Ret, class... Args>
  137. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)> : std::true_type { };
  138. template <class T, class Ret, class... Args>
  139. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)&> : std::true_type { };
  140. template <class T, class Ret, class... Args>
  141. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &&> : std::true_type { };
  142. template <class T, class Ret, class... Args>
  143. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile> : std::true_type { };
  144. template <class T, class Ret, class... Args>
  145. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&> : std::true_type { };
  146. template <class T, class Ret, class... Args>
  147. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&&> : std::true_type { };
  148. template <class T>
  149. struct is_const_or_const_ref : std::false_type { };
  150. template <class T>
  151. struct is_const_or_const_ref<T const&> : std::true_type { };
  152. template <class T>
  153. struct is_const_or_const_ref<T const> : std::true_type { };
  154. #endif
  155. // std::invoke from C++17
  156. // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
  157. template <typename Fn, typename... Args,
  158. #ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  159. typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value && is_const_or_const_ref<Args...>::value)>,
  160. #endif
  161. typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>, int = 0>
  162. constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
  163. -> decltype(std::mem_fn(f)(std::forward<Args>(args)...)) {
  164. return std::mem_fn(f)(std::forward<Args>(args)...);
  165. }
  166. template <typename Fn, typename... Args, typename = enable_if_t<!std::is_member_pointer<decay_t<Fn>>::value>>
  167. constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::forward<Fn>(f)(std::forward<Args>(args)...)))
  168. -> decltype(std::forward<Fn>(f)(std::forward<Args>(args)...)) {
  169. return std::forward<Fn>(f)(std::forward<Args>(args)...);
  170. }
  171. // std::invoke_result from C++17
  172. template <class F, class, class... Us>
  173. struct invoke_result_impl;
  174. template <class F, class... Us>
  175. struct invoke_result_impl<F, decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()), Us...> {
  176. using type = decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
  177. };
  178. template <class F, class... Us>
  179. using invoke_result = invoke_result_impl<F, void, Us...>;
  180. template <class F, class... Us>
  181. using invoke_result_t = typename invoke_result<F, Us...>::type;
  182. #endif
  183. // std::void_t from C++17
  184. template <class...>
  185. struct voider {
  186. using type = void;
  187. };
  188. template <class... Ts>
  189. using void_t = typename voider<Ts...>::type;
  190. // Trait for checking if a type is a sol::optional
  191. template <class T>
  192. struct is_optional_impl : std::false_type { };
  193. template <class T>
  194. struct is_optional_impl<optional<T>> : std::true_type { };
  195. template <class T>
  196. using is_optional = is_optional_impl<decay_t<T>>;
  197. // Change void to sol::monostate
  198. template <class U>
  199. using fixup_void = conditional_t<std::is_void<U>::value, monostate, U>;
  200. template <class F, class U, class = invoke_result_t<F, U>>
  201. using get_map_return = optional<fixup_void<invoke_result_t<F, U>>>;
  202. // Check if invoking F for some Us returns void
  203. template <class F, class = void, class... U>
  204. struct returns_void_impl;
  205. template <class F, class... U>
  206. struct returns_void_impl<F, void_t<invoke_result_t<F, U...>>, U...> : std::is_void<invoke_result_t<F, U...>> { };
  207. template <class F, class... U>
  208. using returns_void = returns_void_impl<F, void, U...>;
  209. template <class T, class... U>
  210. using enable_if_ret_void = enable_if_t<returns_void<T&&, U...>::value>;
  211. template <class T, class... U>
  212. using disable_if_ret_void = enable_if_t<!returns_void<T&&, U...>::value>;
  213. template <class T, class U>
  214. using enable_forward_value = detail::enable_if_t<std::is_constructible<T, U&&>::value && !std::is_same<detail::decay_t<U>, in_place_t>::value
  215. && !std::is_same<optional<T>, detail::decay_t<U>>::value>;
  216. template <class T, class U, class Other>
  217. using enable_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && !std::is_constructible<T, optional<U>&>::value
  218. && !std::is_constructible<T, optional<U>&&>::value && !std::is_constructible<T, const optional<U>&>::value
  219. && !std::is_constructible<T, const optional<U>&&>::value && !std::is_convertible<optional<U>&, T>::value
  220. && !std::is_convertible<optional<U>&&, T>::value && !std::is_convertible<const optional<U>&, T>::value
  221. && !std::is_convertible<const optional<U>&&, T>::value>;
  222. template <class T, class U>
  223. using enable_assign_forward = detail::enable_if_t<!std::is_same<optional<T>, detail::decay_t<U>>::value
  224. && !detail::conjunction<std::is_scalar<T>, std::is_same<T, detail::decay_t<U>>>::value && std::is_constructible<T, U>::value
  225. && std::is_assignable<T&, U>::value>;
  226. template <class T, class U, class Other>
  227. using enable_assign_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && std::is_assignable<T&, Other>::value
  228. && !std::is_constructible<T, optional<U>&>::value && !std::is_constructible<T, optional<U>&&>::value
  229. && !std::is_constructible<T, const optional<U>&>::value && !std::is_constructible<T, const optional<U>&&>::value
  230. && !std::is_convertible<optional<U>&, T>::value && !std::is_convertible<optional<U>&&, T>::value
  231. && !std::is_convertible<const optional<U>&, T>::value && !std::is_convertible<const optional<U>&&, T>::value
  232. && !std::is_assignable<T&, optional<U>&>::value && !std::is_assignable<T&, optional<U>&&>::value
  233. && !std::is_assignable<T&, const optional<U>&>::value && !std::is_assignable<T&, const optional<U>&&>::value>;
  234. #ifdef _MSC_VER
  235. // TODO make a version which works with MSVC
  236. template <class T, class U = T>
  237. struct is_swappable : std::true_type { };
  238. template <class T, class U = T>
  239. struct is_nothrow_swappable : std::true_type { };
  240. #else
  241. // https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept
  242. namespace swap_adl_tests {
  243. // if swap ADL finds this then it would call std::swap otherwise (same
  244. // signature)
  245. struct tag { };
  246. template <class T>
  247. tag swap(T&, T&);
  248. template <class T, std::size_t N>
  249. tag swap(T (&a)[N], T (&b)[N]);
  250. // helper functions to test if an unqualified swap is possible, and if it
  251. // becomes std::swap
  252. template <class, class>
  253. std::false_type can_swap(...) noexcept(false);
  254. template <class T, class U, class = decltype(swap(std::declval<T&>(), std::declval<U&>()))>
  255. std::true_type can_swap(int) noexcept(noexcept(swap(std::declval<T&>(), std::declval<U&>())));
  256. template <class, class>
  257. std::false_type uses_std(...);
  258. template <class T, class U>
  259. std::is_same<decltype(swap(std::declval<T&>(), std::declval<U&>())), tag> uses_std(int);
  260. template <class T>
  261. struct is_std_swap_noexcept
  262. : std::integral_constant<bool, std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value> { };
  263. template <class T, std::size_t N>
  264. struct is_std_swap_noexcept<T[N]> : is_std_swap_noexcept<T> { };
  265. template <class T, class U>
  266. struct is_adl_swap_noexcept : std::integral_constant<bool, noexcept(can_swap<T, U>(0))> { };
  267. } // namespace swap_adl_tests
  268. template <class T, class U = T>
  269. struct is_swappable : std::integral_constant<bool,
  270. decltype(detail::swap_adl_tests::can_swap<T, U>(0))::value
  271. && (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value
  272. || (std::is_move_assignable<T>::value && std::is_move_constructible<T>::value))> { };
  273. template <class T, std::size_t N>
  274. struct is_swappable<T[N], T[N]> : std::integral_constant<bool,
  275. decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value
  276. && (!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value || is_swappable<T, T>::value)> { };
  277. template <class T, class U = T>
  278. struct is_nothrow_swappable
  279. : std::integral_constant<bool,
  280. is_swappable<T, U>::value
  281. && ((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_std_swap_noexcept<T>::value)
  282. || (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_adl_swap_noexcept<T, U>::value))> { };
  283. #endif
  284. // The storage base manages the actual storage, and correctly propagates
  285. // trivial destroyion from T. This case is for when T is not trivially
  286. // destructible.
  287. template <class T, bool = ::std::is_trivially_destructible<T>::value>
  288. struct optional_storage_base {
  289. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
  290. }
  291. template <class... U>
  292. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
  293. }
  294. ~optional_storage_base() {
  295. if (m_has_value) {
  296. m_value.~T();
  297. m_has_value = false;
  298. }
  299. }
  300. struct dummy { };
  301. union {
  302. dummy m_dummy;
  303. T m_value;
  304. };
  305. bool m_has_value;
  306. };
  307. // This case is for when T is trivially destructible.
  308. template <class T>
  309. struct optional_storage_base<T, true> {
  310. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
  311. }
  312. template <class... U>
  313. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
  314. }
  315. // No destructor, so this class is trivially destructible
  316. struct dummy { };
  317. union {
  318. dummy m_dummy;
  319. T m_value;
  320. };
  321. bool m_has_value = false;
  322. };
  323. // This base class provides some handy member functions which can be used in
  324. // further derived classes
  325. template <class T>
  326. struct optional_operations_base : optional_storage_base<T> {
  327. using optional_storage_base<T>::optional_storage_base;
  328. void hard_reset() noexcept {
  329. get().~T();
  330. this->m_has_value = false;
  331. }
  332. template <class... Args>
  333. void construct(Args&&... args) noexcept {
  334. new (std::addressof(this->m_value)) T(std::forward<Args>(args)...);
  335. this->m_has_value = true;
  336. }
  337. template <class Opt>
  338. void assign(Opt&& rhs) {
  339. if (this->has_value()) {
  340. if (rhs.has_value()) {
  341. this->m_value = std::forward<Opt>(rhs).get();
  342. }
  343. else {
  344. this->m_value.~T();
  345. this->m_has_value = false;
  346. }
  347. }
  348. else if (rhs.has_value()) {
  349. construct(std::forward<Opt>(rhs).get());
  350. }
  351. }
  352. bool has_value() const {
  353. return this->m_has_value;
  354. }
  355. SOL_TL_OPTIONAL_11_CONSTEXPR T& get() & {
  356. return this->m_value;
  357. }
  358. SOL_TL_OPTIONAL_11_CONSTEXPR const T& get() const& {
  359. return this->m_value;
  360. }
  361. SOL_TL_OPTIONAL_11_CONSTEXPR T&& get() && {
  362. return std::move(this->m_value);
  363. }
  364. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  365. constexpr const T&& get() const&& {
  366. return std::move(this->m_value);
  367. }
  368. #endif
  369. };
  370. // This class manages conditionally having a trivial copy constructor
  371. // This specialization is for when T is trivially copy constructible
  372. template <class T, bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)>
  373. struct optional_copy_base : optional_operations_base<T> {
  374. using optional_operations_base<T>::optional_operations_base;
  375. };
  376. // This specialization is for when T is not trivially copy constructible
  377. template <class T>
  378. struct optional_copy_base<T, false> : optional_operations_base<T> {
  379. using base_t = optional_operations_base<T>;
  380. using base_t::base_t;
  381. optional_copy_base() = default;
  382. optional_copy_base(const optional_copy_base& rhs) : base_t() {
  383. if (rhs.has_value()) {
  384. this->construct(rhs.get());
  385. }
  386. else {
  387. this->m_has_value = false;
  388. }
  389. }
  390. optional_copy_base(optional_copy_base&& rhs) = default;
  391. optional_copy_base& operator=(const optional_copy_base& rhs) = default;
  392. optional_copy_base& operator=(optional_copy_base&& rhs) = default;
  393. };
  394. // This class manages conditionally having a trivial move constructor
  395. // Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
  396. // doesn't implement an analogue to std::is_trivially_move_constructible. We
  397. // have to make do with a non-trivial move constructor even if T is trivially
  398. // move constructible
  399. #ifndef SOL_TL_OPTIONAL_GCC49
  400. template <class T, bool = std::is_trivially_move_constructible<T>::value>
  401. struct optional_move_base : optional_copy_base<T> {
  402. using optional_copy_base<T>::optional_copy_base;
  403. };
  404. #else
  405. template <class T, bool = false>
  406. struct optional_move_base;
  407. #endif
  408. template <class T>
  409. struct optional_move_base<T, false> : optional_copy_base<T> {
  410. using optional_copy_base<T>::optional_copy_base;
  411. optional_move_base() = default;
  412. optional_move_base(const optional_move_base& rhs) = default;
  413. optional_move_base(optional_move_base&& rhs) noexcept(std::is_nothrow_move_constructible<T>::value) {
  414. if (rhs.has_value()) {
  415. this->construct(std::move(rhs.get()));
  416. }
  417. else {
  418. this->m_has_value = false;
  419. }
  420. }
  421. optional_move_base& operator=(const optional_move_base& rhs) = default;
  422. optional_move_base& operator=(optional_move_base&& rhs) = default;
  423. };
  424. // This class manages conditionally having a trivial copy assignment operator
  425. template <class T,
  426. bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) && SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)
  427. && SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)>
  428. struct optional_copy_assign_base : optional_move_base<T> {
  429. using optional_move_base<T>::optional_move_base;
  430. };
  431. template <class T>
  432. struct optional_copy_assign_base<T, false> : optional_move_base<T> {
  433. using optional_move_base<T>::optional_move_base;
  434. optional_copy_assign_base() = default;
  435. optional_copy_assign_base(const optional_copy_assign_base& rhs) = default;
  436. optional_copy_assign_base(optional_copy_assign_base&& rhs) = default;
  437. optional_copy_assign_base& operator=(const optional_copy_assign_base& rhs) {
  438. this->assign(rhs);
  439. return *this;
  440. }
  441. optional_copy_assign_base& operator=(optional_copy_assign_base&& rhs) = default;
  442. };
  443. // This class manages conditionally having a trivial move assignment operator
  444. // Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
  445. // doesn't implement an analogue to std::is_trivially_move_assignable. We have
  446. // to make do with a non-trivial move assignment operator even if T is trivially
  447. // move assignable
  448. #ifndef SOL_TL_OPTIONAL_GCC49
  449. template <class T,
  450. bool = std::is_trivially_destructible<T>::value&& std::is_trivially_move_constructible<T>::value&& std::is_trivially_move_assignable<T>::value>
  451. struct optional_move_assign_base : optional_copy_assign_base<T> {
  452. using optional_copy_assign_base<T>::optional_copy_assign_base;
  453. };
  454. #else
  455. template <class T, bool = false>
  456. struct optional_move_assign_base;
  457. #endif
  458. template <class T>
  459. struct optional_move_assign_base<T, false> : optional_copy_assign_base<T> {
  460. using optional_copy_assign_base<T>::optional_copy_assign_base;
  461. optional_move_assign_base() = default;
  462. optional_move_assign_base(const optional_move_assign_base& rhs) = default;
  463. optional_move_assign_base(optional_move_assign_base&& rhs) = default;
  464. optional_move_assign_base& operator=(const optional_move_assign_base& rhs) = default;
  465. optional_move_assign_base& operator=(optional_move_assign_base&& rhs) noexcept(
  466. std::is_nothrow_move_constructible<T>::value&& std::is_nothrow_move_assignable<T>::value) {
  467. this->assign(std::move(rhs));
  468. return *this;
  469. }
  470. };
  471. // optional_delete_ctor_base will conditionally delete copy and move
  472. // constructors depending on whether T is copy/move constructible
  473. template <class T, bool EnableCopy = std::is_copy_constructible<T>::value, bool EnableMove = std::is_move_constructible<T>::value>
  474. struct optional_delete_ctor_base {
  475. optional_delete_ctor_base() = default;
  476. optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
  477. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
  478. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  479. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  480. };
  481. template <class T>
  482. struct optional_delete_ctor_base<T, true, false> {
  483. optional_delete_ctor_base() = default;
  484. optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
  485. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
  486. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  487. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  488. };
  489. template <class T>
  490. struct optional_delete_ctor_base<T, false, true> {
  491. optional_delete_ctor_base() = default;
  492. optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
  493. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
  494. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  495. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  496. };
  497. template <class T>
  498. struct optional_delete_ctor_base<T, false, false> {
  499. optional_delete_ctor_base() = default;
  500. optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
  501. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
  502. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  503. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  504. };
  505. // optional_delete_assign_base will conditionally delete copy and move
  506. // constructors depending on whether T is copy/move constructible + assignable
  507. template <class T, bool EnableCopy = (std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value),
  508. bool EnableMove = (std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)>
  509. struct optional_delete_assign_base {
  510. optional_delete_assign_base() = default;
  511. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  512. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  513. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
  514. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
  515. };
  516. template <class T>
  517. struct optional_delete_assign_base<T, true, false> {
  518. optional_delete_assign_base() = default;
  519. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  520. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  521. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
  522. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
  523. };
  524. template <class T>
  525. struct optional_delete_assign_base<T, false, true> {
  526. optional_delete_assign_base() = default;
  527. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  528. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  529. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
  530. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
  531. };
  532. template <class T>
  533. struct optional_delete_assign_base<T, false, false> {
  534. optional_delete_assign_base() = default;
  535. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  536. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  537. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
  538. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
  539. };
  540. } // namespace detail
  541. /// \brief A tag type to represent an empty optional
  542. using nullopt_t = std::nullopt_t;
  543. /// \brief Represents an empty optional
  544. /// \synopsis static constexpr nullopt_t nullopt;
  545. ///
  546. /// *Examples*:
  547. /// ```
  548. /// sol::optional<int> a = sol::nullopt;
  549. /// void foo (sol::optional<int>);
  550. /// foo(sol::nullopt); //pass an empty optional
  551. /// ```
  552. using std::nullopt;
  553. /// @brief An exception for when an optional is accessed through specific methods while it is not engaged.
  554. class bad_optional_access : public std::exception {
  555. public:
  556. /// @brief Default-constructs an optional exception.
  557. bad_optional_access() = default;
  558. /// @brief Returns a pointer to a null-terminated string containing the reason for the exception.
  559. const char* what() const noexcept override {
  560. return "Optional has no value";
  561. }
  562. };
  563. /// An optional object is an object that contains the storage for another
  564. /// object and manages the lifetime of this contained object, if any. The
  565. /// contained object may be initialized after the optional object has been
  566. /// initialized, and may be destroyed before the optional object has been
  567. /// destroyed. The initialization state of the contained object is tracked by
  568. /// the optional object.
  569. template <class T>
  570. class optional : private detail::optional_move_assign_base<T>,
  571. private detail::optional_delete_ctor_base<T>,
  572. private detail::optional_delete_assign_base<T> {
  573. using base = detail::optional_move_assign_base<T>;
  574. static_assert(!std::is_same<T, in_place_t>::value, "instantiation of optional with in_place_t is ill-formed");
  575. static_assert(!std::is_same<detail::decay_t<T>, nullopt_t>::value, "instantiation of optional with nullopt_t is ill-formed");
  576. public:
  577. // The different versions for C++14 and 11 are needed because deduced return
  578. // types are not SFINAE-safe. This provides better support for things like
  579. // generic lambdas. C.f.
  580. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html
  581. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  582. /// \group and_then
  583. /// Carries out some operation which returns an optional on the stored
  584. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  585. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  586. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  587. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  588. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  589. /// is returned.
  590. /// \group and_then
  591. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
  592. template <class F>
  593. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) & {
  594. using result = detail::invoke_result_t<F, T&>;
  595. static_assert(detail::is_optional<result>::value, "F must return an optional");
  596. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  597. }
  598. /// \group and_then
  599. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
  600. template <class F>
  601. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) && {
  602. using result = detail::invoke_result_t<F, T&&>;
  603. static_assert(detail::is_optional<result>::value, "F must return an optional");
  604. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  605. }
  606. /// \group and_then
  607. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
  608. template <class F>
  609. constexpr auto and_then(F&& f) const& {
  610. using result = detail::invoke_result_t<F, const T&>;
  611. static_assert(detail::is_optional<result>::value, "F must return an optional");
  612. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  613. }
  614. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  615. /// \group and_then
  616. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
  617. template <class F>
  618. constexpr auto and_then(F&& f) const&& {
  619. using result = detail::invoke_result_t<F, const T&&>;
  620. static_assert(detail::is_optional<result>::value, "F must return an optional");
  621. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  622. }
  623. #endif
  624. #else
  625. /// \group and_then
  626. /// Carries out some operation which returns an optional on the stored
  627. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  628. /// value())` returns a `std::optional<U>` for some `U`.
  629. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  630. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  631. /// `*this` is empty, otherwise the return value of
  632. /// `std::invoke(std::forward<F>(f), value())` is returned.
  633. /// \group and_then
  634. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
  635. template <class F>
  636. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) & {
  637. using result = detail::invoke_result_t<F, T&>;
  638. static_assert(detail::is_optional<result>::value, "F must return an optional");
  639. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  640. }
  641. /// \group and_then
  642. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
  643. template <class F>
  644. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&&> and_then(F&& f) && {
  645. using result = detail::invoke_result_t<F, T&&>;
  646. static_assert(detail::is_optional<result>::value, "F must return an optional");
  647. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  648. }
  649. /// \group and_then
  650. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
  651. template <class F>
  652. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const& {
  653. using result = detail::invoke_result_t<F, const T&>;
  654. static_assert(detail::is_optional<result>::value, "F must return an optional");
  655. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  656. }
  657. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  658. /// \group and_then
  659. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
  660. template <class F>
  661. constexpr detail::invoke_result_t<F, const T&&> and_then(F&& f) const&& {
  662. using result = detail::invoke_result_t<F, const T&&>;
  663. static_assert(detail::is_optional<result>::value, "F must return an optional");
  664. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  665. }
  666. #endif
  667. #endif
  668. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  669. /// \brief Carries out some operation on the stored object if there is one.
  670. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  671. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  672. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  673. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  674. /// returned.
  675. ///
  676. /// \group map
  677. /// \synopsis template <class F> constexpr auto map(F &&f) &;
  678. template <class F>
  679. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) & {
  680. return optional_map_impl(*this, std::forward<F>(f));
  681. }
  682. /// \group map
  683. /// \synopsis template <class F> constexpr auto map(F &&f) &&;
  684. template <class F>
  685. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) && {
  686. return optional_map_impl(std::move(*this), std::forward<F>(f));
  687. }
  688. /// \group map
  689. /// \synopsis template <class F> constexpr auto map(F &&f) const&;
  690. template <class F>
  691. constexpr auto map(F&& f) const& {
  692. return optional_map_impl(*this, std::forward<F>(f));
  693. }
  694. /// \group map
  695. /// \synopsis template <class F> constexpr auto map(F &&f) const&&;
  696. template <class F>
  697. constexpr auto map(F&& f) const&& {
  698. return optional_map_impl(std::move(*this), std::forward<F>(f));
  699. }
  700. #else
  701. /// \brief Carries out some operation on the stored object if there is one.
  702. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  703. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  704. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  705. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  706. /// returned.
  707. ///
  708. /// \group map
  709. /// \synopsis template <class F> auto map(F &&f) &;
  710. template <class F>
  711. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(), std::declval<F&&>())) map(F&& f) & {
  712. return optional_map_impl(*this, std::forward<F>(f));
  713. }
  714. /// \group map
  715. /// \synopsis template <class F> auto map(F &&f) &&;
  716. template <class F>
  717. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&&>(), std::declval<F&&>())) map(F&& f) && {
  718. return optional_map_impl(std::move(*this), std::forward<F>(f));
  719. }
  720. /// \group map
  721. /// \synopsis template <class F> auto map(F &&f) const&;
  722. template <class F>
  723. constexpr decltype(optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F&& f) const& {
  724. return optional_map_impl(*this, std::forward<F>(f));
  725. }
  726. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  727. /// \group map
  728. /// \synopsis template <class F> auto map(F &&f) const&&;
  729. template <class F>
  730. constexpr decltype(optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F&& f) const&& {
  731. return optional_map_impl(std::move(*this), std::forward<F>(f));
  732. }
  733. #endif
  734. #endif
  735. /// \brief Calls `f` if the optional is empty
  736. /// \requires `std::invoke_result_t<F>` must be void or convertible to
  737. /// `optional<T>`.
  738. /// \effects If `*this` has a value, returns `*this`.
  739. /// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
  740. /// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
  741. ///
  742. /// \group or_else
  743. /// \synopsis template <class F> optional<T> or_else (F &&f) &;
  744. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  745. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  746. if (has_value())
  747. return *this;
  748. std::forward<F>(f)();
  749. return nullopt;
  750. }
  751. /// \exclude
  752. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  753. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  754. return has_value() ? *this : std::forward<F>(f)();
  755. }
  756. /// \group or_else
  757. /// \synopsis template <class F> optional<T> or_else (F &&f) &&;
  758. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  759. optional<T> or_else(F&& f) && {
  760. if (has_value())
  761. return std::move(*this);
  762. std::forward<F>(f)();
  763. return nullopt;
  764. }
  765. /// \exclude
  766. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  767. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) && {
  768. return has_value() ? std::move(*this) : std::forward<F>(f)();
  769. }
  770. /// \group or_else
  771. /// \synopsis template <class F> optional<T> or_else (F &&f) const &;
  772. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  773. optional<T> or_else(F&& f) const& {
  774. if (has_value())
  775. return *this;
  776. std::forward<F>(f)();
  777. return nullopt;
  778. }
  779. /// \exclude
  780. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  781. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) const& {
  782. return has_value() ? *this : std::forward<F>(f)();
  783. }
  784. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  785. /// \exclude
  786. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  787. optional<T> or_else(F&& f) const&& {
  788. if (has_value())
  789. return std::move(*this);
  790. std::forward<F>(f)();
  791. return nullopt;
  792. }
  793. /// \exclude
  794. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  795. optional<T> or_else(F&& f) const&& {
  796. return has_value() ? std::move(*this) : std::forward<F>(f)();
  797. }
  798. #endif
  799. /// \brief Maps the stored value with `f` if there is one, otherwise returns
  800. /// `u`.
  801. ///
  802. /// \details If there is a value stored, then `f` is called with `**this`
  803. /// and the value is returned. Otherwise `u` is returned.
  804. ///
  805. /// \group map_or
  806. template <class F, class U>
  807. U map_or(F&& f, U&& u) & {
  808. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  809. }
  810. /// \group map_or
  811. template <class F, class U>
  812. U map_or(F&& f, U&& u) && {
  813. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  814. }
  815. /// \group map_or
  816. template <class F, class U>
  817. U map_or(F&& f, U&& u) const& {
  818. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  819. }
  820. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  821. /// \group map_or
  822. template <class F, class U>
  823. U map_or(F&& f, U&& u) const&& {
  824. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  825. }
  826. #endif
  827. /// \brief Maps the stored value with `f` if there is one, otherwise calls
  828. /// `u` and returns the result.
  829. ///
  830. /// \details If there is a value stored, then `f` is
  831. /// called with `**this` and the value is returned. Otherwise
  832. /// `std::forward<U>(u)()` is returned.
  833. ///
  834. /// \group map_or_else
  835. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u) &;
  836. template <class F, class U>
  837. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) & {
  838. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  839. }
  840. /// \group map_or_else
  841. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  842. /// &&;
  843. template <class F, class U>
  844. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) && {
  845. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  846. }
  847. /// \group map_or_else
  848. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  849. /// const &;
  850. template <class F, class U>
  851. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const& {
  852. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  853. }
  854. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  855. /// \group map_or_else
  856. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  857. /// const &&;
  858. template <class F, class U>
  859. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const&& {
  860. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  861. }
  862. #endif
  863. /// \returns `u` if `*this` has a value, otherwise an empty optional.
  864. template <class U>
  865. constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const {
  866. using result = optional<detail::decay_t<U>>;
  867. return has_value() ? result { u } : result { nullopt };
  868. }
  869. /// \returns `rhs` if `*this` is empty, otherwise the current value.
  870. /// \group disjunction
  871. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) & {
  872. return has_value() ? *this : rhs;
  873. }
  874. /// \group disjunction
  875. constexpr optional disjunction(const optional& rhs) const& {
  876. return has_value() ? *this : rhs;
  877. }
  878. /// \group disjunction
  879. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) && {
  880. return has_value() ? std::move(*this) : rhs;
  881. }
  882. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  883. /// \group disjunction
  884. constexpr optional disjunction(const optional& rhs) const&& {
  885. return has_value() ? std::move(*this) : rhs;
  886. }
  887. #endif
  888. /// \group disjunction
  889. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) & {
  890. return has_value() ? *this : std::move(rhs);
  891. }
  892. /// \group disjunction
  893. constexpr optional disjunction(optional&& rhs) const& {
  894. return has_value() ? *this : std::move(rhs);
  895. }
  896. /// \group disjunction
  897. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) && {
  898. return has_value() ? std::move(*this) : std::move(rhs);
  899. }
  900. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  901. /// \group disjunction
  902. constexpr optional disjunction(optional&& rhs) const&& {
  903. return has_value() ? std::move(*this) : std::move(rhs);
  904. }
  905. #endif
  906. /// Takes the value out of the optional, leaving it empty
  907. /// \group take
  908. optional take() & {
  909. optional ret = *this;
  910. reset();
  911. return ret;
  912. }
  913. /// \group take
  914. optional take() const& {
  915. optional ret = *this;
  916. reset();
  917. return ret;
  918. }
  919. /// \group take
  920. optional take() && {
  921. optional ret = std::move(*this);
  922. reset();
  923. return ret;
  924. }
  925. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  926. /// \group take
  927. optional take() const&& {
  928. optional ret = std::move(*this);
  929. reset();
  930. return ret;
  931. }
  932. #endif
  933. using value_type = T;
  934. /// Constructs an optional that does not contain a value.
  935. /// \group ctor_empty
  936. constexpr optional() noexcept = default;
  937. /// \group ctor_empty
  938. constexpr optional(nullopt_t) noexcept {
  939. }
  940. /// Copy constructor
  941. ///
  942. /// If `rhs` contains a value, the stored value is direct-initialized with
  943. /// it. Otherwise, the constructed optional is empty.
  944. SOL_TL_OPTIONAL_11_CONSTEXPR optional(const optional& rhs) = default;
  945. /// Move constructor
  946. ///
  947. /// If `rhs` contains a value, the stored value is direct-initialized with
  948. /// it. Otherwise, the constructed optional is empty.
  949. SOL_TL_OPTIONAL_11_CONSTEXPR optional(optional&& rhs) = default;
  950. /// Constructs the stored value in-place using the given arguments.
  951. /// \group in_place
  952. /// \synopsis template <class... Args> constexpr explicit optional(in_place_t, Args&&... args);
  953. template <class... Args>
  954. constexpr explicit optional(detail::enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>, Args&&... args)
  955. : base(in_place, std::forward<Args>(args)...) {
  956. }
  957. /// \group in_place
  958. /// \synopsis template <class U, class... Args> \n constexpr explicit optional(in_place_t, std::initializer_list<U>&, Args&&... args);
  959. template <class U, class... Args>
  960. SOL_TL_OPTIONAL_11_CONSTEXPR explicit optional(detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, in_place_t>,
  961. std::initializer_list<U> il, Args&&... args) {
  962. this->construct(il, std::forward<Args>(args)...);
  963. }
  964. #if 0 // SOL_MODIFICATION
  965. /// Constructs the stored value with `u`.
  966. /// \synopsis template <class U=T> constexpr optional(U &&u);
  967. template <class U = T, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
  968. constexpr optional(U&& u) : base(in_place, std::forward<U>(u)) {
  969. }
  970. /// \exclude
  971. template <class U = T, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
  972. constexpr explicit optional(U&& u) : base(in_place, std::forward<U>(u)) {
  973. }
  974. #else
  975. /// Constructs the stored value with `u`.
  976. /// \synopsis template <class U=T> constexpr optional(U &&u);
  977. constexpr optional(T&& u) : base(in_place, std::move(u)) {
  978. }
  979. /// \exclude
  980. constexpr optional(const T& u) : base(in_place, u) {
  981. }
  982. #endif // sol2 modification
  983. /// Converting copy constructor.
  984. /// \synopsis template <class U> optional(const optional<U> &rhs);
  985. template <class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<std::is_convertible<const U&, T>::value>* = nullptr>
  986. optional(const optional<U>& rhs) {
  987. if (rhs.has_value()) {
  988. this->construct(*rhs);
  989. }
  990. }
  991. /// \exclude
  992. template <class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<!std::is_convertible<const U&, T>::value>* = nullptr>
  993. explicit optional(const optional<U>& rhs) {
  994. if (rhs.has_value()) {
  995. this->construct(*rhs);
  996. }
  997. }
  998. /// Converting move constructor.
  999. /// \synopsis template <class U> optional(optional<U> &&rhs);
  1000. template <class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr>
  1001. optional(optional<U>&& rhs) {
  1002. if (rhs.has_value()) {
  1003. this->construct(std::move(*rhs));
  1004. }
  1005. }
  1006. /// \exclude
  1007. template <class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr>
  1008. explicit optional(optional<U>&& rhs) {
  1009. this->construct(std::move(*rhs));
  1010. }
  1011. /// Destroys the stored value if there is one.
  1012. ~optional() = default;
  1013. /// Assignment to empty.
  1014. ///
  1015. /// Destroys the current value if there is one.
  1016. optional& operator=(nullopt_t) noexcept {
  1017. if (has_value()) {
  1018. this->m_value.~T();
  1019. this->m_has_value = false;
  1020. }
  1021. return *this;
  1022. }
  1023. /// Copy assignment.
  1024. ///
  1025. /// Copies the value from `rhs` if there is one. Otherwise resets the stored
  1026. /// value in `*this`.
  1027. optional& operator=(const optional& rhs) = default;
  1028. /// Move assignment.
  1029. ///
  1030. /// Moves the value from `rhs` if there is one. Otherwise resets the stored
  1031. /// value in `*this`.
  1032. optional& operator=(optional&& rhs) = default;
  1033. /// Assigns the stored value from `u`, destroying the old value if there was
  1034. /// one.
  1035. /// \synopsis optional &operator=(U &&u);
  1036. template <class U = T, detail::enable_assign_forward<T, U>* = nullptr>
  1037. optional& operator=(U&& u) {
  1038. if (has_value()) {
  1039. this->m_value = std::forward<U>(u);
  1040. }
  1041. else {
  1042. this->construct(std::forward<U>(u));
  1043. }
  1044. return *this;
  1045. }
  1046. /// Converting copy assignment operator.
  1047. ///
  1048. /// Copies the value from `rhs` if there is one. Otherwise resets the stored
  1049. /// value in `*this`.
  1050. /// \synopsis optional &operator=(const optional<U> & rhs);
  1051. template <class U, detail::enable_assign_from_other<T, U, const U&>* = nullptr>
  1052. optional& operator=(const optional<U>& rhs) {
  1053. if (has_value()) {
  1054. if (rhs.has_value()) {
  1055. this->m_value = *rhs;
  1056. }
  1057. else {
  1058. this->hard_reset();
  1059. }
  1060. }
  1061. if (rhs.has_value()) {
  1062. this->construct(*rhs);
  1063. }
  1064. return *this;
  1065. }
  1066. // TODO check exception guarantee
  1067. /// Converting move assignment operator.
  1068. ///
  1069. /// Moves the value from `rhs` if there is one. Otherwise resets the stored
  1070. /// value in `*this`.
  1071. /// \synopsis optional &operator=(optional<U> && rhs);
  1072. template <class U, detail::enable_assign_from_other<T, U, U>* = nullptr>
  1073. optional& operator=(optional<U>&& rhs) {
  1074. if (has_value()) {
  1075. if (rhs.has_value()) {
  1076. this->m_value = std::move(*rhs);
  1077. }
  1078. else {
  1079. this->hard_reset();
  1080. }
  1081. }
  1082. if (rhs.has_value()) {
  1083. this->construct(std::move(*rhs));
  1084. }
  1085. return *this;
  1086. }
  1087. /// Constructs the value in-place, destroying the current one if there is
  1088. /// one.
  1089. /// \group emplace
  1090. template <class... Args>
  1091. T& emplace(Args&&... args) {
  1092. static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");
  1093. *this = nullopt;
  1094. this->construct(std::forward<Args>(args)...);
  1095. return value();
  1096. }
  1097. /// \group emplace
  1098. /// \synopsis template <class U, class... Args> \n T& emplace(std::initializer_list<U> il, Args &&... args);
  1099. template <class U, class... Args>
  1100. detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, T&> emplace(std::initializer_list<U> il, Args&&... args) {
  1101. *this = nullopt;
  1102. this->construct(il, std::forward<Args>(args)...);
  1103. return value();
  1104. }
  1105. /// Swaps this optional with the other.
  1106. ///
  1107. /// If neither optionals have a value, nothing happens.
  1108. /// If both have a value, the values are swapped.
  1109. /// If one has a value, it is moved to the other and the movee is left
  1110. /// valueless.
  1111. void swap(optional& rhs) noexcept(std::is_nothrow_move_constructible<T>::value&& detail::is_nothrow_swappable<T>::value) {
  1112. if (has_value()) {
  1113. if (rhs.has_value()) {
  1114. using std::swap;
  1115. swap(**this, *rhs);
  1116. }
  1117. else {
  1118. new (std::addressof(rhs.m_value)) T(std::move(this->m_value));
  1119. this->m_value.T::~T();
  1120. }
  1121. }
  1122. else if (rhs.has_value()) {
  1123. new (std::addressof(this->m_value)) T(std::move(rhs.m_value));
  1124. rhs.m_value.T::~T();
  1125. }
  1126. }
  1127. /// \returns a pointer to the stored value
  1128. /// \requires a value is stored
  1129. /// \group pointer
  1130. /// \synopsis constexpr const T *operator->() const;
  1131. constexpr const T* operator->() const {
  1132. return std::addressof(this->m_value);
  1133. }
  1134. /// \group pointer
  1135. /// \synopsis constexpr T *operator->();
  1136. SOL_TL_OPTIONAL_11_CONSTEXPR T* operator->() {
  1137. return std::addressof(this->m_value);
  1138. }
  1139. /// \returns the stored value
  1140. /// \requires a value is stored
  1141. /// \group deref
  1142. /// \synopsis constexpr T &operator*();
  1143. SOL_TL_OPTIONAL_11_CONSTEXPR T& operator*() & {
  1144. return this->m_value;
  1145. }
  1146. /// \group deref
  1147. /// \synopsis constexpr const T &operator*() const;
  1148. constexpr const T& operator*() const& {
  1149. return this->m_value;
  1150. }
  1151. /// \exclude
  1152. SOL_TL_OPTIONAL_11_CONSTEXPR T&& operator*() && {
  1153. return std::move(this->m_value);
  1154. }
  1155. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1156. /// \exclude
  1157. constexpr const T&& operator*() const&& {
  1158. return std::move(this->m_value);
  1159. }
  1160. #endif
  1161. /// \returns whether or not the optional has a value
  1162. /// \group has_value
  1163. constexpr bool has_value() const noexcept {
  1164. return this->m_has_value;
  1165. }
  1166. /// \group has_value
  1167. constexpr explicit operator bool() const noexcept {
  1168. return this->m_has_value;
  1169. }
  1170. /// \returns the contained value if there is one, otherwise throws
  1171. /// [bad_optional_access]
  1172. /// \group value
  1173. /// \synopsis constexpr T &value();
  1174. SOL_TL_OPTIONAL_11_CONSTEXPR T& value() & {
  1175. if (has_value())
  1176. return this->m_value;
  1177. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1178. std::abort();
  1179. #else
  1180. throw bad_optional_access();
  1181. #endif // No exceptions allowed
  1182. }
  1183. /// \group value
  1184. /// \synopsis constexpr const T &value() const;
  1185. SOL_TL_OPTIONAL_11_CONSTEXPR const T& value() const& {
  1186. if (has_value())
  1187. return this->m_value;
  1188. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1189. std::abort();
  1190. #else
  1191. throw bad_optional_access();
  1192. #endif // No exceptions allowed
  1193. }
  1194. /// \exclude
  1195. SOL_TL_OPTIONAL_11_CONSTEXPR T&& value() && {
  1196. if (has_value())
  1197. return std::move(this->m_value);
  1198. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1199. std::abort();
  1200. #else
  1201. throw bad_optional_access();
  1202. #endif // No exceptions allowed
  1203. }
  1204. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1205. /// \exclude
  1206. SOL_TL_OPTIONAL_11_CONSTEXPR const T&& value() const&& {
  1207. if (has_value())
  1208. return std::move(this->m_value);
  1209. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1210. std::abort();
  1211. #else
  1212. throw bad_optional_access();
  1213. #endif // No exceptions allowed
  1214. }
  1215. #endif
  1216. /// \returns the stored value if there is one, otherwise returns `u`
  1217. /// \group value_or
  1218. template <class U>
  1219. constexpr T value_or(U&& u) const& {
  1220. static_assert(std::is_copy_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be copy constructible and convertible from U");
  1221. return has_value() ? **this : static_cast<T>(std::forward<U>(u));
  1222. }
  1223. /// \group value_or
  1224. template <class U>
  1225. SOL_TL_OPTIONAL_11_CONSTEXPR T value_or(U&& u) && {
  1226. static_assert(std::is_move_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be move constructible and convertible from U");
  1227. return has_value() ? **this : static_cast<T>(std::forward<U>(u));
  1228. }
  1229. /// Destroys the stored value if one exists, making the optional empty
  1230. void reset() noexcept {
  1231. if (has_value()) {
  1232. this->m_value.~T();
  1233. this->m_has_value = false;
  1234. }
  1235. }
  1236. }; // namespace sol
  1237. /// \group relop
  1238. /// \brief Compares two optional objects
  1239. /// \details If both optionals contain a value, they are compared with `T`s
  1240. /// relational operators. Otherwise `lhs` and `rhs` are equal only if they are
  1241. /// both empty, and `lhs` is less than `rhs` only if `rhs` is empty and `lhs`
  1242. /// is not.
  1243. template <class T, class U>
  1244. inline constexpr bool operator==(const optional<T>& lhs, const optional<U>& rhs) {
  1245. return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || *lhs == *rhs);
  1246. }
  1247. /// \group relop
  1248. template <class T, class U>
  1249. inline constexpr bool operator!=(const optional<T>& lhs, const optional<U>& rhs) {
  1250. return lhs.has_value() != rhs.has_value() || (lhs.has_value() && *lhs != *rhs);
  1251. }
  1252. /// \group relop
  1253. template <class T, class U>
  1254. inline constexpr bool operator<(const optional<T>& lhs, const optional<U>& rhs) {
  1255. return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs);
  1256. }
  1257. /// \group relop
  1258. template <class T, class U>
  1259. inline constexpr bool operator>(const optional<T>& lhs, const optional<U>& rhs) {
  1260. return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs);
  1261. }
  1262. /// \group relop
  1263. template <class T, class U>
  1264. inline constexpr bool operator<=(const optional<T>& lhs, const optional<U>& rhs) {
  1265. return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs);
  1266. }
  1267. /// \group relop
  1268. template <class T, class U>
  1269. inline constexpr bool operator>=(const optional<T>& lhs, const optional<U>& rhs) {
  1270. return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs);
  1271. }
  1272. /// \group relop_nullopt
  1273. /// \brief Compares an optional to a `nullopt`
  1274. /// \details Equivalent to comparing the optional to an empty optional
  1275. template <class T>
  1276. inline constexpr bool operator==(const optional<T>& lhs, nullopt_t) noexcept {
  1277. return !lhs.has_value();
  1278. }
  1279. /// \group relop_nullopt
  1280. template <class T>
  1281. inline constexpr bool operator==(nullopt_t, const optional<T>& rhs) noexcept {
  1282. return !rhs.has_value();
  1283. }
  1284. /// \group relop_nullopt
  1285. template <class T>
  1286. inline constexpr bool operator!=(const optional<T>& lhs, nullopt_t) noexcept {
  1287. return lhs.has_value();
  1288. }
  1289. /// \group relop_nullopt
  1290. template <class T>
  1291. inline constexpr bool operator!=(nullopt_t, const optional<T>& rhs) noexcept {
  1292. return rhs.has_value();
  1293. }
  1294. /// \group relop_nullopt
  1295. template <class T>
  1296. inline constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
  1297. return false;
  1298. }
  1299. /// \group relop_nullopt
  1300. template <class T>
  1301. inline constexpr bool operator<(nullopt_t, const optional<T>& rhs) noexcept {
  1302. return rhs.has_value();
  1303. }
  1304. /// \group relop_nullopt
  1305. template <class T>
  1306. inline constexpr bool operator<=(const optional<T>& lhs, nullopt_t) noexcept {
  1307. return !lhs.has_value();
  1308. }
  1309. /// \group relop_nullopt
  1310. template <class T>
  1311. inline constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
  1312. return true;
  1313. }
  1314. /// \group relop_nullopt
  1315. template <class T>
  1316. inline constexpr bool operator>(const optional<T>& lhs, nullopt_t) noexcept {
  1317. return lhs.has_value();
  1318. }
  1319. /// \group relop_nullopt
  1320. template <class T>
  1321. inline constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
  1322. return false;
  1323. }
  1324. /// \group relop_nullopt
  1325. template <class T>
  1326. inline constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
  1327. return true;
  1328. }
  1329. /// \group relop_nullopt
  1330. template <class T>
  1331. inline constexpr bool operator>=(nullopt_t, const optional<T>& rhs) noexcept {
  1332. return !rhs.has_value();
  1333. }
  1334. /// \group relop_t
  1335. /// \brief Compares the optional with a value.
  1336. /// \details If the optional has a value, it is compared with the other value
  1337. /// using `T`s relational operators. Otherwise, the optional is considered
  1338. /// less than the value.
  1339. template <class T, class U>
  1340. inline constexpr bool operator==(const optional<T>& lhs, const U& rhs) {
  1341. return lhs.has_value() ? *lhs == rhs : false;
  1342. }
  1343. /// \group relop_t
  1344. template <class T, class U>
  1345. inline constexpr bool operator==(const U& lhs, const optional<T>& rhs) {
  1346. return rhs.has_value() ? lhs == *rhs : false;
  1347. }
  1348. /// \group relop_t
  1349. template <class T, class U>
  1350. inline constexpr bool operator!=(const optional<T>& lhs, const U& rhs) {
  1351. return lhs.has_value() ? *lhs != rhs : true;
  1352. }
  1353. /// \group relop_t
  1354. template <class T, class U>
  1355. inline constexpr bool operator!=(const U& lhs, const optional<T>& rhs) {
  1356. return rhs.has_value() ? lhs != *rhs : true;
  1357. }
  1358. /// \group relop_t
  1359. template <class T, class U>
  1360. inline constexpr bool operator<(const optional<T>& lhs, const U& rhs) {
  1361. return lhs.has_value() ? *lhs < rhs : true;
  1362. }
  1363. /// \group relop_t
  1364. template <class T, class U>
  1365. inline constexpr bool operator<(const U& lhs, const optional<T>& rhs) {
  1366. return rhs.has_value() ? lhs < *rhs : false;
  1367. }
  1368. /// \group relop_t
  1369. template <class T, class U>
  1370. inline constexpr bool operator<=(const optional<T>& lhs, const U& rhs) {
  1371. return lhs.has_value() ? *lhs <= rhs : true;
  1372. }
  1373. /// \group relop_t
  1374. template <class T, class U>
  1375. inline constexpr bool operator<=(const U& lhs, const optional<T>& rhs) {
  1376. return rhs.has_value() ? lhs <= *rhs : false;
  1377. }
  1378. /// \group relop_t
  1379. template <class T, class U>
  1380. inline constexpr bool operator>(const optional<T>& lhs, const U& rhs) {
  1381. return lhs.has_value() ? *lhs > rhs : false;
  1382. }
  1383. /// \group relop_t
  1384. template <class T, class U>
  1385. inline constexpr bool operator>(const U& lhs, const optional<T>& rhs) {
  1386. return rhs.has_value() ? lhs > *rhs : true;
  1387. }
  1388. /// \group relop_t
  1389. template <class T, class U>
  1390. inline constexpr bool operator>=(const optional<T>& lhs, const U& rhs) {
  1391. return lhs.has_value() ? *lhs >= rhs : false;
  1392. }
  1393. /// \group relop_t
  1394. template <class T, class U>
  1395. inline constexpr bool operator>=(const U& lhs, const optional<T>& rhs) {
  1396. return rhs.has_value() ? lhs >= *rhs : true;
  1397. }
  1398. /// \synopsis template <class T> \n void swap(optional<T> &lhs, optional<T> &rhs);
  1399. template <class T, detail::enable_if_t<std::is_move_constructible<T>::value>* = nullptr, detail::enable_if_t<detail::is_swappable<T>::value>* = nullptr>
  1400. void swap(optional<T>& lhs, optional<T>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
  1401. return lhs.swap(rhs);
  1402. }
  1403. namespace detail {
  1404. struct i_am_secret { };
  1405. } // namespace detail
  1406. template <class T = detail::i_am_secret, class U, class Ret = detail::conditional_t<std::is_same<T, detail::i_am_secret>::value, detail::decay_t<U>, T>>
  1407. inline constexpr optional<Ret> make_optional(U&& v) {
  1408. return optional<Ret>(std::forward<U>(v));
  1409. }
  1410. template <class T, class... Args>
  1411. inline constexpr optional<T> make_optional(Args&&... args) {
  1412. return optional<T>(in_place, std::forward<Args>(args)...);
  1413. }
  1414. template <class T, class U, class... Args>
  1415. inline constexpr optional<T> make_optional(std::initializer_list<U> il, Args&&... args) {
  1416. return optional<T>(in_place, il, std::forward<Args>(args)...);
  1417. }
  1418. #if __cplusplus >= 201703L
  1419. template <class T>
  1420. optional(T) -> optional<T>;
  1421. #endif
  1422. /// \exclude
  1423. namespace detail {
  1424. #ifdef SOL_TL_OPTIONAL_CXX14
  1425. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  1426. detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
  1427. constexpr auto optional_map_impl(Opt&& opt, F&& f) {
  1428. return opt.has_value() ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt)) : optional<Ret>(nullopt);
  1429. }
  1430. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  1431. detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
  1432. auto optional_map_impl(Opt&& opt, F&& f) {
  1433. if (opt.has_value()) {
  1434. detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
  1435. return make_optional(monostate {});
  1436. }
  1437. return optional<monostate>(nullopt);
  1438. }
  1439. #else
  1440. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  1441. detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
  1442. constexpr auto optional_map_impl(Opt&& opt, F&& f) -> optional<Ret> {
  1443. return opt.has_value() ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt)) : optional<Ret>(nullopt);
  1444. }
  1445. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  1446. detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
  1447. auto optional_map_impl(Opt&& opt, F&& f) -> optional<monostate> {
  1448. if (opt.has_value()) {
  1449. detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
  1450. return monostate {};
  1451. }
  1452. return nullopt;
  1453. }
  1454. #endif
  1455. } // namespace detail
  1456. /// Specialization for when `T` is a reference. `optional<T&>` acts similarly
  1457. /// to a `T*`, but provides more operations and shows intent more clearly.
  1458. ///
  1459. /// *Examples*:
  1460. ///
  1461. /// ```
  1462. /// int i = 42;
  1463. /// sol::optional<int&> o = i;
  1464. /// *o == 42; //true
  1465. /// i = 12;
  1466. /// *o = 12; //true
  1467. /// &*o == &i; //true
  1468. /// ```
  1469. ///
  1470. /// Assignment has rebind semantics rather than assign-through semantics:
  1471. ///
  1472. /// ```
  1473. /// int j = 8;
  1474. /// o = j;
  1475. ///
  1476. /// &*o == &j; //true
  1477. /// ```
  1478. template <class T>
  1479. class optional<T&> {
  1480. public:
  1481. // The different versions for C++14 and 11 are needed because deduced return
  1482. // types are not SFINAE-safe. This provides better support for things like
  1483. // generic lambdas. C.f.
  1484. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html
  1485. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  1486. /// \group and_then
  1487. /// Carries out some operation which returns an optional on the stored
  1488. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  1489. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  1490. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  1491. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  1492. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  1493. /// is returned.
  1494. /// \group and_then
  1495. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
  1496. template <class F>
  1497. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) & {
  1498. using result = detail::invoke_result_t<F, T&>;
  1499. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1500. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1501. }
  1502. /// \group and_then
  1503. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
  1504. template <class F>
  1505. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) && {
  1506. using result = detail::invoke_result_t<F, T&>;
  1507. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1508. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1509. }
  1510. /// \group and_then
  1511. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
  1512. template <class F>
  1513. constexpr auto and_then(F&& f) const& {
  1514. using result = detail::invoke_result_t<F, const T&>;
  1515. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1516. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1517. }
  1518. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1519. /// \group and_then
  1520. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
  1521. template <class F>
  1522. constexpr auto and_then(F&& f) const&& {
  1523. using result = detail::invoke_result_t<F, const T&>;
  1524. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1525. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1526. }
  1527. #endif
  1528. #else
  1529. /// \group and_then
  1530. /// Carries out some operation which returns an optional on the stored
  1531. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  1532. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  1533. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  1534. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  1535. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  1536. /// is returned.
  1537. /// \group and_then
  1538. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
  1539. template <class F>
  1540. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) & {
  1541. using result = detail::invoke_result_t<F, T&>;
  1542. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1543. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1544. }
  1545. /// \group and_then
  1546. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
  1547. template <class F>
  1548. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) && {
  1549. using result = detail::invoke_result_t<F, T&>;
  1550. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1551. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1552. }
  1553. /// \group and_then
  1554. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
  1555. template <class F>
  1556. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const& {
  1557. using result = detail::invoke_result_t<F, const T&>;
  1558. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1559. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1560. }
  1561. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1562. /// \group and_then
  1563. /// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
  1564. template <class F>
  1565. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const&& {
  1566. using result = detail::invoke_result_t<F, const T&>;
  1567. static_assert(detail::is_optional<result>::value, "F must return an optional");
  1568. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  1569. }
  1570. #endif
  1571. #endif
  1572. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  1573. /// \brief Carries out some operation on the stored object if there is one.
  1574. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  1575. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  1576. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  1577. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  1578. /// returned.
  1579. ///
  1580. /// \group map
  1581. /// \synopsis template <class F> constexpr auto map(F &&f) &;
  1582. template <class F>
  1583. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) & {
  1584. return detail::optional_map_impl(*this, std::forward<F>(f));
  1585. }
  1586. /// \group map
  1587. /// \synopsis template <class F> constexpr auto map(F &&f) &&;
  1588. template <class F>
  1589. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) && {
  1590. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  1591. }
  1592. /// \group map
  1593. /// \synopsis template <class F> constexpr auto map(F &&f) const&;
  1594. template <class F>
  1595. constexpr auto map(F&& f) const& {
  1596. return detail::optional_map_impl(*this, std::forward<F>(f));
  1597. }
  1598. /// \group map
  1599. /// \synopsis template <class F> constexpr auto map(F &&f) const&&;
  1600. template <class F>
  1601. constexpr auto map(F&& f) const&& {
  1602. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  1603. }
  1604. #else
  1605. /// \brief Carries out some operation on the stored object if there is one.
  1606. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  1607. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  1608. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  1609. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  1610. /// returned.
  1611. ///
  1612. /// \group map
  1613. /// \synopsis template <class F> auto map(F &&f) &;
  1614. template <class F>
  1615. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&>(), std::declval<F&&>())) map(F&& f) & {
  1616. return detail::optional_map_impl(*this, std::forward<F>(f));
  1617. }
  1618. /// \group map
  1619. /// \synopsis template <class F> auto map(F &&f) &&;
  1620. template <class F>
  1621. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&&>(), std::declval<F&&>())) map(F&& f) && {
  1622. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  1623. }
  1624. /// \group map
  1625. /// \synopsis template <class F> auto map(F &&f) const&;
  1626. template <class F>
  1627. constexpr decltype(detail::optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F&& f) const& {
  1628. return detail::optional_map_impl(*this, std::forward<F>(f));
  1629. }
  1630. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1631. /// \group map
  1632. /// \synopsis template <class F> auto map(F &&f) const&&;
  1633. template <class F>
  1634. constexpr decltype(detail::optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F&& f) const&& {
  1635. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  1636. }
  1637. #endif
  1638. #endif
  1639. /// \brief Calls `f` if the optional is empty
  1640. /// \requires `std::invoke_result_t<F>` must be void or convertible to
  1641. /// `optional<T>`. \effects If `*this` has a value, returns `*this`.
  1642. /// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
  1643. /// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
  1644. ///
  1645. /// \group or_else
  1646. /// \synopsis template <class F> optional<T> or_else (F &&f) &;
  1647. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  1648. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  1649. if (has_value())
  1650. return *this;
  1651. std::forward<F>(f)();
  1652. return nullopt;
  1653. }
  1654. /// \exclude
  1655. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  1656. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  1657. return has_value() ? *this : std::forward<F>(f)();
  1658. }
  1659. /// \group or_else
  1660. /// \synopsis template <class F> optional<T> or_else (F &&f) &&;
  1661. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  1662. optional<T> or_else(F&& f) && {
  1663. if (has_value())
  1664. return std::move(*this);
  1665. std::forward<F>(f)();
  1666. return nullopt;
  1667. }
  1668. /// \exclude
  1669. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  1670. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) && {
  1671. return has_value() ? std::move(*this) : std::forward<F>(f)();
  1672. }
  1673. /// \group or_else
  1674. /// \synopsis template <class F> optional<T> or_else (F &&f) const &;
  1675. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  1676. optional<T> or_else(F&& f) const& {
  1677. if (has_value())
  1678. return *this;
  1679. std::forward<F>(f)();
  1680. return nullopt;
  1681. }
  1682. /// \exclude
  1683. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  1684. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) const& {
  1685. return has_value() ? *this : std::forward<F>(f)();
  1686. }
  1687. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1688. /// \exclude
  1689. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  1690. optional<T> or_else(F&& f) const&& {
  1691. if (has_value())
  1692. return std::move(*this);
  1693. std::forward<F>(f)();
  1694. return nullopt;
  1695. }
  1696. /// \exclude
  1697. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  1698. optional<T> or_else(F&& f) const&& {
  1699. return has_value() ? std::move(*this) : std::forward<F>(f)();
  1700. }
  1701. #endif
  1702. /// \brief Maps the stored value with `f` if there is one, otherwise returns
  1703. /// `u`.
  1704. ///
  1705. /// \details If there is a value stored, then `f` is called with `**this`
  1706. /// and the value is returned. Otherwise `u` is returned.
  1707. ///
  1708. /// \group map_or
  1709. template <class F, class U>
  1710. U map_or(F&& f, U&& u) & {
  1711. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  1712. }
  1713. /// \group map_or
  1714. template <class F, class U>
  1715. U map_or(F&& f, U&& u) && {
  1716. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  1717. }
  1718. /// \group map_or
  1719. template <class F, class U>
  1720. U map_or(F&& f, U&& u) const& {
  1721. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  1722. }
  1723. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1724. /// \group map_or
  1725. template <class F, class U>
  1726. U map_or(F&& f, U&& u) const&& {
  1727. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  1728. }
  1729. #endif
  1730. /// \brief Maps the stored value with `f` if there is one, otherwise calls
  1731. /// `u` and returns the result.
  1732. ///
  1733. /// \details If there is a value stored, then `f` is
  1734. /// called with `**this` and the value is returned. Otherwise
  1735. /// `std::forward<U>(u)()` is returned.
  1736. ///
  1737. /// \group map_or_else
  1738. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u) &;
  1739. template <class F, class U>
  1740. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) & {
  1741. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  1742. }
  1743. /// \group map_or_else
  1744. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  1745. /// &&;
  1746. template <class F, class U>
  1747. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) && {
  1748. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  1749. }
  1750. /// \group map_or_else
  1751. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  1752. /// const &;
  1753. template <class F, class U>
  1754. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const& {
  1755. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  1756. }
  1757. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1758. /// \group map_or_else
  1759. /// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
  1760. /// const &&;
  1761. template <class F, class U>
  1762. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const&& {
  1763. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  1764. }
  1765. #endif
  1766. /// \returns `u` if `*this` has a value, otherwise an empty optional.
  1767. template <class U>
  1768. constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const {
  1769. using result = optional<detail::decay_t<U>>;
  1770. return has_value() ? result { u } : result { nullopt };
  1771. }
  1772. /// \returns `rhs` if `*this` is empty, otherwise the current value.
  1773. /// \group disjunction
  1774. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) & {
  1775. return has_value() ? *this : rhs;
  1776. }
  1777. /// \group disjunction
  1778. constexpr optional disjunction(const optional& rhs) const& {
  1779. return has_value() ? *this : rhs;
  1780. }
  1781. /// \group disjunction
  1782. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) && {
  1783. return has_value() ? std::move(*this) : rhs;
  1784. }
  1785. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1786. /// \group disjunction
  1787. constexpr optional disjunction(const optional& rhs) const&& {
  1788. return has_value() ? std::move(*this) : rhs;
  1789. }
  1790. #endif
  1791. /// \group disjunction
  1792. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) & {
  1793. return has_value() ? *this : std::move(rhs);
  1794. }
  1795. /// \group disjunction
  1796. constexpr optional disjunction(optional&& rhs) const& {
  1797. return has_value() ? *this : std::move(rhs);
  1798. }
  1799. /// \group disjunction
  1800. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) && {
  1801. return has_value() ? std::move(*this) : std::move(rhs);
  1802. }
  1803. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1804. /// \group disjunction
  1805. constexpr optional disjunction(optional&& rhs) const&& {
  1806. return has_value() ? std::move(*this) : std::move(rhs);
  1807. }
  1808. #endif
  1809. /// Takes the value out of the optional, leaving it empty
  1810. /// \group take
  1811. optional take() & {
  1812. optional ret = *this;
  1813. reset();
  1814. return ret;
  1815. }
  1816. /// \group take
  1817. optional take() const& {
  1818. optional ret = *this;
  1819. reset();
  1820. return ret;
  1821. }
  1822. /// \group take
  1823. optional take() && {
  1824. optional ret = std::move(*this);
  1825. reset();
  1826. return ret;
  1827. }
  1828. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  1829. /// \group take
  1830. optional take() const&& {
  1831. optional ret = std::move(*this);
  1832. reset();
  1833. return ret;
  1834. }
  1835. #endif
  1836. using value_type = T&;
  1837. /// Constructs an optional that does not contain a value.
  1838. /// \group ctor_empty
  1839. constexpr optional() noexcept : m_value(nullptr) {
  1840. }
  1841. /// \group ctor_empty
  1842. constexpr optional(nullopt_t) noexcept : m_value(nullptr) {
  1843. }
  1844. /// Copy constructor
  1845. ///
  1846. /// If `rhs` contains a value, the stored value is direct-initialized with
  1847. /// it. Otherwise, the constructed optional is empty.
  1848. SOL_TL_OPTIONAL_11_CONSTEXPR optional(const optional& rhs) noexcept = default;
  1849. /// Move constructor
  1850. ///
  1851. /// If `rhs` contains a value, the stored value is direct-initialized with
  1852. /// it. Otherwise, the constructed optional is empty.
  1853. SOL_TL_OPTIONAL_11_CONSTEXPR optional(optional&& rhs) = default;
  1854. /// Constructs the stored value with `u`.
  1855. /// \synopsis template <class U=T> constexpr optional(U &&u);
  1856. template <class U = T, detail::enable_if_t<!detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
  1857. constexpr optional(U&& u) : m_value(std::addressof(u)) {
  1858. static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
  1859. }
  1860. /// \exclude
  1861. template <class U>
  1862. constexpr explicit optional(const optional<U>& rhs) : optional(*rhs) {
  1863. }
  1864. /// No-op
  1865. ~optional() = default;
  1866. /// Assignment to empty.
  1867. ///
  1868. /// Destroys the current value if there is one.
  1869. optional& operator=(nullopt_t) noexcept {
  1870. m_value = nullptr;
  1871. return *this;
  1872. }
  1873. /// Copy assignment.
  1874. ///
  1875. /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise
  1876. /// resets the stored value in `*this`.
  1877. optional& operator=(const optional& rhs) = default;
  1878. /// Rebinds this optional to `u`.
  1879. ///
  1880. /// \requires `U` must be an lvalue reference.
  1881. /// \synopsis optional &operator=(U &&u);
  1882. template <class U = T, detail::enable_if_t<!detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
  1883. optional& operator=(U&& u) {
  1884. static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
  1885. m_value = std::addressof(u);
  1886. return *this;
  1887. }
  1888. /// Converting copy assignment operator.
  1889. ///
  1890. /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise
  1891. /// resets the stored value in `*this`.
  1892. template <class U>
  1893. optional& operator=(const optional<U>& rhs) {
  1894. m_value = std::addressof(rhs.value());
  1895. return *this;
  1896. }
  1897. /// Constructs the value in-place, destroying the current one if there is
  1898. /// one.
  1899. ///
  1900. /// \group emplace
  1901. template <class... Args>
  1902. T& emplace(Args&&... args) noexcept {
  1903. static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");
  1904. *this = nullopt;
  1905. this->construct(std::forward<Args>(args)...);
  1906. }
  1907. /// Swaps this optional with the other.
  1908. ///
  1909. /// If neither optionals have a value, nothing happens.
  1910. /// If both have a value, the values are swapped.
  1911. /// If one has a value, it is moved to the other and the movee is left
  1912. /// valueless.
  1913. void swap(optional& rhs) noexcept {
  1914. std::swap(m_value, rhs.m_value);
  1915. }
  1916. /// \returns a pointer to the stored value
  1917. /// \requires a value is stored
  1918. /// \group pointer
  1919. /// \synopsis constexpr const T *operator->() const;
  1920. constexpr const T* operator->() const {
  1921. return m_value;
  1922. }
  1923. /// \group pointer
  1924. /// \synopsis constexpr T *operator->();
  1925. SOL_TL_OPTIONAL_11_CONSTEXPR T* operator->() {
  1926. return m_value;
  1927. }
  1928. /// \returns the stored value
  1929. /// \requires a value is stored
  1930. /// \group deref
  1931. /// \synopsis constexpr T &operator*();
  1932. SOL_TL_OPTIONAL_11_CONSTEXPR T& operator*() {
  1933. return *m_value;
  1934. }
  1935. /// \group deref
  1936. /// \synopsis constexpr const T &operator*() const;
  1937. constexpr const T& operator*() const {
  1938. return *m_value;
  1939. }
  1940. /// \returns whether or not the optional has a value
  1941. /// \group has_value
  1942. constexpr bool has_value() const noexcept {
  1943. return m_value != nullptr;
  1944. }
  1945. /// \group has_value
  1946. constexpr explicit operator bool() const noexcept {
  1947. return m_value != nullptr;
  1948. }
  1949. /// \returns the contained value if there is one, otherwise throws
  1950. /// [bad_optional_access]
  1951. /// \group value
  1952. /// synopsis constexpr T &value();
  1953. SOL_TL_OPTIONAL_11_CONSTEXPR T& value() {
  1954. if (has_value())
  1955. return *m_value;
  1956. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1957. std::abort();
  1958. #else
  1959. throw bad_optional_access();
  1960. #endif // No exceptions allowed
  1961. }
  1962. /// \group value
  1963. /// \synopsis constexpr const T &value() const;
  1964. SOL_TL_OPTIONAL_11_CONSTEXPR const T& value() const {
  1965. if (has_value())
  1966. return *m_value;
  1967. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  1968. std::abort();
  1969. #else
  1970. throw bad_optional_access();
  1971. #endif // No exceptions allowed
  1972. }
  1973. /// \returns the stored value if there is one, otherwise returns `u`
  1974. /// \group value_or
  1975. template <class U>
  1976. constexpr T& value_or(U&& u) const {
  1977. static_assert(std::is_convertible<U&&, T&>::value, "T must be convertible from U");
  1978. return has_value() ? const_cast<T&>(**this) : static_cast<T&>(std::forward<U>(u));
  1979. }
  1980. /// Destroys the stored value if one exists, making the optional empty
  1981. void reset() noexcept {
  1982. m_value = nullptr;
  1983. }
  1984. private:
  1985. T* m_value;
  1986. };
  1987. } // namespace sol
  1988. namespace std {
  1989. // TODO SFINAE
  1990. template <class T>
  1991. struct hash<::sol::optional<T>> {
  1992. ::std::size_t operator()(const ::sol::optional<T>& o) const {
  1993. if (!o.has_value())
  1994. return 0;
  1995. return ::std::hash<::sol::detail::remove_const_t<T>>()(*o);
  1996. }
  1997. };
  1998. } // namespace std
  1999. #endif // SOL_TL_OPTIONAL_HPP