error.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_ERROR_HPP
  19. #define SOL_ERROR_HPP
  20. #include <sol/compatibility.hpp>
  21. #include <stdexcept>
  22. #include <string>
  23. #include <array>
  24. namespace sol {
  25. namespace detail {
  26. struct direct_error_tag { };
  27. const auto direct_error = direct_error_tag {};
  28. struct error_result {
  29. int results;
  30. const char* format_string;
  31. std::array<const char*, 4> argument_strings;
  32. error_result() : results(0), format_string(nullptr) {
  33. }
  34. error_result(int results_) : results(results_), format_string(nullptr) {
  35. }
  36. error_result(const char* format_string_, const char* first_message_) : results(0), format_string(format_string_), argument_strings() {
  37. argument_strings[0] = first_message_;
  38. }
  39. };
  40. inline int handle_errors(lua_State* L, const error_result& er) {
  41. if (er.format_string == nullptr) {
  42. return er.results;
  43. }
  44. return luaL_error(L, er.format_string, er.argument_strings[0], er.argument_strings[1], er.argument_strings[2], er.argument_strings[3]);
  45. }
  46. } // namespace detail
  47. class error : public std::runtime_error {
  48. private:
  49. // Because VC++ is upsetting, most of the time!
  50. std::string what_reason;
  51. public:
  52. error(const std::string& str) : error(detail::direct_error, "lua: error: " + str) {
  53. }
  54. error(std::string&& str) : error(detail::direct_error, "lua: error: " + std::move(str)) {
  55. }
  56. error(detail::direct_error_tag, const std::string& str) : std::runtime_error(""), what_reason(str) {
  57. }
  58. error(detail::direct_error_tag, std::string&& str) : std::runtime_error(""), what_reason(std::move(str)) {
  59. }
  60. error(const error& e) = default;
  61. error(error&& e) = default;
  62. error& operator=(const error& e) = default;
  63. error& operator=(error&& e) = default;
  64. virtual const char* what() const noexcept override {
  65. return what_reason.c_str();
  66. }
  67. };
  68. } // namespace sol
  69. #endif // SOL_ERROR_HPP