streamutils.hpp 737 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
#ifndef RTG_GUARD_STREAMUTILS_HPP
#define RTG_GUARD_STREAMUTILS_HPP

#include <ostream>
#include <algorithm>

namespace rtg {

Paul's avatar
Paul committed
9
template <class T>
Paul's avatar
Paul committed
10
11
12
struct stream_range_container
{
    const T* r;
Paul's avatar
Paul committed
13
    stream_range_container(const T& x) : r(&x) {}
Paul's avatar
Paul committed
14
15
16
17
18
19
20

    friend std::ostream& operator<<(std::ostream& os, const stream_range_container& sr)
    {
        assert(sr.r != nullptr);
        if(!sr.r->empty())
        {
            os << sr.r->front();
Paul's avatar
Paul committed
21
22
            std::for_each(
                std::next(sr.r->begin()), sr.r->end(), [&](auto&& x) { os << ", " << x; });
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        }
        return os;
    }
};

template <class Range>
inline stream_range_container<Range> stream_range(const Range& r)
{
    return {r};
}

} // namespace rtg

#endif