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.

197 lines
6.9KB

  1. /*
  2. * Carla misc utils based on Juce
  3. * Copyright (C) 2013 Raw Material Software Ltd.
  4. * Copyright (C) 2013 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, __LINE__);
  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()
  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. {
  87. numObjects = 0;
  88. }
  89. ~LeakCounter()
  90. {
  91. if (numObjects > 0)
  92. {
  93. /** If you hit this, then you've leaked one or more objects of the type specified by
  94. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  95. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  96. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  97. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  98. */
  99. carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects, getLeakedObjectClassName());
  100. }
  101. }
  102. volatile int numObjects;
  103. };
  104. static const char* getLeakedObjectClassName()
  105. {
  106. return OwnerClass::getLeakedObjectClassName();
  107. }
  108. static LeakCounter& getCounter() noexcept
  109. {
  110. static LeakCounter counter;
  111. return counter;
  112. }
  113. };
  114. //==============================================================================
  115. /**
  116. Helper class providing an RAII-based mechanism for temporarily setting and
  117. then re-setting a value.
  118. E.g. @code
  119. int x = 1;
  120. {
  121. ScopedValueSetter setter (x, 2);
  122. // x is now 2
  123. }
  124. // x is now 1 again
  125. {
  126. ScopedValueSetter setter (x, 3, 4);
  127. // x is now 3
  128. }
  129. // x is now 4
  130. @endcode
  131. */
  132. template <typename ValueType>
  133. class ScopedValueSetter
  134. {
  135. public:
  136. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  137. given new value, and will then reset it to its original value when this object is deleted.
  138. */
  139. ScopedValueSetter(ValueType& valueToSet, ValueType newValue)
  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)
  149. : value(valueToSet),
  150. originalValue(valueWhenDeleted)
  151. {
  152. valueToSet = newValue;
  153. }
  154. ~ScopedValueSetter()
  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