Audio plugin host https://kx.studio/carla
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.

CarlaJuceUtils.hpp 7.8KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Carla misc utils based on Juce
  3. * Copyright (C) 2013 Raw Material Software Ltd.
  4. * Copyright (C) 2013-2015 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef CARLA_JUCE_UTILS_HPP_INCLUDED
  18. #define CARLA_JUCE_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. /** A good old-fashioned C macro concatenation helper.
  21. This combines two items (which may themselves be macros) into a single string,
  22. avoiding the pitfalls of the ## macro operator.
  23. */
  24. #define CARLA_JOIN_MACRO_HELPER(a, b) a ## b
  25. #define CARLA_JOIN_MACRO(item1, item2) CARLA_JOIN_MACRO_HELPER(item1, item2)
  26. #ifdef DEBUG
  27. /** This macro lets you embed a leak-detecting object inside a class.
  28. To use it, simply declare a CARLA_LEAK_DETECTOR(YourClassName) inside a private section
  29. of the class declaration. E.g.
  30. @code
  31. class MyClass
  32. {
  33. public:
  34. MyClass();
  35. void blahBlah();
  36. private:
  37. CARLA_LEAK_DETECTOR(MyClass)
  38. };
  39. @endcode
  40. */
  41. # define CARLA_LEAK_DETECTOR(ClassName) \
  42. friend class ::LeakedObjectDetector<ClassName>; \
  43. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  44. ::LeakedObjectDetector<ClassName> CARLA_JOIN_MACRO(leakDetector_, ClassName);
  45. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  46. CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
  47. CARLA_LEAK_DETECTOR(ClassName)
  48. #else
  49. /** Don't use leak detection on release builds. */
  50. # define CARLA_LEAK_DETECTOR(ClassName)
  51. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  52. CARLA_DECLARE_NON_COPY_CLASS(ClassName)
  53. #endif
  54. //=====================================================================================================================
  55. /**
  56. Embedding an instance of this class inside another class can be used as a low-overhead
  57. way of detecting leaked instances.
  58. This class keeps an internal static count of the number of instances that are
  59. active, so that when the app is shutdown and the static destructors are called,
  60. it can check whether there are any left-over instances that may have been leaked.
  61. To use it, use the CARLA_LEAK_DETECTOR macro as a simple way to put one in your
  62. class declaration.
  63. */
  64. template <class OwnerClass>
  65. class LeakedObjectDetector
  66. {
  67. public:
  68. //=================================================================================================================
  69. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  70. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  71. ~LeakedObjectDetector() noexcept
  72. {
  73. if (--(getCounter().numObjects) < 0)
  74. {
  75. /** If you hit this, then you've managed to delete more instances of this class than you've
  76. created.. That indicates that you're deleting some dangling pointers.
  77. Note that although this assertion will have been triggered during a destructor, it might
  78. not be this particular deletion that's at fault - the incorrect one may have happened
  79. at an earlier point in the program, and simply not been detected until now.
  80. Most errors like this are caused by using old-fashioned, non-RAII techniques for
  81. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  82. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  83. */
  84. carla_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(),
  85. getCounter().numObjects);
  86. }
  87. }
  88. private:
  89. //=================================================================================================================
  90. class LeakCounter
  91. {
  92. public:
  93. LeakCounter() noexcept
  94. : numObjects(0) {}
  95. ~LeakCounter() noexcept
  96. {
  97. if (numObjects > 0)
  98. {
  99. /** If you hit this, then you've leaked one or more objects of the type specified by
  100. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  101. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  102. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  103. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  104. */
  105. carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects,
  106. getLeakedObjectClassName());
  107. }
  108. }
  109. // this should be an atomic...
  110. volatile int numObjects;
  111. };
  112. static const char* getLeakedObjectClassName() noexcept
  113. {
  114. return OwnerClass::getLeakedObjectClassName();
  115. }
  116. static LeakCounter& getCounter() noexcept
  117. {
  118. static LeakCounter counter;
  119. return counter;
  120. }
  121. };
  122. //=====================================================================================================================
  123. /**
  124. Helper class providing an RAII-based mechanism for temporarily setting and
  125. then re-setting a value.
  126. E.g. @code
  127. int x = 1;
  128. {
  129. ScopedValueSetter setter (x, 2);
  130. // x is now 2
  131. }
  132. // x is now 1 again
  133. {
  134. ScopedValueSetter setter (x, 3, 4);
  135. // x is now 3
  136. }
  137. // x is now 4
  138. @endcode
  139. */
  140. template <typename ValueType>
  141. class ScopedValueSetter
  142. {
  143. public:
  144. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  145. given new value, and will then reset it to its original value when this object is deleted.
  146. Must be used only for 'noexcept' compatible types.
  147. */
  148. ScopedValueSetter(ValueType& valueToSet, ValueType newValue) noexcept
  149. : value(valueToSet),
  150. originalValue(valueToSet)
  151. {
  152. valueToSet = newValue;
  153. }
  154. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  155. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  156. */
  157. ScopedValueSetter(ValueType& valueToSet, ValueType newValue, ValueType valueWhenDeleted) noexcept
  158. : value(valueToSet),
  159. originalValue(valueWhenDeleted)
  160. {
  161. valueToSet = newValue;
  162. }
  163. ~ScopedValueSetter() noexcept
  164. {
  165. value = originalValue;
  166. }
  167. private:
  168. //=================================================================================================================
  169. ValueType& value;
  170. const ValueType originalValue;
  171. CARLA_DECLARE_NON_COPY_CLASS(ScopedValueSetter)
  172. CARLA_PREVENT_HEAP_ALLOCATION
  173. };
  174. #ifdef CARLA_OS_WIN
  175. namespace juce {
  176. extern bool juce_isRunningInWine();
  177. }
  178. #endif
  179. //=====================================================================================================================
  180. #endif // CARLA_JUCE_UTILS_HPP_INCLUDED