dump_handler.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_DUMP_HANDLER_HPP
  19. #define SOL_DUMP_HANDLER_HPP
  20. #include <sol/compatibility.hpp>
  21. #include <cstdint>
  22. #include <exception>
  23. namespace sol {
  24. class dump_error : public error {
  25. private:
  26. int m_ec;
  27. public:
  28. dump_error(int error_code_) : error("dump returned non-zero error of " + std::to_string(error_code_)), m_ec(error_code_) {
  29. }
  30. int error_code() const {
  31. return m_ec;
  32. }
  33. };
  34. inline int dump_pass_on_error(lua_State* L_, int result_code, lua_Writer writer_function, void* userdata_pointer_, bool strip) {
  35. (void)L_;
  36. (void)writer_function;
  37. (void)userdata_pointer_;
  38. (void)strip;
  39. return result_code;
  40. }
  41. inline int dump_panic_on_error(lua_State* L_, int result_code, lua_Writer writer_function, void* userdata_pointer_, bool strip) {
  42. (void)L_;
  43. (void)writer_function;
  44. (void)userdata_pointer_;
  45. (void)strip;
  46. return luaL_error(L_, "a non-zero error code (%d) was returned by the lua_Writer for the dump function", result_code);
  47. }
  48. inline int dump_throw_on_error(lua_State* L_, int result_code, lua_Writer writer_function, void* userdata_pointer_, bool strip) {
  49. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  50. return dump_panic_on_error(L_, result_code, writer_function, userdata_pointer_, strip);
  51. #else
  52. (void)L_;
  53. (void)writer_function;
  54. (void)userdata_pointer_;
  55. (void)strip;
  56. throw dump_error(result_code);
  57. #endif // no exceptions stuff
  58. }
  59. } // namespace sol
  60. #endif // SOL_DUMP_HANDLER_HPP