maybe a simpler unique_ptr

This commit is contained in:
Eric Mertens 2023-01-14 20:00:55 -08:00
parent 0d3c9e3422
commit 6614e5e8ea
3 changed files with 7 additions and 25 deletions

View File

@ -13,6 +13,8 @@
namespace { namespace {
using It = std::istream_iterator<std::string>;
// //
template <typename T> template <typename T>
auto DivMod(T dividend, T divisor) -> std::pair<T, T> auto DivMod(T dividend, T divisor) -> std::pair<T, T>
@ -59,8 +61,7 @@ auto ToInt(std::string const& str) -> T
auto Solve(std::istream & in) -> std::string auto Solve(std::istream & in) -> std::string
{ {
using It = std::istream_iterator<std::string>; return FromInt(std::transform_reduce(It{in}, It{}, std::int64_t{}, std::plus(), ToInt<std::int64_t>));
return FromInt(std::transform_reduce(It(in), It(), 0, std::plus(), ToInt<std::int64_t>));
} }
} // namespace } // namespace

View File

@ -8,17 +8,7 @@
namespace aocpp { namespace aocpp {
class ConditionalDeleter { auto Startup(int argc, char ** argv) -> std::unique_ptr<std::istream, void(*)(std::istream*)>;
bool should_delete_;
public:
ConditionalDeleter(bool should_delete) : should_delete_ {should_delete} {}
template <typename T>
auto operator()(T * pointer) -> void { if (should_delete_) { delete pointer; }}
};
auto Startup(int argc, char ** argv) -> std::unique_ptr<std::istream, ConditionalDeleter>;
} }

View File

@ -12,29 +12,20 @@
namespace aocpp { namespace aocpp {
auto Startup(int argc, char ** argv) -> std::unique_ptr<std::istream, ConditionalDeleter> { auto Startup(int argc, char ** argv) -> std::unique_ptr<std::istream, void(*)(std::istream*)> {
if (std::getenv("DOCTEST")) { if (std::getenv("DOCTEST")) {
exit(doctest::Context{argc, argv}.run()); exit(doctest::Context{argc, argv}.run());
} }
bool should_delete;
std::istream* result_ptr;
switch (argc) { switch (argc) {
case 2: case 2:
should_delete = true; return {new std::ifstream{argv[1]}, [](std::istream* p) { delete p; }};
result_ptr = new std::ifstream{argv[1]};
break;
case 1: case 1:
should_delete = false; return {&std::cin, [](std::istream*){}};
result_ptr = &std::cin;
break;
default: default:
std::cerr << "bad arguments\n"; std::cerr << "bad arguments\n";
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return {result_ptr, should_delete};
} }
} }