new example: hello_world
[luabind.git] / luabind / error.hpp
blob5c5b214ef99c73931e3e79473fd6ab51c2a3c804
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 struct lua_State;
32 namespace luabind
35 #ifndef LUABIND_NO_EXCEPTIONS
37 // this exception usually means that the lua function you called
38 // from C++ failed with an error code. You will have to
39 // read the error code from the top of the lua stack
40 // the reason why this exception class doesn't contain
41 // the message itself is that std::string's copy constructor
42 // may throw, if the copy constructor of an exception that is
43 // being thrown throws another exception, terminate will be called
44 // and the entire application is killed.
45 class error : public std::exception
47 public:
48 error(lua_State* L): m_L(L) {}
49 lua_State* state() const throw() { return m_L; }
50 virtual const char* what() const throw()
52 return "lua runtime error";
54 private:
55 lua_State* m_L;
58 // if an object_cast<>() fails, this is thrown
59 // it is also thrown if the return value of
60 // a lua function cannot be converted
61 class cast_failed : public std::exception
63 public:
64 cast_failed(lua_State* L, LUABIND_TYPE_INFO i): m_L(L), m_info(i) {}
65 lua_State* state() const throw() { return m_L; }
66 LUABIND_TYPE_INFO info() const throw() { return m_info; }
67 virtual const char* what() const throw() { return "unable to make cast"; }
68 private:
69 lua_State* m_L;
70 LUABIND_TYPE_INFO m_info;
73 #else
75 typedef void(*error_callback_fun)(lua_State*);
76 typedef void(*cast_failed_callback_fun)(lua_State*, LUABIND_TYPE_INFO);
78 void set_error_callback(error_callback_fun e);
79 void set_cast_failed_callback(cast_failed_callback_fun c);
80 error_callback_fun get_error_callback();
81 cast_failed_callback_fun get_cast_failed_callback();
83 #endif
85 typedef int(*pcall_callback_fun)(lua_State*);
86 void set_pcall_callback(pcall_callback_fun e);
87 pcall_callback_fun get_pcall_callback();
91 #endif // LUABIND_ERROR_HPP_INCLUDED