-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallbackPool.h
66 lines (55 loc) · 1.59 KB
/
CallbackPool.h
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
namespace facebook {
namespace memcache {
namespace mcrouter {
/**
* Simple thread-safe class to handle pool of callbacks.
* One can register callback with 'subscribe' and call all registered callbacks
* with 'notify', order of calls in undefined. Exceptions in callbacks are
* caught and logged.
*
* @param Args arguments to pass to callbacks on 'notify'.
* NOTE: arguments would be copied for each callback. Use const reference
* or pointer type to avoid copy.
*/
template <typename... Args>
class CallbackPool {
private:
struct CallbackHandleImpl;
struct Data;
public:
/**
* Callback function for this CallbackPool.
*/
typedef std::function<void(Args...)> OnUpdateFunc;
/**
* Callback handle for this CallbackPool. Once it is destroyed corresponding
* callback will be unsubscribed.
*/
typedef std::unique_ptr<CallbackHandleImpl> CallbackHandle;
CallbackPool();
/**
* Adds one more callback function. This callback will be called
* on next 'notify' call. The callback is unsubscribed when returned
* CallbackHandle is destroyed.
*/
CallbackHandle subscribe(OnUpdateFunc callback);
/**
* Call all subscribed callbacks. Order of callbacks is undefined.
*/
void notify(Args... args);
private:
std::shared_ptr<Data> data_;
};
}
}
} // facebook::memcache::mcrouter
#include "CallbackPool-inl.h"