Remove redundant code.
[luabind.git] / luabind / exception_handler.hpp
blob0048563af19508da2c899813064aa877a3890f52
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)
5 #ifndef LUABIND_EXCEPTION_HANDLER_050601_HPP
6 # define LUABIND_EXCEPTION_HANDLER_050601_HPP
8 # include <luabind/lua_include.hpp>
9 # include <luabind/config.hpp>
10 # include <boost/optional.hpp>
11 # include <boost/type.hpp>
13 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
14 # include <boost/mpl/if.hpp>
15 # include <boost/type_traits/is_pointer.hpp>
16 # endif
18 namespace luabind {
20 # ifndef LUABIND_NO_EXCEPTIONS
22 namespace detail
25 struct LUABIND_API exception_handler_base
27 exception_handler_base()
28 : next(0)
31 virtual ~exception_handler_base() {}
32 virtual void handle(lua_State*) const = 0;
34 void try_next(lua_State*) const;
36 exception_handler_base* next;
39 namespace mpl = boost::mpl;
41 template<class E, class Handler>
42 struct exception_handler : exception_handler_base
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
52 exception_handler(Handler handler)
53 : handler(handler)
56 void handle(lua_State* L) const
58 try
60 try_next(L);
62 catch (argument e)
64 handler(L, e);
68 Handler handler;
71 LUABIND_API void handle_exception_aux(lua_State* L);
72 LUABIND_API void register_exception_handler(exception_handler_base*);
74 } // namespace detail
76 # endif
78 template<class E, class Handler>
79 void register_exception_handler(Handler handler, boost::type<E>* = 0)
81 # ifndef LUABIND_NO_EXCEPTIONS
82 detail::register_exception_handler(
83 new detail::exception_handler<E, Handler>(handler)
85 # endif
88 template<class R, class F>
89 boost::optional<R> handle_exceptions(lua_State* L, F fn, boost::type<R>* = 0)
91 # ifndef LUABIND_NO_EXCEPTIONS
92 try
94 return fn();
96 catch (...)
98 detail::handle_exception_aux(L);
101 return boost::optional<R>();
102 # else
103 return fn();
104 # endif
107 } // namespace luabind
109 #endif // LUABIND_EXCEPTION_HANDLER_050601_HPP