Skip to content

Commit

Permalink
Rework logic for processing exception_info
Browse files Browse the repository at this point in the history
...to avoid issue with MSVC exceptions being stored in the stack.
  • Loading branch information
K-ballo committed Jul 21, 2017
1 parent 4307260 commit 6e48d68
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 53 deletions.
90 changes: 45 additions & 45 deletions hpx/exception.hpp
Expand Up @@ -421,9 +421,9 @@ namespace hpx
template <typename E>
std::string diagnostic_information(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return diagnostic_information(*xi);
return std::string("<unknown>");
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? diagnostic_information(*xi) : std::string("<unknown>");
});
}
/// \endcond

Expand Down Expand Up @@ -464,9 +464,9 @@ namespace hpx
template <typename E>
std::string get_error_what(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_what(*xi);
return std::string("<unknown>");
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_what(*xi) : std::string("<unknown>");
});
}

inline std::string get_error_what(hpx::error_code const& e)
Expand Down Expand Up @@ -519,9 +519,9 @@ namespace hpx
template <typename E>
std::uint32_t get_error_locality_id(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_locality_id(*xi);
return naming::invalid_locality_id;
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_locality_id(*xi) : naming::invalid_locality_id;
});
}
/// \endcond

Expand Down Expand Up @@ -597,9 +597,9 @@ namespace hpx
template <typename E>
std::string get_error_host_name(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_host_name(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_host_name(*xi) : std::string();
});
}
/// \endcond

Expand Down Expand Up @@ -638,9 +638,9 @@ namespace hpx
template <typename E>
std::int64_t get_error_process_id(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_process_id(*xi);
return -1;
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_process_id(*xi) : -1;
});
}
/// \endcond

Expand Down Expand Up @@ -679,9 +679,9 @@ namespace hpx
template <typename E>
std::string get_error_env(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_env(*xi);
return "<unknown>";
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_env(*xi) : std::string("<unknown>");
});
}
/// \endcond

Expand Down Expand Up @@ -719,9 +719,9 @@ namespace hpx
template <typename E>
std::string get_error_function_name(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_function_name(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_function_name(*xi) : std::string();
});
}
/// \endcond

Expand Down Expand Up @@ -759,9 +759,9 @@ namespace hpx
template <typename E>
std::string get_error_backtrace(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_backtrace(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_backtrace(*xi) : std::string();
});
}
/// \endcond

Expand Down Expand Up @@ -801,9 +801,9 @@ namespace hpx
template <typename E>
std::string get_error_file_name(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_file_name(*xi);
return "<unknown>";
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_file_name(*xi) : std::string("<unknown>");
});
}
/// \endcond

Expand Down Expand Up @@ -842,9 +842,9 @@ namespace hpx
template <typename E>
long get_error_line_number(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_line_number(*xi);
return -1;
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_line_number(*xi) : -1;
});
}
/// \endcond

Expand Down Expand Up @@ -884,9 +884,9 @@ namespace hpx
template <typename E>
std::size_t get_error_os_thread(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_os_thread(*xi);
return std::size_t(-1);
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_os_thread(*xi) : std::size_t(-1);
});
}
/// \endcond

Expand Down Expand Up @@ -926,9 +926,9 @@ namespace hpx
template <typename E>
std::size_t get_error_thread_id(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_thread_id(*xi);
return std::size_t(-1);
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_thread_id(*xi) : std::size_t(-1);
});
}
/// \endcond

Expand Down Expand Up @@ -967,9 +967,9 @@ namespace hpx
template <typename E>
std::string get_error_thread_description(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_thread_description(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_thread_description(*xi) : std::string();
});
}
/// \endcond

Expand Down Expand Up @@ -1008,9 +1008,9 @@ namespace hpx
template <typename E>
std::string get_error_config(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_config(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_config(*xi) : std::string();
});
}
/// \endcond

Expand Down Expand Up @@ -1049,9 +1049,9 @@ namespace hpx
template <typename E>
std::string get_error_state(E const& e)
{
if (exception_info const* xi = get_exception_info(e))
return get_error_state(*xi);
return std::string();
return invoke_with_exception_info(e, [](exception_info const* xi) {
return xi ? get_error_state(*xi) : std::string();
});
}
/// \endcond

Expand Down
30 changes: 22 additions & 8 deletions hpx/exception_info.hpp
Expand Up @@ -210,33 +210,47 @@ namespace hpx
}

///////////////////////////////////////////////////////////////////////////
template <typename E>
exception_info* get_exception_info(E& e)
{
return dynamic_cast<exception_info*>(std::addressof(e));
}

template <typename E>
exception_info const* get_exception_info(E const& e)
{
return dynamic_cast<exception_info const*>(std::addressof(e));
}

template <typename E>
exception_info* get_exception_info(E& e)
///////////////////////////////////////////////////////////////////////////
template <typename E, typename F>
auto invoke_with_exception_info(E const& e, F&& f)
-> decltype(std::forward<F>(f)(std::declval<exception_info const*>()))
{
return dynamic_cast<exception_info*>(std::addressof(e));
return std::forward<F>(f)(
dynamic_cast<exception_info const*>(std::addressof(e)));
}

inline exception_info const* get_exception_info(std::exception_ptr const& p)
template <typename F>
auto invoke_with_exception_info(std::exception_ptr const& p, F&& f)
-> decltype(std::forward<F>(f)(std::declval<exception_info const*>()))
{
try
{
if (p) std::rethrow_exception(p);
} catch (exception_info const& xi) {
return &xi;
return std::forward<F>(f)(&xi);
} catch (...) {
}
return nullptr;
return std::forward<F>(f)(nullptr);
}

inline exception_info const* get_exception_info(hpx::error_code const& ec)
template <typename F>
auto invoke_with_exception_info(hpx::error_code const& ec, F&& f)
-> decltype(std::forward<F>(f)(std::declval<exception_info const*>()))
{
return get_exception_info(detail::access_exception(ec));
return invoke_with_exception_info(
detail::access_exception(ec), std::forward<F>(f));
}
}

Expand Down

0 comments on commit 6e48d68

Please sign in to comment.