Skip to content

Commit 20c2a4c

Browse files
committed
Directly initialize tuples
1 parent 3a71e95 commit 20c2a4c

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

hdr/sqlite_modern_cpp.h

+13-22
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ namespace sqlite {
139139
return *this;
140140
}
141141
template<class ...Types>
142-
value_type &operator >>(std::tuple<Types...>& values);
142+
value_type &operator >>(std::tuple<Types...>& values) {
143+
values = handle_tuple<std::tuple<typename std::decay<Types>::type...>>(std::index_sequence_for<Types...>());
144+
next_index += sizeof...(Types);
145+
return *this;
146+
}
143147
template<class ...Types>
144148
value_type &operator >>(std::tuple<Types...>&& values) {
145149
return *this >> values;
@@ -154,6 +158,14 @@ namespace sqlite {
154158
return sqlite3_column_count(_binder->_stmt.get()) >= next_index;
155159
}
156160
private:
161+
template<class Tuple, std::size_t ...Index>
162+
Tuple handle_tuple(std::index_sequence<Index...>) {
163+
return Tuple(
164+
get_col_from_db(
165+
_binder->_stmt.get(),
166+
next_index + Index,
167+
result_type<typename std::tuple_element<Index, Tuple>::type>())...);
168+
}
157169
database_binder *_binder;
158170
int next_index = 0;
159171
};
@@ -198,27 +210,6 @@ namespace sqlite {
198210
mutable value_type value{_binder}; // mutable, because `changing` the value is just reading it
199211
};
200212

201-
namespace detail {
202-
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
203-
static void iterate(Tuple& t, row_iterator::value_type& row) {
204-
row >> std::get<Element>(t);
205-
tuple_iterate<Tuple, Element + 1>::iterate(t, row);
206-
}
207-
};
208-
209-
template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
210-
static void iterate(Tuple&, row_iterator::value_type&) {}
211-
};
212-
}
213-
214-
template<class ...Types>
215-
row_iterator::value_type &row_iterator::value_type::operator >>(std::tuple<Types...>& values) {
216-
assert(!next_index);
217-
detail::tuple_iterate<std::tuple<Types...>>::iterate(values, *this);
218-
next_index = sizeof...(Types) + 1;
219-
return *this;
220-
}
221-
222213
inline row_iterator database_binder::begin() {
223214
return row_iterator(*this);
224215
}

0 commit comments

Comments
 (0)