2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
22 * @brief provides a method for delayed execution of functions.
24 * @copyright © Copyright 2008-2009 by Intra2net AG
26 #ifndef ___ASYNC_CALLOUT_HPP__
27 #define ___ASYNC_CALLOUT_HPP__
29 #include "async_io.hpp"
31 #include <boost/enable_shared_from_this.hpp>
32 #include <boost/weak_ptr.hpp>
33 #include <boost/shared_ptr.hpp>
39 // forward declarations:
44 typedef boost::shared_ptr< Caller > CallerPtr;
45 typedef boost::weak_ptr< Caller > CallerWeakPtr;
47 } // eo namespace Detail
51 * @brief represents an id for a deferred call.
53 * Also provides methods for modifying the call
54 * (like thaw or delete it).
58 friend class Detail::Caller;
63 unsigned long getValue() const {return m_value;}
71 MilliTime remaining_time();
75 CallOutId(unsigned long value);
79 unsigned long m_value;
81 Detail::CallerWeakPtr m_caller_weak_ptr;
83 }; // eo class CallOutId
94 * @brief tool class for holding and executing a deferred call.
99 , public boost::enable_shared_from_this< Caller >
102 Caller( boost::function< void() > f, long delta_sec, long delta_msec=0, bool frozen=false );
105 CallOutId getCallOutId() const { return m_call_out_id; }
115 virtual void execute();
119 CallOutId m_call_out_id;
120 boost::function< void() > m_func;
122 }; // eo class Caller
125 } // eo namespace Detail
132 * @brief initiates a deferred call of a function.
134 * @param f the function which should be called.
135 * @param delta_sec the delta time (in seconds) when the function should be called.
136 * @return an id to identify the call (may be used for preliminary removal of the call)
138 * A deferred call is done by passing a function object and a delay when it should
140 * The backend loop will call the function after the delay is passed and the backend loop is active.
142 * Thus the call might not not be exactly after the desired delay, but at least this time has
143 * to pass until the function is called.
145 * The result id can be used to remove the call if it becomes obsolete.
146 * This is useful when You call timeout handlers and can remove them if an event happens and
147 * obsoletes the timeout call.
151 template< typename F >
152 CallOutId callOut( boost::function< void() > f, F delta_sec );
154 template<> CallOutId callOut( boost::function< void() > f, long delta_sec );
155 template<> CallOutId callOut( boost::function< void() > f, double delta_sec );
156 template<> CallOutId callOut( boost::function< void() > f, float delta_sec );
157 template<> CallOutId callOut( boost::function< void() > f, int delta_sec );
161 * @brief initiates a frozen call of a function.
163 * @param f the function which should be called.
164 * @param delta_sec the delta time (in seconds) when the call will be (silently) removed.
165 * @return an id to identify the call; necessary for thaw the call.
167 * A frozen call is a function call which will be prepared for a given period of time
168 * but not executed. Within that time period that call might be activated ("thawed")
169 * which means the call will be executed the next time when the backend loop is running.
170 * If the prepared call is not activated, then it vanishes.
172 * The returned call id provides a method to thaw a frozen call.
176 template< typename F >
177 CallOutId frozenCall( boost::function< void() > f, F delta_sec);
179 template<> CallOutId frozenCall( boost::function< void() > f, long delta_sec );
180 template<> CallOutId frozenCall( boost::function< void() > f, double delta_sec );
181 template<> CallOutId frozenCall( boost::function< void() > f, float delta_sec );
182 template<> CallOutId frozenCall( boost::function< void() > f, int delta_sec );
186 bool removeCallOut( CallOutId& id );
189 } // eo namespace AsyncIo