From b3de910db676da46999aba8fae8e3348b7665cb3 Mon Sep 17 00:00:00 2001
From: sbund <sbund@wiback.org>
Date: Tue, 25 Jul 2006 09:05:26 +0000
Subject: [PATCH] Support arbitrary file-handle-equivalent arguments to
 Scheduler::add and remove

---
 Scheduler/Scheduler.ct      | 39 ++++++++++++++++++++++++++
 Scheduler/Scheduler.cti     | 55 +++++++++++++++++++++++++++++++++++++
 Scheduler/Scheduler.hh      | 20 ++++++++++++--
 Scheduler/Scheduler.test.cc | 25 +++++++++++++++--
 4 files changed, 133 insertions(+), 6 deletions(-)
 create mode 100644 Scheduler/Scheduler.ct
 create mode 100644 Scheduler/Scheduler.cti

diff --git a/Scheduler/Scheduler.ct b/Scheduler/Scheduler.ct
new file mode 100644
index 000000000..05afa48ce
--- /dev/null
+++ b/Scheduler/Scheduler.ct
@@ -0,0 +1,39 @@
+// $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:
diff --git a/Scheduler/Scheduler.cti b/Scheduler/Scheduler.cti
new file mode 100644
index 000000000..7ef1148ba
--- /dev/null
+++ b/Scheduler/Scheduler.cti
@@ -0,0 +1,55 @@
+// $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:
diff --git a/Scheduler/Scheduler.hh b/Scheduler/Scheduler.hh
index 5bb9443e9..e731db4b4 100644
--- a/Scheduler/Scheduler.hh
+++ b/Scheduler/Scheduler.hh
@@ -27,6 +27,7 @@
 #include <map>
 #include <boost/function.hpp>
 #include <boost/utility.hpp>
+#include <boost/call_traits.hpp>
 
 //#include "scheduler.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -52,7 +53,13 @@ namespace lib {
         enum EventId { EV_NONE=0, 
                        EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, 
                        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
@@ -72,6 +79,13 @@ namespace lib {
         void add(int fd, Callback const & cb, 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 terminate();
@@ -103,8 +117,8 @@ namespace lib {
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "Scheduler.cci"
-//#include "Scheduler.ct"
-//#include "Scheduler.cti"
+#include "Scheduler.ct"
+#include "Scheduler.cti"
 #endif
 
 
diff --git a/Scheduler/Scheduler.test.cc b/Scheduler/Scheduler.test.cc
index 71b33027a..fb5ae3590 100644
--- a/Scheduler/Scheduler.test.cc
+++ b/Scheduler/Scheduler.test.cc
@@ -163,7 +163,25 @@ namespace {
         }
         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)
@@ -198,14 +216,15 @@ BOOST_AUTO_UNIT_TEST(scheduler)
     buffer[size]=0;
     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");
     size=5;
     event = Scheduler::EV_NONE;
     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
     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;
     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
-- 
GitLab