// // mp::dispatch // // Copyright (C) 2008 FURUHASHI Sadayuki // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #ifndef MP_DISPATCH_H__ #define MP_DISPATCH_H__ #include "mp/event.h" #include "mp/functional.h" namespace mp { template class dispatch { public: typedef Data data_t; typedef function callback_t; public: dispatch(); inline int add(int fd, short event, callback_t callback) { return m_event.add(fd, event, callback); } MP_ARGS_BEGIN template inline int add(int fd, short event, callback_t callback, MP_ARGS_PARAMS) { return m_event.add(fd, event, callback, MP_ARGS_FUNC); } MP_ARGS_END inline int remove(int fd, short oldevent); inline int modify(int fd, short oldevent, short newevent); inline int modify(int fd, callback_t callback); inline int modify(int fd, callback_t callback, data_t data); inline int modify(int fd, short oldevent, short newevent, callback_t callback); inline int modify(int fd, short oldevent, short newevent, callback_t callback, data_t data); inline data_t& data(int fd); inline const data_t& data(int fd) const; int run(void); void end(void); private: struct cb_t { cb_t(callback_t c) : callback(c) {} MP_ARGS_BEGIN template cb_t(callback_t c, MP_ARGS_PARAMS) : data(MP_ARGS_FUNC), callback(c) {} MP_ARGS_END data_t data; callback_t callback; }; typedef event event_t; event_t m_event; volatile sig_atomic_t m_end_flag; }; template <> class dispatch { public: typedef function callback_t; public: dispatch(); int add(int fd, short event, callback_t callback) { return m_event.add(fd, event, callback); } int remove(int fd, short oldevent); int modify(int fd, short oldevent, short newevent); int modify(int fd, callback_t callback); int modify(int fd, short oldevent, short newevent, callback_t callback); int run(void); void end(void); private: struct cb_t { cb_t() {} cb_t(callback_t c) : callback(c) {} callback_t callback; }; typedef event event_t; event_t m_event; volatile sig_atomic_t m_end_flag; }; } // namespace mp #include "mp/dispatch_impl.h" #endif /* mp/dispatch.h */