1 // NOTE: This file is in doxygen[1] format. Maybe you should try to run
2 // doxygen to get a better looking documentation ;)
4 // [1] http://www.stack.nl/~dimitri/doxygen/
10 @libevent is a popular API that provides a mechanism to execute a callback
11 function when a specific event occurs on a file descriptor or after a
12 timeout has been reached. Furthermore, @libevent also support callbacks due
13 to signals or regular timeouts.
15 @eventxx is a simple, direct, one-header inline C++ wrapper for @libevent.
16 Yes, it's just one header file, so if you want to use it you can just copy
17 the file to your project and you are set (well, you'll need to link to
20 It's designed to be as close to use to @libevent (without compromising
21 modern C++ programming techniques) and efficient (since all implementation
22 is trivial and inline, theoretically, it imposes no overhead at all) as
25 Please, visit the @eventxx website for the latest version of this
30 You can always get the <a href="@home/releases/eventxx.tar.gz">current
31 release</a> from the <a href="@home/releases/">release directory</a> or
32 grab the <a href="@gitweb?a=snapshot">most up to date sources</a> from
33 the <a href="http://git.or.cz/">git</a> repository.
35 You can also take a look the the <a href="@gitweb">eventxx gitweb</a>
36 interface to see the latest changes online or subscribe to its
37 <a href="@gitweb?a=atom">Atom feed</a> to follow the development.
39 For more information, bug reports or support, please visit the
40 <a href="@redmine">eventxx project</a>.
45 You probably should read @libevent documentation to get started or at least
46 just for reference, although @eventxx is pretty simple so you can jump right
47 into the \ref Example section (or the example list) and write a working
48 program without much trouble.
50 This wrapper was designed to be used just like @libevent, but with C++ style
51 syntax sugar (or poison, depends on your point of view ;) and goodies. The
52 main difference to libevent is you always have to instance a
53 eventxx::dispatcher to get an event loop. There is no implicit global event
54 loop. This adds just an extra line of code for single threaded applications
55 and makes things much more simpler, so I hope nobody complains about it ;).
56 See eventxx::dispatcher documentation for more details.
58 You can use use the same plain functions callbacks @libevent use or the other
59 kind of function objects (see @ref events section for details on event
62 @eventxx uses @ref exceptions to report errors. All functions have exception
63 specifications, so it's easy to find out what to expect. See @ref exceptions
64 section for more detail.
66 A @c timespec abstraction is provided as eventxx::time for convenient
67 argument passing. Even more, it's a @c timespec itself, with some convenient
68 methods for accessing the attributes with pritier names. And even more,
69 @eventxx is such a direct mapping that all eventxx::event's are @libevent
70 event structs too, so theoretically you can pass a eventxx::event to
71 @libevent C functions without much trouble. eventxx::dispatcher is the only
72 class that is not derived from @libevent struct (@c event_base) because this
73 struct it's not defined on the libevent header (just declared).
75 Maybe you shouldn't know this implementation details to keep the abstraction,
76 but this is a basic design goal of this wrapper so there is not much chance
77 that this changes in the future (but use this knowledge with care, you have
90 eventxx::dispatcher& d;
92 handler(eventxx::dispatcher& d): d(d), i(0) {}
93 void operator() (int signum, eventxx::type event)
95 std::cout << ++i << " interrupts, ";
96 if (i < 5) std::cout << "keep going...\n";
99 std::cout << "done!\n";
105 void timer_handler(int, short, void*)
107 std::cout << "Press Ctrl-C 5 times to quit.\n";
112 eventxx::dispatcher d;
114 eventxx::ctimer t(timer_handler);
115 eventxx::signal< handler > e(SIGINT, h);
116 d.add(t, eventxx::time(1)); // 1 second
123 You can see more examples on the test directory of the distribution or on the
124 examples related page.
129 I think the library is stable now. The library has no support for buffered
130 events yet, but patches are welcome. It doesn't support the HTTP stuff, and
131 probably never will because that has nothing to do with event handling.
133 @libevent had a memory leak before version 1.3b (before 1.2 it didn't even
134 had a way free that memory, from version 1.2 to 1.3a, if you tried to free
135 the memory the program @c abort() because a failed assertion). Because of
136 that, there is a way to disable the @link eventxx::dispatcher::~dispatcher()
137 dispatcher destructor @endlink (which calls the inexistent/broken
138 @c event_base_free() function in the broken versions). So if you use a
139 @libevent version previous to 1.3b, you have to compile your programs
140 defining the @c EVENTXX_NO_EVENT_BASE_FREE macro.
142 If something is broken it would be really easy to fix because @eventxx is
143 just a simple wrapper around @libevent. So, please try it out, and if you
144 have any problems, <a href="mailto:llucax+eventxx@gmail.com">drop me an
145 e-mail</a> and and I'll fix it ASAP (or provide a patch and you will be my
148 If you use this library, please drop me an e-mail with your thoughts, or
149 simply saying "I use it", so I can keep track of how many people really use
152 @author Leandro Lucarella <llucax+eventxx@gmail.com>
157 This program is under the <a href="http://auriga.wearlab.de/~alb/bola/">BOLA
158 License</a> (see the license website or the
159 <a href="@gitweb?a=blob_plain;f=LICENSE">LICENSE</a>
160 file itself for more details, it's very short and it basically says it's
165 /** @example c-way.cpp
166 This is a simple example illustrating the usage with C-like callback
170 /** @example functor-way.cpp
171 This is a simple example illustrating the usage with function object
175 /** @example wrapped-functor-way.cpp
176 This is a simple example illustrating the usage with an arbitrary member
177 function as an event handler callbacks.
180 /** @example mixed-way.cpp
181 This is a simple example illustrating the usage with a mix of C-like callbacks
182 and function object callbacks.
185 /** @example bench.cpp
186 This is a benchmark example, extracted from libevent and ported to eventxx.
189 /** @example prio-test.cpp
190 This is a priority usage example.
193 /** @example test-time.cpp
194 This is a timer usage example ported from libevent.
197 /** @example test-eof.cpp
198 This is some kind of test of EOF ported from libevent.
201 /** @example test-weof.cpp
202 Another test of EOF ported from libevent.
205 /** @example trivial.cpp
206 This is the most trivial example.