324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
1 |
// -*- Mode: C++ -*-
|
2 |
||
3 |
#include <iosfwd> |
|
4 |
#include <string> |
|
5 |
||
6 |
namespace ioutil { |
|
7 |
||
8 |
/**
|
|
9 |
Helper class for join() I/O manipulator.
|
|
10 |
||
11 |
This is a join I/O manipulator with arguments for @c join(). These has to be
|
|
12 |
*/
|
|
13 |
template <class FwdIter> class joiner { |
|
14 |
friend std::ostream& operator<<(std::ostream& out, const joiner& j) { |
|
15 |
j.write(out); |
|
16 |
return out; |
|
17 |
}
|
|
18 |
||
19 |
public: |
|
20 |
explicit joiner(const std::string& separator, FwdIter start, FwdIter finish) |
|
21 |
: m_sep(separator), m_start(start), m_finish(finish) |
|
22 |
{ } |
|
23 |
||
24 |
||
25 |
private: |
|
26 |
std::string m_sep; |
|
27 |
FwdIter m_start, m_finish; |
|
28 |
||
29 |
void write(std::ostream& out) const { |
|
30 |
if (m_start == m_finish) |
|
31 |
return; |
|
32 |
||
33 |
FwdIter fi = m_start; |
|
34 |
while (true) { |
|
35 |
out << *fi; |
|
36 |
if (++fi == m_finish) |
|
37 |
break; |
|
38 |
out << m_sep; |
|
39 |
}
|
|
40 |
}
|
|
41 |
};
|
|
42 |
||
43 |
||
44 |
/**
|
|
45 |
Join manipulators for writing delimiter-separated strings to an
|
|
46 |
ostream object.
|
|
47 |
||
48 |
Use the manipulator as follows:
|
|
49 |
@code
|
|
50 |
std::cout << ioutil::join(",", list.begin(), list.end()) << std::endl;
|
|
51 |
std::cout << ioutil::join(",", list) << std::endl;
|
|
52 |
@endcode
|
|
53 |
*/
|
|
54 |
template <class FwdIter> |
|
55 |
joiner<FwdIter> |
|
56 |
join(const std::string& delim, FwdIter start, FwdIter finish) { |
|
57 |
return joiner<FwdIter>(delim, start, finish); |
|
58 |
}
|
|
59 |
||
60 |
/** @overload */
|
|
61 |
template <class Container> |
|
62 |
joiner<typename Container::const_iterator> |
|
63 |
join(const std::string& delim, Container seq) { |
|
64 |
typedef typename Container::const_iterator FwdIter; |
|
65 |
return joiner<FwdIter>(delim, seq.begin(), seq.end()); |
|
66 |
}
|
|
67 |
}
|