Skip to content

Commit

Permalink
Generalize transactor callable (#279)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
mkrupcale authored Feb 19, 2020
1 parent 7662d18 commit ffc5f23
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions include/pqxx/transactor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "pqxx/compiler-public.hxx"
#include "pqxx/internal/compiler-internal-pre.hxx"

#include <functional>

#include "pqxx/connection.hxx"
#include "pqxx/transaction.hxx"

Expand Down Expand Up @@ -94,8 +96,8 @@ namespace pqxx
* @return Whatever your callback returns.
*/
template<typename TRANSACTION_CALLBACK>
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{
Expand All @@ -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 &)
{
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_transactor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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.");
}


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.");
}

Expand Down

0 comments on commit ffc5f23

Please sign in to comment.