VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / utils / CarlaJuceUtils.hpp
blob16842f1e56148d2868f7fb28949f2ec08ada3080
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>
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.
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.
19 #ifndef CARLA_JUCE_UTILS_HPP_INCLUDED
20 #define CARLA_JUCE_UTILS_HPP_INCLUDED
22 #include "CarlaUtils.hpp"
24 #include <algorithm>
25 #include <cmath>
27 /** A good old-fashioned C macro concatenation helper.
28 This combines two items (which may themselves be macros) into a single string,
29 avoiding the pitfalls of the ## macro operator.
31 #define CARLA_JOIN_MACRO_HELPER(a, b) a ## b
32 #define CARLA_JOIN_MACRO(item1, item2) CARLA_JOIN_MACRO_HELPER(item1, item2)
34 /** Same but for joining 3 items */
35 #define CARLA_JOIN_MACRO_HELPER3(a, b, c) a ## b ## c
36 #define CARLA_JOIN_MACRO3(item1, item2, item3) CARLA_JOIN_MACRO_HELPER3(item1, item2, item3)
38 #ifdef DEBUG
39 /** This macro lets you embed a leak-detecting object inside a class.
40 To use it, simply declare a CARLA_LEAK_DETECTOR(YourClassName) inside a private section
41 of the class declaration. E.g.
42 @code
43 class MyClass
45 public:
46 MyClass();
47 void blahBlah();
49 private:
50 CARLA_LEAK_DETECTOR(MyClass)
52 @endcode
54 # define CARLA_LEAK_DETECTOR(ClassName) \
55 friend class ::LeakedObjectDetector<ClassName>; \
56 static const char* getLeakedObjectClassName() noexcept { return #ClassName; } \
57 ::LeakedObjectDetector<ClassName> CARLA_JOIN_MACRO(leakDetector_, ClassName);
59 # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
60 CARLA_DECLARE_NON_COPYABLE(ClassName) \
61 CARLA_LEAK_DETECTOR(ClassName)
62 #else
63 /** Don't use leak detection on release builds. */
64 # define CARLA_LEAK_DETECTOR(ClassName)
65 # define CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ClassName) \
66 CARLA_DECLARE_NON_COPYABLE(ClassName)
67 #endif
69 //=====================================================================================================================
70 /** Converts a dBFS value to its equivalent gain level.
72 A gain of 1.0 = 0 dB, and lower gains map onto negative decibel values. Any
73 decibel value lower than minusInfinityDb will return a gain of 0.
75 static inline
76 float decibelsToGain (const double decibels, const double minusInfinityDb = -100.0)
78 return decibels > minusInfinityDb
79 ? static_cast<float>(std::pow(10.0, decibels * 0.05))
80 : 0.0f;
83 //=====================================================================================================================
84 /**
85 Embedding an instance of this class inside another class can be used as a low-overhead
86 way of detecting leaked instances.
88 This class keeps an internal static count of the number of instances that are
89 active, so that when the app is shutdown and the static destructors are called,
90 it can check whether there are any left-over instances that may have been leaked.
92 To use it, use the CARLA_LEAK_DETECTOR macro as a simple way to put one in your
93 class declaration.
95 template <class OwnerClass>
96 class LeakedObjectDetector
98 public:
99 //=================================================================================================================
100 LeakedObjectDetector() noexcept { ++(getCounter().numObjects); }
101 LeakedObjectDetector(const LeakedObjectDetector&) noexcept { ++(getCounter().numObjects); }
103 ~LeakedObjectDetector() noexcept
105 if (--(getCounter().numObjects) < 0)
107 /** If you hit this, then you've managed to delete more instances of this class than you've
108 created.. That indicates that you're deleting some dangling pointers.
110 Note that although this assertion will have been triggered during a destructor, it might
111 not be this particular deletion that's at fault - the incorrect one may have happened
112 at an earlier point in the program, and simply not been detected until now.
114 Most errors like this are caused by using old-fashioned, non-RAII techniques for
115 your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
116 ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
118 carla_stderr2("*** Dangling pointer deletion! Class: '%s', Count: %i", getLeakedObjectClassName(),
119 getCounter().numObjects);
123 private:
124 //=================================================================================================================
125 class LeakCounter
127 public:
128 LeakCounter() noexcept
129 : numObjects(0) {}
131 ~LeakCounter() noexcept
133 if (numObjects > 0)
135 /** If you hit this, then you've leaked one or more objects of the type specified by
136 the 'OwnerClass' template parameter - the name should have been printed by the line above.
138 If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
139 your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
140 ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
142 carla_stderr2("*** Leaked objects detected: %i instance(s) of class '%s'", numObjects,
143 getLeakedObjectClassName());
147 // this should be an atomic...
148 volatile int numObjects;
151 static const char* getLeakedObjectClassName() noexcept
153 return OwnerClass::getLeakedObjectClassName();
156 static LeakCounter& getCounter() noexcept
158 static LeakCounter counter;
159 return counter;
163 //=====================================================================================================================
165 #endif // CARLA_JUCE_UTILS_HPP_INCLUDED