31 bool operator()(
const Value & p1 ,
const Value & p2 )
33 return p1.first < p2.first ;
47 G_ASSERT( handler !=
nullptr ) ;
if( handler ==
nullptr ) return ;
48 G_ASSERT( eh !=
nullptr ) ;
if( eh ==
nullptr ) return ;
50 G_DEBUG(
"GNet::EventHandlerList::add: " << m_type <<
"-list: " <<
"adding " << fd << (m_lock?
" (pending)":
"") ) ;
51 addImp( m_lock?m_pending_list:m_list , fd , handler , eh ) ;
56 typedef std::pair<List::iterator,List::iterator> Range ;
57 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd) , fdless() ) ;
58 if( range.first == range.second )
59 list.insert( range.first , List::value_type(fd,handler,eh) ) ;
61 *range.first = List::value_type(fd,handler,eh) ;
66 G_DEBUG(
"GNet::EventHandlerList::remove: " << m_type <<
"-list: " <<
"removing " << fd ) ;
69 if( disable(m_list,fd) ) m_has_garbage = true ;
70 disable( m_pending_list , fd ) ;
74 remove( m_list , fd ) ;
78 bool GNet::EventHandlerList::disable( List & list ,
Descriptor fd )
80 typedef std::pair<List::iterator,List::iterator> Range ;
81 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd) , fdless() ) ;
82 const bool found = range.first != range.second ;
85 (*range.first).second =
nullptr ;
92 typedef std::pair<List::iterator,List::iterator> Range ;
93 Range range = std::equal_range( list.begin() , list.end() , List::value_type(fd) , fdless() ) ;
94 const bool found = range.first != range.second ;
96 list.erase( range.first ) ;
102 disarm( m_list , eh ) ;
103 disarm( m_pending_list , eh ) ;
108 for( List::iterator p = list.begin() ; p != list.end() ; ++p )
110 if( (*p).third == eh )
111 (*p).third = nullptr ;
117 typedef std::pair<List::const_iterator,List::const_iterator> Range ;
118 Range range = std::equal_range( m_list.begin() , m_list.end() , List::value_type(fd) , fdless() ) ;
119 return range.first == range.second ?
Iterator(m_list.end(),m_list.end()) :
Iterator(range.first,m_list.end()) ;
124 typedef std::pair<List::const_iterator,List::const_iterator> Range ;
125 Range range = std::equal_range( m_pending_list.begin() , m_pending_list.end() , List::value_type(fd) , fdless() ) ;
126 if( range.first == range.second )
127 range = std::equal_range( m_list.begin() , m_list.end() , List::value_type(fd) , fdless() ) ;
128 return range.first != range.second && (*range.first).second !=
nullptr ;
138 G_ASSERT( m_lock != 0U ) ;
140 bool updated = false ;
143 updated = !m_pending_list.empty() || m_has_garbage ;
150 void GNet::EventHandlerList::commitPending()
152 const List::iterator end = m_pending_list.end() ;
153 for( List::iterator p = m_pending_list.begin() ; p != end ; ++p )
155 if( (*p).second !=
nullptr )
157 G_DEBUG(
"GNet::EventHandlerList::commitPending: " << m_type <<
"-list: " <<
"commiting " << (*p).first ) ;
158 addImp( m_list , (*p).first , (*p).second , (*p).third ) ;
161 m_pending_list.clear() ;
164 void GNet::EventHandlerList::collectGarbage()
168 m_has_garbage = false ;
169 for( List::iterator p = m_list.begin() ; p != m_list.end() ; )
171 if( (*p).second ==
nullptr )
172 p = m_list.erase( p ) ;
181 void GNet::EventHandlerList::Iterator::raiseEvent(
void (EventHandler::*method)() )
185 if( m_p != m_end && handler() !=
nullptr )
186 (handler()->*method)() ;
188 catch( std::exception & e )
190 if( m_p != m_end && eh() != nullptr )
197 void GNet::EventHandlerList::Iterator::raiseEvent(
void (EventHandler::*method)(EventHandler::Reason) , EventHandler::Reason reason )
201 if( m_p != m_end && handler() !=
nullptr )
202 (handler()->*method)( reason ) ;
204 catch( std::exception & e )
206 if( m_p != m_end && eh() != nullptr )
215 GNet::EventHandlerList::Lock::Lock( EventHandlerList & list ,
bool * invalid_p ) :
217 m_invalid_p(invalid_p)
222 GNet::EventHandlerList::Lock::~Lock()
224 if( m_list.unlock() && m_invalid_p != nullptr )
225 *m_invalid_p =
true ;
Iterator find(Descriptor fd) const
Finds an entry in the list.
A class that encapsulates a network socket file descriptor and an associated windows event handle...
void disarm(ExceptionHandler *)
Resets any matching ExceptionHandler pointers.
void lock()
To be called at the start of an begin()/end() iteration if the list might change during the iteration...
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
bool unlock()
Called at the end of a begin()/end() iteration to match a call to lock().
bool contains(Descriptor fd) const
Returns true if the list, taking account of any pending changes, contains the given descriptor...
A base class for classes that handle asynchronous events from the event loop.
void add(Descriptor fd, EventHandler *handler, ExceptionHandler *)
Adds a file-descriptor/handler tuple to the list.
void remove(Descriptor fd)
Removes a file-descriptor from the list.
A tuple for GNet::EventHandlerList.
virtual void onException(std::exception &)=0
Called by the event loop when an exception is thrown out of an event loop callback.
An iterator for GNet::EventHandlerList.
EventHandlerList(const std::string &type)
Constructor.