Skip to content

Commit e761452

Browse files
committed
Implement tuple extraction via operator>>
1 parent 4b765b1 commit e761452

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,6 @@ namespace sqlite {
7070

7171
typedef std::shared_ptr<sqlite3> connection_type;
7272

73-
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
74-
static void iterate(Tuple& t, database_binder& db) {
75-
get_col_from_db(db, Element, std::get<Element>(t));
76-
tuple_iterate<Tuple, Element + 1>::iterate(t, db);
77-
}
78-
};
79-
80-
template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
81-
static void iterate(Tuple&, database_binder&) {}
82-
};
83-
8473
class row_iterator;
8574
class database_binder {
8675

@@ -270,12 +259,7 @@ namespace sqlite {
270259
return *this;
271260
}
272261
template<class ...Types>
273-
value_type &operator >>(std::tuple<Types...>& values) {
274-
assert(!next_index);
275-
tuple_iterate<std::tuple<Types...>>::iterate(values, *_binder);
276-
next_index = sizeof...(Types) + 1;
277-
return *this;
278-
}
262+
value_type &operator >>(std::tuple<Types...>& values);
279263
template<class ...Types>
280264
value_type &operator >>(std::tuple<Types...>&& values) {
281265
return *this >> values;
@@ -336,6 +320,27 @@ namespace sqlite {
336320
mutable value_type value{_binder}; // mutable, because `changing` the value is just reading it
337321
};
338322

323+
namespace detail {
324+
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
325+
static void iterate(Tuple& t, row_iterator::value_type& row) {
326+
row >> std::get<Element>(t);
327+
tuple_iterate<Tuple, Element + 1>::iterate(t, row);
328+
}
329+
};
330+
331+
template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
332+
static void iterate(Tuple&, row_iterator::value_type&) {}
333+
};
334+
}
335+
336+
template<class ...Types>
337+
row_iterator::value_type &row_iterator::value_type::operator >>(std::tuple<Types...>& values) {
338+
assert(!next_index);
339+
detail::tuple_iterate<std::tuple<Types...>>::iterate(values, *this);
340+
next_index = sizeof...(Types) + 1;
341+
return *this;
342+
}
343+
339344
inline row_iterator database_binder::begin() {
340345
return row_iterator(*this);
341346
}

0 commit comments

Comments
 (0)