forked from juce-framework/JUCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentBase.cpp
98 lines (82 loc) · 2.32 KB
/
ComponentBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*!
@file AudioUnitSDK/ComponentBase.cpp
@copyright © 2000-2021 Apple Inc. All rights reserved.
*/
// self
#include <AudioUnitSDK/AUUtility.h>
#include <AudioUnitSDK/ComponentBase.h>
// std
#include <mutex>
namespace ausdk {
static OSStatus CB_GetComponentDescription(
AudioComponentInstance inInstance, AudioComponentDescription* outDesc);
std::recursive_mutex& ComponentBase::InitializationMutex()
{
__attribute__((no_destroy)) static std::recursive_mutex global;
return global;
}
ComponentBase::ComponentBase(AudioComponentInstance inInstance) : mComponentInstance(inInstance)
{
(void)GetComponentDescription();
}
void ComponentBase::DoPostConstructor()
{
PostConstructorInternal();
PostConstructor();
}
void ComponentBase::DoPreDestructor()
{
PreDestructor();
PreDestructorInternal();
}
OSStatus ComponentBase::AP_Open(void* self, AudioComponentInstance compInstance)
{
OSStatus result = noErr;
const auto acpi = static_cast<AudioComponentPlugInInstance*>(self);
try {
const std::lock_guard guard{ InitializationMutex() };
auto* const cb =
static_cast<ComponentBase*>((*acpi->mConstruct)(&acpi->mInstanceStorage, compInstance));
cb->DoPostConstructor(); // allows base class to do additional initialization
// once the derived class is fully constructed
result = noErr;
}
AUSDK_Catch(result)
if (result != noErr) {
delete acpi; // NOLINT
}
return result;
}
OSStatus ComponentBase::AP_Close(void* self)
{
OSStatus result = noErr;
try {
const auto acpi = static_cast<AudioComponentPlugInInstance*>(self);
if (const auto acImp =
reinterpret_cast<ComponentBase*>(&acpi->mInstanceStorage)) { // NOLINT
acImp->DoPreDestructor();
(*acpi->mDestruct)(&acpi->mInstanceStorage);
free(self); // NOLINT manual memory management
}
}
AUSDK_Catch(result)
return result;
}
AudioComponentDescription ComponentBase::GetComponentDescription() const
{
AudioComponentDescription desc = {};
if (CB_GetComponentDescription(mComponentInstance, &desc) == noErr) {
return desc;
}
return {};
}
static OSStatus CB_GetComponentDescription(
AudioComponentInstance inInstance, AudioComponentDescription* outDesc)
{
const AudioComponent comp = AudioComponentInstanceGetComponent(inInstance);
if (comp != nullptr) {
return AudioComponentGetDescription(comp, outDesc);
}
return kAudio_ParamError;
}
} // namespace ausdk