-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fn_function.cpp
120 lines (94 loc) · 2.49 KB
/
Fn_function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef LIB_TDB_FUNCTORS_FN_FUNCTION_CPP_
#define LIB_TDB_FUNCTORS_FN_FUNCTION_CPP_
#include "../Fn_function.hpp"
#include <iostream>
#include <tdb/tdb_sqlite.hpp>
//test code
namespace{
[[maybe_unused]] void example(){
typedef tdb::Tag_sqlite Tag_xxx;
tdb::Connection_t<Tag_xxx> connection("/tmp/test.sqlite");
//--- User defined functors ---
struct Fn_void{
Fn_void(const std::string & prefix_):prefix(prefix_){}
void operator()(int i){std::cout<< prefix <<i<<std::endl;}
std::string prefix;
};
struct Fn_bool{
Fn_bool(const int & threshold_):threshold(threshold_){}
bool operator()(int i){if(i>threshold){return false;} std::cout<<i<<std::endl; return true;}
int threshold;
};
//--- Fn_function (void) ---
//multi thread
tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
true,
Fn_void
> fn_function1(
connection,
tdb::sql<Tag_xxx>("select i1 from test where d1 != $1 and d2 != $2"),
"hello:" //args to construct Fn_void
);
fn_function1(1.0,1.0);
//single thread
tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
false,
Fn_void
> fn_function2(
connection,
tdb::sql<Tag_xxx>("select i1 from test where d1 != $1 and d2 != $2"),
"hello:" //args to construct Fn_void
);
fn_function2(1.0,1.0);
//--- Fn_function (bool) ---
//if function returns false, stop fetching
//multi thread
tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
true,
Fn_bool
> fn_function3(
connection,
tdb::sql<Tag_xxx>("select i1 from test where d1 != $1 and d2 != $2"),
5 //args to construct Fn_void
);
[[maybe_unused]] bool b1 = fn_function3(1.0,1.0);
//single thread
tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
false,
Fn_bool
> fn_function4(
connection,
tdb::sql<Tag_xxx>("select i1 from test where d1 != $1 and d2 != $2"),
5 //args to construct Fn_void
);
[[maybe_unused]] bool b2 = fn_function4(1.0,1.0);
//--- check return type ---
static_assert(tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
true,
Fn_bool
>::fn_returns_bool,"");
static_assert(!tdb::Fn_function<
Tag_xxx ,
std::tuple<int>,
std::tuple<double,double>,
true,
Fn_void
>::fn_returns_bool,"");
}
}
#endif /* LIB_TDB_FUNCTORS_FN_FUNCTION_CPP_ */