Skip to content

Commit 47cb1a0

Browse files
authored
Merge pull request #61 from aminroosta/lvalue_functors
Support lvalue functors
2 parents 2d6e3f9 + 6b370a3 commit 47cb1a0

File tree

6 files changed

+67
-94
lines changed

6 files changed

+67
-94
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ namespace sqlite {
280280

281281
template <typename Function>
282282
typename std::enable_if<!is_sqlite_value<Function>::value, void>::type operator>>(
283-
Function func) {
283+
Function&& func) {
284284
typedef utility::function_traits<Function> traits;
285285

286286
this->_extract([&func, this]() {
@@ -359,7 +359,7 @@ namespace sqlite {
359359
>
360360
static typename std::enable_if<(sizeof...(Values) < Boundary), void>::type run(
361361
database_binder& db,
362-
Function& function,
362+
Function&& function,
363363
Values&&... values
364364
) {
365365
nth_argument_type<Function, sizeof...(Values)> value{};
@@ -375,7 +375,7 @@ namespace sqlite {
375375
>
376376
static typename std::enable_if<(sizeof...(Values) == Boundary), void>::type run(
377377
database_binder&,
378-
Function& function,
378+
Function&& function,
379379
Values&&... values
380380
) {
381381
function(std::move(values)...);

hdr/sqlite_modern_cpp/extensions/boost_json_spirit.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

hdr/sqlite_modern_cpp/extensions/boost_uuid.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

hdr/sqlite_modern_cpp/extensions/std_time_t.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

hdr/sqlite_modern_cpp/utility/function_traits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <tuple>
4+
#include<type_traits>
45

56
namespace sqlite {
67
namespace utility {
@@ -9,7 +10,7 @@ namespace sqlite {
910

1011
template <typename Function>
1112
struct function_traits : public function_traits<
12-
decltype(&Function::operator())
13+
decltype(&std::remove_reference<Function>::type::operator())
1314
> { };
1415

1516
template <

tests/lvalue_functor_example.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<iostream>
2+
#include<sqlite_modern_cpp.h>
3+
#include<string>
4+
#include<vector>
5+
using namespace sqlite;
6+
using namespace std;
7+
8+
template<typename Target, typename... AttrTypes>
9+
struct builder {
10+
vector<Target> results;
11+
12+
void operator()(AttrTypes... args) {
13+
results.emplace_back(std::forward<AttrTypes&&>(args)...);
14+
};
15+
};
16+
17+
18+
struct user {
19+
int age;
20+
string name;
21+
double weight;
22+
23+
user(int age, string name, double weight) : age(age), name(name), weight(weight) { }
24+
25+
static std::vector<user> all(sqlite::database& db) {
26+
builder<user, int, std::string, double> person_builder;
27+
db << "SELECT * FROM user;"
28+
>> person_builder;
29+
return std::move(person_builder.results); // move to avoid copying data ;-)
30+
};
31+
};
32+
33+
int main() {
34+
35+
try {
36+
database db(":memory:");
37+
38+
db <<
39+
"create table if not exists user ("
40+
" age int,"
41+
" name text,"
42+
" weight real"
43+
");";
44+
45+
db << "insert into user (age,name,weight) values (?,?,?);" << 20 << u"chandler" << 83.25;
46+
db << "insert into user (age,name,weight) values (?,?,?);" << 21 << u"monika" << 86.25;
47+
db << "insert into user (age,name,weight) values (?,?,?);" << 22 << u"ross" << 88.25;
48+
49+
auto users = user::all(db);
50+
51+
for(auto u : users)
52+
cout << u.age << ' ' << u.name << ' ' << u.weight << endl;
53+
if(users.size() != 3)
54+
exit(EXIT_FAILURE);
55+
}
56+
catch (exception& e) {
57+
cerr << e.what() << endl;
58+
exit(EXIT_FAILURE);
59+
}
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)