forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakReference.cpp
More file actions
69 lines (56 loc) · 1.59 KB
/
WeakReference.cpp
File metadata and controls
69 lines (56 loc) · 1.59 KB
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
/*
// Copyright (c) 2015-2022 Pierre Guillot and Timothy Schoen.
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#include "Utility/Config.h"
#include <juce_gui_basics/juce_gui_basics.h>
extern "C" {
#include <s_inter.h>
}
#include "WeakReference.h"
#include "Instance.h"
pd::WeakReference::WeakReference(void* p, Instance* instance)
: ptr(p)
, pd(instance)
{
pd->registerWeakReference(ptr, &weakRef);
}
pd::WeakReference::WeakReference(Instance* instance)
: ptr(nullptr)
, pd(instance)
{
}
pd::WeakReference::WeakReference(WeakReference const& toCopy)
: ptr(toCopy.ptr)
, pd(toCopy.pd)
{
weakRef = toCopy.weakRef.load();
pd->registerWeakReference(ptr, &weakRef);
}
pd::WeakReference::~WeakReference()
{
if (pd)
pd->unregisterWeakReference(ptr, &weakRef);
}
pd::WeakReference& pd::WeakReference::operator=(pd::WeakReference const& other)
{
bool valid = other.ptr && other.pd;
if (valid && this != &other) // Check for self-assignment
{
pd = other.pd;
pd->weakReferenceLock.enter();
pd->unregisterWeakReference(ptr, &other.weakRef);
// Use atomic exchange to safely copy the weakRef value
weakRef.store(other.weakRef.load());
ptr = other.ptr;
pd->registerWeakReference(ptr, &weakRef);
pd->weakReferenceLock.exit();
}
return *this;
}
void pd::WeakReference::setThis() const
{
if (pd)
pd->setThis();
}