Move callbacks into lambdas

This commit is contained in:
antao 2018-12-30 15:16:27 +08:00
parent 13c5fca57e
commit d8c932218a
3 changed files with 8 additions and 11 deletions

View File

@ -26,21 +26,19 @@ void SqlBinder::exec()
if (_mode == Mode::NonBlocking)
{
//nonblocking mode,default mode
auto holder = std::move(_callbackHolder);
auto exceptCb = std::move(_exceptCallback);
auto exceptPtrCb = std::move(_exceptPtrCallback);
auto isExceptPtr = _isExceptPtr;
//Retain shared_ptrs of parameters until we get the result;
std::shared_ptr<decltype(_objs)> objs = std::make_shared<decltype(_objs)>(std::move(_objs));
_client.execSql(_sql, _paraNum, _parameters, _length, _format,
[holder, objs](const Result &r) {
[holder = std::move(_callbackHolder), objs](const Result &r) {
objs->clear();
if (holder)
{
holder->execCallback(r);
}
},
[exceptCb, exceptPtrCb, isExceptPtr](const std::exception_ptr &exception) {
[exceptCb = std::move(_exceptCallback),
exceptPtrCb = std::move(_exceptPtrCallback),
isExceptPtr = _isExceptPtr](const std::exception_ptr &exception) {
//LOG_DEBUG<<"exp callback "<<isExceptPtr;
if (!isExceptPtr)
{

View File

@ -33,11 +33,8 @@ TransactionImpl::~TransactionImpl()
assert(!_isWorking);
if (!_isCommitedOrRolledback)
{
auto ucb = std::move(_usedUpCallback);
auto commitCb = std::move(_commitCallback);
auto loop = _connectionPtr->loop();
auto conn = _connectionPtr;
loop->queueInLoop([conn, ucb, commitCb]() {
loop->queueInLoop([conn = _connectionPtr, ucb = std::move(_usedUpCallback), commitCb = std::move(_commitCallback)]() {
conn->execSql("commit",
0,
std::vector<const char *>(),

View File

@ -57,7 +57,9 @@ int main()
std::cout << e.base().what() << std::endl;
};
{
auto trans = clientPtr->newTransaction();
auto trans = clientPtr->newTransaction([](bool success) {
LOG_DEBUG << (success ? "commit success!" : "commit failed!");
});
Mapper<drogon_model::sqlite3::Groups> mapper(trans);
mapper.findAll([trans](const std::vector<drogon_model::sqlite3::Groups> &v) {