Event.h revision 343218
1//===-- Event.h -------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLDB_UTILITY_EVENT_H
11#define LLDB_UTILITY_EVENT_H
12
13#include "lldb/Utility/Broadcaster.h"
14#include "lldb/Utility/ConstString.h"
15#include "lldb/Utility/Predicate.h"
16#include "lldb/Utility/StructuredData.h"
17#include "lldb/lldb-defines.h"
18#include "lldb/lldb-forward.h"
19
20#include "llvm/ADT/StringRef.h"
21
22#include <chrono>
23#include <memory>
24#include <string>
25
26#include <stddef.h>
27#include <stdint.h>
28
29namespace lldb_private {
30class Event;
31}
32namespace lldb_private {
33class Stream;
34}
35
36namespace lldb_private {
37
38//----------------------------------------------------------------------
39// lldb::EventData
40//----------------------------------------------------------------------
41class EventData {
42  friend class Event;
43
44public:
45  EventData();
46
47  virtual ~EventData();
48
49  virtual const ConstString &GetFlavor() const = 0;
50
51  virtual void Dump(Stream *s) const;
52
53private:
54  virtual void DoOnRemoval(Event *event_ptr) {}
55
56  DISALLOW_COPY_AND_ASSIGN(EventData);
57};
58
59//----------------------------------------------------------------------
60// lldb::EventDataBytes
61//----------------------------------------------------------------------
62class EventDataBytes : public EventData {
63public:
64  //------------------------------------------------------------------
65  // Constructors
66  //------------------------------------------------------------------
67  EventDataBytes();
68
69  EventDataBytes(const char *cstr);
70
71  EventDataBytes(llvm::StringRef str);
72
73  EventDataBytes(const void *src, size_t src_len);
74
75  ~EventDataBytes() override;
76
77  //------------------------------------------------------------------
78  // Member functions
79  //------------------------------------------------------------------
80  const ConstString &GetFlavor() const override;
81
82  void Dump(Stream *s) const override;
83
84  const void *GetBytes() const;
85
86  size_t GetByteSize() const;
87
88  void SetBytes(const void *src, size_t src_len);
89
90  void SwapBytes(std::string &new_bytes);
91
92  void SetBytesFromCString(const char *cstr);
93
94  //------------------------------------------------------------------
95  // Static functions
96  //------------------------------------------------------------------
97  static const EventDataBytes *GetEventDataFromEvent(const Event *event_ptr);
98
99  static const void *GetBytesFromEvent(const Event *event_ptr);
100
101  static size_t GetByteSizeFromEvent(const Event *event_ptr);
102
103  static const ConstString &GetFlavorString();
104
105private:
106  std::string m_bytes;
107
108  DISALLOW_COPY_AND_ASSIGN(EventDataBytes);
109};
110
111class EventDataReceipt : public EventData {
112public:
113  EventDataReceipt() : EventData(), m_predicate(false) {}
114
115  ~EventDataReceipt() override {}
116
117  static const ConstString &GetFlavorString() {
118    static ConstString g_flavor("Process::ProcessEventData");
119    return g_flavor;
120  }
121
122  const ConstString &GetFlavor() const override { return GetFlavorString(); }
123
124  bool WaitForEventReceived(const Timeout<std::micro> &timeout = llvm::None) {
125    return m_predicate.WaitForValueEqualTo(true, timeout);
126  }
127
128private:
129  Predicate<bool> m_predicate;
130
131  void DoOnRemoval(Event *event_ptr) override {
132    m_predicate.SetValue(true, eBroadcastAlways);
133  }
134};
135
136//----------------------------------------------------------------------
137/// This class handles one or more StructuredData::Dictionary entries
138/// that are raised for structured data events.
139//----------------------------------------------------------------------
140
141class EventDataStructuredData : public EventData {
142public:
143  //------------------------------------------------------------------
144  // Constructors
145  //------------------------------------------------------------------
146  EventDataStructuredData();
147
148  EventDataStructuredData(const lldb::ProcessSP &process_sp,
149                          const StructuredData::ObjectSP &object_sp,
150                          const lldb::StructuredDataPluginSP &plugin_sp);
151
152  ~EventDataStructuredData() override;
153
154  //------------------------------------------------------------------
155  // Member functions
156  //------------------------------------------------------------------
157  const ConstString &GetFlavor() const override;
158
159  void Dump(Stream *s) const override;
160
161  const lldb::ProcessSP &GetProcess() const;
162
163  const StructuredData::ObjectSP &GetObject() const;
164
165  const lldb::StructuredDataPluginSP &GetStructuredDataPlugin() const;
166
167  void SetProcess(const lldb::ProcessSP &process_sp);
168
169  void SetObject(const StructuredData::ObjectSP &object_sp);
170
171  void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp);
172
173  //------------------------------------------------------------------
174  // Static functions
175  //------------------------------------------------------------------
176  static const EventDataStructuredData *
177  GetEventDataFromEvent(const Event *event_ptr);
178
179  static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr);
180
181  static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr);
182
183  static lldb::StructuredDataPluginSP
184  GetPluginFromEvent(const Event *event_ptr);
185
186  static const ConstString &GetFlavorString();
187
188private:
189  lldb::ProcessSP m_process_sp;
190  StructuredData::ObjectSP m_object_sp;
191  lldb::StructuredDataPluginSP m_plugin_sp;
192
193  DISALLOW_COPY_AND_ASSIGN(EventDataStructuredData);
194};
195
196//----------------------------------------------------------------------
197// lldb::Event
198//----------------------------------------------------------------------
199class Event {
200  friend class Listener;
201  friend class EventData;
202  friend class Broadcaster::BroadcasterImpl;
203
204public:
205  Event(Broadcaster *broadcaster, uint32_t event_type,
206        EventData *data = nullptr);
207
208  Event(Broadcaster *broadcaster, uint32_t event_type,
209        const lldb::EventDataSP &event_data_sp);
210
211  Event(uint32_t event_type, EventData *data = nullptr);
212
213  Event(uint32_t event_type, const lldb::EventDataSP &event_data_sp);
214
215  ~Event();
216
217  void Dump(Stream *s) const;
218
219  EventData *GetData() { return m_data_sp.get(); }
220
221  const EventData *GetData() const { return m_data_sp.get(); }
222
223  void SetData(EventData *new_data) { m_data_sp.reset(new_data); }
224
225  uint32_t GetType() const { return m_type; }
226
227  void SetType(uint32_t new_type) { m_type = new_type; }
228
229  Broadcaster *GetBroadcaster() const {
230    Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
231        m_broadcaster_wp.lock();
232    if (broadcaster_impl_sp)
233      return broadcaster_impl_sp->GetBroadcaster();
234    else
235      return nullptr;
236  }
237
238  bool BroadcasterIs(Broadcaster *broadcaster) {
239    Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
240        m_broadcaster_wp.lock();
241    if (broadcaster_impl_sp)
242      return broadcaster_impl_sp->GetBroadcaster() == broadcaster;
243    else
244      return false;
245  }
246
247  void Clear() { m_data_sp.reset(); }
248
249private:
250  // This is only called by Listener when it pops an event off the queue for
251  // the listener.  It calls the Event Data's DoOnRemoval() method, which is
252  // virtual and can be overridden by the specific data classes.
253
254  void DoOnRemoval();
255
256  // Called by Broadcaster::BroadcastEvent prior to letting all the listeners
257  // know about it update the contained broadcaster so that events can be
258  // popped off one queue and re-broadcast to others.
259  void SetBroadcaster(Broadcaster *broadcaster) {
260    m_broadcaster_wp = broadcaster->GetBroadcasterImpl();
261  }
262
263  Broadcaster::BroadcasterImplWP
264      m_broadcaster_wp;        // The broadcaster that sent this event
265  uint32_t m_type;             // The bit describing this event
266  lldb::EventDataSP m_data_sp; // User specific data for this event
267
268  DISALLOW_COPY_AND_ASSIGN(Event);
269  Event(); // Disallow default constructor
270};
271
272} // namespace lldb_private
273
274#endif // LLDB_UTILITY_EVENT_H
275