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.

236 lines
7.6KB

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