-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use a functor to replace the lambda one? #11
Comments
Hi, its a problem with mapping all those different result types we handle. So the Template got a bit complex and it looks like it does not handle this case. The new one has a more specific error but still does not handle this case. Here is an example how you could do it with the type extensions. // The thing you want
struct Item {
int id;
string name;
double price;
int qty;
};
// to get it back we need to give a deserialization method:
namespace sqlite {
template<> void get_col_from_db(database_binder& db, int inx, ::Item*& ret) { // specify the type it will be used on, a pointer is necessary if you’re type has no default constructor. Otherwise a object ref does the job too.
ret = new Item();
get_col_from_db(db, inx++, ret->id);
get_col_from_db(db, inx++, ret->name);
get_col_from_db(db, inx++, ret->price);
get_col_from_db(db, inx++, ret->qty);
}
}
{// usage
Item* item;
db << "select val,name,price,qty from test" >> item;
std::cout << item->name;
delete item;
} But this is ugly and definitely needs to change, i will get rid of this pointer mess in the next Version. |
There is however a shorter version, just convert youre functor to a lambda in this way: auto ItemResultHandler = [](Item& item) -> std::function<void(int, std::string , double , int )> {
return [&item](int id, std::string name, double price, int qty) mutable {
item.id = id;
item.name = name;
item.price = price;
item.qty = qty;
};
}; and just use it like this: Item item;
db << "select val,name,price,qty from test" >> ItemResultHandler(item); |
I used the shorter version, and wrap the code in a factory function, something like MakeItemResultHandler. |
@Killili please review https://github.com/aminroosta/sqlite_modern_cpp/pull/42 |
Hello,
Thank you for this great sqlite wrapper! It's very convenient.
While using the wrapper is delightful, a problem about eliminating similar code occurs.
The functor:
The code:
It has compiling errors:
I have posted a question in SO. Could you please have a look at it? Thank you in advance.
http://stackoverflow.com/questions/30387896/how-to-eliminate-multiple-similar-sql-queries-with-one-resultset-hander
The text was updated successfully, but these errors were encountered: