The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. using Nullopt = std::nullopt_t;
  20. constexpr auto nullopt = std::nullopt;
  21. // Without this, our tests can emit "unreachable code" warnings during
  22. // link time code generation.
  23. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4702)
  24. #ifndef DOXYGEN
  25. #define JUCE_OPTIONAL_OPERATORS X(==) X(!=) X(<) X(<=) X(>) X(>=)
  26. #endif
  27. /**
  28. A simple optional type.
  29. In new code, you should probably prefer using std::optional directly.
  30. This is intended to stand-in for std::optional while JUCE's minimum
  31. supported language standard is lower than C++17. When the minimum language
  32. standard moves to C++17, this class will probably be deprecated, in much
  33. the same way that juce::ScopedPointer was deprecated in favour of
  34. std::unique_ptr after C++11.
  35. This isn't really intended to be used by JUCE clients. Instead, it's to be
  36. used internally in JUCE code, with an API close-enough to std::optional
  37. that the types can be swapped with fairly minor disruption at some point in
  38. the future, but *without breaking any public APIs*.
  39. @tags{Core}
  40. */
  41. template <typename Value>
  42. class Optional
  43. {
  44. template <typename> struct IsOptional : std::false_type {};
  45. template <typename T> struct IsOptional<Optional<T>> : std::true_type {};
  46. public:
  47. Optional() = default;
  48. Optional (const Optional&) = default;
  49. Optional (Optional&&) = default;
  50. Optional& operator= (const Optional&) = default;
  51. Optional& operator= (Optional&&) = default;
  52. Optional (Nullopt) noexcept {}
  53. template <typename Head, typename... Tail, std::enable_if_t<! IsOptional<std::decay_t<Head>>::value, int> = 0>
  54. Optional (Head&& head, Tail&&... tail)
  55. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, Head, Tail...>)
  56. : opt (std::forward<Head> (head), std::forward<Tail> (tail)...) {}
  57. template <typename Other>
  58. Optional (const Optional<Other>& other)
  59. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, const std::optional<Other>&>)
  60. : opt (other.opt) {}
  61. template <typename Other>
  62. Optional (Optional<Other>&& other)
  63. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, std::optional<Other>&&>)
  64. : opt (std::move (other.opt)) {}
  65. template <typename Other, std::enable_if_t<! IsOptional<std::decay_t<Other>>::value, int> = 0>
  66. Optional& operator= (Other&& other)
  67. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, Other>)
  68. {
  69. opt = std::forward<Other> (other);
  70. return *this;
  71. }
  72. template <typename Other>
  73. Optional& operator= (const Optional<Other>& other)
  74. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, const std::optional<Other>&>)
  75. {
  76. opt = other.opt;
  77. return *this;
  78. }
  79. template <typename Other>
  80. Optional& operator= (Optional<Other>&& other)
  81. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, std::optional<Other>&&>)
  82. {
  83. opt = std::move (other.opt);
  84. return *this;
  85. }
  86. template <typename... Other>
  87. auto& emplace (Other&&... other)
  88. {
  89. return opt.emplace (std::forward<Other> (other)...);
  90. }
  91. void reset() noexcept
  92. {
  93. opt.reset();
  94. }
  95. void swap (Optional& other)
  96. noexcept (std::is_nothrow_swappable_v<std::optional<Value>>)
  97. {
  98. opt.swap (other.opt);
  99. }
  100. decltype (auto) operator->() { return opt.operator->(); }
  101. decltype (auto) operator->() const { return opt.operator->(); }
  102. decltype (auto) operator* () { return opt.operator* (); }
  103. decltype (auto) operator* () const { return opt.operator* (); }
  104. explicit operator bool() const noexcept { return opt.has_value(); }
  105. bool hasValue() const noexcept { return opt.has_value(); }
  106. template <typename U>
  107. decltype (auto) orFallback (U&& fallback) const& { return opt.value_or (std::forward<U> (fallback)); }
  108. template <typename U>
  109. decltype (auto) orFallback (U&& fallback) & { return opt.value_or (std::forward<U> (fallback)); }
  110. #define X(op) \
  111. template <typename T, typename U> friend bool operator op (const Optional<T>&, const Optional<U>&); \
  112. template <typename T> friend bool operator op (const Optional<T>&, Nullopt); \
  113. template <typename T> friend bool operator op (Nullopt, const Optional<T>&); \
  114. template <typename T, typename U> friend bool operator op (const Optional<T>&, const U&); \
  115. template <typename T, typename U> friend bool operator op (const T&, const Optional<U>&);
  116. JUCE_OPTIONAL_OPERATORS
  117. #undef X
  118. private:
  119. template <typename Other>
  120. friend class Optional;
  121. std::optional<Value> opt;
  122. };
  123. JUCE_END_IGNORE_WARNINGS_MSVC
  124. template <typename Value>
  125. Optional<std::decay_t<Value>> makeOptional (Value&& v)
  126. {
  127. return std::forward<Value> (v);
  128. }
  129. #ifndef DOXYGEN
  130. #define X(op) \
  131. template <typename T, typename U> bool operator op (const Optional<T>& lhs, const Optional<U>& rhs) { return lhs.opt op rhs.opt; } \
  132. template <typename T> bool operator op (const Optional<T>& lhs, Nullopt rhs) { return lhs.opt op rhs; } \
  133. template <typename T> bool operator op (Nullopt lhs, const Optional<T>& rhs) { return lhs op rhs.opt; } \
  134. template <typename T, typename U> bool operator op (const Optional<T>& lhs, const U& rhs) { return lhs.opt op rhs; } \
  135. template <typename T, typename U> bool operator op (const T& lhs, const Optional<U>& rhs) { return lhs op rhs.opt; }
  136. JUCE_OPTIONAL_OPERATORS
  137. #undef X
  138. #undef JUCE_OPTIONAL_OPERATORS
  139. #endif
  140. } // namespace juce