| 1 | /** |
|---|
| 2 | * Copyright (c) 2008 Bert JW Regeer <xistence@0x58.com> |
|---|
| 3 | * |
|---|
| 4 | * Permission to use, copy, modify, and distribute this software for any |
|---|
| 5 | * purpose with or without fee is hereby granted, provided that the above |
|---|
| 6 | * copyright notice and this permission notice appear in all copies. |
|---|
| 7 | * |
|---|
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|---|
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|---|
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|---|
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|---|
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|---|
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|---|
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 15 | * |
|---|
| 16 | **/ |
|---|
| 17 | |
|---|
| 18 | #ifndef x58UNIX_UEVENT_H |
|---|
| 19 | #define x58UNIX_UEVENT_H |
|---|
| 20 | |
|---|
| 21 | #include <iostream> |
|---|
| 22 | #include <string> |
|---|
| 23 | #include <vector> |
|---|
| 24 | |
|---|
| 25 | namespace x58unix { |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | TODO Implement other types of events. Went with read and write for now, for simplicity. |
|---|
| 29 | **/ |
|---|
| 30 | |
|---|
| 31 | class Event { |
|---|
| 32 | public: |
|---|
| 33 | // Always require a constructor and a destructor |
|---|
| 34 | Event (); |
|---|
| 35 | virtual ~Event() = 0; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | Function definitions for the events, contains the following arguments |
|---|
| 39 | |
|---|
| 40 | fd File descriptor for the event, if event does not take a file descriptor it can also be an ident |
|---|
| 41 | oneshot As soon as the event fires, it is removed from the event mechanism, and the callee will have to re-add it to the queue |
|---|
| 42 | cb Call back function pointer. |
|---|
| 43 | **/ |
|---|
| 44 | |
|---|
| 45 | virtual bool read(int fd, bool oneshot, void * cb) = 0; |
|---|
| 46 | virtual bool write(int fd, bool oneshot, void * cb) = 0; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | Once called, waits forever until an event occurs, calls the callback for said event and goes back into a loop. |
|---|
| 50 | **/ |
|---|
| 51 | |
|---|
| 52 | virtual void dispatch() = 0; |
|---|
| 53 | |
|---|
| 54 | private: |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | namespace eventImplementation { |
|---|
| 58 | template<class E> x58unix::Event* event_factory() { |
|---|
| 59 | return new E; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | typedef x58unix::Event* (*event_fptr)(void); |
|---|
| 63 | |
|---|
| 64 | class EventRegistry { |
|---|
| 65 | private: |
|---|
| 66 | typedef struct Event_t |
|---|
| 67 | { |
|---|
| 68 | std::string name; |
|---|
| 69 | int weight; |
|---|
| 70 | event_fptr fptr; |
|---|
| 71 | } event_t; |
|---|
| 72 | |
|---|
| 73 | std::vector<event_t> _roll; |
|---|
| 74 | |
|---|
| 75 | public: |
|---|
| 76 | static EventRegistry& retrieve(); |
|---|
| 77 | event_fptr search(std::string); |
|---|
| 78 | void enroll(std::string, int, event_fptr); |
|---|
| 79 | |
|---|
| 80 | friend std::ostream& operator<< (std::ostream&, const EventRegistry&); |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | class EventRegister { |
|---|
| 84 | public: |
|---|
| 85 | EventRegister(std::string, int, event_fptr); |
|---|
| 86 | }; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | #define REGISTER_EVENT(ename, weight, event) x58unix::eventImplementation::EventRegister _event_register_##ename(#ename, weight, &x58unix::eventImplementation::event_factory<event>); |
|---|
| 92 | #endif /* x58UNIX_UEVENT_H */ |
|---|