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.

231 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. namespace detail
  18. {
  19. template <typename Ret, typename... Args>
  20. struct Vtable
  21. {
  22. using Storage = void*;
  23. using Move = void (*) (Storage, Storage);
  24. using Call = Ret (*) (Storage, Args...);
  25. using Clear = void (*) (Storage);
  26. constexpr Vtable (Move moveIn, Call callIn, Clear clearIn) noexcept
  27. : move (moveIn), call (callIn), clear (clearIn) {}
  28. Move move = nullptr;
  29. Call call = nullptr;
  30. Clear clear = nullptr;
  31. };
  32. template <typename Fn>
  33. void move (void* from, void* to)
  34. {
  35. new (to) Fn (std::move (*reinterpret_cast<Fn*> (from)));
  36. }
  37. template <typename Fn, typename Ret, typename... Args>
  38. typename std::enable_if<std::is_same<Ret, void>::value, Ret>::type call (void* s, Args... args)
  39. {
  40. (*reinterpret_cast<Fn*> (s)) (args...);
  41. }
  42. template <typename Fn, typename Ret, typename... Args>
  43. typename std::enable_if<! std::is_same<Ret, void>::value, Ret>::type call (void* s, Args... args)
  44. {
  45. return (*reinterpret_cast<Fn*> (s)) (std::forward<Args> (args)...);
  46. }
  47. template <typename Fn>
  48. void clear (void* s)
  49. {
  50. auto& fn = *reinterpret_cast<Fn*> (s);
  51. fn.~Fn();
  52. // I know this looks insane, for some reason MSVC 14 sometimes thinks fn is unreferenced
  53. juce::ignoreUnused (fn);
  54. }
  55. template <typename Fn, typename Ret, typename... Args>
  56. constexpr Vtable<Ret, Args...> makeVtable()
  57. {
  58. return { move <Fn>, call <Fn, Ret, Args...>, clear<Fn> };
  59. }
  60. } // namespace detail
  61. template <size_t len, typename T>
  62. class FixedSizeFunction;
  63. /**
  64. A type similar to `std::function` that holds a callable object.
  65. Unlike `std::function`, the callable object will always be stored in
  66. a buffer of size `len` that is internal to the FixedSizeFunction instance.
  67. This in turn means that creating a FixedSizeFunction instance will never allocate,
  68. making FixedSizeFunctions suitable for use in realtime contexts.
  69. */
  70. template <size_t len, typename Ret, typename... Args>
  71. class FixedSizeFunction<len, Ret (Args...)>
  72. {
  73. private:
  74. using Storage = typename std::aligned_storage<len>::type;
  75. template <typename Item>
  76. using Decay = typename std::decay<Item>::type;
  77. template <typename Item, typename Fn = Decay<Item>>
  78. using IntIfValidConversion = typename std::enable_if<sizeof (Fn) <= len
  79. && alignof (Fn) <= alignof (Storage)
  80. && ! std::is_same<FixedSizeFunction, Fn>::value,
  81. int>::type;
  82. public:
  83. /** Create an empty function. */
  84. FixedSizeFunction() noexcept = default;
  85. /** Create an empty function. */
  86. FixedSizeFunction (std::nullptr_t) noexcept
  87. : FixedSizeFunction() {}
  88. FixedSizeFunction (const FixedSizeFunction&) = delete;
  89. /** Forwards the passed Callable into the internal storage buffer. */
  90. template <typename Callable,
  91. typename Fn = Decay<Callable>,
  92. IntIfValidConversion<Callable> = 0>
  93. FixedSizeFunction (Callable&& callable)
  94. {
  95. static_assert (sizeof (Fn) <= len,
  96. "The requested function cannot fit in this FixedSizeFunction");
  97. static_assert (alignof (Fn) <= alignof (Storage),
  98. "FixedSizeFunction cannot accommodate the requested alignment requirements");
  99. static constexpr auto vtableForCallable = detail::makeVtable<Fn, Ret, Args...>();
  100. vtable = &vtableForCallable;
  101. auto* ptr = new (&storage) Fn (std::forward<Callable> (callable));
  102. jassert ((void*) ptr == (void*) &storage);
  103. juce::ignoreUnused (ptr);
  104. }
  105. /** Move constructor. */
  106. FixedSizeFunction (FixedSizeFunction&& other) noexcept
  107. : vtable (other.vtable)
  108. {
  109. move (std::move (other));
  110. }
  111. /** Converting constructor from smaller FixedSizeFunctions. */
  112. template <size_t otherLen, typename std::enable_if<(otherLen < len), int>::type = 0>
  113. FixedSizeFunction (FixedSizeFunction<otherLen, Ret (Args...)>&& other) noexcept
  114. : vtable (other.vtable)
  115. {
  116. move (std::move (other));
  117. }
  118. /** Nulls this instance. */
  119. FixedSizeFunction& operator= (std::nullptr_t) noexcept
  120. {
  121. return *this = FixedSizeFunction();
  122. }
  123. FixedSizeFunction& operator= (const FixedSizeFunction&) = delete;
  124. /** Assigns a new callable to this instance. */
  125. template <typename Callable, IntIfValidConversion<Callable> = 0>
  126. FixedSizeFunction& operator= (Callable&& callable)
  127. {
  128. return *this = FixedSizeFunction (std::forward<Callable> (callable));
  129. }
  130. /** Move assignment from smaller FixedSizeFunctions. */
  131. template <size_t otherLen, typename std::enable_if<(otherLen < len), int>::type = 0>
  132. FixedSizeFunction& operator= (FixedSizeFunction<otherLen, Ret (Args...)>&& other) noexcept
  133. {
  134. return *this = FixedSizeFunction (std::move (other));
  135. }
  136. /** Move assignment operator. */
  137. FixedSizeFunction& operator= (FixedSizeFunction&& other) noexcept
  138. {
  139. clear();
  140. vtable = other.vtable;
  141. move (std::move (other));
  142. return *this;
  143. }
  144. /** Destructor. */
  145. ~FixedSizeFunction() noexcept { clear(); }
  146. /** If this instance is currently storing a callable object, calls that object,
  147. otherwise throws `std::bad_function_call`.
  148. */
  149. Ret operator() (Args... args) const
  150. {
  151. if (vtable != nullptr)
  152. return vtable->call (&storage, std::forward<Args> (args)...);
  153. throw std::bad_function_call();
  154. }
  155. /** Returns true if this instance currently holds a callable. */
  156. explicit operator bool() const noexcept { return vtable != nullptr; }
  157. private:
  158. template <size_t, typename>
  159. friend class FixedSizeFunction;
  160. void clear() noexcept
  161. {
  162. if (vtable != nullptr)
  163. vtable->clear (&storage);
  164. }
  165. template <size_t otherLen, typename T>
  166. void move (FixedSizeFunction<otherLen, T>&& other) noexcept
  167. {
  168. if (vtable != nullptr)
  169. vtable->move (&other.storage, &storage);
  170. }
  171. const detail::Vtable<Ret, Args...>* vtable = nullptr;
  172. mutable Storage storage;
  173. };
  174. template <size_t len, typename T>
  175. bool operator!= (const FixedSizeFunction<len, T>& fn, std::nullptr_t) { return bool (fn); }
  176. template <size_t len, typename T>
  177. bool operator!= (std::nullptr_t, const FixedSizeFunction<len, T>& fn) { return bool (fn); }
  178. template <size_t len, typename T>
  179. bool operator== (const FixedSizeFunction<len, T>& fn, std::nullptr_t) { return ! (fn != nullptr); }
  180. template <size_t len, typename T>
  181. bool operator== (std::nullptr_t, const FixedSizeFunction<len, T>& fn) { return ! (fn != nullptr); }
  182. }
  183. }