summaryrefslogtreecommitdiff
path: root/tiger-compiler/lib/misc/separator.hxx
blob: 103b78dcda1c8b35adc8034577ed3cd65201f304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
 ** \file misc/separator.hxx
 ** \brief Output containers with a separator between items.
 */

#pragma once

#include <ostream>

#include <misc/deref.hh>
#include <misc/separator.hh>

namespace misc
{
  template <class C, class S>
  inline separator<C, S>::separator(const C& c, const S& s)
    : container_(c)
    , separator_(s)
  {}

  template <class C, typename S>
  inline std::ostream& separator<C, S>::operator()(std::ostream& o) const
  {
    for (auto i = container_.begin(); i != container_.end(); ++i)
      {
        if (i != container_.begin())
          o << separator_;
        o << deref << *i;
      }
    return o;
  }

  template <class C, typename S>
  separator<C, S> separate(const C& c, const S& s)
  {
    return separator<C, S>(c, s);
  }

  template <class C> separator<C, char> separate(const C& c)
  {
    return separate(c, '\n');
  }

  template <class C, typename S>
  inline std::ostream& operator<<(std::ostream& o, const separator<C, S>& s)
  {
    return s(o);
  }

  template <class A, typename B>
  inline std::ostream& operator<<(std::ostream& o, const std::pair<A, B>& p)
  {
    return o << p.first << p.second;
  }
} // namespace misc