Skip to content

Return values instead of assigning to references #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,15 @@ namespace sqlite {
value_type(database_binder *_binder): _binder(_binder) {};
template<class T>
typename std::enable_if<is_sqlite_value<T>::value, value_type &>::type operator >>(T &result) {
get_col_from_db(_binder->_stmt.get(), next_index++, result);
result = get_col_from_db(_binder->_stmt.get(), next_index++, result_type<T>());
return *this;
}
template<class ...Types>
value_type &operator >>(std::tuple<Types...>& values);
value_type &operator >>(std::tuple<Types...>& values) {
values = handle_tuple<std::tuple<typename std::decay<Types>::type...>>(std::index_sequence_for<Types...>());
next_index += sizeof...(Types);
return *this;
}
template<class ...Types>
value_type &operator >>(std::tuple<Types...>&& values) {
return *this >> values;
Expand All @@ -154,6 +158,14 @@ namespace sqlite {
return sqlite3_column_count(_binder->_stmt.get()) >= next_index;
}
private:
template<class Tuple, std::size_t ...Index>
Tuple handle_tuple(std::index_sequence<Index...>) {
return Tuple(
get_col_from_db(
_binder->_stmt.get(),
next_index + Index,
result_type<typename std::tuple_element<Index, Tuple>::type>())...);
}
database_binder *_binder;
int next_index = 0;
};
Expand Down Expand Up @@ -198,27 +210,6 @@ namespace sqlite {
mutable value_type value{_binder}; // mutable, because `changing` the value is just reading it
};

namespace detail {
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
static void iterate(Tuple& t, row_iterator::value_type& row) {
row >> std::get<Element>(t);
tuple_iterate<Tuple, Element + 1>::iterate(t, row);
}
};

template<typename Tuple, int Element> struct tuple_iterate<Tuple, Element, true> {
static void iterate(Tuple&, row_iterator::value_type&) {}
};
}

template<class ...Types>
row_iterator::value_type &row_iterator::value_type::operator >>(std::tuple<Types...>& values) {
assert(!next_index);
detail::tuple_iterate<std::tuple<Types...>>::iterate(values, *this);
next_index = sizeof...(Types) + 1;
return *this;
}

inline row_iterator database_binder::begin() {
return row_iterator(*this);
}
Expand Down Expand Up @@ -558,16 +549,20 @@ namespace sqlite {
sqlite3_value** vals,
Values&&... values
) {
typename std::remove_cv<
using arg_type = typename std::remove_cv<
typename std::remove_reference<
typename utility::function_traits<
typename Functions::first_type
>::template argument<sizeof...(Values)>
>::type
>::type value{};
get_val_from_db(vals[sizeof...(Values) - 1], value);
>::type;

step<Count, Functions>(db, count, vals, std::forward<Values>(values)..., std::move(value));
step<Count, Functions>(
db,
count,
vals,
std::forward<Values>(values)...,
get_val_from_db(vals[sizeof...(Values) - 1], result_type<arg_type>()));
}

template<
Expand Down Expand Up @@ -618,14 +613,18 @@ namespace sqlite {
sqlite3_value** vals,
Values&&... values
) {
typename std::remove_cv<
using arg_type = typename std::remove_cv<
typename std::remove_reference<
typename utility::function_traits<Function>::template argument<sizeof...(Values)>
>::type
>::type value{};
get_val_from_db(vals[sizeof...(Values)], value);

scalar<Count, Function>(db, count, vals, std::forward<Values>(values)..., std::move(value));
>::type;

scalar<Count, Function>(
db,
count,
vals,
std::forward<Values>(values)...,
get_val_from_db(vals[sizeof...(Values)], result_type<arg_type>()));
}

template<
Expand Down
Loading