summaryrefslogtreecommitdiffstats
path: root/client/events_loop.h
blob: 50a83e1d1800e22dbce8dc5e6fa625c513cfbe36 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_EVENTS_LOOP
#define _H_EVENTS_LOOP

#include "common.h"
#include "events_loop_p.h"

class EventSource;

class EventsLoop: public EventsLoop_p {
public:
    class Trigger;
    class Socket;
    class File;

    EventsLoop();
    virtual ~EventsLoop();

    void add_trigger(Trigger& trigger);
    void remove_trigger(Trigger& trigger);
    void add_socket(Socket& socket);
    void remove_socket(Socket& socket);
    void add_file(File& file);
    void remove_file(File& file);
    void run();
    // FIXME: temporary - need to adjust the loop for the main thread
    void run_once(int timeout_milli = INFINITE);
};

class EventSource {
public:
    virtual ~EventSource() {}
    virtual void on_event() = 0;

private:
    virtual void action() {on_event();}

    friend class EventsLoop;
};

class EventsLoop::Trigger: public EventSource, private Trigger_p {
public:
    Trigger();
    virtual ~Trigger();
    virtual void trigger();
    virtual void reset();

private:
    virtual void action();

    friend class EventsLoop;
};

class EventsLoop::Socket: public EventSource {
protected:
    virtual int get_socket() = 0;

    friend class EventsLoop;
};


class EventsLoop::File: public EventSource {
protected:
    virtual int get_fd() = 0;

    friend class EventsLoop;
};

#endif