dlr-gtlab / gt-logging

Basic C++14 logging library based on QsLog
1 stars 1 forks source link

Add support for std::tuple types #39

Closed rainman110 closed 11 months ago

rainman110 commented 1 year ago

In GitLab by @mariusalexander on Dec 14, 2022, 13:32

std::tuple types should be loggable as well.

Make use of a static for loop:

namespace detail
{

template<typename Lambda, std::size_t ... Is>
constexpr void
static_for_impl(Lambda&& f, std::index_sequence<Is...>)
{
    struct Idx {};
    // unpack into init list for "looping" in correct order wo recursion
    (void)std::initializer_list<Idx > {
        ((void)f(std::integral_constant<unsigned, Is>()), Idx{})...
    };
}

} // namespace detail

namespace mpl
{

/**
 * @brief Calls a method for each n in N. Static for loop for compound types
 * @param f Function to call for each iteration.
 * @tparam N Number of iterations
 * @tparam Lambda Function to call. Must have a single parameter
 * representing the current index
 */
template<unsigned N, typename Lambda>
constexpr void
static_for(Lambda&& f)
{
    detail::static_for_impl(std::forward<Lambda>(f),
                            std::make_index_sequence<N>());
}

} // namespace mpl

Output operator could look something like this

template<typename... Ts>
gt::log::Stream& operator<<(gt::log::Stream& s, std::tuple<Ts...> const& tuple)
{
    gt::log::StreamStateSaver saver(s);
    s.nospace() << "tuple(";
    mpl::static_for<sizeof...(Ts)-1>([&](auto const idx){
        s << std::get<idx>(tuple) << ", ";
    });
    s << std::get<sizeof...(Ts)-1>(tuple) << ')';
    return s;
}
rainman110 commented 1 year ago

In GitLab by @mariusalexander on Dec 20, 2022, 08:27

created branch 39-add-support-for-std-tuple-types to address this issue

rainman110 commented 1 year ago

In GitLab by @rainman110 on Feb 2, 2023, 15:50

mentioned in commit 6390f61e5a568951dffe3e4506fb8882154befe7