| | 1 | // |
| | 2 | // mp::dispatch |
| | 3 | // |
| | 4 | // Copyright (C) 2008 FURUHASHI Sadayuki |
| | 5 | // |
| | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | 7 | // you may not use this file except in compliance with the License. |
| | 8 | // You may obtain a copy of the License at |
| | 9 | // |
| | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
| | 11 | // |
| | 12 | // Unless required by applicable law or agreed to in writing, software |
| | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
| | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 15 | // See the License for the specific language governing permissions and |
| | 16 | // limitations under the License. |
| | 17 | // |
| | 18 | |
| 71 | | |
| 72 | | |
| 73 | | template <typename Data> |
| 74 | | dispatch<Data>::dispatch() {} |
| 75 | | dispatch<void>::dispatch() {} |
| 76 | | |
| 77 | | template <typename Data> |
| 78 | | int dispatch<Data>::remove(int fd, short oldevent) |
| 79 | | { |
| 80 | | return m_event.remove(fd, oldevent); |
| 81 | | } |
| 82 | | int dispatch<void>::remove(int fd, short oldevent) |
| 83 | | { |
| 84 | | return m_event.remove(fd, oldevent); |
| 85 | | } |
| 86 | | |
| 87 | | template <typename Data> |
| 88 | | int dispatch<Data>::modify(int fd, short oldevent, short newevent) |
| 89 | | { |
| 90 | | return m_event.modify(fd, oldevent, newevent); |
| 91 | | } |
| 92 | | int dispatch<void>::modify(int fd, short oldevent, short newevent) |
| 93 | | { |
| 94 | | return m_event.modify(fd, oldevent, newevent); |
| 95 | | } |
| 96 | | |
| 97 | | template <typename Data> |
| 98 | | int dispatch<Data>::modify(int fd, callback_t callback) |
| 99 | | { |
| 100 | | m_event.data(fd).callback = callback; |
| 101 | | return 0; |
| 102 | | } |
| 103 | | int dispatch<void>::modify(int fd, callback_t callback) |
| 104 | | { |
| 105 | | m_event.data(fd).callback = callback; |
| 106 | | return 0; |
| 107 | | } |
| 108 | | |
| 109 | | template <typename Data> |
| 110 | | int dispatch<Data>::modify(int fd, callback_t callback, data_t data) |
| 111 | | { |
| 112 | | m_event.data(fd).callback = callback; |
| 113 | | m_event.data(fd).data = data; |
| 114 | | return 0; |
| 115 | | } |
| 116 | | |
| 117 | | template <typename Data> |
| 118 | | int dispatch<Data>::modify(int fd, short oldevent, short newevent, callback_t callback) |
| 119 | | { |
| 120 | | m_event.data(fd).callback = callback; |
| 121 | | return modify(fd, oldevent, newevent); |
| 122 | | } |
| 123 | | int dispatch<void>::modify(int fd, short oldevent, short newevent, callback_t callback) |
| 124 | | { |
| 125 | | m_event.data(fd).callback = callback; |
| 126 | | return modify(fd, oldevent, newevent); |
| 127 | | } |
| 128 | | |
| 129 | | template <typename Data> |
| 130 | | int dispatch<Data>::modify(int fd, short oldevent, short newevent, callback_t callback, data_t data) |
| 131 | | { |
| 132 | | m_event.data(fd).callback = callback; |
| 133 | | m_event.data(fd).data = data; |
| 134 | | return modify(fd, oldevent, newevent); |
| 135 | | } |
| 136 | | |
| 137 | | template <typename Data> |
| 138 | | Data& dispatch<Data>::data(int fd) |
| 139 | | { |
| 140 | | return m_event.data(fd).data; |
| 141 | | } |
| 142 | | |
| 143 | | template <typename Data> |
| 144 | | const Data& dispatch<Data>::data(int fd) const |
| 145 | | { |
| 146 | | return m_event.data(fd).data; |
| 147 | | } |
| 148 | | |
| 149 | | template <typename Data> |
| 150 | | int dispatch<Data>::run(void) |
| 151 | | { |
| 152 | | cb_t* pcb; |
| 153 | | int fd; |
| 154 | | short event; |
| 155 | | int ret; |
| 156 | | while(1) { |
| 157 | | while( m_event.next(&fd, &event, &pcb) ) { |
| 158 | | ret = pcb->callback(fd, event, pcb->data); |
| 159 | | if( ret != 0 ) { return ret; } |
| 160 | | } |
| 161 | | if( (ret = m_event.wait()) < 0 ) { return ret; } |
| 162 | | } |
| 163 | | } |
| 164 | | int dispatch<void>::run(void) |
| 165 | | { |
| 166 | | cb_t* pcb; |
| 167 | | int fd; |
| 168 | | short event; |
| 169 | | int ret; |
| 170 | | while(1) { |
| 171 | | while( m_event.next(&fd, &event, &pcb) ) { |
| 172 | | ret = pcb->callback(fd, event); |
| 173 | | if( ret != 0 ) { return ret; } |
| 174 | | } |
| 175 | | if( (ret = m_event.wait()) < 0 ) { return ret; } |
| 176 | | } |
| 177 | | } |