remove unique_ptr from main

This commit is contained in:
Eric Mertens 2023-01-31 20:46:03 -08:00
parent c074e66157
commit 8004218a79

View File

@ -2,10 +2,9 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <optional>
#include <memory>
#include <fstream> #include <fstream>
#include <utility> #include <utility>
#include <unistd.h> #include <unistd.h>
#define DOCTEST_CONFIG_IMPLEMENT #define DOCTEST_CONFIG_IMPLEMENT
@ -41,27 +40,22 @@ auto main(int argc, char ** argv) -> int
return doctest::Context{argc, argv}.run(); return doctest::Context{argc, argv}.run();
} }
std::unique_ptr<std::istream, void(*)(std::istream*)> in std::ifstream fin;
{in_name std::ofstream fout;
? decltype(in){new std::ifstream{in_name}, [](auto p) { delete p; }} std::istream & in {in_name ? fin = std::ifstream{in_name } : std::cin };
: decltype(in){&std::cin, [](auto){}}}; std::ostream & out {out_name ? fout = std::ofstream{out_name} : std::cout};
if (in->fail()) { if (in.fail()) {
std::cerr << "Bad input file" << std::endl; std::cerr << "Bad input file" << std::endl;
return 1; return 1;
} }
std::unique_ptr<std::ostream, void(*)(std::ostream*)> out if (out.fail()) {
{out_name
? decltype(out){new std::ofstream{out_name}, [](auto p) { delete p; }}
: decltype(out){&std::cout, [](auto){}}};
if (out->fail()) {
std::cerr << "Bad output file" << std::endl; std::cerr << "Bad output file" << std::endl;
return 1; return 1;
} }
Main(*in, *out); Main(in, out);
} catch (std::exception const& e) { } catch (std::exception const& e) {
std::cerr << "Program failed: " << e.what() << std::endl; std::cerr << "Program failed: " << e.what() << std::endl;
return 1; return 1;