Advertisement
Guest User

C++11 lambdas and sqlite3_exec

a guest
May 12th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. bool Database::Execute(std::string const& query, RowHandler handler)
  2. {
  3.   // Create a closure that matches the callback signature sqlite3_exec wants.
  4.   // Handler param passed as user data, so cast that to the correct type and call it.
  5.   // Explicitly setting return type as int isn't 100% necessary as it can be deduced
  6.   // from return type of RowHandler.
  7.   auto wrapper = [](void* userData, int columnCount, char **columnValues, char **columnNames) -> int
  8.   {
  9.     auto handler = reinterpret_cast<RowHandler*>(userData);
  10.     return (*handler)(columnCount, columnValues, columnNames);
  11.   };
  12.  
  13.   return sqlite3_exec(db, query.c_str(), wrapper, &handler, /* error handling can wait */ nullptr);
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement