Skip to content

Commit b6a1321

Browse files
authored
Merge pull request SqliteModernCpp#154 from zauguin/new_value_interface
Return values instead of assigning to references
2 parents 8e9d851 + 20c2a4c commit b6a1321

File tree

2 files changed

+134
-182
lines changed

2 files changed

+134
-182
lines changed

hdr/sqlite_modern_cpp.h

+31-32
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ namespace sqlite {
135135
value_type(database_binder *_binder): _binder(_binder) {};
136136
template<class T>
137137
typename std::enable_if<is_sqlite_value<T>::value, value_type &>::type operator >>(T &result) {
138-
get_col_from_db(_binder->_stmt.get(), next_index++, result);
138+
result = get_col_from_db(_binder->_stmt.get(), next_index++, result_type<T>());
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
}
@@ -558,16 +549,20 @@ namespace sqlite {
558549
sqlite3_value** vals,
559550
Values&&... values
560551
) {
561-
typename std::remove_cv<
552+
using arg_type = typename std::remove_cv<
562553
typename std::remove_reference<
563554
typename utility::function_traits<
564555
typename Functions::first_type
565556
>::template argument<sizeof...(Values)>
566557
>::type
567-
>::type value{};
568-
get_val_from_db(vals[sizeof...(Values) - 1], value);
558+
>::type;
569559

570-
step<Count, Functions>(db, count, vals, std::forward<Values>(values)..., std::move(value));
560+
step<Count, Functions>(
561+
db,
562+
count,
563+
vals,
564+
std::forward<Values>(values)...,
565+
get_val_from_db(vals[sizeof...(Values) - 1], result_type<arg_type>()));
571566
}
572567

573568
template<
@@ -618,14 +613,18 @@ namespace sqlite {
618613
sqlite3_value** vals,
619614
Values&&... values
620615
) {
621-
typename std::remove_cv<
616+
using arg_type = typename std::remove_cv<
622617
typename std::remove_reference<
623618
typename utility::function_traits<Function>::template argument<sizeof...(Values)>
624619
>::type
625-
>::type value{};
626-
get_val_from_db(vals[sizeof...(Values)], value);
627-
628-
scalar<Count, Function>(db, count, vals, std::forward<Values>(values)..., std::move(value));
620+
>::type;
621+
622+
scalar<Count, Function>(
623+
db,
624+
count,
625+
vals,
626+
std::forward<Values>(values)...,
627+
get_val_from_db(vals[sizeof...(Values)], result_type<arg_type>()));
629628
}
630629

631630
template<

0 commit comments

Comments
 (0)