optional.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_OPTIONAL_HPP
  19. #define SOL_OPTIONAL_HPP
  20. #include <sol/forward.hpp>
  21. #include <sol/in_place.hpp>
  22. #include <sol/traits.hpp>
  23. #if SOL_IS_ON(SOL_USE_BOOST)
  24. #include <boost/optional.hpp>
  25. #else
  26. #include <sol/optional_implementation.hpp>
  27. #endif // Boost vs. Better optional
  28. #include <optional>
  29. namespace sol {
  30. #if SOL_IS_ON(SOL_USE_BOOST)
  31. template <typename T>
  32. using optional = boost::optional<T>;
  33. using nullopt_t = boost::none_t;
  34. SOL_BOOST_NONE_CONSTEXPR_I_ nullopt_t nullopt = boost::none;
  35. #endif // Boost vs. Better optional
  36. namespace meta {
  37. template <typename T>
  38. using is_optional = any<is_specialization_of<T, optional>, is_specialization_of<T, std::optional>>;
  39. template <typename T>
  40. constexpr inline bool is_optional_v = is_optional<T>::value;
  41. } // namespace meta
  42. namespace detail {
  43. template <typename T>
  44. struct associated_nullopt {
  45. inline static constexpr std::nullopt_t value = std::nullopt;
  46. };
  47. #if SOL_IS_ON(SOL_USE_BOOST)
  48. template <typename T>
  49. struct associated_nullopt<boost::optional<T>> {
  50. inline static SOL_BOOST_NONE_CONSTEXPR_I_ boost::none_t value = boost::none;
  51. };
  52. #endif // Boost nullopt
  53. #if SOL_IS_ON(SOL_USE_BOOST)
  54. template <typename T>
  55. inline SOL_BOOST_NONE_CONSTEXPR_I_ auto associated_nullopt_v = associated_nullopt<T>::value;
  56. #else
  57. template <typename T>
  58. inline constexpr auto associated_nullopt_v = associated_nullopt<T>::value;
  59. #endif // Boost continues to lag behind, to not many people's surprise...
  60. } // namespace detail
  61. } // namespace sol
  62. #if SOL_IS_ON(SOL_USE_BOOST)
  63. #undef SOL_BOOST_NONE_CONSTEXPR_I_
  64. #endif
  65. #endif // SOL_OPTIONAL_HPP