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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifdef DEBUG
  30. /** This macro lets you embed a leak-detecting object inside a class.
  31. To use it, simply declare a CARLA_LEAK_DETECTOR(YourClassName) inside a private section
  32. of the class declaration. E.g.
  33. @code
  34. class MyClass
  35. {
  36. public:
  37. MyClass();
  38. void blahBlah();
  39. private:
  40. CARLA_LEAK_DETECTOR(MyClass)
  41. };
  42. @endcode
  43. */
  44. # define CARLA_LEAK_DETECTOR(ClassName) \
  45. friend class ::LeakedObjectDetector<ClassName>; \
  46. static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
  47. ::LeakedObjectDetector<ClassName> CARLA_JOIN_MACRO(leakDetector_, ClassName);
  48. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  49. CARLA_DECLARE_NON_COPY_CLASS(ClassName) \
  50. CARLA_LEAK_DETECTOR(ClassName)
  51. #else
  52. /** Don't use leak detection on release builds. */
  53. # define CARLA_LEAK_DETECTOR(ClassName)
  54. # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
  55. CARLA_DECLARE_NON_COPY_CLASS(ClassName)
  56. #endif
  57. //=====================================================================================================================
  58. /** Converts a dBFS value to its equivalent gain level.
  59. A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
  60. decibel value lower than minusInfinityDb will return a gain of 0.
  61. */
  62. static inline
  63. float decibelsToGain (const double decibels, const double minusInfinityDb = -100.0)
  64. {
  65. return decibels > minusInfinityDb
  66. ? static_cast<float>(std::pow(10.0, decibels * 0.05))
  67. : 0.0f;
  68. }
  69. //=====================================================================================================================
  70. /**
  71. Embedding an instance of this class inside another class can be used as a low-overhead
  72. way of detecting leaked instances.
  73. This class keeps an internal static count of the number of instances that are
  74. active, so that when the app is shutdown and the static destructors are called,
  75. it can check whether there are any left-over instances that may have been leaked.
  76. To use it, use the CARLA_LEAK_DETECTOR macro as a simple way to put one in your
  77. class declaration.
  78. */
  79. template <class OwnerClass>
  80. class LeakedObjectDetector
  81. {
  82. public:
  83. //=================================================================================================================
  84. LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
  85. LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
  86. ~LeakedObjectDetector() noexcept
  87. {
  88. if (--(getCounter().numObjects) < 0)
  89. {
  90. /** If you hit this, then you've managed to delete more instances of this class than you've
  91. created.. That indicates that you're deleting some dangling pointers.
  92. Note that although this assertion will have been triggered during a destructor, it might
  93. not be this particular deletion that's at fault - the incorrect one may have happened
  94. at an earlier point in the program, and simply not been detected until now.
  95. Most errors like this are caused by 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("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(),
  100. getCounter().numObjects);
  101. }
  102. }
  103. private:
  104. //=================================================================================================================
  105. class LeakCounter
  106. {
  107. public:
  108. LeakCounter() noexcept
  109. : numObjects(0) {}
  110. ~LeakCounter() noexcept
  111. {
  112. if (numObjects > 0)
  113. {
  114. /** If you hit this, then you've leaked one or more objects of the type specified by
  115. the 'OwnerClass' template parameter - the name should have been printed by the line above.
  116. If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
  117. your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
  118. ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
  119. */
  120. carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects,
  121. getLeakedObjectClassName());
  122. }
  123. }
  124. // this should be an atomic...
  125. volatile int numObjects;
  126. };
  127. static const char* getLeakedObjectClassName() noexcept
  128. {
  129. return OwnerClass::getLeakedObjectClassName();
  130. }
  131. static LeakCounter& getCounter() noexcept
  132. {
  133. static LeakCounter counter;
  134. return counter;
  135. }
  136. };
  137. //=====================================================================================================================
  138. #endif // CARLA_JUCE_UTILS_HPP_INCLUDED