1 | //===-- SBBroadcaster.cpp -------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Utility/Broadcaster.h" |
10 | #include "lldb/Utility/Instrumentation.h" |
11 | |
12 | #include "lldb/API/SBBroadcaster.h" |
13 | #include "lldb/API/SBEvent.h" |
14 | #include "lldb/API/SBListener.h" |
15 | |
16 | using namespace lldb; |
17 | using namespace lldb_private; |
18 | |
19 | SBBroadcaster::SBBroadcaster() { LLDB_INSTRUMENT_VA(this); } |
20 | |
21 | SBBroadcaster::SBBroadcaster(const char *name) |
22 | : m_opaque_sp(new Broadcaster(nullptr, name)) { |
23 | LLDB_INSTRUMENT_VA(this, name); |
24 | |
25 | m_opaque_ptr = m_opaque_sp.get(); |
26 | } |
27 | |
28 | SBBroadcaster::SBBroadcaster(lldb_private::Broadcaster *broadcaster, bool owns) |
29 | : m_opaque_sp(owns ? broadcaster : nullptr), m_opaque_ptr(broadcaster) {} |
30 | |
31 | SBBroadcaster::SBBroadcaster(const SBBroadcaster &rhs) |
32 | : m_opaque_sp(rhs.m_opaque_sp), m_opaque_ptr(rhs.m_opaque_ptr) { |
33 | LLDB_INSTRUMENT_VA(this, rhs); |
34 | } |
35 | |
36 | const SBBroadcaster &SBBroadcaster::operator=(const SBBroadcaster &rhs) { |
37 | LLDB_INSTRUMENT_VA(this, rhs); |
38 | |
39 | if (this != &rhs) { |
40 | m_opaque_sp = rhs.m_opaque_sp; |
41 | m_opaque_ptr = rhs.m_opaque_ptr; |
42 | } |
43 | return *this; |
44 | } |
45 | |
46 | SBBroadcaster::~SBBroadcaster() { reset(broadcaster: nullptr, owns: false); } |
47 | |
48 | void SBBroadcaster::BroadcastEventByType(uint32_t event_type, bool unique) { |
49 | LLDB_INSTRUMENT_VA(this, event_type, unique); |
50 | |
51 | if (m_opaque_ptr == nullptr) |
52 | return; |
53 | |
54 | if (unique) |
55 | m_opaque_ptr->BroadcastEventIfUnique(event_type); |
56 | else |
57 | m_opaque_ptr->BroadcastEvent(event_type); |
58 | } |
59 | |
60 | void SBBroadcaster::BroadcastEvent(const SBEvent &event, bool unique) { |
61 | LLDB_INSTRUMENT_VA(this, event, unique); |
62 | |
63 | if (m_opaque_ptr == nullptr) |
64 | return; |
65 | |
66 | EventSP event_sp = event.GetSP(); |
67 | if (unique) |
68 | m_opaque_ptr->BroadcastEventIfUnique(event_sp); |
69 | else |
70 | m_opaque_ptr->BroadcastEvent(event_sp); |
71 | } |
72 | |
73 | void SBBroadcaster::AddInitialEventsToListener(const SBListener &listener, |
74 | uint32_t requested_events) { |
75 | LLDB_INSTRUMENT_VA(this, listener, requested_events); |
76 | |
77 | if (m_opaque_ptr) |
78 | m_opaque_ptr->AddInitialEventsToListener(listener_sp: listener.m_opaque_sp, |
79 | requested_events); |
80 | } |
81 | |
82 | uint32_t SBBroadcaster::AddListener(const SBListener &listener, |
83 | uint32_t event_mask) { |
84 | LLDB_INSTRUMENT_VA(this, listener, event_mask); |
85 | |
86 | if (m_opaque_ptr) |
87 | return m_opaque_ptr->AddListener(listener_sp: listener.m_opaque_sp, event_mask); |
88 | return 0; |
89 | } |
90 | |
91 | const char *SBBroadcaster::GetName() const { |
92 | LLDB_INSTRUMENT_VA(this); |
93 | |
94 | if (m_opaque_ptr) |
95 | return ConstString(m_opaque_ptr->GetBroadcasterName()).GetCString(); |
96 | return nullptr; |
97 | } |
98 | |
99 | bool SBBroadcaster::EventTypeHasListeners(uint32_t event_type) { |
100 | LLDB_INSTRUMENT_VA(this, event_type); |
101 | |
102 | if (m_opaque_ptr) |
103 | return m_opaque_ptr->EventTypeHasListeners(event_type); |
104 | return false; |
105 | } |
106 | |
107 | bool SBBroadcaster::RemoveListener(const SBListener &listener, |
108 | uint32_t event_mask) { |
109 | LLDB_INSTRUMENT_VA(this, listener, event_mask); |
110 | |
111 | if (m_opaque_ptr) |
112 | return m_opaque_ptr->RemoveListener(listener_sp: listener.m_opaque_sp, event_mask); |
113 | return false; |
114 | } |
115 | |
116 | Broadcaster *SBBroadcaster::get() const { return m_opaque_ptr; } |
117 | |
118 | void SBBroadcaster::reset(Broadcaster *broadcaster, bool owns) { |
119 | if (owns) |
120 | m_opaque_sp.reset(p: broadcaster); |
121 | else |
122 | m_opaque_sp.reset(); |
123 | m_opaque_ptr = broadcaster; |
124 | } |
125 | |
126 | bool SBBroadcaster::IsValid() const { |
127 | LLDB_INSTRUMENT_VA(this); |
128 | return this->operator bool(); |
129 | } |
130 | SBBroadcaster::operator bool() const { |
131 | LLDB_INSTRUMENT_VA(this); |
132 | |
133 | return m_opaque_ptr != nullptr; |
134 | } |
135 | |
136 | void SBBroadcaster::Clear() { |
137 | LLDB_INSTRUMENT_VA(this); |
138 | |
139 | m_opaque_sp.reset(); |
140 | m_opaque_ptr = nullptr; |
141 | } |
142 | |
143 | bool SBBroadcaster::operator==(const SBBroadcaster &rhs) const { |
144 | LLDB_INSTRUMENT_VA(this, rhs); |
145 | |
146 | return m_opaque_ptr == rhs.m_opaque_ptr; |
147 | } |
148 | |
149 | bool SBBroadcaster::operator!=(const SBBroadcaster &rhs) const { |
150 | LLDB_INSTRUMENT_VA(this, rhs); |
151 | |
152 | return m_opaque_ptr != rhs.m_opaque_ptr; |
153 | } |
154 | |
155 | bool SBBroadcaster::operator<(const SBBroadcaster &rhs) const { |
156 | LLDB_INSTRUMENT_VA(this, rhs); |
157 | |
158 | return m_opaque_ptr < rhs.m_opaque_ptr; |
159 | } |
160 | |