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:
parent
f57ba13955
commit
b9133cf3a1
43
include/sqlpp11/vendor/interpret_tuple.h
vendored
43
include/sqlpp11/vendor/interpret_tuple.h
vendored
@ -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>
|
|
||||||
static void _(const Tuple& t, const Separator& separator, Context& context)
|
|
||||||
{
|
|
||||||
_impl(t, separator, context, type<0>());
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
template<size_t> struct type {};
|
|
||||||
|
|
||||||
template<typename Separator, size_t index>
|
|
||||||
static void _impl(const Tuple& t, const Separator& separator, Context& context, const type<index>&)
|
|
||||||
{
|
{
|
||||||
if (index)
|
if (index)
|
||||||
context << separator;
|
context << separator;
|
||||||
const auto& entry = std::get<index>(t);
|
if (requires_braces_t<Element>::value)
|
||||||
using entry_type = typename std::tuple_element<index, Tuple>::type;
|
|
||||||
if (requires_braces_t<entry_type>::value)
|
|
||||||
context << "(";
|
context << "(";
|
||||||
serialize(entry, context);
|
serialize(element, context);
|
||||||
if (requires_braces_t<entry_type>::value)
|
if (requires_braces_t<Element>::value)
|
||||||
context << ")";
|
context << ")";
|
||||||
_impl(t, separator, context, type<index + 1>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Separator>
|
template<typename Tuple, typename Separator, typename Context, size_t... Is>
|
||||||
static void _impl(const Tuple& t, const Separator& separator, Context& context, const type<std::tuple_size<Tuple>::value>&)
|
auto interpret_tuple_impl(const Tuple& t, const Separator& separator, Context& context, const ::sqlpp::detail::index_sequence<Is...>&)
|
||||||
|
-> Context&
|
||||||
{
|
{
|
||||||
|
// 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"
|
||||||
|
// See also: "http://stackoverflow.com/questions/6245735/pretty-print-stdtuple/6245777#6245777"
|
||||||
|
// Beware of gcc-bug: "http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51253", otherwise an empty swallow struct could be used
|
||||||
|
using swallow = int[];
|
||||||
|
(void) swallow{(interpret_tuple_element(std::get<Is>(t), separator, context, Is) ,0)...};
|
||||||
|
return context;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
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>{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user