-
Notifications
You must be signed in to change notification settings - Fork 478
Expand file tree
/
Copy pathevents.hh
More file actions
74 lines (61 loc) · 1.95 KB
/
Copy pathevents.hh
File metadata and controls
74 lines (61 loc) · 1.95 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
70
71
72
73
74
/* Copyright (c) 2019 The node-webrtc project authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license that can be found
* in the LICENSE.md file in the root of the source tree. All contributing
* project authors may be found in the AUTHORS file in the root of the source
* tree.
*/
#pragma once
#include <functional>
#include <memory>
namespace node_webrtc {
/**
* Event represents an event that can be dispatched to a target.
* @tparam T the target type
*/
template <typename T> class Event {
public:
Event(const Event &) = delete;
Event(Event &&) = delete;
Event &operator=(const Event &) = delete;
Event &operator=(Event &&) = delete;
Event() = default;
virtual ~Event() = default;
/**
* Dispatch the Event to the target.
* @param target the target to dispatch to
*/
virtual void Dispatch(T &) {
// Do nothing.
}
static std::unique_ptr<Event<T>> Create() {
return std::unique_ptr<Event<T>>(new Event<T>());
}
};
template <typename F, typename T> class Callback : public Event<T> {
public:
void Dispatch(T &) override { _callback(); }
static std::unique_ptr<Callback<F, T>> Create(F callback) {
return std::unique_ptr<Callback<F, T>>(new Callback(std::move(callback)));
}
private:
explicit Callback(F callback) : _callback(std::move(callback)) {}
F _callback;
};
template <typename T, typename F>
static std::unique_ptr<Callback<F, T>> CreateCallback(F callback) {
return Callback<F, T>::Create(std::move(callback));
}
template <typename T> class Callback1 : public Event<T> {
public:
void Dispatch(T &target) override { _callback(target); }
static std::unique_ptr<Callback1<T>>
Create(std::function<void(T &)> callback) {
return std::unique_ptr<Callback1<T>>(new Callback1(std::move(callback)));
}
private:
explicit Callback1(const std::function<void(T &)> &callback)
: _callback(callback) {}
std::function<void(T &)> _callback;
};
} // namespace node_webrtc