eventbase.cpp

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** \file eventbase.cpp Implementation of Event Handling Subsystem */
00020 
00021 #include <hn/hnprec.h>
00022 #include <hn/eventbase.h>
00023 
00024 EventMain& EventMain::instance() {
00025         static EventMain s_eMain;
00026         return s_eMain;
00027 }
00028 
00029 void EventMain::mainLoop() {
00030         while (true) {
00031                 EventTableBase::waitForEvent();
00032                 while (EventTableBase::hasPending()) {
00033                         EventTableBase::setNoPending();
00034                         handlePending();
00035                 }
00036                 boost::mutex::scoped_lock l(m_runningLock);
00037                 if (!m_running) {
00038                         break;
00039                 }
00040         }
00041 }
00042 void EventMain::handlePending() {
00043         for (LIter i = m_list.begin(); i != m_list.end(); i++) {
00044                 (*i)->handleEvents();
00045         }
00046 }
00047 
00048 void EventMain::exitMainLoop() {
00049         boost::mutex::scoped_lock l(m_runningLock);
00050         m_running = false;
00051         EventTableBase::notify();
00052 }
00053 
00054 // EventTableBase class
00055 //
00056 boost::mutex EventTableBase::s_notifyLock;
00057 boost::condition EventTableBase::s_eventNotify;
00058 bool EventTableBase::s_hasPending = false;
00059 EventTableBase::EventTableBase() {
00060         EventMain::instance().addEventTable(this);
00061 }
00062 EventTableBase::~EventTableBase() {
00063 //      EventMain::instance().removeEventTable(this);
00064 }
00065 void EventTableBase::notify() {
00066         boost::mutex::scoped_lock l(s_notifyLock);
00067         s_eventNotify.notify_all();
00068         s_hasPending = true;
00069 }
00070 void EventTableBase::waitForEvent() {
00071         boost::mutex::scoped_lock l(s_notifyLock);
00072         s_eventNotify.wait(l);
00073 }
00074 bool EventTableBase::hasPending() {
00075         boost::mutex::scoped_lock l(s_notifyLock);
00076         return s_hasPending;
00077 }
00078 void EventTableBase::setNoPending() {
00079         boost::mutex::scoped_lock l(s_notifyLock);
00080         s_hasPending = false;
00081 }