Skip to content

Commit e7b18f9

Browse files
committed
Implement tuple extraction via operator>>
1 parent 73f9328 commit e7b18f9

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

hdr/sqlite_modern_cpp.h

+22-17
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,6 @@ namespace sqlite {
5151

5252
typedef std::shared_ptr<sqlite3> connection_type;
5353

54-
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
55-
static void iterate(Tuple& t, database_binder& db) {
56-
get_col_from_db(db, Element, std::get<Element>(t));
57-
tuple_iterate<Tuple, Element + 1>::iterate(t, db);
58-
}
59-
};
60-
61-
template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
62-
static void iterate(Tuple&, database_binder&) {}
63-
};
64-
6554
class row_iterator;
6655
class database_binder {
6756

@@ -251,12 +240,7 @@ namespace sqlite {
251240
return *this;
252241
}
253242
template<class ...Types>
254-
value_type &operator >>(std::tuple<Types...>& values) {
255-
assert(!next_index);
256-
tuple_iterate<std::tuple<Types...>>::iterate(values, *_binder);
257-
next_index = sizeof...(Types) + 1;
258-
return *this;
259-
}
243+
value_type &operator >>(std::tuple<Types...>& values);
260244
template<class ...Types>
261245
value_type &operator >>(std::tuple<Types...>&& values) {
262246
return *this >> values;
@@ -317,6 +301,27 @@ namespace sqlite {
317301
mutable value_type value{_binder}; // mutable, because `changing` the value is just reading it
318302
};
319303

304+
namespace detail {
305+
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
306+
static void iterate(Tuple& t, row_iterator::value_type& row) {
307+
row >> std::get<Element>(t);
308+
tuple_iterate<Tuple, Element + 1>::iterate(t, row);
309+
}
310+
};
311+
312+
template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
313+
static void iterate(Tuple&, row_iterator::value_type&) {}
314+
};
315+
}
316+
317+
template<class ...Types>
318+
row_iterator::value_type &row_iterator::value_type::operator >>(std::tuple<Types...>& values) {
319+
assert(!next_index);
320+
detail::tuple_iterate<std::tuple<Types...>>::iterate(values, *this);
321+
next_index = sizeof...(Types) + 1;
322+
return *this;
323+
}
324+
320325
inline row_iterator database_binder::begin() {
321326
return row_iterator(*this);
322327
}

0 commit comments

Comments
 (0)