0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Used index_sequence to get rid of recursion in interpret_tuple.

Wonderful stuff, shaped after code samples from cppreference and
stackoverflow.
This commit is contained in:
rbock 2014-05-06 22:41:19 +02:00
parent f57ba13955
commit b9133cf3a1

View File

@ -30,47 +30,40 @@
#include <tuple> #include <tuple>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/serialize.h> #include <sqlpp11/serialize.h>
#include <sqlpp11/detail/index_sequence.h>
namespace sqlpp namespace sqlpp
{ {
template<typename Context, typename Tuple> template<typename Element, typename Separator, typename Context>
struct tuple_interpreter_t static void interpret_tuple_element(const Element& element, const Separator& separator, Context& context, size_t index)
{ {
template<typename Separator> if (index)
static void _(const Tuple& t, const Separator& separator, Context& context) context << separator;
{ if (requires_braces_t<Element>::value)
_impl(t, separator, context, type<0>()); context << "(";
}; serialize(element, context);
if (requires_braces_t<Element>::value)
context << ")";
}
private: template<typename Tuple, typename Separator, typename Context, size_t... Is>
template<size_t> struct type {}; auto interpret_tuple_impl(const Tuple& t, const Separator& separator, Context& context, const ::sqlpp::detail::index_sequence<Is...>&)
-> Context&
template<typename Separator, size_t index> {
static void _impl(const Tuple& t, const Separator& separator, Context& context, const type<index>&) // Note: A braced-init-list does guarantee the order of evaluation according to 12.6.1 [class.explicit.init] paragraph 2 and 8.5.4 [dcl.init.list] paragraph 4.
{ // See for example: "http://en.cppreference.com/w/cpp/utility/integer_sequence"
if (index) // See also: "http://stackoverflow.com/questions/6245735/pretty-print-stdtuple/6245777#6245777"
context << separator; // Beware of gcc-bug: "http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51253", otherwise an empty swallow struct could be used
const auto& entry = std::get<index>(t); using swallow = int[];
using entry_type = typename std::tuple_element<index, Tuple>::type; (void) swallow{(interpret_tuple_element(std::get<Is>(t), separator, context, Is) ,0)...};
if (requires_braces_t<entry_type>::value) return context;
context << "("; }
serialize(entry, context);
if (requires_braces_t<entry_type>::value)
context << ")";
_impl(t, separator, context, type<index + 1>());
}
template<typename Separator>
static void _impl(const Tuple& t, const Separator& separator, Context& context, const type<std::tuple_size<Tuple>::value>&)
{
}
};
template<typename Tuple, typename Separator, typename Context> template<typename Tuple, typename Separator, typename Context>
auto interpret_tuple(const Tuple& t, const Separator& separator, Context& context) auto interpret_tuple(const Tuple& t, const Separator& separator, Context& context)
-> decltype(tuple_interpreter_t<Context, Tuple>::_(t, separator, context)) -> Context&
{ {
return tuple_interpreter_t<Context, Tuple>::_(t, separator, context); return interpret_tuple_impl(t, separator, context, ::sqlpp::detail::make_index_sequence<std::tuple_size<Tuple>::value>{});
} }
} }