Bitcoin ABC  0.26.3
P2P Digital Currency
events.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_SUPPORT_EVENTS_H
6 #define BITCOIN_SUPPORT_EVENTS_H
7 
8 #include <ios>
9 #include <memory>
10 
11 #include <event2/event.h>
12 #include <event2/http.h>
13 
14 #define MAKE_RAII(type) \
15  /* deleter */ \
16  struct type##_deleter { \
17  void operator()(struct type *ob) { type##_free(ob); } \
18  }; \
19  /* unique ptr typedef */ \
20  typedef std::unique_ptr<struct type, type##_deleter> raii_##type
21 
22 MAKE_RAII(event_base);
23 MAKE_RAII(event);
24 MAKE_RAII(evhttp);
25 MAKE_RAII(evhttp_request);
26 MAKE_RAII(evhttp_connection);
27 
28 inline raii_event_base obtain_event_base() {
29  auto result = raii_event_base(event_base_new());
30  if (!result.get()) {
31  throw std::runtime_error("cannot create event_base");
32  }
33  return result;
34 }
35 
36 inline raii_event obtain_event(struct event_base *base, evutil_socket_t s,
37  short events, event_callback_fn cb, void *arg) {
38  return raii_event(event_new(base, s, events, cb, arg));
39 }
40 
41 inline raii_evhttp obtain_evhttp(struct event_base *base) {
42  return raii_evhttp(evhttp_new(base));
43 }
44 
45 inline raii_evhttp_request
46 obtain_evhttp_request(void (*cb)(struct evhttp_request *, void *), void *arg) {
47  return raii_evhttp_request(evhttp_request_new(cb, arg));
48 }
49 
50 inline raii_evhttp_connection
51 obtain_evhttp_connection_base(struct event_base *base, std::string host,
52  uint16_t port) {
53  auto result = raii_evhttp_connection(
54  evhttp_connection_base_new(base, nullptr, host.c_str(), port));
55  if (!result.get()) {
56  throw std::runtime_error("create connection failed");
57  }
58  return result;
59 }
60 
61 #endif // BITCOIN_SUPPORT_EVENTS_H
#define MAKE_RAII(type)
Definition: events.h:14
raii_evhttp obtain_evhttp(struct event_base *base)
Definition: events.h:41
raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg)
Definition: events.h:46
raii_evhttp_connection obtain_evhttp_connection_base(struct event_base *base, std::string host, uint16_t port)
Definition: events.h:51
raii_event obtain_event(struct event_base *base, evutil_socket_t s, short events, event_callback_fn cb, void *arg)
Definition: events.h:36
raii_event_base obtain_event_base()
Definition: events.h:28