*** empty log message ***
[luabind.git] / luabind / error.hpp
blob3c739b13e4427e826c87c6a5fec90a6b76315a5b
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
26 #include <luabind/prefix.hpp>
27 #include <exception>
28 #include <luabind/config.hpp>
30 namespace luabind
33 #ifndef LUABIND_NO_EXCEPTIONS
35 // this exception usually means that the lua function you called
36 // from C++ failed with an error code. You will have to
37 // read the error code from the top of the lua stack
38 // the reason why this exception class doesn't contain
39 // the message itself is that std::string's copy constructor
40 // may throw, if the copy constructor of an exception that is
41 // being thrown throws another exception, terminate will be called
42 // and the entire application is killed.
43 class error : public std::exception
45 public:
46 error(lua_State* L): m_L(L) {}
47 lua_State* state() const throw() { return m_L; }
48 virtual const char* what() const throw()
50 return "lua runtime error";
52 private:
53 lua_State* m_L;
56 // if an object_cast<>() fails, this is thrown
57 // it is also thrown if the return value of
58 // a lua function cannot be converted
59 class cast_failed : public std::exception
61 public:
62 cast_failed(lua_State* L, LUABIND_TYPE_INFO i): m_L(L), m_info(i) {}
63 lua_State* state() const throw() { return m_L; }
64 LUABIND_TYPE_INFO info() const throw() { return m_info; }
65 virtual const char* what() const throw() { return "unable to make cast"; }
66 private:
67 lua_State* m_L;
68 LUABIND_TYPE_INFO m_info;
71 #else
73 typedef void(*error_callback_fun)(lua_State*);
74 typedef void(*cast_failed_callback_fun)(lua_State*, LUABIND_TYPE_INFO);
76 namespace detail
78 // we use a singleton instead of a global variable
79 // because we want luabind to be headers only
81 struct error_callback
83 error_callback_fun err;
84 cast_failed_callback_fun cast;
86 error_callback(): err(0), cast(0) {}
88 static error_callback& get()
90 static error_callback instance;
91 return instance;
96 inline void set_error_callback(error_callback_fun e)
98 detail::error_callback::get().err = e;
101 inline void set_cast_failed_callback(cast_failed_callback_fun c)
103 detail::error_callback::get().cast = c;
106 #endif
110 #endif // LUABIND_ERROR_HPP_INCLUDED