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 6.8KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
6 years ago
6 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Carla misc utils based on Juce
  3. * Copyright (C) 2013 Raw Material Software Ltd.
  4. * Copyright (c) 2016 ROLI Ltd.
  5. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  8. * or without fee is hereby granted, provided that the above copyright notice and this
  9. * permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  12. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  13. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  16. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef CARLA_JUCE_UTILS_HPP_INCLUDED
  19. #define CARLA_JUCE_UTILS_HPP_INCLUDED
  20. #include "CarlaUtils.hpp"
  21. #include <algorithm>
  22. #include <cmath>
  23. /** A good old-fashioned C macro concatenation helper.
  24. This combines two items (which may themselves be macros) into a single string,
  25. avoiding the pitfalls of the ## macro operator.
  26. */
  27. #define CARLA_JOIN_MACRO_HELPER(a, b) a ## b
  28. #define CARLA_JOIN_MACRO(item1, item2) CARLA_JOIN_MACRO_HELPER(item1, item2)
  29. /** Same but for joining 3 items */
  30. #define CARLA_JOIN_MACRO_HELPER3(a, b, c) a ## b ## c
  31. #define CARLA_JOIN_MACRO3(item1, item2, item3) CARLA_JOIN_MACRO_HELPER3(item1, item2, item3)
  32. #ifdef DEBUG
  33. /** This macro lets you embed a leak-detecting object inside a class.
  34. To use it, simply declare a CARLA_LEAK_DETECTOR(YourClassName) inside a private section
  35. of the class declaration. E.g.
  36. @code
  37. class MyClass
  38. {
  39. public:
  40. MyClass();
  41. void blahBlah();
  42. private:
  43. CARLA_LEAK_DETECTOR(MyClass)
  44. };
  45. @endcode
  46. */
  47. # define CARLA_LEAK_DETECTOR(ClassName) \
  48. friend class ::LeakedObjectDetector<ClassName>; \
  49. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  50. ::LeakedObjectDetector<ClassName> CARLA_JOIN_MACRO(leakDetector_, ClassName);
  51. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  52. CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
  53. CARLA_LEAK_DETECTOR(ClassName)
  54. #else
  55. /** Don't use leak detection on release builds. */
  56. # define CARLA_LEAK_DETECTOR(ClassName)
  57. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  58. CARLA_DECLARE_NON_COPY_CLASS(ClassName)
  59. #endif
  60. //=====================================================================================================================
  61. /** Converts a dBFS value to its equivalent gain level.
  62. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  63. decibel value lower than minusInfinityDb will return a gain of 0.
  64. */
  65. static inline
  66. float decibelsToGain (const double decibels, const double minusInfinityDb = -100.0)
  67. {
  68. return decibels > minusInfinityDb
  69. ? static_cast<float>(std::pow(10.0, decibels * 0.05))
  70. : 0.0f;
  71. }
  72. //=====================================================================================================================
  73. /**
  74. Embedding an instance of this class inside another class can be used as a low-overhead
  75. way of detecting leaked instances.
  76. This class keeps an internal static count of the number of instances that are
  77. active, so that when the app is shutdown and the static destructors are called,
  78. it can check whether there are any left-over instances that may have been leaked.
  79. To use it, use the CARLA_LEAK_DETECTOR macro as a simple way to put one in your
  80. class declaration.
  81. */
  82. template <class OwnerClass>
  83. class LeakedObjectDetector
  84. {
  85. public:
  86. //=================================================================================================================
  87. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  88. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  89. ~LeakedObjectDetector() noexcept
  90. {
  91. if (--(getCounter().numObjects) < 0)
  92. {
  93. /** If you hit this, then you've managed to delete more instances of this class than you've
  94. created.. That indicates that you're deleting some dangling pointers.
  95. Note that although this assertion will have been triggered during a destructor, it might
  96. not be this particular deletion that's at fault - the incorrect one may have happened
  97. at an earlier point in the program, and simply not been detected until now.
  98. Most errors like this are caused by using old-fashioned, non-RAII techniques for
  99. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  100. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  101. */
  102. carla_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(),
  103. getCounter().numObjects);
  104. }
  105. }
  106. private:
  107. //=================================================================================================================
  108. class LeakCounter
  109. {
  110. public:
  111. LeakCounter() noexcept
  112. : numObjects(0) {}
  113. ~LeakCounter() noexcept
  114. {
  115. if (numObjects > 0)
  116. {
  117. /** If you hit this, then you've leaked one or more objects of the type specified by
  118. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  119. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  120. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  121. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  122. */
  123. carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects,
  124. getLeakedObjectClassName());
  125. }
  126. }
  127. // this should be an atomic...
  128. volatile int numObjects;
  129. };
  130. static const char* getLeakedObjectClassName() noexcept
  131. {
  132. return OwnerClass::getLeakedObjectClassName();
  133. }
  134. static LeakCounter& getCounter() noexcept
  135. {
  136. static LeakCounter counter;
  137. return counter;
  138. }
  139. };
  140. //=====================================================================================================================
  141. #endif // CARLA_JUCE_UTILS_HPP_INCLUDED