~drizzle-trunk/drizzle/development

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
56
57
58
59
60
61
62
63
64
65
66
67
// -*- Mode: C++ -*-

#include <iosfwd>
#include <string>

namespace ioutil {

  /**
     Helper class for join() I/O manipulator.

     This is a join I/O manipulator with arguments for @c join(). These has to be
  */
  template <class FwdIter> class joiner {
    friend std::ostream& operator<<(std::ostream& out, const joiner& j) {
      j.write(out);
      return out;
    }

  public:
    explicit joiner(const std::string& separator, FwdIter start, FwdIter finish)
      : m_sep(separator), m_start(start), m_finish(finish)
    { }


  private:
    std::string m_sep;
    FwdIter m_start, m_finish;

    void write(std::ostream& out) const {
      if (m_start == m_finish)
        return;

      FwdIter fi = m_start;
      while (true) {
        out << *fi;
        if (++fi == m_finish)
          break;
        out << m_sep;
      }
    }
  };


  /**
     Join manipulators for writing delimiter-separated strings to an
     ostream object.

     Use the manipulator as follows:
     @code
     std::cout << ioutil::join(",", list.begin(), list.end()) << std::endl;
     std::cout << ioutil::join(",", list) << std::endl;
     @endcode
  */
  template <class FwdIter>
  joiner<FwdIter>
  join(const std::string& delim, FwdIter start, FwdIter finish) {
    return joiner<FwdIter>(delim, start, finish);
  }

  /** @overload */
  template <class Container>
  joiner<typename Container::const_iterator>
  join(const std::string& delim, Container seq) {
    typedef typename Container::const_iterator FwdIter;
    return joiner<FwdIter>(delim, seq.begin(), seq.end());
  }
}