geventhandler.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_EVENT_HANDLER_H
22 #define G_EVENT_HANDLER_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gdatetime.h"
27 #include "gdescriptor.h"
28 #include <map>
29 #include <string>
30 
32 namespace GNet
33 {
34  class EventHandler ;
35  class EventHandlerList ;
36  class TimerList ;
37 }
38 
55 {
56 public:
57  virtual ~EventHandler() ;
59 
60  virtual void readEvent() ;
63 
64  virtual void writeEvent() ;
67 
68  virtual void exceptionEvent() ;
72 
73  virtual void onException( std::exception & ) = 0 ;
87 
88 private:
89  void operator=( const EventHandler & ) ; // not implemented
90 } ;
91 
97 {
98 public:
99  typedef std::map<Descriptor,EventHandler*> Map ;
100  typedef Map::const_iterator Iterator ;
101 
102 public:
103  explicit EventHandlerList( const std::string & type ) ;
106 
107  void add( Descriptor fd , EventHandler * handler ) ;
110 
111  void remove( Descriptor fd ) ;
113 
114  bool contains( Descriptor fd ) const ;
117 
121 
122  void lock() ;
125 
126  void unlock() ;
128 
129  Iterator begin() const ;
131 
132  Iterator end() const ;
134 
135  static Descriptor fd( Iterator i ) ;
137 
138  static EventHandler * handler( Iterator i ) ;
142 
143 private:
145  void operator=( const EventHandlerList & ) ;
146  void collectGarbage() ;
147 
148 private:
149  std::string m_type ;
150  Map m_map ;
151  unsigned int m_lock ;
152  bool m_has_garbage ;
153 } ;
154 
155 inline
157 {
158  return (*i).first ;
159 }
160 
161 inline
163 {
164  return (*i).second ;
165 }
166 
167 #endif
168 
Network classes.
Iterator begin() const
Returns a forward iterator.
virtual void readEvent()
Called for a read event.
A network file descriptor.
Definition: gdescriptor.h:37
void lock()
Called at the start of an iteration which might change the list.
std::map< Descriptor, EventHandler * > Map
Definition: geventhandler.h:99
virtual void onException(std::exception &)=0
Called when an exception is thrown out of readEvent(), writeEvent() or exceptionEvent().
virtual void writeEvent()
Called for a write event.
Map::const_iterator Iterator
bool contains(Descriptor fd) const
Returns true if the list contains the given file-descriptor.
static Descriptor fd(Iterator i)
Returns the iterator's file descriptor.
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
static EventHandler * handler(Iterator i)
Returns the iterator's handler.
void add(Descriptor fd, EventHandler *handler)
Adds a file-descriptor/handler pair to the list.
void unlock()
Called at the end of an iteration.
Iterator end() const
Returns an end iterator.
EventHandlerList(const std::string &type)
Constructor.
EventHandler * find(Descriptor fd)
Finds the handler associated with the given file descriptor.
virtual ~EventHandler()
Destructor.
virtual void exceptionEvent()
Called for an exception event.
A class which can be used in the implemention of classes derived from GNet::EventLoop.
Definition: geventhandler.h:96