1 // Copyright Daniel Wallin 2005. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5 #ifndef LUABIND_EXCEPTION_HANDLER_050601_HPP
6 # define LUABIND_EXCEPTION_HANDLER_050601_HPP
7
8 # include <luabind/lua_include.hpp>
9 # include <luabind/config.hpp>
10 # include <boost/optional.hpp>
11 # include <boost/type.hpp>
12
13 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
14 # include <boost/mpl/if.hpp>
15 # include <boost/type_traits/is_pointer.hpp>
16 # endif
17
18 namespace luabind {
19
20 # ifndef LUABIND_NO_EXCEPTIONS
21
22 namespace detail
23 {
24
25 struct LUABIND_API exception_handler_base
26 {
exception_handler_baseluabind::detail::exception_handler_base27 exception_handler_base()
28 : next(0)
29 {}
30
~exception_handler_baseluabind::detail::exception_handler_base31 virtual ~exception_handler_base() {}
32 virtual void handle(lua_State*) const = 0;
33
34 void try_next(lua_State*) const;
35
36 exception_handler_base* next;
37 };
38
39 namespace mpl = boost::mpl;
40
41 template<class E, class Handler>
42 struct exception_handler : exception_handler_base
43 {
44 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
45 typedef typename mpl::if_<
46 boost::is_pointer<E>, E, E const&
47 >::type argument;
48 # else
49 typedef E const& argument;
50 # endif
51
exception_handlerluabind::detail::exception_handler52 exception_handler(Handler handler)
53 : handler(handler)
54 {}
55
handleluabind::detail::exception_handler56 void handle(lua_State* L) const
57 {
58 try
59 {
60 try_next(L);
61 }
62 catch (argument e)
63 {
64 handler(L, e);
65 }
66 }
67
68 Handler handler;
69 };
70
71 LUABIND_API void handle_exception_aux(lua_State* L);
72 LUABIND_API void register_exception_handler(exception_handler_base*);
73
74 } // namespace detail
75
76 # endif
77
78 template<class E, class Handler>
register_exception_handler(Handler handler,boost::type<E> * =0)79 void register_exception_handler(Handler handler, boost::type<E>* = 0)
80 {
81 # ifndef LUABIND_NO_EXCEPTIONS
82 detail::register_exception_handler(
83 new detail::exception_handler<E, Handler>(handler)
84 );
85 # endif
86 }
87
88 template<class R, class F>
handle_exceptions(lua_State * L,F fn,boost::type<R> * =0)89 boost::optional<R> handle_exceptions(lua_State* L, F fn, boost::type<R>* = 0)
90 {
91 # ifndef LUABIND_NO_EXCEPTIONS
92 try
93 {
94 return fn();
95 }
96 catch (...)
97 {
98 detail::handle_exception_aux(L);
99 }
100
101 return boost::optional<R>();
102 # else
103 return fn();
104 # endif
105 }
106
107 } // namespace luabind
108
109 #endif // LUABIND_EXCEPTION_HANDLER_050601_HPP
110
111