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.

208 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. namespace std
  24. {
  25. /**
  26. This class provides an alternative to std::function that is compatible
  27. with OS X 10.6 and earlier. This will only be used in OS X versions 10.6
  28. and earlier and the Projucer live build.
  29. */
  30. template <typename>
  31. class function;
  32. template <typename Result, typename... Arguments>
  33. class function<Result (Arguments...)>
  34. {
  35. public:
  36. /** Creates an empty function. */
  37. function() noexcept {}
  38. /** Creates an empty function. */
  39. function (decltype (nullptr)) noexcept {}
  40. /** Creates a function targetting the provided Functor. */
  41. template <typename Functor>
  42. function (Functor f)
  43. {
  44. functorHolderHelper = getFunctorStorage (sizeof (FunctorHolder<Functor, Result, Arguments...>));
  45. new (functorHolderHelper) FunctorHolder<Functor, Result, Arguments...> (f);
  46. }
  47. /** Copy constructor. */
  48. function (function const& other)
  49. {
  50. copy (other);
  51. }
  52. /** Move constructor */
  53. function (function&& other)
  54. {
  55. move (other);
  56. }
  57. /** Destructor. */
  58. ~function()
  59. {
  60. release();
  61. }
  62. /** Replaces the contents of this function with the contents of another. */
  63. function& operator= (function const& other)
  64. {
  65. release();
  66. copy (other);
  67. return *this;
  68. }
  69. /** Moves the contents of another function into this one. */
  70. function& operator= (function&& other)
  71. {
  72. release();
  73. move (other);
  74. return *this;
  75. }
  76. /** Allows conditional expressions to test if this function is empty. */
  77. explicit operator bool() const noexcept
  78. {
  79. return functorHolderHelper != nullptr;
  80. }
  81. /** Swaps the contents of this function with another. After this operation the
  82. two functions will be pointing at each other's targets. */
  83. void swap (function& other)
  84. {
  85. function<Result (Arguments...)> tmp (*this);
  86. *this = other;
  87. other = tmp;
  88. }
  89. /** Invokes the target of this function. */
  90. Result operator() (Arguments... args) const
  91. {
  92. return (*functorHolderHelper) (args...);
  93. }
  94. bool operator== (decltype (nullptr)) const noexcept { return (functorHolderHelper == nullptr); }
  95. bool operator!= (decltype (nullptr)) const noexcept { return (functorHolderHelper != nullptr); }
  96. private:
  97. //==============================================================================
  98. template <typename ReturnType, typename... Args>
  99. struct FunctorHolderBase
  100. {
  101. virtual ~FunctorHolderBase() {}
  102. virtual int getSize() const noexcept = 0;
  103. virtual void copy (void*) const = 0;
  104. virtual ReturnType operator()(Args...) = 0;
  105. };
  106. template <typename Functor, typename ReturnType, typename... Args>
  107. struct FunctorHolder : FunctorHolderBase<Result, Arguments...>
  108. {
  109. FunctorHolder (Functor func) : f (func) {}
  110. int getSize() const noexcept override final
  111. {
  112. return sizeof (*this);
  113. }
  114. void copy (void* destination) const override final
  115. {
  116. new (destination) FunctorHolder (f);
  117. }
  118. ReturnType operator()(Args... args) override final
  119. {
  120. return f (args...);
  121. }
  122. Functor f;
  123. };
  124. FunctorHolderBase<Result, Arguments...>* getFunctorStorage (int size)
  125. {
  126. return reinterpret_cast<FunctorHolderBase<Result, Arguments...>*>
  127. (size > functorHolderStackSize ? new char [size]
  128. : &(stackFunctorStorage[0]));
  129. }
  130. void copy (function const& other)
  131. {
  132. if (other.functorHolderHelper != nullptr)
  133. {
  134. functorHolderHelper = getFunctorStorage (other.functorHolderHelper->getSize());
  135. other.functorHolderHelper->copy (functorHolderHelper);
  136. }
  137. }
  138. void move (function& other)
  139. {
  140. if (other.functorHolderHelper != nullptr)
  141. {
  142. if (other.functorHolderHelper->getSize() > functorHolderStackSize)
  143. {
  144. functorHolderHelper = other.functorHolderHelper;
  145. }
  146. else
  147. {
  148. std::copy (other.stackFunctorStorage, other.stackFunctorStorage + functorHolderStackSize,
  149. stackFunctorStorage);
  150. functorHolderHelper = reinterpret_cast<FunctorHolderBase<Result, Arguments...>*> (&(stackFunctorStorage[0]));
  151. }
  152. other.functorHolderHelper = nullptr;
  153. }
  154. }
  155. void release()
  156. {
  157. if (functorHolderHelper != nullptr)
  158. {
  159. if (functorHolderHelper->getSize() > functorHolderStackSize)
  160. delete[] reinterpret_cast<char*> (functorHolderHelper);
  161. else
  162. functorHolderHelper->~FunctorHolderBase<Result, Arguments...>();
  163. functorHolderHelper = nullptr;
  164. }
  165. }
  166. static const int functorHolderStackSize = 24;
  167. char stackFunctorStorage[functorHolderStackSize];
  168. FunctorHolderBase<Result, Arguments...>* functorHolderHelper = nullptr;
  169. };
  170. }