Revert "Remove exception specification shared_ptr use"

This reverts commit e1cf8b9eb1d59d49149454424ec49bd0d8a3b28f.
This commit is contained in:
Jason Turner 2017-08-15 10:13:20 -06:00
parent 1ca857b890
commit 0fc420f69d
2 changed files with 26 additions and 33 deletions

View File

@ -25,36 +25,26 @@ class bad_boxed_cast;
namespace chaiscript namespace chaiscript
{ {
namespace detail namespace detail
{ {
struct Exception_Handler struct Exception_Handler_Base
{ {
virtual void operator()(const Boxed_Value &, const detail::Dispatch_Engine &) const virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine) = 0;
{
}
Exception_Handler() = default; virtual ~Exception_Handler_Base() = default;
Exception_Handler(Exception_Handler &&) = default;
Exception_Handler &operator=(Exception_Handler &&) = default;
virtual ~Exception_Handler() noexcept = default;
protected: protected:
Exception_Handler(const Exception_Handler &) = default; template<typename T>
Exception_Handler &operator=(const Exception_Handler &) = default;
};
template<typename ... T>
struct Exception_Handler_Impl final : Exception_Handler
{
template<typename ExceptionType>
static void throw_type(const Boxed_Value &bv, const Dispatch_Engine &t_engine) static void throw_type(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
try { throw t_engine.boxed_cast<ExceptionType>(bv); } catch (const chaiscript::exception::bad_boxed_cast &) {} try { T t = t_engine.boxed_cast<T>(bv); throw t; } catch (const chaiscript::exception::bad_boxed_cast &) {}
} }
};
void operator()(const Boxed_Value &bv, const Dispatch_Engine &t_engine) const final template<typename ... T>
struct Exception_Handler_Impl : Exception_Handler_Base
{
void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine) override
{ {
(void)std::initializer_list<int>{(throw_type<T>(bv, t_engine), 0)...}; (void)std::initializer_list<int>{(throw_type<T>(bv, t_engine), 0)...};
} }
@ -111,13 +101,14 @@ namespace chaiscript
/// ///
/// \sa chaiscript::exception_specification for creation of chaiscript::Exception_Handler objects /// \sa chaiscript::exception_specification for creation of chaiscript::Exception_Handler objects
/// \sa \ref exceptions /// \sa \ref exceptions
typedef std::shared_ptr<detail::Exception_Handler_Base> Exception_Handler;
/// \brief creates a chaiscript::Exception_Handler which handles one type of exception unboxing /// \brief creates a chaiscript::Exception_Handler which handles one type of exception unboxing
/// \sa \ref exceptions /// \sa \ref exceptions
template<typename ... T> template<typename ... T>
auto exception_specification() noexcept Exception_Handler exception_specification()
{ {
return detail::Exception_Handler_Impl<T...>(); return std::make_shared<detail::Exception_Handler_Impl<T...>>();
} }
} }

View File

@ -623,12 +623,12 @@ explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&pars
/// \brief Evaluates a string. Equivalent to ChaiScript::eval. /// \brief Evaluates a string. Equivalent to ChaiScript::eval.
/// ///
/// \param[in] t_script Script to execute /// \param[in] t_script Script to execute
/// \param[in] t_handler Optional detail::Exception_Handler used for automatic unboxing of script thrown exceptions /// \param[in] t_handler Optional Exception_Handler used for automatic unboxing of script thrown exceptions
/// ///
/// \return result of the script execution /// \return result of the script execution
/// ///
/// \throw chaiscript::exception::eval_error In the case that evaluation fails. /// \throw chaiscript::exception::eval_error In the case that evaluation fails.
Boxed_Value operator()(const std::string &t_script, const detail::Exception_Handler &t_handler = detail::Exception_Handler()) Boxed_Value operator()(const std::string &t_script, const Exception_Handler &t_handler = Exception_Handler())
{ {
return eval(t_script, t_handler); return eval(t_script, t_handler);
} }
@ -637,7 +637,7 @@ explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&pars
/// ///
/// \tparam T Type to extract from the result value of the script execution /// \tparam T Type to extract from the result value of the script execution
/// \param[in] t_input Script to execute /// \param[in] t_input Script to execute
/// \param[in] t_handler Optional detail::Exception_Handler used for automatic unboxing of script thrown exceptions /// \param[in] t_handler Optional Exception_Handler used for automatic unboxing of script thrown exceptions
/// \param[in] t_filename Optional filename to report to the user for where the error occured. Useful /// \param[in] t_filename Optional filename to report to the user for where the error occured. Useful
/// in special cases where you are loading a file internally instead of using eval_file /// in special cases where you are loading a file internally instead of using eval_file
/// ///
@ -647,7 +647,7 @@ explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&pars
/// \throw chaiscript::exception::bad_boxed_cast In the case that evaluation succeeds but the result value cannot be converted /// \throw chaiscript::exception::bad_boxed_cast In the case that evaluation succeeds but the result value cannot be converted
/// to the requested type. /// to the requested type.
template<typename T> template<typename T>
T eval(const std::string &t_input, const detail::Exception_Handler &t_handler = detail::Exception_Handler(), const std::string &t_filename="__EVAL__") T eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__")
{ {
return m_engine.boxed_cast<T>(eval(t_input, t_handler, t_filename)); return m_engine.boxed_cast<T>(eval(t_input, t_handler, t_filename));
} }
@ -663,42 +663,44 @@ explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&pars
/// \brief Evaluates a string. /// \brief Evaluates a string.
/// ///
/// \param[in] t_input Script to execute /// \param[in] t_input Script to execute
/// \param[in] t_handler Optional detail::Exception_Handler used for automatic unboxing of script thrown exceptions /// \param[in] t_handler Optional Exception_Handler used for automatic unboxing of script thrown exceptions
/// \param[in] t_filename Optional filename to report to the user for where the error occurred. Useful /// \param[in] t_filename Optional filename to report to the user for where the error occurred. Useful
/// in special cases where you are loading a file internally instead of using eval_file /// in special cases where you are loading a file internally instead of using eval_file
/// ///
/// \return result of the script execution /// \return result of the script execution
/// ///
/// \throw exception::eval_error In the case that evaluation fails. /// \throw exception::eval_error In the case that evaluation fails.
Boxed_Value eval(const std::string &t_input, const detail::Exception_Handler &t_handler = detail::Exception_Handler(), const std::string &t_filename="__EVAL__") Boxed_Value eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__")
{ {
try { try {
return do_eval(t_input, t_filename); return do_eval(t_input, t_filename);
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
t_handler(bv, m_engine); if (t_handler) {
t_handler->handle(bv, m_engine);
}
throw; throw;
} }
} }
/// \brief Loads the file specified by filename, evaluates it, and returns the result. /// \brief Loads the file specified by filename, evaluates it, and returns the result.
/// \param[in] t_filename File to load and parse. /// \param[in] t_filename File to load and parse.
/// \param[in] t_handler Optional detail::Exception_Handler used for automatic unboxing of script thrown exceptions /// \param[in] t_handler Optional Exception_Handler used for automatic unboxing of script thrown exceptions
/// \return result of the script execution /// \return result of the script execution
/// \throw chaiscript::exception::eval_error In the case that evaluation fails. /// \throw chaiscript::exception::eval_error In the case that evaluation fails.
Boxed_Value eval_file(const std::string &t_filename, const detail::Exception_Handler &t_handler = detail::Exception_Handler()) { Boxed_Value eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
return eval(load_file(t_filename), t_handler, t_filename); return eval(load_file(t_filename), t_handler, t_filename);
} }
/// \brief Loads the file specified by filename, evaluates it, and returns the type safe result. /// \brief Loads the file specified by filename, evaluates it, and returns the type safe result.
/// \tparam T Type to extract from the result value of the script execution /// \tparam T Type to extract from the result value of the script execution
/// \param[in] t_filename File to load and parse. /// \param[in] t_filename File to load and parse.
/// \param[in] t_handler Optional detail::Exception_Handler used for automatic unboxing of script thrown exceptions /// \param[in] t_handler Optional Exception_Handler used for automatic unboxing of script thrown exceptions
/// \return result of the script execution /// \return result of the script execution
/// \throw chaiscript::exception::eval_error In the case that evaluation fails. /// \throw chaiscript::exception::eval_error In the case that evaluation fails.
/// \throw chaiscript::exception::bad_boxed_cast In the case that evaluation succeeds but the result value cannot be converted /// \throw chaiscript::exception::bad_boxed_cast In the case that evaluation succeeds but the result value cannot be converted
/// to the requested type. /// to the requested type.
template<typename T> template<typename T>
T eval_file(const std::string &t_filename, const detail::Exception_Handler &t_handler = detail::Exception_Handler()) { T eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
return m_engine.boxed_cast<T>(eval_file(t_filename, t_handler)); return m_engine.boxed_cast<T>(eval_file(t_filename, t_handler));
} }
}; };