Properly fix the license for C++ template usage. This means we needed to change from...
[libasyncio] / asyncio / async_callout.hpp
CommitLineData
8c15b8c7
TJ
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*/
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 36namespace AsyncIo
5c8a3d40
RP
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 */
138template< typename F >
139CallOutId callOut( boost::function< void() > f, F delta_sec );
140
141template<> CallOutId callOut( boost::function< void() > f, long delta_sec );
142template<> CallOutId callOut( boost::function< void() > f, double delta_sec );
143template<> CallOutId callOut( boost::function< void() > f, float delta_sec );
144template<> 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 */
154template< typename F >
155CallOutId frozenCall( boost::function< void() > f, F delta_sec);
156
157template<> CallOutId frozenCall( boost::function< void() > f, long delta_sec );
158template<> CallOutId frozenCall( boost::function< void() > f, double delta_sec );
159template<> CallOutId frozenCall( boost::function< void() > f, float delta_sec );
160template<> CallOutId frozenCall( boost::function< void() > f, int delta_sec );
161
162
163
164bool removeCallOut( CallOutId& id );
165
166
42b7c46d 167} // eo namespace AsyncIo
5c8a3d40
RP
168
169#endif