drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
ioutil.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22#include <iosfwd>
23#include <string>
24
25namespace drizzled
26{
27namespace message
28{
29namespace ioutil
30{
31
37 template <class FwdIter> class joiner {
38 friend std::ostream& operator<<(std::ostream& out, const joiner& j) {
39 j.write(out);
40 return out;
41 }
42
43 public:
44 explicit joiner(const std::string& separator, FwdIter start, FwdIter finish)
45 : m_sep(separator), m_start(start), m_finish(finish)
46 { }
47
48
49 private:
50 std::string m_sep;
51 FwdIter m_start, m_finish;
52
53 void write(std::ostream& out) const {
54 if (m_start == m_finish)
55 return;
56
57 FwdIter fi = m_start;
58 while (true) {
59 out << *fi;
60 if (++fi == m_finish)
61 break;
62 out << m_sep;
63 }
64 }
65 };
66
67
78 template <class FwdIter>
80 join(const std::string& delim, FwdIter start, FwdIter finish) {
81 return joiner<FwdIter>(delim, start, finish);
82 }
83
85 template <class Container>
86 joiner<typename Container::const_iterator>
87 join(const std::string& delim, Container seq) {
88 typedef typename Container::const_iterator FwdIter;
89 return joiner<FwdIter>(delim, seq.begin(), seq.end());
90 }
91
92} /* namespace ioutil */
93} /* namespace message */
94} /* namespace drizzled */
95
TODO: Rename this file - func.h is stupid.