LCOV - code coverage report
Current view: top level - src/support - events.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 78.3 % 23 18
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 2 2
Branches: 42.9 % 28 12

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2016-2018 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) {\
      18                 :             :         type##_free(ob);\
      19                 :             :     }\
      20                 :             : };\
      21                 :             : /* unique ptr typedef */\
      22                 :             : typedef std::unique_ptr<struct type, type##_deleter> raii_##type
      23                 :             : 
      24                 :         486 : MAKE_RAII(event_base);
      25                 :           2 : MAKE_RAII(event);
      26                 :           0 : MAKE_RAII(evhttp);
      27                 :           4 : MAKE_RAII(evhttp_request);
      28                 :         483 : MAKE_RAII(evhttp_connection);
      29                 :             : 
      30                 :        1464 : inline raii_event_base obtain_event_base() {
      31         [ -  + ]:        1464 :     auto result = raii_event_base(event_base_new());
      32         [ -  + ]:        1464 :     if (!result.get())
      33         [ #  # ]:           0 :         throw std::runtime_error("cannot create event_base");
      34                 :        1464 :     return result;
      35                 :           0 : }
      36                 :             : 
      37                 :           2 : inline raii_event obtain_event(struct event_base* base, evutil_socket_t s, short events, event_callback_fn cb, void* arg) {
      38   [ +  -  +  -  :           2 :     return raii_event(event_new(base, s, events, cb, arg));
             +  -  +  - ]
      39                 :             : }
      40                 :             : 
      41                 :         978 : inline raii_evhttp obtain_evhttp(struct event_base* base) {
      42   [ +  -  -  + ]:         978 :     return raii_evhttp(evhttp_new(base));
      43                 :             : }
      44                 :             : 
      45                 :         483 : inline raii_evhttp_request obtain_evhttp_request(void(*cb)(struct evhttp_request *, void *), void *arg) {
      46   [ +  -  -  + ]:         483 :     return raii_evhttp_request(evhttp_request_new(cb, arg));
      47                 :             : }
      48                 :             : 
      49                 :         483 : inline raii_evhttp_connection obtain_evhttp_connection_base(struct event_base* base, std::string host, uint16_t port) {
      50         [ -  + ]:         483 :     auto result = raii_evhttp_connection(evhttp_connection_base_new(base, nullptr, host.c_str(), port));
      51         [ -  + ]:         483 :     if (!result.get())
      52         [ #  # ]:           0 :         throw std::runtime_error("create connection failed");
      53                 :         483 :     return result;
      54                 :           0 : }
      55                 :             : 
      56                 :             : #endif // BITCOIN_SUPPORT_EVENTS_H
        

Generated by: LCOV version 2.0-1