streamutils.hpp 753 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_STREAMUTILS_HPP
#define MIGRAPH_GUARD_STREAMUTILS_HPP
Paul's avatar
Paul committed
3
4
5
6

#include <ostream>
#include <algorithm>

Paul's avatar
Paul committed
7
namespace migraph {
Paul's avatar
Paul committed
8

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
        }
        return os;
    }
};

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

Paul's avatar
Paul committed
34
} // namespace migraph
Paul's avatar
Paul committed
35
36

#endif