summaryrefslogtreecommitdiff
path: root/gfx/angle/src/libANGLE/signal_utils.cpp
blob: 62d45b953fc7e6c563b9817e4e90a94457c80f88 (plain)
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
99
100
101
102
103
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// signal_utils:
//   Helper classes for tracking dependent state changes between objects.
//   These changes are signaled to the dependent class via channels.

#include "libANGLE/signal_utils.h"

#include <algorithm>

#include "common/debug.h"

namespace angle
{

BroadcastChannel::BroadcastChannel()
{
}

BroadcastChannel::~BroadcastChannel()
{
    reset();
}

void BroadcastChannel::addReceiver(ChannelBinding *receiver)
{
    ASSERT(std::find(mReceivers.begin(), mReceivers.end(), receiver) == mReceivers.end());
    mReceivers.push_back(receiver);
}

void BroadcastChannel::removeReceiver(ChannelBinding *receiver)
{
    auto iter = std::find(mReceivers.begin(), mReceivers.end(), receiver);
    ASSERT(iter != mReceivers.end());
    mReceivers.erase(iter);
}

void BroadcastChannel::signal() const
{
    if (mReceivers.empty())
        return;

    for (const auto *receiver : mReceivers)
    {
        receiver->signal();
    }
}

void BroadcastChannel::reset()
{
    for (auto receiver : mReceivers)
    {
        receiver->onChannelClosed();
    }
    mReceivers.clear();
}

ChannelBinding::ChannelBinding(SignalReceiver *receiver, SignalToken token)
    : mChannel(nullptr), mReceiver(receiver), mToken(token)
{
    ASSERT(receiver);
}

ChannelBinding::~ChannelBinding()
{
    reset();
}

void ChannelBinding::bind(BroadcastChannel *channel)
{
    ASSERT(mReceiver);
    if (mChannel)
    {
        mChannel->removeReceiver(this);
    }

    mChannel = channel;

    if (mChannel)
    {
        mChannel->addReceiver(this);
    }
}

void ChannelBinding::reset()
{
    bind(nullptr);
}

void ChannelBinding::signal() const
{
    mReceiver->signal(mToken);
}

void ChannelBinding::onChannelClosed()
{
    mChannel = nullptr;
}

}  // namespace angle