• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stack>
13 #include <string>
14 #include <boost/iostreams/device/back_inserter.hpp>
15 #include <boost/iostreams/filtering_stream.hpp>
16 #include <boost/noncopyable.hpp>
17 #include <boost/ref.hpp>
18 #include <boost/shared_ptr.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 
strquickbook::string_stream30         std::string const& str() const
31         {
32             stream_ptr->flush();
33             return *buffer_ptr.get();
34         }
35 
getquickbook::string_stream36         std::ostream& get() const { return *stream_ptr.get(); }
37 
clearquickbook::string_stream38         void clear() { buffer_ptr->clear(); }
39 
swapquickbook::string_stream40         void swap(std::string& other)
41         {
42             stream_ptr->flush();
43             std::swap(other, *buffer_ptr.get());
44         }
45 
appendquickbook::string_stream46         void append(std::string const& other)
47         {
48             stream_ptr->flush();
49             *buffer_ptr.get() += other;
50         }
51 
52       private:
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();
62 
63         void push();
64         void pop();
65 
getquickbook::collector66         std::ostream& get() const { return top.get().get(); }
67 
strquickbook::collector68         std::string const& str() const { return top.get().str(); }
69 
clearquickbook::collector70         void clear() { top.get().clear(); }
71 
swapquickbook::collector72         void swap(std::string& other) { top.get().swap(other); }
73 
appendquickbook::collector74         void append(std::string const& other) { top.get().append(other); }
75 
76       private:
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     };
82 
83     template <typename T>
operator <<(collector & out,T const & val)84     inline collector& operator<<(collector& out, T const& val)
85     {
86         out.get() << val;
87         return out;
88     }
89 
operator <<(collector & out,std::string const & val)90     inline collector& operator<<(collector& out, std::string const& val)
91     {
92         out.append(val);
93         return out;
94     }
95 }
96 
97 #endif // BOOST_SPIRIT_QUICKBOOK_COLLECTOR_HPP
98