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,
85 performance_warning
= 0x200,
87 all_categories
= 0xffffffff
93 // a timestamp is automatically created in the constructor
94 ptime
timestamp() const;
96 virtual char const* what() const = 0;
97 virtual std::string
message() const = 0;
98 virtual int category() const = 0;
100 #ifndef TORRENT_NO_DEPRECATE
101 severity_t
severity() const TORRENT_DEPRECATED
{ return warning
; }
104 virtual std::auto_ptr
<alert
> clone() const = 0;
110 class TORRENT_EXPORT alert_manager
116 void post_alert(const alert
& alert_
);
117 bool pending() const;
118 std::auto_ptr
<alert
> get();
121 bool should_post() const { return (m_alert_mask
& T::static_category
) != 0; }
123 alert
const* wait_for_alert(time_duration max_wait
);
125 void set_alert_mask(int m
) { m_alert_mask
= m
; }
128 std::queue
<alert
*> m_alerts
;
129 mutable boost::mutex m_mutex
;
130 boost::condition m_condition
;
134 struct TORRENT_EXPORT unhandled_alert
: std::exception
143 template<class Handler
144 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES
, class T
)>
145 void handle_alert_dispatch(
146 const std::auto_ptr
<alert
>& alert_
, const Handler
& handler
147 , const std::type_info
& typeid_
148 , BOOST_PP_ENUM_BINARY_PARAMS(TORRENT_MAX_ALERT_TYPES
, T
, *p
))
150 if (typeid_
== typeid(T0
))
151 handler(*static_cast<T0
*>(alert_
.get()));
153 handle_alert_dispatch(alert_
, handler
, typeid_
154 , BOOST_PP_ENUM_SHIFTED_PARAMS(
155 TORRENT_MAX_ALERT_TYPES
, p
), (void_
*)0);
158 template<class Handler
>
159 void handle_alert_dispatch(
160 const std::auto_ptr
<alert
>& alert_
161 , const Handler
& handler
162 , const std::type_info
& typeid_
163 , BOOST_PP_ENUM_PARAMS(TORRENT_MAX_ALERT_TYPES
, void_
* BOOST_PP_INTERCEPT
))
165 throw unhandled_alert();
168 } // namespace detail
170 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
171 TORRENT_MAX_ALERT_TYPES
, class T
, detail::void_
)>
172 struct TORRENT_EXPORT handle_alert
174 template<class Handler
>
175 handle_alert(const std::auto_ptr
<alert
>& alert_
176 , const Handler
& handler
)
178 #define ALERT_POINTER_TYPE(z, n, text) (BOOST_PP_CAT(T, n)*)0
180 detail::handle_alert_dispatch(alert_
, handler
, typeid(*alert_
)
181 , BOOST_PP_ENUM(TORRENT_MAX_ALERT_TYPES
, ALERT_POINTER_TYPE
, _
));
183 #undef ALERT_POINTER_TYPE
187 } // namespace libtorrent
189 #endif // TORRENT_ALERT_HPP_INCLUDED