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 {
using It = std::istream_iterator<std::string>;
//
template <typename 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
{
using It = std::istream_iterator<std::string>;
return FromInt(std::transform_reduce(It(in), It(), 0, std::plus(), ToInt<std::int64_t>));
return FromInt(std::transform_reduce(It{in}, It{}, std::int64_t{}, std::plus(), ToInt<std::int64_t>));
}
} // namespace

View File

@ -8,17 +8,7 @@
namespace aocpp {
class ConditionalDeleter {
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>;
auto Startup(int argc, char ** argv) -> std::unique_ptr<std::istream, void(*)(std::istream*)>;
}

View File

@ -12,29 +12,20 @@
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")) {
exit(doctest::Context{argc, argv}.run());
}
bool should_delete;
std::istream* result_ptr;
switch (argc) {
case 2:
should_delete = true;
result_ptr = new std::ifstream{argv[1]};
break;
return {new std::ifstream{argv[1]}, [](std::istream* p) { delete p; }};
case 1:
should_delete = false;
result_ptr = &std::cin;
break;
return {&std::cin, [](std::istream*){}};
default:
std::cerr << "bad arguments\n";
exit(EXIT_FAILURE);
}
return {result_ptr, should_delete};
}
}