added error_callback
[luabind.git] / luabind / detail / error.hpp
blob0d6cc46a89ddbdc59de72c08cbce49d191ef3514
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 <exception>
27 #include <luabind/config.hpp>
29 namespace luabind
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 struct error : public std::exception
44 virtual const char* what() const throw()
46 return "lua runtime error";
50 // this exception is thrown when you call a lua function
51 // from C++, and the return value on the lua stack cannot
52 // be converted to the type you specified in the C++ call
53 // (as a template parameter)
54 class cant_convert_return_value: public std::exception
56 public:
58 virtual const char* what() const throw()
60 return "The return value from the called function cannot be converted by the given return value policy";
65 // if a object_cast<>() fails, this is thrown
66 class cast_failed : public std::exception
68 public:
69 virtual const char* what() const throw() { return "unable to make cast"; }
72 #else
74 typedef void(*error_callback_fun)(lua_State*);
75 typedef void(*cast_failed_callback_fun)(lua_State*);
77 namespace detail
79 // we use a singleton instead of a global variable
80 // because we want luabind to be headers only
82 struct error_callback
84 error_callback_fun err;
85 cast_failed_callback_fun cast;
87 error_callback(): err(0), cast(0) {}
89 static error_callback& get()
91 static error_callback instance;
92 return instance;
97 inline void set_error_callback(error_callback_fun e)
99 detail::error_callback::get().err = e;
102 inline void set_cast_failed_callback(cast_failed_callback_fun c)
104 detail::error_callback::get().cast = c;
107 #endif
111 #endif // LUABIND_ERROR_HPP_INCLUDED