Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/selector.h

00001 /*
00002  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
00003  * Copyright (C) 2006-2007 Marc Boris Duerner
00004  * Copyright (C) 2006-2007 PTV AG
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * As a special exception, you may use this file as part of a free
00012  * software library without restriction. Specifically, if other files
00013  * instantiate templates or use macros or inline functions from this
00014  * file, or you compile this file and link it with other files to
00015  * produce an executable, this file does not by itself cause the
00016  * resulting executable to be covered by the GNU General Public
00017  * License. This exception does not however invalidate any other
00018  * reasons why the executable file might be covered by the GNU Library
00019  * General Public License.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00029  */
00030 #ifndef CXXTOOLS_SYSTEM_SELECTOR_H
00031 #define CXXTOOLS_SYSTEM_SELECTOR_H
00032 
00033 #include <cxxtools/timespan.h>
00034 #include <cxxtools/noncopyable.h>
00035 #include <cxxtools/connectable.h>
00036 #include <cxxtools/api.h>
00037 #include <map>
00038 
00039 namespace cxxtools {
00040 
00041     class Timer;
00042     class Selectable;
00043     class Application;
00044     class SelectorImpl;
00045 
00082     class CXXTOOLS_API SelectorBase : public Connectable
00083                                      , protected NonCopyable
00084     {
00085         friend class Selectable;
00086         friend class Timer;
00087 
00088         public:
00089             static const std::size_t WaitInfinite = static_cast<const std::size_t>(-1);
00090 
00092             virtual ~SelectorBase();
00093 
00099             void add(Selectable& s);
00100 
00103             void remove(Selectable& s);
00104 
00112             void add(Timer& timer);
00113 
00118             void remove(Timer& timer);
00119 
00128             bool wait(std::size_t msecs = WaitInfinite);
00129 
00136             void wake();
00137 
00138         protected:
00140             SelectorBase();
00141 
00142             void onAddTimer(Timer& timer);
00143 
00144             void onRemoveTimer( Timer& timer );
00145 
00146             void onTimerChanged( Timer& timer );
00147 
00152             virtual void onAdd(Selectable&) = 0;
00153 
00158             virtual void onRemove(Selectable&) = 0;
00159 
00164             virtual void onReinit(Selectable&) = 0;
00165 
00170             virtual void onChanged(Selectable& s) = 0;
00171 
00172             virtual bool onWait(std::size_t msecs) = 0;
00173 
00174             virtual void onWake() = 0;
00175 
00176         private:
00181             bool updateTimer(size_t& timeout);
00182 
00184             typedef std::multimap<Timespan, Timer*> TimerMap;
00185 
00187             TimerMap _timers;
00188 
00189             void* _reserved;
00190     };
00191 
00192     class CXXTOOLS_API Selector : public SelectorBase
00193     {
00194         public:
00195             Selector();
00196 
00197             virtual ~Selector();
00198 
00199             SelectorImpl& impl();
00200 
00201         protected:
00202             void onAdd( Selectable& dev );
00203 
00204             void onRemove( Selectable& dev );
00205 
00206             void onReinit(Selectable&);
00207 
00208             void onChanged(Selectable&);
00209 
00210             bool onWait(std::size_t msecs = WaitInfinite);
00211 
00212             void onWake();
00213 
00214         private:
00216             class SelectorImpl* _impl;
00217     };
00218 
00219 } //namespace cxxtools
00220 
00221 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6