summaryrefslogtreecommitdiffstats
blob: 9df2d9c151b4f1d0f1dae223dcfe4d099346073c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//  Copyright (c) 2001, Daniel C. Nuffer
//  Copyright (c) 2001-2011 Hartmut Kaiser
// 
//  Distributed under 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)

#if !defined(BOOST_SPIRIT_ITERATOR_BUF_ID_CHECK_POLICY_MAR_16_2007_1108AM)
#define BOOST_SPIRIT_ITERATOR_BUF_ID_CHECK_POLICY_MAR_16_2007_1108AM

#include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
#include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <exception>    // for std::exception

namespace boost { namespace spirit { namespace iterator_policies
{
    ///////////////////////////////////////////////////////////////////////////
    //  class illegal_backtracking
    //  thrown by buf_id_check CheckingPolicy if an instance of an iterator is
    //  used after another one has invalidated the queue
    ///////////////////////////////////////////////////////////////////////////
    class illegal_backtracking : public std::exception
    {
    public:
        illegal_backtracking() throw() {}
        ~illegal_backtracking() throw() {}

        char const* what() const throw()
        { 
            return "boost::spirit::multi_pass::illegal_backtracking"; 
        }
    };

    ///////////////////////////////////////////////////////////////////////////////
    //  class buf_id_check
    //  Implementation of the CheckingPolicy used by multi_pass
    //  This policy is most effective when used together with the std_deque
    //  StoragePolicy.
    // 
    //  If used with the fixed_size_queue StoragePolicy, it will not detect
    //  iterator dereferences that are out of the range of the queue.
    ///////////////////////////////////////////////////////////////////////////////
    struct buf_id_check
    {
        ///////////////////////////////////////////////////////////////////////
        struct unique //: detail::default_checking_policy
        {
            unique() : buf_id(0) {}
            unique(unique const& x) : buf_id(x.buf_id) {}

            void swap(unique& x)
            {
                boost::swap(buf_id, x.buf_id);
            }

            // called to verify that everything is ok.
            template <typename MultiPass>
            static void docheck(MultiPass const& mp) 
            {
                if (mp.buf_id != mp.shared()->shared_buf_id)
                    boost::throw_exception(illegal_backtracking());
            }

            // called from multi_pass::clear_queue, so we can increment the count
            template <typename MultiPass>
            static void clear_queue(MultiPass& mp)
            {
                ++mp.shared()->shared_buf_id;
                ++mp.buf_id;
            }

            template <typename MultiPass>
            static void destroy(MultiPass&) {}

        protected:
            unsigned long buf_id;
        };

        ///////////////////////////////////////////////////////////////////////
        struct shared
        {
            shared() : shared_buf_id(0) {}
            unsigned long shared_buf_id;
        };
    };

}}}

#endif