00001 /* 00002 libwftk - Worldforge Toolkit - a widget library 00003 Copyright (C) 2003 Ron Steinke <rsteinke@w-link.net> 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 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef _POLL_H_ 00022 #define _POLL_H_ 00023 00024 #include <set> 00025 #include <sigc++/object.h> 00026 #include <SDL/SDL_types.h> 00027 00028 #if defined( __WIN32__ ) || defined( __CYGWIN32__ ) 00029 #include <winsock2.h> 00030 #endif 00031 00032 #include "application.h" 00033 00034 namespace wftk { 00035 00036 class PollData; 00037 00039 class PollBase : virtual public SigC::Object 00040 { 00041 public: 00043 #if defined( __WIN32__ ) || defined( __CYGWIN32__ ) 00044 typedef SOCKET Socket; 00045 #else 00046 typedef int Socket; 00047 #endif 00048 00049 enum { 00050 READ = 1 << 0, 00051 WRITE = 1 << 1, 00052 EXCEPT = 1 << 2, 00053 MASK = (1 << 3) - 1 00054 }; 00055 00056 PollBase(); 00057 virtual ~PollBase(); 00058 00060 static void poll(Uint32 wait); 00061 00062 protected: 00063 // wrappers for some PollData functions, so we don't 00064 // have to export fd_set 00065 00067 static void addPoll(PollData&, Socket, int mask); 00069 static int checkPoll(PollData&, Socket, int mask); 00071 static void refPoll(PollData&); 00073 static void unrefPoll(PollData&); 00074 00075 // virtual functions to implement polling for generic sets of sockets 00076 00078 virtual void setup(PollData&) = 0; 00079 00084 virtual void pushEvent(PollData&) = 0; 00085 00086 private: 00087 static std::set<PollBase*> polls_; 00088 }; 00089 00091 class Poll : public PollBase 00092 { 00094 Poll(Socket, int mask); 00096 ~Poll(); 00097 00099 int mask() const {return mask_;} 00100 00102 void setMask(int m) {mask_ = m;} 00103 00104 protected: 00105 00106 virtual void setup(PollData& data) {addPoll(data, socket_, mask_);} 00107 virtual void pushEvent(PollData&); 00108 00109 private: 00110 // unimplemented 00111 Poll(const Poll&); 00112 Poll& operator=(const Poll&); 00113 00115 virtual void event(int mask) = 0; 00116 00117 typedef Application::FloatingEvent<Poll> BaseEvent; 00118 class Event : public BaseEvent 00119 { 00120 public: 00121 Event(Poll& poll, int mask) : BaseEvent(poll), mask_(mask) {} 00122 00123 virtual void operator()() {if(data()) data()->event(mask_);} 00124 00125 private: 00126 int mask_; 00127 }; 00128 00130 Socket socket_; 00132 int mask_; 00133 public: 00134 BaseEvent* currentEvent_; 00135 }; 00136 00137 } // namespace 00138 00139 #endif
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.