]>
Commit | Line | Data |
---|---|---|
ab3e8a6c BH |
1 | #include <boost/function.hpp> |
2 | #include <boost/any.hpp> | |
3 | #include <map> | |
4 | #include <stdexcept> | |
5 | #include <string> | |
6 | ||
7 | class FDMultiplexerException : public std::runtime_error | |
8 | { | |
9 | public: | |
10 | FDMultiplexerException(const std::string& str) : std::runtime_error(str) | |
11 | {} | |
12 | }; | |
13 | ||
14 | class FDMultiplexer | |
15 | { | |
16 | protected: | |
17 | typedef boost::function< void(int, boost::any&) > callbackfunc_t; | |
18 | struct Callback | |
19 | { | |
20 | callbackfunc_t d_callback; | |
21 | boost::any d_parameter; | |
22 | }; | |
23 | ||
24 | public: | |
25 | FDMultiplexer() : d_inrun(false) | |
26 | {} | |
27 | virtual ~FDMultiplexer() | |
28 | {} | |
29 | ||
30 | virtual int run(struct timeval* tv=0) = 0; | |
31 | ||
32 | virtual void addReadFD(int fd, callbackfunc_t toDo, boost::any parameter=boost::any()) | |
33 | { | |
34 | this->addFD(d_inrun ? d_newReadCallbacks : d_readCallbacks, fd, toDo, parameter); | |
35 | } | |
36 | ||
37 | virtual void addWriteFD(int fd, callbackfunc_t toDo, boost::any parameter=boost::any()) | |
38 | { | |
39 | this->addFD(d_inrun ? d_newWriteCallbacks : d_writeCallbacks, fd, toDo, parameter); | |
40 | } | |
41 | ||
42 | virtual void removeReadFD(int fd) | |
43 | { | |
44 | this->removeFD(d_inrun ? d_newReadCallbacks : d_readCallbacks, fd); | |
45 | } | |
46 | virtual void removeWriteFD(int fd) | |
47 | { | |
48 | this->removeFD(d_inrun ? d_newWriteCallbacks : d_writeCallbacks, fd); | |
49 | } | |
50 | ||
51 | virtual boost::any& getReadParameter(int fd) | |
52 | { | |
53 | return d_readCallbacks[fd].d_parameter; | |
54 | } | |
55 | ||
56 | protected: | |
57 | typedef std::map<int, Callback> callbackmap_t; | |
58 | callbackmap_t d_readCallbacks, d_writeCallbacks; | |
59 | callbackmap_t d_newReadCallbacks, d_newWriteCallbacks; | |
60 | ||
61 | virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, boost::any parameter)=0; | |
62 | virtual void removeFD(callbackmap_t& cbmap, int fd)=0; | |
63 | bool d_inrun; | |
64 | ||
65 | }; | |
66 | ||
67 | class SelectFDMultiplexer : public FDMultiplexer | |
68 | { | |
69 | public: | |
70 | SelectFDMultiplexer() | |
71 | {} | |
72 | virtual ~SelectFDMultiplexer() | |
73 | {} | |
74 | ||
75 | virtual int run(struct timeval* tv=0); | |
76 | ||
77 | virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, boost::any parameter); | |
78 | virtual void removeFD(callbackmap_t& cbmap, int fd); | |
79 | ||
80 | private: | |
81 | }; | |
82 |