summaryrefslogtreecommitdiff
path: root/tools/quickbook/src/collector.cpp
blob: fb2daec90161f2f9be84c1320efbfd133d996a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*=============================================================================
    Copyright (c) 2002 2004 2006 Joel de Guzman
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "collector.hpp"
#include <boost/assert.hpp>

namespace quickbook
{
    string_stream::string_stream()
        : buffer_ptr(new std::string())
        , stream_ptr(new ostream(boost::iostreams::back_inserter(*buffer_ptr.get())))
    {}

    string_stream::string_stream(string_stream const& other)
        : buffer_ptr(other.buffer_ptr)
        , stream_ptr(other.stream_ptr)
    {}
    
    string_stream&
    string_stream::operator=(string_stream const& other)
    {
        buffer_ptr = other.buffer_ptr;
        stream_ptr = other.stream_ptr;
        return *this;
    }
        
    collector::collector()
        : main(default_)
        , top(default_)
    {
    }

    collector::collector(string_stream& out)
        : main(out) 
        , top(out) 
    {
    }
    
    collector::~collector()
    {
        BOOST_ASSERT(streams.empty()); // assert there are no more pushes than pops!!!
    }
    
    void 
    collector::push()
    {
        streams.push(string_stream());
        top = boost::ref(streams.top());
    }
    
    void 
    collector::pop()
    {
        BOOST_ASSERT(!streams.empty());
        streams.pop();

        if (streams.empty())
            top = boost::ref(main);
        else
            top = boost::ref(streams.top());
    }
}