Skip to content

Commit

Permalink
ec use locations.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Feb 8, 2023
1 parent 6fd1200 commit 81ad643
Show file tree
Hide file tree
Showing 26 changed files with 189 additions and 168 deletions.
2 changes: 1 addition & 1 deletion include/boost/process/v2/cstring_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct basic_cstring_ref
BOOST_CXX14_CONSTEXPR const_reference at(size_type pos) const
{
if (pos >= size())
throw std::out_of_range("cstring-view out of range");
throw_exception(std::out_of_range("cstring-view out of range"));
return view_[pos];
}
BOOST_CONSTEXPR const_reference front() const {return *view_;}
Expand Down
29 changes: 27 additions & 2 deletions include/boost/process/v2/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ namespace filesystem = std::filesystem;
using std::quoted;
using std::optional;

#define BOOST_PROCESS_V2_RETURN_EC(ev) \
return ::BOOST_PROCESS_V2_NAMESPACE::error_code(ev, ::BOOST_PROCESS_V2_NAMESPACE::system_category()); \

#define BOOST_PROCESS_V2_ASSIGN_EC(ec, ...) ec.assign(__VA_ARGS__);
#define BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) \
ec.assign(::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error()); \


#else

using boost::system::error_code ;
Expand All @@ -112,6 +120,25 @@ namespace filesystem = std::filesystem;
namespace filesystem = boost::filesystem;
#endif

#define BOOST_PROCESS_V2_RETURN_EC(ev) \
{ \
static constexpr auto loc##__LINE__((BOOST_CURRENT_LOCATION)); \
return ::BOOST_PROCESS_V2_NAMESPACE::error_code(ev, ::BOOST_PROCESS_V2_NAMESPACE::system_category(), &loc##__LINE__); \
}

#define BOOST_PROCESS_V2_ASSIGN_EC(ec, ...) \
{ \
static constexpr auto loc##__LINE__((BOOST_CURRENT_LOCATION)); \
ec.assign(__VA_ARGS__, &loc##__LINE__); \
}

#define BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) \
{ \
static constexpr auto loc##__LINE__((BOOST_CURRENT_LOCATION)); \
ec.assign(::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error(), &loc##__LINE__); \
}


#endif

BOOST_PROCESS_V2_END_NAMESPACE
Expand Down Expand Up @@ -150,6 +177,4 @@ BOOST_PROCESS_V2_END_NAMESPACE
#define BOOST_PROCESS_V2_HAS_PROCESS_HANDLE 1
#endif



#endif //BOOST_PROCESS_V2_DETAIL_CONFIG_HPP
6 changes: 3 additions & 3 deletions include/boost/process/v2/detail/impl/environment_posix.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ basic_cstring_ref<char_type, value_char_traits<char>> get(
auto res = ::getenv(key.c_str());
if (res == nullptr)
{
ec.assign(ENOENT, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, ENOENT, system_category())
return {};
}
return res;
Expand All @@ -45,13 +45,13 @@ void set(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
error_code & ec)
{
if (::setenv(key.c_str(), value.c_str(), true))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> key, error_code & ec)
{
if (::unsetenv(key.c_str()))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}


Expand Down
12 changes: 6 additions & 6 deletions include/boost/process/v2/detail/impl/environment_win.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ std::basic_string<char_type, value_char_traits<char_type>> get(
buf.resize(size);

if (buf.size() == 0)
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)

return buf;
}
Expand All @@ -60,14 +60,14 @@ void set(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
error_code & ec)
{
if (!::SetEnvironmentVariableW(key.c_str(), value.c_str()))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
error_code & ec)
{
if (!::SetEnvironmentVariableW(key.c_str(), nullptr))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}


Expand All @@ -88,7 +88,7 @@ std::basic_string<char, value_char_traits<char>> get(
buf.resize(size);

if (buf.size() == 0)
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)

return buf;
}
Expand All @@ -98,14 +98,14 @@ void set(basic_cstring_ref<char, key_char_traits<char>> key,
error_code & ec)
{
if (!::SetEnvironmentVariableA(key.c_str(), value.c_str()))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void unset(basic_cstring_ref<char, key_char_traits<char>> key,
error_code & ec)
{
if (!::SetEnvironmentVariableA(key.c_str(), nullptr))
ec = ::BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}


Expand Down
14 changes: 0 additions & 14 deletions include/boost/process/v2/detail/impl/last_error.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ error_code get_last_error()

}

void throw_last_error()
{
throw system_error(get_last_error());
}
void throw_last_error(const char * msg)
{
throw system_error(get_last_error(), msg);
}
void throw_last_error(const std::string & msg)
{
throw system_error(get_last_error(), msg);
}


}
BOOST_PROCESS_V2_END_NAMESPACE

Expand Down
31 changes: 18 additions & 13 deletions include/boost/process/v2/detail/impl/process_handle_windows.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ void get_exit_code_(
error_code & ec)
{
if (!::GetExitCodeProcess(handle, &exit_code))
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}


HANDLE open_process_(DWORD pid)
{
auto proc = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pid);
if (proc == nullptr)
detail::throw_last_error("open_process()");
{
error_code ec;
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
throw system_error(ec, "open_process()");
}

return proc;
}

