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

/home/tommi/cxxtools/include/cxxtools/eventloop.h

00001 /*
00002  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
00003  * Copyright (C) 2006-2008 Marc Boris Duerner
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  * As a special exception, you may use this file as part of a free
00011  * software library without restriction. Specifically, if other files
00012  * instantiate templates or use macros or inline functions from this
00013  * file, or you compile this file and link it with other files to
00014  * produce an executable, this file does not by itself cause the
00015  * resulting executable to be covered by the GNU General Public
00016  * License. This exception does not however invalidate any other
00017  * reasons why the executable file might be covered by the GNU Library
00018  * General Public License.
00019  * 
00020  * This library is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023  * Lesser General Public License for more details.
00024  * 
00025  * You should have received a copy of the GNU Lesser General Public
00026  * License along with this library; if not, write to the Free Software
00027  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00028  */
00029 #ifndef CXXTOOLS_SYSTEM_EVENTLOOP_H
00030 #define CXXTOOLS_SYSTEM_EVENTLOOP_H
00031 
00032 #include <cxxtools/event.h>
00033 #include <cxxtools/signal.h>
00034 #include <cxxtools/allocator.h>
00035 #include <cxxtools/api.h>
00036 #include <cxxtools/mutex.h>
00037 #include <cxxtools/selector.h>
00038 #include <cxxtools/eventsink.h>
00039 #include <deque>
00040 
00041 namespace cxxtools {
00042 
00043     class Selectable;
00044 
00047     class EventLoopBase : public SelectorBase
00048                         , public EventSink
00049     {
00050         public:
00053             virtual ~EventLoopBase()
00054             {}
00055 
00058             void run()
00059             { this->onRun(); }
00060 
00063             void processEvents()
00064             { this->onProcessEvents(); }
00065 
00068             void exit()
00069             { this->onExit(); }
00070 
00073             void setIdleTimeout(size_t msecs)
00074             { _timeout = msecs; }
00075 
00078             unsigned int idleTimeout() const
00079             { return _timeout; }
00080 
00085             Signal<> timeout;
00086 
00090             Signal<const Event&> event;
00091 
00094             Signal<> exited;
00095 
00096         protected:
00099             EventLoopBase()
00100             : _timeout(WaitInfinite)
00101             {}
00102 
00103             virtual void onRun() = 0;
00104 
00105             virtual void onExit() = 0;
00106 
00107             virtual void onProcessEvents() = 0;
00108 
00109         private:
00110             size_t _timeout;
00111     };
00112 
00139     class CXXTOOLS_API EventLoop : public EventLoopBase
00140     {
00141         public:
00144             EventLoop();
00145 
00148             virtual ~EventLoop();
00149 
00150         protected:
00151             virtual void onAdd( Selectable& s );
00152 
00153             virtual void onRemove( Selectable& s );
00154 
00155             virtual void onReinit(Selectable& s);
00156 
00157             virtual void onChanged(Selectable& s);
00158 
00159             virtual void onRun();
00160 
00161             virtual bool onWait(std::size_t msecs);
00162 
00163             virtual void onWake();
00164 
00165             virtual void onExit();
00166 
00167             virtual void onCommitEvent(const Event& event);
00168 
00169             virtual void onProcessEvents();
00170 
00171         private:
00172             bool _exitLoop;
00173             SelectorImpl* _selector;
00174             Allocator _allocator;
00175             std::deque<Event* > _eventQueue;
00176             RecursiveMutex _queueMutex;
00177     };
00178 
00179 } // namespace cxxtools
00180 
00181 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6