3 Copyright (c) 2003, Arvid Norberg, Daniel Wallin
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_ALERT_HPP_INCLUDED
34 #define TORRENT_ALERT_HPP_INCLUDED
42 #pragma warning(push, 1)
45 #include <boost/thread/mutex.hpp>
46 #include <boost/thread/condition.hpp>
48 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
49 #include <boost/preprocessor/repetition/enum.hpp>
50 #include <boost/preprocessor/repetition/enum_params.hpp>
51 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
57 #include "libtorrent/time.hpp"
58 #include "libtorrent/config.hpp"
59 #include "libtorrent/assert.hpp"
61 #ifndef TORRENT_MAX_ALERT_TYPES
62 #define TORRENT_MAX_ALERT_TYPES 15
65 namespace libtorrent
{
67 class TORRENT_EXPORT alert
71 // only here for backwards compatibility
72 enum severity_t
{ debug
, info
, warning
, critical
, fatal
, none
};
76 error_notification
= 0x1,
77 peer_notification
= 0x2,
78 port_mapping_notification
= 0x4,
79 storage_notification
= 0x8,
80 tracker_notification
= 0x10,
81 debug_notification
= 0x20,
82 status_notification
= 0x40,
83 progress_notification
= 0x80,
84 ip_block_notification
= 0x100,
86 all_categories
= 0xffffffff
92 // a timestamp is automatically created in the constructor
93 ptime
timestamp() const;
95 virtual char const* what() const = 0;
96 virtual std::string
message() const = 0;
97 virtual int category() const = 0;
99 #ifndef TORRENT_NO_DEPRECATE
100 severity_t
severity() const TORRENT_DEPRECATED
{ return warning
; }
103 virtual std::auto_ptr
<alert
> clone() const = 0;
109 class TORRENT_EXPORT alert_manager
115 void post_alert(const alert
& alert_
);
116 bool pending() const;
117 std::auto_ptr
<alert
> get();
120 bool should_post() const { return (m_alert_mask
& T::static_category
) != 0; }
122 alert
const* wait_for_alert(time_duration max_wait
);
124 void set_alert_mask(int m
) { m_alert_mask
= m
; }
127 std::queue
<alert
*> m_alerts
;
128 mutable boost::mutex m_mutex
;
129 boost::condition m_condition
;
133 struct TORRENT_EXPORT unhandled_alert
: std::exception
142 template<class Handler
143 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES
, class T
)>
144 void handle_alert_dispatch(
145 const std::auto_ptr
<alert
>& alert_
, const Handler
& handler
146 , const std::type_info
& typeid_
147 , BOOST_PP_ENUM_BINARY_PARAMS(TORRENT_MAX_ALERT_TYPES
, T
, *p
))
149 if (typeid_
== typeid(T0
))
150 handler(*static_cast<T0
*>(alert_
.get()));
152 handle_alert_dispatch(alert_
, handler
, typeid_
153 , BOOST_PP_ENUM_SHIFTED_PARAMS(
154 TORRENT_MAX_ALERT_TYPES
, p
), (void_
*)0);
157 template<class Handler
>
158 void handle_alert_dispatch(
159 const std::auto_ptr
<alert
>& alert_
160 , const Handler
& handler
161 , const std::type_info
& typeid_
162 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES
, void_
* BOOST_PP_INTERCEPT
))
164 throw unhandled_alert();
167 } // namespace detail
169 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
170 TORRENT_MAX_ALERT_TYPES
, class T
, detail::void_
)>
171 struct TORRENT_EXPORT handle_alert
173 template<class Handler
>
174 handle_alert(const std::auto_ptr
<alert
>& alert_
175 , const Handler
& handler
)
177 #define ALERT_POINTER_TYPE(z, n, text) (BOOST_PP_CAT(T, n)*)0
179 detail::handle_alert_dispatch(alert_
, handler
, typeid(*alert_
)
180 , BOOST_PP_ENUM(TORRENT_MAX_ALERT_TYPES
, ALERT_POINTER_TYPE
, _
));
182 #undef ALERT_POINTER_TYPE
186 } // namespace libtorrent
188 #endif // TORRENT_ALERT_HPP_INCLUDED