Expand All @@ -61,7 +66,7 @@ bool check_handle_(HANDLE handle, error_code & ec)
{
if (handle == INVALID_HANDLE_VALUE)
{
ec.assign(ERROR_INVALID_HANDLE_STATE, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_INVALID_HANDLE_STATE, system_category())
return false;
}
return true;
Expand All @@ -71,7 +76,7 @@ bool check_pid_(pid_type pid_, error_code & ec)
{
if (pid_ == 0)
{
ec.assign(ERROR_INVALID_HANDLE_STATE, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_INVALID_HANDLE_STATE, system_category())
return false;
}
return true;
Expand All @@ -94,7 +99,7 @@ static BOOL CALLBACK enum_window(HWND hwnd, LPARAM param)
LRESULT res = ::SendMessageW(hwnd, WM_CLOSE, 0, 0);

if (res)
data->ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(data->ec)
return res == 0;
}

Expand All @@ -103,25 +108,25 @@ void request_exit_(pid_type pid_, error_code & ec)
enum_windows_data_t data{ec, pid_};

if (!::EnumWindows(enum_window, reinterpret_cast<LONG_PTR>(&data)))
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void interrupt_(pid_type pid_, error_code & ec)
{
if (!::GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid_))
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void terminate_(HANDLE handle, error_code & ec, DWORD & exit_status)
{
if (!::TerminateProcess(handle, 260))
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void check_running_(HANDLE handle, error_code & ec, DWORD & exit_status)
{
if (!::GetExitCodeProcess(handle, &exit_status))
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

#if !defined(BOOST_PROCESS_V2_DISABLE_UNDOCUMENTED_API)
Expand All @@ -130,25 +135,25 @@ void suspend_(HANDLE handle, error_code & ec)
auto nt_err = NtSuspendProcess(handle);
ULONG dos_err = RtlNtStatusToDosError(nt_err);
if (dos_err)
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}

void resume_(HANDLE handle, error_code & ec)
{
auto nt_err = NtResumeProcess(handle);
ULONG dos_err = RtlNtStatusToDosError(nt_err);
if (dos_err)
ec = detail::get_last_error();
BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
}
#else
void suspend_(HANDLE, error_code & ec)
{
ec.assign(ERROR_CALL_NOT_IMPLEMENTED, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_CALL_NOT_IMPLEMENTED, system_category())
}

void resume_(HANDLE handle, error_code & ec)
{
ec.assign(ERROR_CALL_NOT_IMPLEMENTED, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, ERROR_CALL_NOT_IMPLEMENTED, system_category())
}
#endif

Expand Down
20 changes: 10 additions & 10 deletions include/boost/process/v2/detail/impl/utf8.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ inline void handle_error(error_code & ec)
switch (err)
{
case ERROR_INSUFFICIENT_BUFFER:
ec.assign(error::insufficient_buffer, error::utf8_category);
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::insufficient_buffer, error::utf8_category)
break;
case ERROR_NO_UNICODE_TRANSLATION:
ec.assign(error::invalid_character, error::utf8_category);
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::invalid_character, error::utf8_category)
break;
default:
ec.assign(err, system_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, err, system_category())
}
}

