1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef LUABIND_ERROR_HPP_INCLUDED
24 #define LUABIND_ERROR_HPP_INCLUDED
27 #include <luabind/config.hpp>
32 #ifndef LUABIND_NO_EXCEPTIONS
34 // this exception usually means that the lua function you called
35 // from C++ failed with an error code. You will have to
36 // read the error code from the top of the lua stack
37 // the reason why this exception class doesn't contain
38 // the message itself is that std::string's copy constructor
39 // may throw, if the copy constructor of an exception that is
40 // being thrown throws another exception, terminate will be called
41 // and the entire application is killed.
42 class error
: public std::exception
45 error(lua_State
* L
): m_L(L
) {}
46 lua_State
* state() const throw() { return m_L
; }
47 virtual const char* what() const throw()
49 return "lua runtime error";
55 // if an object_cast<>() fails, this is thrown
56 // it is also thrown if the return value of
57 // a lua function cannot be converted
58 class cast_failed
: public std::exception
61 cast_failed(lua_State
* L
, LUABIND_TYPE_INFO i
): m_L(L
), m_info(i
) {}
62 lua_State
* state() const throw() { return m_L
; }
63 LUABIND_TYPE_INFO
info() const throw() { return m_info
; }
64 virtual const char* what() const throw() { return "unable to make cast"; }
67 LUABIND_TYPE_INFO m_info
;
72 typedef void(*error_callback_fun
)(lua_State
*);
73 typedef void(*cast_failed_callback_fun
)(lua_State
*, LUABIND_TYPE_INFO
);
77 // we use a singleton instead of a global variable
78 // because we want luabind to be headers only
82 error_callback_fun err
;
83 cast_failed_callback_fun cast
;
85 error_callback(): err(0), cast(0) {}
87 static error_callback
& get()
89 static error_callback instance
;
95 inline void set_error_callback(error_callback_fun e
)
97 detail::error_callback::get().err
= e
;
100 inline void set_cast_failed_callback(cast_failed_callback_fun c
)
102 detail::error_callback::get().cast
= c
;
109 #endif // LUABIND_ERROR_HPP_INCLUDED