From ffc5f23242032fa18b7e9df7d0a53abe90940d9a Mon Sep 17 00:00:00 2001 From: Matthew Krupcale Date: Wed, 19 Feb 2020 04:38:23 -0500 Subject: [PATCH] Generalize transactor callable (#279) * Generalize transactor callable This permits calling pqxx::perform on a more general set of callable[1] objects by using std::invoke[2] for perfect forwarding. [1] https://en.cppreference.com/w/cpp/named_req/Callable [2] https://en.cppreference.com/w/cpp/utility/functional/invoke * Mark transactor unit tests noexcept as necessary Tests are run with -Werror=noexcept, so we need to mark these lambdas as noexcept in this case. --- include/pqxx/transactor.hxx | 8 +++++--- test/unit/test_transactor.cxx | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/pqxx/transactor.hxx b/include/pqxx/transactor.hxx index eab5c2ffb..928fe67c9 100644 --- a/include/pqxx/transactor.hxx +++ b/include/pqxx/transactor.hxx @@ -14,6 +14,8 @@ #include "pqxx/compiler-public.hxx" #include "pqxx/internal/compiler-internal-pre.hxx" +#include + #include "pqxx/connection.hxx" #include "pqxx/transaction.hxx" @@ -94,8 +96,8 @@ namespace pqxx * @return Whatever your callback returns. */ template -inline auto perform(TRANSACTION_CALLBACK const &callback, int attempts = 3) - -> decltype(callback()) +inline auto perform(TRANSACTION_CALLBACK &&callback, int attempts = 3) + -> decltype(std::invoke(callback)) { if (attempts <= 0) throw std::invalid_argument{ @@ -105,7 +107,7 @@ inline auto perform(TRANSACTION_CALLBACK const &callback, int attempts = 3) { try { - return callback(); + return std::invoke(callback); } catch (in_doubt_error const &) { diff --git a/test/unit/test_transactor.cxx b/test/unit/test_transactor.cxx index 56e01db31..b1fedfb7e 100644 --- a/test/unit/test_transactor.cxx +++ b/test/unit/test_transactor.cxx @@ -19,7 +19,7 @@ void test_transactor_newstyle_executes_simple_query() void test_transactor_newstyle_can_return_void() { bool done{false}; - pqxx::perform([&done] { done = true; }); + pqxx::perform([&done]() noexcept { done = true; }); PQXX_CHECK(done, "Callback was not executed."); } @@ -27,7 +27,7 @@ void test_transactor_newstyle_can_return_void() void test_transactor_newstyle_completes_upon_success() { int attempts{0}; - pqxx::perform([&attempts]() { attempts++; }); + pqxx::perform([&attempts]() noexcept { attempts++; }); PQXX_CHECK_EQUAL(attempts, 1, "Successful transactor didn't run 1 time."); }