Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_exception_handlers.cpp
blobc12ebd077a1dec2552b8bea05269c1d5a035daa2
1 // Copyright Daniel Wallin 2008. 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 #include "test.hpp"
6 #include <luabind/luabind.hpp>
7 #include <luabind/exception_handler.hpp>
9 struct my_exception {};
11 void translate_my_exception(lua_State* L, my_exception const&)
13 lua_pushstring(L, "my_exception");
16 struct derived_std_exception : std::exception
18 char const* what() const throw()
20 return "derived_std_exception";
24 void translate_derived_exception(lua_State* L, derived_std_exception const&)
26 lua_pushstring(L, "derived_std_exception");
29 void raise()
31 throw my_exception();
34 void raise_derived()
36 throw derived_std_exception();
39 void test_main(lua_State* L)
41 using namespace luabind;
43 register_exception_handler<my_exception>(&translate_my_exception);
45 module(L) [
46 def("raise", &raise),
47 def("raise_derived", &raise_derived)
50 DOSTRING(L,
51 "status, msg = pcall(raise)\n"
52 "assert(status == false)\n"
53 "assert(msg == 'my_exception')\n");
55 DOSTRING(L,
56 "status, msg = pcall(raise_derived)\n"
57 "assert(status == false)\n"
58 "assert(msg == 'std::exception: \\'derived_std_exception\\'')\n");
60 register_exception_handler<derived_std_exception>(&translate_derived_exception);
62 DOSTRING(L,
63 "status, msg = pcall(raise_derived)\n"
64 "assert(status == false)\n"
65 "assert(msg == 'derived_std_exception')\n");