Migrate libasyncio from boost.signal to signals2 (#8756)
[libasyncio] / asyncio / async_callout.hpp
... / ...
CommitLineData
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
20/**
21 * @file
22 * @brief provides a method for delayed execution of functions.
23 *
24 * @copyright © Copyright 2008-2009 by Intra2net AG
25 */
26#ifndef ___ASYNC_CALLOUT_HPP__
27#define ___ASYNC_CALLOUT_HPP__
28
29#include "async_io.hpp"
30
31#include <boost/enable_shared_from_this.hpp>
32#include <boost/weak_ptr.hpp>
33#include <boost/shared_ptr.hpp>
34
35
36namespace AsyncIo
37{
38
39// forward declarations:
40namespace Detail {
41
42class Caller;
43
44typedef boost::shared_ptr< Caller > CallerPtr;
45typedef boost::weak_ptr< Caller > CallerWeakPtr;
46
47} // eo namespace Detail
48
49
50/**
51 * @brief represents an id for a deferred call.
52 *
53 * Also provides methods for modifying the call
54 * (like thaw or delete it).
55 */
56class CallOutId
57{
58 friend class Detail::Caller;
59
60 public:
61 CallOutId();
62
63 unsigned long getValue() const {return m_value;}
64
65 bool thaw() const;
66 bool remove();
67
68 bool active() const;
69 bool frozen() const;
70
71 MilliTime remaining_time();
72
73 private:
74
75 CallOutId(unsigned long value);
76
77 private:
78
79 unsigned long m_value;
80
81 Detail::CallerWeakPtr m_caller_weak_ptr;
82
83}; // eo class CallOutId
84
85
86
87/*
88**
89*/
90
91namespace Detail {
92
93/**
94 * @brief tool class for holding and executing a deferred call.
95 *
96 */
97class Caller
98: public TimerBase
99, public boost::enable_shared_from_this< Caller >
100{
101 public:
102 Caller( boost::function< void() > f, long delta_sec, long delta_msec=0, bool frozen=false );
103 virtual ~Caller();
104
105 CallOutId getCallOutId() const { return m_call_out_id; }
106
107 bool thaw();
108
109 bool joinId();
110
111 bool frozen() const;
112
113 protected:
114
115 virtual void execute();
116
117 private:
118
119 CallOutId m_call_out_id;
120 boost::function< void() > m_func;
121 bool m_waiting;
122}; // eo class Caller
123
124
125} // eo namespace Detail
126
127/*
128**
129*/
130
131/**
132 * @brief initiates a deferred call of a function.
133 *
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)
137 *
138 * A deferred call is done by passing a function object and a delay when it should
139 * be called.
140 * The backend loop will call the function after the delay is passed and the backend loop is active.
141 *
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.
144 *
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.
148 *
149 * @see CallOutId
150 */
151template< typename F >
152CallOutId callOut( boost::function< void() > f, F delta_sec );
153
154template<> CallOutId callOut( boost::function< void() > f, long delta_sec );
155template<> CallOutId callOut( boost::function< void() > f, double delta_sec );
156template<> CallOutId callOut( boost::function< void() > f, float delta_sec );
157template<> CallOutId callOut( boost::function< void() > f, int delta_sec );
158
159
160/**
161 * @brief initiates a frozen call of a function.
162 *
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.
166 *
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.
171 *
172 * The returned call id provides a method to thaw a frozen call.
173 *
174 * @see CallOutId
175 */
176template< typename F >
177CallOutId frozenCall( boost::function< void() > f, F delta_sec);
178
179template<> CallOutId frozenCall( boost::function< void() > f, long delta_sec );
180template<> CallOutId frozenCall( boost::function< void() > f, double delta_sec );
181template<> CallOutId frozenCall( boost::function< void() > f, float delta_sec );
182template<> CallOutId frozenCall( boost::function< void() > f, int delta_sec );
183
184
185
186bool removeCallOut( CallOutId& id );
187
188
189} // eo namespace AsyncIo
190
191#endif