e8b535bd891270eff27a5e235513f8a9eaa0b145
[senf.git] / Scheduler / Binding.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 /** \file
21     \brief Binding public header */
22
23 #ifndef HH_Binding_
24 #define HH_Binding_ 1
25
26 // Custom includes
27 #include <boost/utility.hpp>
28 #include "Scheduler.hh"
29
30 //#include "Binding.mpp"
31 ///////////////////////////////hh.p////////////////////////////////////////
32
33 namespace senf {
34
35     /** \brief Manage scheduler handle binding
36
37         This class will manage the binding of an arbitrary handle to the scheduler: The handle will
38         automatically be removed from the Scheduler when this instance is destroyed. Example using a
39         SocketHandle instance for the handle:
40         \code
41         class Foo
42         {
43         public:
44             Foo(SocketHandle handle) : binding_ (handle, senf::membind(&cb, this),
45                                                  senf::Scheduler::EV_READ) {}
46
47             void blarf() { binding_.disable(); }
48
49         private:
50            void cb(Scheduler::EventId event);
51
52            senf::SchedulerBinding binding_;
53         };
54         \endcode
55
56         The handle will be registered automatically in the constructor and will be unregistered in
57         the destructor. Additionally, the helper has enable() and disable() members to register or
58         remove the handle to/from the Scheduler.
59      */
60     class SchedulerBinding
61         : boost::noncopyable
62     {
63     public:
64         template <class Handle>
65         SchedulerBinding(Handle const & handle, Scheduler::FdCallback cb, 
66                          int eventMask = Scheduler::EV_ALL, bool enabled = true);
67                                         ///< Register handle with the Scheduler
68                                         /**< This constructor is like calling Scheduler::add()
69                                              unless \a enabled is \c false, in which case the handle
70                                              is \e not initially registered (it may be registered by
71                                              caling enable() 
72                                              \param[in] handle Handle to register
73                                              \param[in] cb Callback
74                                              \param[in] eventMask type of events to register for
75                                              \param[in] enabled wether to add handle to Scheduler
76                                                  automatically  */
77
78         ~SchedulerBinding();            ///< Remove scheduler registration
79         
80         void enable();                  ///< Add handle to Scheduler
81                                         /**< Adds the handle if it is not already registered */
82         void disable();                 ///< Remove handle from Scheduler
83                                         /**< Remove handle from Scheduler if registered */
84
85         bool enabled();                 ///< \c true, if handle is registered
86
87     protected:
88
89     private:
90         int fd_;
91         Scheduler::FdCallback cb_;
92         int eventMask_;
93         bool enabled_;
94     };
95 }
96
97 ///////////////////////////////hh.e////////////////////////////////////////
98 #include "Binding.cci"
99 //#include "Binding.ct"
100 #include "Binding.cti"
101 #endif
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // fill-column: 100
107 // comment-column: 40
108 // c-file-style: "senf"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -u test"
112 // End: