eventloop.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *  D-Bus++ - C++ bindings for D-Bus
00004  *
00005  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
00006  *
00007  *
00008  *  This library is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Lesser General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 
00025 #ifndef __DBUSXX_EVENTLOOP_H
00026 #define __DBUSXX_EVENTLOOP_H
00027 
00028 #include <pthread.h>
00029 #include <list>
00030 
00031 #include "api.h"
00032 #include "util.h"
00033 
00034 namespace DBus
00035 {
00036 
00037 /*
00038  * these Default *classes implement a very simple event loop which
00039  * is used here as the default main loop, if you want to hook
00040  * a different one use the Bus *classes in eventloop-integration.h
00041  * or the Glib::Bus *classes as a reference
00042  */
00043 
00044 class DefaultMainLoop;
00045 
00046 class DXXAPI DefaultTimeout
00047 {
00048 public:
00049 
00050   DefaultTimeout(int interval, bool repeat, DefaultMainLoop *);
00051 
00052   virtual ~DefaultTimeout();
00053 
00054   bool enabled()
00055   {
00056     return _enabled;
00057   }
00058   void enabled(bool e)
00059   {
00060     _enabled = e;
00061   }
00062 
00063   int interval()
00064   {
00065     return _interval;
00066   }
00067   void interval(int i)
00068   {
00069     _interval = i;
00070   }
00071 
00072   bool repeat()
00073   {
00074     return _repeat;
00075   }
00076   void repeat(bool r)
00077   {
00078     _repeat = r;
00079   }
00080 
00081   void *data()
00082   {
00083     return _data;
00084   }
00085   void data(void *d)
00086   {
00087     _data = d;
00088   }
00089 
00090   Slot<void, DefaultTimeout &> expired;
00091 
00092 private:
00093 
00094   bool _enabled;
00095 
00096   int _interval;
00097   bool _repeat;
00098 
00099   double _expiration;
00100 
00101   void *_data;
00102 
00103   DefaultMainLoop *_disp;
00104 
00105   friend class DefaultMainLoop;
00106 };
00107 
00108 typedef std::list< DefaultTimeout *> DefaultTimeouts;
00109 
00110 class DXXAPI DefaultWatch
00111 {
00112 public:
00113 
00114   DefaultWatch(int fd, int flags, DefaultMainLoop *);
00115 
00116   virtual ~DefaultWatch();
00117 
00118   bool enabled()
00119   {
00120     return _enabled;
00121   }
00122   void enabled(bool e)
00123   {
00124     _enabled = e;
00125   }
00126 
00127   int descriptor()
00128   {
00129     return _fd;
00130   }
00131 
00132   int flags()
00133   {
00134     return _flags;
00135   }
00136   void flags(int f)
00137   {
00138     _flags = f;
00139   }
00140 
00141   int state()
00142   {
00143     return _state;
00144   }
00145 
00146   void *data()
00147   {
00148     return _data;
00149   }
00150   void data(void *d)
00151   {
00152     _data = d;
00153   }
00154 
00155   Slot<void, DefaultWatch &> ready;
00156 
00157 private:
00158 
00159   bool _enabled;
00160 
00161   int _fd;
00162   int _flags;
00163   int _state;
00164 
00165   void *_data;
00166 
00167   DefaultMainLoop *_disp;
00168 
00169   friend class DefaultMainLoop;
00170 };
00171 
00172 typedef std::list< DefaultWatch *> DefaultWatches;
00173 
00174 class DXXAPI DefaultMutex
00175 {
00176 public:
00177 
00181   DefaultMutex();
00182 
00187   DefaultMutex(bool recursive);
00188 
00189   ~DefaultMutex();
00190 
00191   void lock();
00192 
00193   void unlock();
00194 
00195 private:
00196 
00197   pthread_mutex_t _mutex;
00198 };
00199 
00200 class DXXAPI DefaultMainLoop
00201 {
00202 public:
00203 
00204   DefaultMainLoop();
00205 
00206   virtual ~DefaultMainLoop();
00207 
00208   virtual void dispatch();
00209 
00210   int _fdunlock[2];
00211 private:
00212 
00213   DefaultMutex _mutex_t;
00214   DefaultTimeouts _timeouts;
00215 
00216   DefaultMutex _mutex_w;
00217   DefaultWatches _watches;
00218 
00219   friend class DefaultTimeout;
00220   friend class DefaultWatch;
00221 };
00222 
00223 } /* namespace DBus */
00224 
00225 #endif//__DBUSXX_EVENTLOOP_H