error.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_ERROR_HPP_INCLUDED
  20. #define LUABIND_ERROR_HPP_INCLUDED
  21. #include <luabind/prefix.hpp>
  22. #include <exception>
  23. #include <luabind/config.hpp>
  24. #include <luabind/typeid.hpp>
  25. struct lua_State;
  26. namespace luabind
  27. {
  28. #ifndef LUABIND_NO_EXCEPTIONS
  29. // this exception usually means that the lua function you called
  30. // from C++ failed with an error code. You will have to
  31. // read the error code from the top of the lua stack
  32. // the reason why this exception class doesn't contain
  33. // the message itself is that std::string's copy constructor
  34. // may throw, if the copy constructor of an exception that is
  35. // being thrown throws another exception, terminate will be called
  36. // and the entire application is killed.
  37. class LUABIND_API error : public std::exception
  38. {
  39. public:
  40. explicit error(lua_State* L): m_L(L) {}
  41. lua_State* state() const throw() { return m_L; }
  42. virtual const char* what() const throw()
  43. {
  44. return "lua runtime error";
  45. }
  46. private:
  47. lua_State* m_L;
  48. };
  49. // if an object_cast<>() fails, this is thrown
  50. // it is also thrown if the return value of
  51. // a lua function cannot be converted
  52. class LUABIND_API cast_failed : public std::exception
  53. {
  54. public:
  55. cast_failed(lua_State* L, type_id const& i): m_L(L), m_info(i) {}
  56. lua_State* state() const throw() { return m_L; }
  57. type_id info() const throw() { return m_info; }
  58. virtual const char* what() const throw() { return "unable to make cast"; }
  59. private:
  60. lua_State* m_L;
  61. type_id m_info;
  62. };
  63. #else
  64. typedef void(*error_callback_fun)(lua_State*);
  65. typedef void(*cast_failed_callback_fun)(lua_State*, type_id const&);
  66. LUABIND_API void set_error_callback(error_callback_fun e);
  67. LUABIND_API void set_cast_failed_callback(cast_failed_callback_fun c);
  68. LUABIND_API error_callback_fun get_error_callback();
  69. LUABIND_API cast_failed_callback_fun get_cast_failed_callback();
  70. #endif
  71. typedef int(*pcall_callback_fun)(lua_State*);
  72. LUABIND_API void set_pcall_callback(pcall_callback_fun e);
  73. LUABIND_API pcall_callback_fun get_pcall_callback();
  74. }
  75. #endif // LUABIND_ERROR_HPP_INCLUDED