Commit | Line | Data |
---|---|---|
8c15b8c7 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
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. | |
13 | ||
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. | |
16 | ||
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. | |
19 | */ | |
5c8a3d40 RP |
20 | /** |
21 | * @file | |
22 | * @brief provides a method for delayed execution of functions. | |
23 | * | |
5c8a3d40 | 24 | * @copyright © Copyright 2008 by Intra2net AG |
5c8a3d40 | 25 | */ |
42b7c46d RP |
26 | #ifndef ___ASYNC_CALLOUT_HPP__ |
27 | #define ___ASYNC_CALLOUT_HPP__ | |
5c8a3d40 | 28 | |
42b7c46d | 29 | #include "async_io.hpp" |
5c8a3d40 RP |
30 | |
31 | #include <boost/enable_shared_from_this.hpp> | |
32 | #include <boost/weak_ptr.hpp> | |
33 | #include <boost/shared_ptr.hpp> | |
34 | ||
35 | ||
42b7c46d | 36 | namespace AsyncIo |
5c8a3d40 RP |
37 | { |
38 | ||
39 | // forward declarations: | |
40 | namespace Detail { | |
41 | ||
42 | class Caller; | |
43 | ||
44 | typedef boost::shared_ptr< Caller > CallerPtr; | |
45 | typedef 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 | */ | |
56 | class 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 | ||
91 | namespace Detail { | |
92 | ||
93 | /** | |
94 | * @brief tool class for holding and executing a deferred call. | |
95 | * | |
96 | */ | |
97 | class 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 | template< typename F > | |
139 | CallOutId callOut( boost::function< void() > f, F delta_sec ); | |
140 | ||
141 | template<> CallOutId callOut( boost::function< void() > f, long delta_sec ); | |
142 | template<> CallOutId callOut( boost::function< void() > f, double delta_sec ); | |
143 | template<> CallOutId callOut( boost::function< void() > f, float delta_sec ); | |
144 | template<> CallOutId callOut( boost::function< void() > f, int delta_sec ); | |
145 | ||
146 | ||
147 | /** | |
148 | * @brief initiates a frozen call of a function. | |
149 | * | |
150 | * @param f the function which should be called. | |
151 | * @param delta_sec the delta time (in seconds) when the call will be (silently) removed. | |
152 | * @return an id to identify the call; neccessary for thaw the call. | |
153 | */ | |
154 | template< typename F > | |
155 | CallOutId frozenCall( boost::function< void() > f, F delta_sec); | |
156 | ||
157 | template<> CallOutId frozenCall( boost::function< void() > f, long delta_sec ); | |
158 | template<> CallOutId frozenCall( boost::function< void() > f, double delta_sec ); | |
159 | template<> CallOutId frozenCall( boost::function< void() > f, float delta_sec ); | |
160 | template<> CallOutId frozenCall( boost::function< void() > f, int delta_sec ); | |
161 | ||
162 | ||
163 | ||
164 | bool removeCallOut( CallOutId& id ); | |
165 | ||
166 | ||
42b7c46d | 167 | } // eo namespace AsyncIo |
5c8a3d40 RP |
168 | |
169 | #endif |