- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
54 class GNet::EventHandler
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
96 class GNet::EventHandlerList
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
118 EventHandler * find( Descriptor fd ) ;
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:
144 EventHandlerList( const EventHandlerList & ) ;
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
156 GNet::Descriptor GNet::EventHandlerList::fd( Iterator i )
157 {
158 return (*i).first ;
159 }
160
161 inline
162 GNet::EventHandler * GNet::EventHandlerList::handler( Iterator i )
163 {
164 return (*i).second ;
165 }
166
167 #endif
168
GNet
Network classes.
GNet::EventHandlerList::begin
Iterator begin() const
Returns a forward iterator.
Definition: geventhandler.cpp:59
GNet::EventHandler::readEvent
virtual void readEvent()
Called for a read event.
Definition: geventhandler.cpp:35
GNet::Descriptor
A network file descriptor.
Definition: gdescriptor.h:37
GNet::EventHandlerList::lock
void lock()
Called at the start of an iteration which might change the list.
Definition: geventhandler.cpp:106
GNet::EventHandlerList::Map
std::map< Descriptor, EventHandler * > Map
Definition: geventhandler.h:99
GNet::EventHandler::onException
virtual void onException(std::exception &)=0
Called when an exception is thrown out of readEvent(), writeEvent() or exceptionEvent().
GNet::EventHandler::writeEvent
virtual void writeEvent()
Called for a write event.
Definition: geventhandler.cpp:40
GNet::EventHandlerList::Iterator
Map::const_iterator Iterator
Definition: geventhandler.h:100
GNet::EventHandlerList::contains
bool contains(Descriptor fd) const
Returns true if the list contains the given file-descriptor.
Definition: geventhandler.cpp:69
GNet::EventHandlerList::fd
static Descriptor fd(Iterator i)
Returns the iterator's file descriptor.
Definition: geventhandler.h:156
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
GNet::EventHandlerList::handler
static EventHandler * handler(Iterator i)
Returns the iterator's handler.
Definition: geventhandler.h:162
GNet::EventHandlerList::add
void add(Descriptor fd, EventHandler *handler)
Adds a file-descriptor/handler pair to the list.
Definition: geventhandler.cpp:80
GNet::EventHandlerList::unlock
void unlock()
Called at the end of an iteration.
Definition: geventhandler.cpp:111
gdescriptor.h
gdatetime.h
GNet::EventHandlerList::end
Iterator end() const
Returns an end iterator.
Definition: geventhandler.cpp:64
GNet::EventHandlerList::EventHandlerList
EventHandlerList(const std::string &type)
Constructor.
Definition: geventhandler.cpp:52
GNet::EventHandlerList::find
EventHandler * find(Descriptor fd)
Finds the handler associated with the given file descriptor.
Definition: geventhandler.cpp:74
GNet::EventHandler::~EventHandler
virtual ~EventHandler()
Destructor.
Definition: geventhandler.cpp:31
GNet::EventHandler::exceptionEvent
virtual void exceptionEvent()
Called for an exception event.
Definition: geventhandler.cpp:45
GNet::EventHandlerList
A class which can be used in the implemention of classes derived from GNet::EventLoop.
Definition: geventhandler.h:96