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.

178 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. #define JUCE_OPTIONAL_OPERATORS X(==) X(!=) X(<) X(<=) X(>) X(>=)
  25. /**
  26. A simple optional type.
  27. In new code, you should probably prefer using std::optional directly.
  28. This is intended to stand-in for std::optional while JUCE's minimum
  29. supported language standard is lower than C++17. When the minimum language
  30. standard moves to C++17, this class will probably be deprecated, in much
  31. the same way that juce::ScopedPointer was deprecated in favour of
  32. std::unique_ptr after C++11.
  33. This isn't really intended to be used by JUCE clients. Instead, it's to be
  34. used internally in JUCE code, with an API close-enough to std::optional
  35. that the types can be swapped with fairly minor disruption at some point in
  36. the future, but *without breaking any public APIs*.
  37. @tags{Core}
  38. */
  39. template <typename Value>
  40. class Optional
  41. {
  42. template <typename> struct IsOptional : std::false_type {};
  43. template <typename T> struct IsOptional<Optional<T>> : std::true_type {};
  44. public:
  45. Optional() = default;
  46. Optional (const Optional&) = default;
  47. Optional (Optional&&) = default;
  48. Optional& operator= (const Optional&) = default;
  49. Optional& operator= (Optional&&) = default;
  50. Optional (Nullopt) noexcept {}
  51. template <typename Head, typename... Tail, std::enable_if_t<! IsOptional<std::decay_t<Head>>::value, int> = 0>
  52. Optional (Head&& head, Tail&&... tail)
  53. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, Head, Tail...>)
  54. : opt (std::forward<Head> (head), std::forward<Tail> (tail)...) {}
  55. template <typename Other>
  56. Optional (const Optional<Other>& other)
  57. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, const std::optional<Other>&>)
  58. : opt (other.opt) {}
  59. template <typename Other>
  60. Optional (Optional<Other>&& other)
  61. noexcept (std::is_nothrow_constructible_v<std::optional<Value>, std::optional<Other>&&>)
  62. : opt (std::move (other.opt)) {}
  63. template <typename Other, std::enable_if_t<! IsOptional<std::decay_t<Other>>::value, int> = 0>
  64. Optional& operator= (Other&& other)
  65. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, Other>)
  66. {
  67. opt = std::forward<Other> (other);
  68. return *this;
  69. }
  70. template <typename Other>
  71. Optional& operator= (const Optional<Other>& other)
  72. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, const std::optional<Other>&>)
  73. {
  74. opt = other.opt;
  75. return *this;
  76. }
  77. template <typename Other>
  78. Optional& operator= (Optional<Other>&& other)
  79. noexcept (std::is_nothrow_assignable_v<std::optional<Value>, std::optional<Other>&&>)
  80. {
  81. opt = std::move (other.opt);
  82. return *this;
  83. }
  84. template <typename... Other>
  85. auto& emplace (Other&&... other)
  86. {
  87. return opt.emplace (std::forward<Other> (other)...);
  88. }
  89. void reset() noexcept
  90. {
  91. opt.reset();
  92. }
  93. void swap (Optional& other)
  94. noexcept (std::is_nothrow_swappable_v<std::optional<Value>>)
  95. {
  96. opt.swap (other.opt);
  97. }
  98. decltype (auto) operator->() { return opt.operator->(); }
  99. decltype (auto) operator->() const { return opt.operator->(); }
  100. decltype (auto) operator* () { return opt.operator* (); }
  101. decltype (auto) operator* () const { return opt.operator* (); }
  102. explicit operator bool() const noexcept { return opt.has_value(); }
  103. bool hasValue() const noexcept { return opt.has_value(); }
  104. template <typename U>
  105. decltype (auto) orFallback (U&& fallback) const& { return opt.value_or (std::forward<U> (fallback)); }
  106. template <typename U>
  107. decltype (auto) orFallback (U&& fallback) & { return opt.value_or (std::forward<U> (fallback)); }
  108. #define X(op) \
  109. template <typename T, typename U> friend bool operator op (const Optional<T>&, const Optional<U>&); \
  110. template <typename T> friend bool operator op (const Optional<T>&, Nullopt); \
  111. template <typename T> friend bool operator op (Nullopt, const Optional<T>&); \
  112. template <typename T, typename U> friend bool operator op (const Optional<T>&, const U&); \
  113. template <typename T, typename U> friend bool operator op (const T&, const Optional<U>&);
  114. JUCE_OPTIONAL_OPERATORS
  115. #undef X
  116. private:
  117. template <typename Other>
  118. friend class Optional;
  119. std::optional<Value> opt;
  120. };
  121. JUCE_END_IGNORE_WARNINGS_MSVC
  122. template <typename Value>
  123. Optional<std::decay_t<Value>> makeOptional (Value&& v)
  124. {
  125. return std::forward<Value> (v);
  126. }
  127. #define X(op) \
  128. template <typename T, typename U> bool operator op (const Optional<T>& lhs, const Optional<U>& rhs) { return lhs.opt op rhs.opt; } \
  129. template <typename T> bool operator op (const Optional<T>& lhs, Nullopt rhs) { return lhs.opt op rhs; } \
  130. template <typename T> bool operator op (Nullopt lhs, const Optional<T>& rhs) { return lhs op rhs.opt; } \
  131. template <typename T, typename U> bool operator op (const Optional<T>& lhs, const U& rhs) { return lhs.opt op rhs; } \
  132. template <typename T, typename U> bool operator op (const T& lhs, const Optional<U>& rhs) { return lhs op rhs.opt; }
  133. JUCE_OPTIONAL_OPERATORS
  134. #undef X
  135. #undef JUCE_OPTIONAL_OPERATORS
  136. } // namespace juce