Skip to content

Commit

Permalink
Implement tuple extraction via operator>>
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Jul 24, 2017
1 parent 73f9328 commit e7b18f9
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ namespace sqlite {

typedef std::shared_ptr<sqlite3> connection_type;

template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
static void iterate(Tuple& t, database_binder& db) {
get_col_from_db(db, Element, std::get<Element>(t));
tuple_iterate<Tuple, Element + 1>::iterate(t, db);
}
};

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

class row_iterator;
class database_binder {

Expand Down Expand Up @@ -251,12 +240,7 @@ namespace sqlite {
return *this;
}
template<class ...Types>
value_type &operator >>(std::tuple<Types...>& values) {
assert(!next_index);
tuple_iterate<std::tuple<Types...>>::iterate(values, *_binder);
next_index = sizeof...(Types) + 1;
return *this;
}
value_type &operator >>(std::tuple<Types...>& values);
template<class ...Types>
value_type &operator >>(std::tuple<Types...>&& values) {
return *this >> values;
Expand Down Expand Up @@ -317,6 +301,27 @@ 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

0 comments on commit e7b18f9

Please sign in to comment.