bad_boxed_cast.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is distributed under the BSD License.
  2. // See "license.txt" for details.
  3. // Copyright 2009-2012, Jonathan Turner ([email protected])
  4. // Copyright 2009-2017, Jason Turner ([email protected])
  5. // http://www.chaiscript.com
  6. // This is an open source non-commercial project. Dear PVS-Studio, please check it.
  7. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  8. #ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_
  9. #define CHAISCRIPT_BAD_BOXED_CAST_HPP_
  10. #include <string>
  11. #include <typeinfo>
  12. #include "../chaiscript_defines.hpp"
  13. #include "type_info.hpp"
  14. namespace chaiscript {
  15. class Type_Info;
  16. } // namespace chaiscript
  17. namespace chaiscript
  18. {
  19. namespace exception
  20. {
  21. /// \brief Thrown in the event that a Boxed_Value cannot be cast to the desired type
  22. ///
  23. /// It is used internally during function dispatch and may be used by the end user.
  24. ///
  25. /// \sa chaiscript::boxed_cast
  26. class bad_boxed_cast : public std::bad_cast
  27. {
  28. public:
  29. bad_boxed_cast(Type_Info t_from, const std::type_info &t_to,
  30. std::string t_what) noexcept
  31. : from(t_from), to(&t_to), m_what(std::move(t_what))
  32. {
  33. }
  34. bad_boxed_cast(Type_Info t_from, const std::type_info &t_to)
  35. : from(t_from), to(&t_to), m_what("Cannot perform boxed_cast: " + t_from.name() + " to: " + t_to.name())
  36. {
  37. }
  38. explicit bad_boxed_cast(std::string t_what) noexcept
  39. : m_what(std::move(t_what))
  40. {
  41. }
  42. bad_boxed_cast(const bad_boxed_cast &) = default;
  43. ~bad_boxed_cast() noexcept override = default;
  44. /// \brief Description of what error occurred
  45. const char * what() const noexcept override
  46. {
  47. return m_what.c_str();
  48. }
  49. Type_Info from; ///< Type_Info contained in the Boxed_Value
  50. const std::type_info *to = nullptr; ///< std::type_info of the desired (but failed) result type
  51. private:
  52. std::string m_what;
  53. };
  54. }
  55. }
  56. #endif