Skip to content
Snippets Groups Projects
Commit b3de910d authored by sbund's avatar sbund
Browse files

Support arbitrary file-handle-equivalent arguments to Scheduler::add and remove

parent b544dd64
No related branches found
No related tags found
No related merge requests found
// $Id$
//
// Copyright (C) 2006
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
// Stefan Bund <stefan.bund@fokus.fraunhofer.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Definition of non-inline template functions
//#include "Scheduler.ih"
// Custom includes
#define prefix_
///////////////////////////////ct.p////////////////////////////////////////
///////////////////////////////ct.e////////////////////////////////////////
#undef prefix_
// Local Variables:
// mode: c++
// c-file-style: "satcom"
// End:
// $Id$
//
// Copyright (C) 2006
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
// Stefan Bund <stefan.bund@fokus.fraunhofer.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Definition of inline template functions
//#include "Scheduler.ih"
// Custom includes
#include <boost/bind.hpp>
#define prefix_ inline
///////////////////////////////cti.p///////////////////////////////////////
template <class Handle>
prefix_ void satcom::lib::Scheduler::add(Handle const & handle,
typename GenericCallback<Handle>::Callback const & cb,
EventId eventMask)
{
add(retrieve_filehandle(handle),boost::bind(cb,handle,_2),eventMask);
}
template <class Handle>
prefix_ void satcom::lib::Scheduler::remove(Handle const & handle, EventId eventMask)
{
// retrieve_filehandle is found via ADL
remove(retrieve_filehandle(handle),eventMask);
}
///////////////////////////////cti.e///////////////////////////////////////
#undef prefix_
// Local Variables:
// mode: c++
// c-file-style: "satcom"
// End:
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <map> #include <map>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/call_traits.hpp>
//#include "scheduler.mpp" //#include "scheduler.mpp"
///////////////////////////////hh.p//////////////////////////////////////// ///////////////////////////////hh.p////////////////////////////////////////
...@@ -52,7 +53,13 @@ namespace lib { ...@@ -52,7 +53,13 @@ namespace lib {
enum EventId { EV_NONE=0, enum EventId { EV_NONE=0,
EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16,
EV_ALL=31 }; EV_ALL=31 };
typedef boost::function<void (int fd, EventId event)> Callback;
template <class Handle>
struct GenericCallback {
typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
EventId) > Callback;
};
typedef GenericCallback<int>::Callback Callback;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
///\name Structors and default members ///\name Structors and default members
...@@ -72,6 +79,13 @@ namespace lib { ...@@ -72,6 +79,13 @@ namespace lib {
void add(int fd, Callback const & cb, EventId eventMask = EV_ALL); void add(int fd, Callback const & cb, EventId eventMask = EV_ALL);
void remove(int fd, EventId eventMask = EV_ALL); void remove(int fd, EventId eventMask = EV_ALL);
template <class Handle>
void add(Handle const & handle,
typename GenericCallback<Handle>::Callback const & cb,
EventId eventMask = EV_ALL);
template <class Handle>
void remove(Handle const & handle, EventId eventMask = EV_ALL);
void process(); void process();
void terminate(); void terminate();
...@@ -103,8 +117,8 @@ namespace lib { ...@@ -103,8 +117,8 @@ namespace lib {
///////////////////////////////hh.e//////////////////////////////////////// ///////////////////////////////hh.e////////////////////////////////////////
#include "Scheduler.cci" #include "Scheduler.cci"
//#include "Scheduler.ct" #include "Scheduler.ct"
//#include "Scheduler.cti" #include "Scheduler.cti"
#endif #endif
......
...@@ -163,7 +163,25 @@ namespace { ...@@ -163,7 +163,25 @@ namespace {
} }
Scheduler::instance().terminate(); Scheduler::instance().terminate();
} }
struct HandleWrapper
{
HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
int fd_;
std::string tag_;
};
int retrieve_filehandle(HandleWrapper const & handle)
{
return handle.fd_;
}
void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
{
if (handle.tag_ != "TheTag")
return;
callback(handle.fd_,event);
}
} }
BOOST_AUTO_UNIT_TEST(scheduler) BOOST_AUTO_UNIT_TEST(scheduler)
...@@ -198,14 +216,15 @@ BOOST_AUTO_UNIT_TEST(scheduler) ...@@ -198,14 +216,15 @@ BOOST_AUTO_UNIT_TEST(scheduler)
buffer[size]=0; buffer[size]=0;
BOOST_CHECK_EQUAL( buffer, "READ" ); BOOST_CHECK_EQUAL( buffer, "READ" );
BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_WRITE) ); HandleWrapper handle(sock,"TheTag");
BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
strcpy(buffer,"WRITE"); strcpy(buffer,"WRITE");
size=5; size=5;
event = Scheduler::EV_NONE; event = Scheduler::EV_NONE;
BOOST_CHECK_NO_THROW( Scheduler::instance().process() ); BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE ); BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
BOOST_CHECK_NO_THROW( Scheduler::instance().remove(sock,Scheduler::EV_WRITE) ); BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
event = Scheduler::EV_NONE; event = Scheduler::EV_NONE;
BOOST_CHECK_NO_THROW( Scheduler::instance().process() ); BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
BOOST_CHECK_EQUAL( event, Scheduler::EV_READ ); BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment