]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/quickbook/src/collector.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / tools / quickbook / src / collector.hpp
1 /*=============================================================================
2 Copyright (c) 2002 2004 2006 Joel de Guzman
3 http://spirit.sourceforge.net/
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_QUICKBOOK_COLLECTOR_HPP)
10 #define BOOST_SPIRIT_QUICKBOOK_COLLECTOR_HPP
11
12 #include <string>
13 #include <stack>
14 #include <boost/ref.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/noncopyable.hpp>
17 #include <boost/iostreams/device/back_inserter.hpp>
18 #include <boost/iostreams/filtering_stream.hpp>
19
20 namespace quickbook
21 {
22 struct string_stream
23 {
24 typedef boost::iostreams::filtering_ostream ostream;
25
26 string_stream();
27 string_stream(string_stream const& other);
28 string_stream& operator=(string_stream const& other);
29
30 std::string const& str() const
31 {
32 stream_ptr->flush();
33 return *buffer_ptr.get();
34 }
35
36 std::ostream& get() const
37 {
38 return *stream_ptr.get();
39 }
40
41 void clear()
42 {
43 buffer_ptr->clear();
44 }
45
46 void swap(std::string& other)
47 {
48 stream_ptr->flush();
49 std::swap(other, *buffer_ptr.get());
50 }
51
52 void append(std::string const& other)
53 {
54 stream_ptr->flush();
55 *buffer_ptr.get() += other;
56 }
57
58 private:
59
60 boost::shared_ptr<std::string> buffer_ptr;
61 boost::shared_ptr<ostream> stream_ptr;
62 };
63
64 struct collector : boost::noncopyable
65 {
66 collector();
67 collector(string_stream& out);
68 ~collector();
69
70 void push();
71 void pop();
72
73 std::ostream& get() const
74 {
75 return top.get().get();
76 }
77
78 std::string const& str() const
79 {
80 return top.get().str();
81 }
82
83 void clear()
84 {
85 top.get().clear();
86 }
87
88 void swap(std::string& other)
89 {
90 top.get().swap(other);
91 }
92
93 void append(std::string const& other)
94 {
95 top.get().append(other);
96 }
97
98 private:
99
100 std::stack<string_stream> streams;
101 boost::reference_wrapper<string_stream> main;
102 boost::reference_wrapper<string_stream> top;
103 string_stream default_;
104 };
105
106 template <typename T>
107 inline collector&
108 operator<<(collector& out, T const& val)
109 {
110 out.get() << val;
111 return out;
112 }
113
114 inline collector&
115 operator<<(collector& out, std::string const& val)
116 {
117 out.append(val);
118 return out;
119 }
120 }
121
122 #endif // BOOST_SPIRIT_QUICKBOOK_COLLECTOR_HPP
123