Expand Down Expand Up @@ -242,7 +242,7 @@ std::size_t convert_to_utf8(const wchar_t * in, std::size_t size,
if (*from > max_wchar) {
from_next = from;
to_next = to;
ec.assign(error::invalid_character, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::invalid_character, error::get_utf8_category())
return 0u;
}

Expand Down Expand Up @@ -270,7 +270,7 @@ std::size_t convert_to_utf8(const wchar_t * in, std::size_t size,
if (to == to_end && i != cont_octet_count) {
from_next = from;
to_next = to - (i + 1);
ec.assign(error::insufficient_buffer, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::insufficient_buffer, error::get_utf8_category())
return 0u;
}
++from;
Expand All @@ -280,7 +280,7 @@ std::size_t convert_to_utf8(const wchar_t * in, std::size_t size,

// Were we done or did we run out of destination space
if (from != from_end)
ec.assign(error::insufficient_buffer, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::insufficient_buffer, error::get_utf8_category())

return to_next - out;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ std::size_t convert_to_wide(const char * in, std::size_t size,
if (invalid_leading_octet(*from)) {
from_next = from;
to_next = to;
ec.assign(error::invalid_character, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::invalid_character, error::get_utf8_category())
return 0u;
}

Expand All @@ -339,7 +339,7 @@ std::size_t convert_to_wide(const char * in, std::size_t size,
if (invalid_continuing_octet(*from)) {
from_next = from;
to_next = to;
ec.assign(error::invalid_character, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::invalid_character, error::get_utf8_category())
return 0u;
}

Expand All @@ -356,7 +356,7 @@ std::size_t convert_to_wide(const char * in, std::size_t size,
// rewind "from" to before the current character translation
from_next = from - (i + 1);
to_next = to;
ec.assign(error::insufficient_buffer, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::insufficient_buffer, error::get_utf8_category())
return 0u;
}
*to++ = ucs_result;
Expand All @@ -365,7 +365,7 @@ std::size_t convert_to_wide(const char * in, std::size_t size,
to_next = to;

if (from != from_end)
ec.assign(error::insufficient_buffer, error::get_utf8_category());
BOOST_PROCESS_V2_ASSIGN_EC(ec, error::insufficient_buffer, error::get_utf8_category())

return to_next - out;
}
Expand Down
7 changes: 3 additions & 4 deletions include/boost/process/v2/detail/last_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

BOOST_PROCESS_V2_BEGIN_NAMESPACE

namespace detail {
namespace detail
{

BOOST_PROCESS_V2_DECL error_code get_last_error();
BOOST_PROCESS_V2_DECL void throw_last_error();
BOOST_PROCESS_V2_DECL void throw_last_error(const char * msg);
BOOST_PROCESS_V2_DECL void throw_last_error(const std::string & msg);

}

BOOST_PROCESS_V2_END_NAMESPACE

#if defined(BOOST_PROCESS_V2_HEADER_ONLY)
Expand Down
2 changes: 1 addition & 1 deletion include/boost/process/v2/exit_code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct code_as_error_handler
void operator()(error_code ec, native_exit_code_type code)
{
if (!ec)
ec.assign(code, category);
BOOST_PROCESS_V2_ASSIGN_EC(ec, code, category)
std::move(handler_)(ec);
}

Expand Down
Loading

0 comments on commit 81ad643

Please sign in to comment.