Commit a40a1ccb authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

main: replace ostream with fmt::formatter

parent 27bb7597
Pipeline #6262 passed with stage
in 3 minutes and 22 seconds
......@@ -673,15 +673,14 @@ try_benchmark(const baryonyx::context_ptr& ctx,
"{}\n",
e.line(),
e.column(),
file_format_error_format(e.failure()));
e.failure());
} catch (const baryonyx::problem_definition_failure& e) {
fmt::print(stderr,
"definition problem error at {}: {}\n",
e.element(),
problem_definition_error_format(e.failure()));
e.failure());
} catch (const baryonyx::solver_failure& e) {
fmt::print(
stderr, "solver error: {}\n", solver_error_format(e.failure()));
fmt::print(stderr, "solver error: {}\n", e.failure());
} catch (const std::exception& e) {
fmt::print(stderr, "failure: {}.\n", e.what());
}
......
......@@ -1170,10 +1170,7 @@ main(int argc, const char* argv[])
if (params.filenames.size() == 1) {
auto pb = baryonyx::make_problem(ctx, params.filenames.front());
if (!pb) {
fmt::print(stderr,
"Fail to read file: {} ({})\n",
file_format_error_format(pb.status),
static_cast<int>(pb.status));
fmt::print(stderr, "Fail to read file: {}\n", pb.status);
} else {
try {
auto filename = fmt::format(
......@@ -1244,16 +1241,14 @@ main(int argc, const char* argv[])
"{}\n",
e.line(),
e.column(),
file_format_error_format(e.failure()));
e.failure());
} catch (const baryonyx::problem_definition_failure& e) {
fmt::print(stderr,
"definition problem error at {}: {}\n",
e.element(),
problem_definition_error_format(e.failure()));
e.failure());
} catch (const baryonyx::solver_failure& e) {
fmt::print(stderr,
"solver error: {}\n",
solver_error_format(e.failure()));
fmt::print(stderr, "solver error: {}\n", e.failure());
} catch (const std::exception& e) {
fmt::print(stderr, "failure: {}.\n", e.what());
}
......@@ -1265,10 +1260,7 @@ main(int argc, const char* argv[])
for (auto& elem : params.filenames) {
auto pb = baryonyx::make_problem(ctx, elem);
if (!pb) {
fmt::print(stderr,
"Fail to read file: {} ({})\n",
file_format_error_format(pb.status),
static_cast<int>(pb.status));
fmt::print(stderr, "Fail to read file: {}\n", pb.status);
continue;
}
......@@ -1310,16 +1302,14 @@ main(int argc, const char* argv[])
"{}\n",
e.line(),
e.column(),
file_format_error_format(e.failure()));
e.failure());
} catch (const baryonyx::problem_definition_failure& e) {
fmt::print(stderr,
"definition problem error at {}: {}\n",
e.element(),
problem_definition_error_format(e.failure()));
e.failure());
} catch (const baryonyx::solver_failure& e) {
fmt::print(stderr,
"solver error: {}\n",
solver_error_format(e.failure()));
fmt::print(stderr, "solver error: {}\n", e.failure());
} catch (const std::exception& e) {
fmt::print(stderr, "failure: {}.\n", e.what());
}
......
......@@ -138,53 +138,4 @@ to_long(const std::string_view s) noexcept
return std::nullopt;
}
constexpr std::string_view file_format_error_string[] = {
"success",
"file_not_found",
"bad_end_of_file",
"bad_general",
"bad_binary",
"bad_objective_function_type",
"bad_objective",
"bad_objective_quadratic",
"bad_bound",
"bad_end",
"bad_constraint",
"too_many_variables",
"too_many_constraints",
"bad_name"
};
constexpr std::string_view
file_format_error_format(baryonyx::file_format_error_tag failure) noexcept
{
return file_format_error_string[static_cast<
std::underlying_type<baryonyx::file_format_error_tag>::type>(failure)];
}
constexpr const char*
problem_definition_error_format(
baryonyx::problem_definition_error_tag failure) noexcept
{
constexpr const char* const tag[] = {
"empty variables",
"empty objective function",
"variable not used",
"bad bound",
"multiple constraints with different value"
};
return tag[static_cast<int>(failure)];
}
constexpr const char*
solver_error_format(baryonyx::solver_error_tag failure) noexcept
{
constexpr const char* tag[] = { "no solver available",
"unrealisable constraint",
"not enough memory" };
return tag[static_cast<int>(failure)];
}
#endif
......@@ -85,6 +85,37 @@ struct fmt::formatter<baryonyx::file_format_error_tag>
}
};
template<>
struct fmt::formatter<baryonyx::problem_definition_error_tag>
: fmt::formatter<string_view>
{
template<typename FormatContext>
auto format(baryonyx::problem_definition_error_tag c, FormatContext& ctx)
{
string_view name = "unknown";
switch (c) {
case baryonyx::problem_definition_error_tag::empty_variables:
name = "empty_variables";
break;
case baryonyx::problem_definition_error_tag::empty_objective_function:
name = "empty_objective_function";
break;
case baryonyx::problem_definition_error_tag::variable_not_used:
name = "variable_not_used";
break;
case baryonyx::problem_definition_error_tag::bad_bound:
name = "bad_bound";
break;
case baryonyx::problem_definition_error_tag::multiple_constraint:
name = "multiple_constraint";
break;
}
return formatter<string_view>::format(name, ctx);
}
};
template<>
struct fmt::formatter<baryonyx::result_status> : fmt::formatter<string_view>
{
......
......@@ -36,8 +36,6 @@
#include <unordered_map>
#include <utility>
#include <fmt/printf.h>
#include <cerrno>
#include <climits>
#include <cmath>
......
......@@ -32,8 +32,6 @@
#include <baryonyx/core>
#include <utility>
#include <fmt/printf.h>
void
test_examples_1()
{
......@@ -722,7 +720,7 @@ test_verger_5_5()
}
int
main(int /* argc */, char* /* argv */ [])
main(int /* argc */, char* /* argv */[])
{
unit_test::checks("examples_1", test_examples_1);
unit_test::checks("examples_quadratic_1", test_examples_quadratic_0);
......
......@@ -31,8 +31,6 @@
#include <baryonyx/core>
#include <fmt/printf.h>
#include <functional>
#include <numeric>
......@@ -895,7 +893,7 @@ check_bit_array()
}
int
main(int /* argc */, char* /* argv */ [])
main(int /* argc */, char* /* argv */[])
{
unit_test::checks("check_print_api", check_print_api);
unit_test::checks("check_numeric_cast", check_numeric_cast);
......
......@@ -26,8 +26,6 @@
#include <baryonyx/core-test>
#include <baryonyx/core>
#include <fmt/printf.h>
#include <fstream>
#include <map>
#include <numeric>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment