MPD: system/EPollFD.hxx Source File

MPD 0.20.15

EPollFD.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_EPOLL_FD_HXX
21 #define MPD_EPOLL_FD_HXX
22
23 #include <assert.h>
24 #include <sys/epoll.h>
25 #include <unistd.h>
26 #include <stdint.h>
27
28 #include "check.h"
29
30 struct epoll_event;
31
37 class EPollFD {
38 const int fd;
39
40 public:
41 EPollFD();
42
43 ~EPollFD() {
44 assert(fd >= 0);
45
46 ::close(fd);
47 }
48
49 EPollFD(const EPollFD &other) = delete;
50 EPollFD &operator=(const EPollFD &other) = delete;
51
52 int Wait(epoll_event *events, int maxevents, int timeout) {
53 return ::epoll_wait(fd, events, maxevents, timeout);
54 }
55
56 bool Control(int op, int _fd, epoll_event *event) {
57 return ::epoll_ctl(fd, op, _fd, event) >= 0;
58 }
59
60 bool Add(int _fd, uint32_t events, void *ptr) {
61 epoll_event e;
62 e.events = events;
63 e.data.ptr = ptr;
64
65 return Control(EPOLL_CTL_ADD, _fd, &e);
66 }
67
68 bool Modify(int _fd, uint32_t events, void *ptr) {
69 epoll_event e;
70 e.events = events;
71 e.data.ptr = ptr;
72
73 return Control(EPOLL_CTL_MOD, _fd, &e);
74 }
75
76 bool Remove(int _fd) {
77 return Control(EPOLL_CTL_DEL, _fd, nullptr);
78 }
79 };
80
81 #endif
EPollFD::~EPollFD
~EPollFD()
Definition: EPollFD.hxx:43
check.h
EPollFD::Wait
int Wait(epoll_event *events, int maxevents, int timeout)
Definition: EPollFD.hxx:52
EPollFD::Control
bool Control(int op, int _fd, epoll_event *event)
Definition: EPollFD.hxx:56
EPollFD
A class that wraps Linux epoll.
Definition: EPollFD.hxx:37
EPollFD::Add
bool Add(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:60
EPollFD::Modify
bool Modify(int _fd, uint32_t events, void *ptr)
Definition: EPollFD.hxx:68
e
int e
Definition: Log.hxx:115
EPollFD::operator=
EPollFD & operator=(const EPollFD &other)=delete
EPollFD::EPollFD
EPollFD()
EPollFD::Remove
bool Remove(int _fd)
Definition: EPollFD.hxx:76