]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae | 12 | #include <stack> |
11fdf7f2 | 13 | #include <string> |
7c673cae FG |
14 | #include <boost/iostreams/device/back_inserter.hpp> |
15 | #include <boost/iostreams/filtering_stream.hpp> | |
11fdf7f2 TL |
16 | #include <boost/noncopyable.hpp> |
17 | #include <boost/ref.hpp> | |
18 | #include <boost/shared_ptr.hpp> | |
7c673cae FG |
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 | } | |
11fdf7f2 TL |
35 | |
36 | std::ostream& get() const { return *stream_ptr.get(); } | |
37 | ||
38 | void clear() { buffer_ptr->clear(); } | |
7c673cae FG |
39 | |
40 | void swap(std::string& other) | |
41 | { | |
42 | stream_ptr->flush(); | |
43 | std::swap(other, *buffer_ptr.get()); | |
44 | } | |
45 | ||
46 | void append(std::string const& other) | |
47 | { | |
48 | stream_ptr->flush(); | |
49 | *buffer_ptr.get() += other; | |
50 | } | |
51 | ||
11fdf7f2 | 52 | private: |
7c673cae FG |
53 | boost::shared_ptr<std::string> buffer_ptr; |
54 | boost::shared_ptr<ostream> stream_ptr; | |
55 | }; | |
56 | ||
57 | struct collector : boost::noncopyable | |
58 | { | |
59 | collector(); | |
60 | collector(string_stream& out); | |
61 | ~collector(); | |
11fdf7f2 | 62 | |
7c673cae FG |
63 | void push(); |
64 | void pop(); | |
65 | ||
11fdf7f2 | 66 | std::ostream& get() const { return top.get().get(); } |
7c673cae | 67 | |
11fdf7f2 | 68 | std::string const& str() const { return top.get().str(); } |
7c673cae | 69 | |
11fdf7f2 | 70 | void clear() { top.get().clear(); } |
7c673cae | 71 | |
11fdf7f2 TL |
72 | void swap(std::string& other) { top.get().swap(other); } |
73 | ||
74 | void append(std::string const& other) { top.get().append(other); } | |
75 | ||
76 | private: | |
7c673cae FG |
77 | std::stack<string_stream> streams; |
78 | boost::reference_wrapper<string_stream> main; | |
79 | boost::reference_wrapper<string_stream> top; | |
80 | string_stream default_; | |
81 | }; | |
11fdf7f2 | 82 | |
7c673cae | 83 | template <typename T> |
11fdf7f2 | 84 | inline collector& operator<<(collector& out, T const& val) |
7c673cae FG |
85 | { |
86 | out.get() << val; | |
87 | return out; | |
88 | } | |
89 | ||
11fdf7f2 | 90 | inline collector& operator<<(collector& out, std::string const& val) |
7c673cae FG |
91 | { |
92 | out.append(val); | |
93 | return out; | |
94 | } | |
95 | } | |
96 | ||
97 | #endif // BOOST_SPIRIT_QUICKBOOK_COLLECTOR_HPP |