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.0KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Carla misc utils based on Juce
  3. * Copyright (C) 2013 Raw Material Software Ltd.
  4. * Copyright (C) 2013-2014 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. /** This macro lets you embed a leak-detecting object inside a class.\n
  27. To use it, simply declare a CARLA_LEAK_DETECTOR(YourClassName) inside a private section
  28. of the class declaration. E.g.
  29. \code
  30. class MyClass
  31. {
  32. public:
  33. MyClass();
  34. void blahBlah();
  35. private:
  36. CARLA_LEAK_DETECTOR(MyClass)
  37. };
  38. \endcode
  39. */
  40. #define CARLA_LEAK_DETECTOR(ClassName) \
  41. friend class ::LeakedObjectDetector<ClassName>; \
  42. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  43. ::LeakedObjectDetector<ClassName> CARLA_JOIN_MACRO(leakDetector_, ClassName);
  44. #define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  45. CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
  46. CARLA_LEAK_DETECTOR(ClassName)
  47. //==============================================================================
  48. /**
  49. Embedding an instance of this class inside another class can be used as a low-overhead
  50. way of detecting leaked instances.
  51. This class keeps an internal static count of the number of instances that are
  52. active, so that when the app is shutdown and the static destructors are called,
  53. it can check whether there are any left-over instances that may have been leaked.
  54. To use it, use the CARLA_LEAK_DETECTOR macro as a simple way to put one in your
  55. class declaration.
  56. */
  57. template <class OwnerClass>
  58. class LeakedObjectDetector
  59. {
  60. public:
  61. //==============================================================================
  62. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  63. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  64. ~LeakedObjectDetector() noexcept
  65. {
  66. if (--(getCounter().numObjects) < 0)
  67. {
  68. /** If you hit this, then you've managed to delete more instances of this class than you've
  69. created.. That indicates that you're deleting some dangling pointers.
  70. Note that although this assertion will have been triggered during a destructor, it might
  71. not be this particular deletion that's at fault - the incorrect one may have happened
  72. at an earlier point in the program, and simply not been detected until now.
  73. Most errors like this are caused by using old-fashioned, non-RAII techniques for
  74. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  75. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  76. */
  77. carla_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(), getCounter().numObjects);
  78. }
  79. }
  80. private:
  81. //==============================================================================
  82. class LeakCounter
  83. {
  84. public:
  85. LeakCounter() noexcept
  86. : numObjects(0) {}
  87. ~LeakCounter() noexcept
  88. {
  89. if (numObjects > 0)
  90. {
  91. /** If you hit this, then you've leaked one or more objects of the type specified by
  92. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  93. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  94. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  95. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  96. */
  97. carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects, getLeakedObjectClassName());
  98. }
  99. }
  100. // this should be an atomic...
  101. volatile int numObjects;
  102. };
  103. static const char* getLeakedObjectClassName() noexcept
  104. {
  105. return OwnerClass::getLeakedObjectClassName();
  106. }
  107. static LeakCounter& getCounter() noexcept
  108. {
  109. static LeakCounter counter;
  110. return counter;
  111. }
  112. };
  113. //==============================================================================
  114. /**
  115. Helper class providing an RAII-based mechanism for temporarily setting and
  116. then re-setting a value.
  117. E.g. @code
  118. int x = 1;
  119. {
  120. ScopedValueSetter setter (x, 2);
  121. // x is now 2
  122. }
  123. // x is now 1 again
  124. {
  125. ScopedValueSetter setter (x, 3, 4);
  126. // x is now 3
  127. }
  128. // x is now 4
  129. @endcode
  130. */
  131. template <typename ValueType>
  132. class ScopedValueSetter
  133. {
  134. public:
  135. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  136. given new value, and will then reset it to its original value when this object is deleted.
  137. Must be used only for 'noexcept' compatible types.
  138. */
  139. ScopedValueSetter(ValueType& valueToSet, ValueType newValue) noexcept
  140. : value(valueToSet),
  141. originalValue(valueToSet)
  142. {
  143. valueToSet = newValue;
  144. }
  145. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  146. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  147. */
  148. ScopedValueSetter(ValueType& valueToSet, ValueType newValue, ValueType valueWhenDeleted) noexcept
  149. : value(valueToSet),
  150. originalValue(valueWhenDeleted)
  151. {
  152. valueToSet = newValue;
  153. }
  154. ~ScopedValueSetter() noexcept
  155. {
  156. value = originalValue;
  157. }
  158. private:
  159. //==============================================================================
  160. ValueType& value;
  161. const ValueType originalValue;
  162. CARLA_DECLARE_NON_COPY_CLASS(ScopedValueSetter)
  163. CARLA_PREVENT_HEAP_ALLOCATION
  164. };
  165. #endif // CARLA_JUCE_UTILS_HPP_INCLUDED