vcpkg/toolsrc/include/vcpkg_Checks.h

55 lines
2.1 KiB
C
Raw Normal View History

2016-09-19 11:50:08 +08:00
#pragma once
2017-03-14 08:14:00 +08:00
#include "LineInfo.h"
#include "vcpkg_Strings.h"
2017-03-14 07:04:29 +08:00
2017-01-06 04:47:08 +08:00
namespace vcpkg::Checks
2016-09-19 11:50:08 +08:00
{
void register_console_ctrl_handler();
2017-08-19 11:32:35 +08:00
// Indicate that an internal error has occurred and exit the tool. This should be used when invariants have been
// broken.
[[noreturn]] void unreachable(const LineInfo& line_info);
2016-09-19 11:50:08 +08:00
[[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code);
2017-08-19 11:32:35 +08:00
// Exit the tool without an error message.
[[noreturn]] inline void exit_fail(const LineInfo& line_info) { exit_with_code(line_info, EXIT_FAILURE); }
2017-08-19 11:32:35 +08:00
// Exit the tool successfully.
[[noreturn]] inline void exit_success(const LineInfo& line_info) { exit_with_code(line_info, EXIT_SUCCESS); }
2017-08-19 11:32:35 +08:00
// Display an error message to the user and exit the tool.
2017-09-02 08:05:22 +08:00
[[noreturn]] void exit_with_message(const LineInfo& line_info, const CStringView error_message);
2016-09-19 11:50:08 +08:00
template<class Arg1, class... Args>
2017-08-19 11:32:35 +08:00
// Display an error message to the user and exit the tool.
[[noreturn]] void exit_with_message(const LineInfo& line_info,
2017-09-02 08:05:22 +08:00
const char* error_message_template,
const Arg1 error_message_arg1,
const Args&... error_message_args)
2016-09-19 11:50:08 +08:00
{
2017-09-02 08:05:22 +08:00
exit_with_message(line_info,
Strings::format(error_message_template, error_message_arg1, error_message_args...));
2016-09-19 11:50:08 +08:00
}
void check_exit(const LineInfo& line_info, bool expression);
2017-09-02 08:05:22 +08:00
void check_exit(const LineInfo& line_info, bool expression, const CStringView error_message);
2016-09-19 11:50:08 +08:00
template<class Conditional, class Arg1, class... Args>
void check_exit(const LineInfo& line_info,
Conditional&& expression,
2017-09-02 08:05:22 +08:00
const char* error_message_template,
const Arg1 error_message_arg1,
const Args&... error_message_args)
2016-09-19 11:50:08 +08:00
{
if (!expression)
{
// Only create the string if the expression is false
2017-09-02 08:05:22 +08:00
exit_with_message(line_info,
Strings::format(error_message_template, error_message_arg1, error_message_args...));
2016-09-19 11:50:08 +08:00
}
}
2017-01-06 04:47:08 +08:00
}