source: NonGTP/Boost/boost/spirit/iterator/impl/position_iterator.ipp @ 857

Revision 857, 4.1 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2002 Juan Carlos Arevalo-Baeza
3    Copyright (c) 2002-2003 Hartmut Kaiser
4    Copyright (c) 2003 Giovanni Bajo
5    http://spirit.sourceforge.net/
6
7    Use, modification and distribution is subject to the Boost Software
8    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9    http://www.boost.org/LICENSE_1_0.txt)
10=============================================================================*/
11#ifndef POSITION_ITERATOR_IPP
12#define POSITION_ITERATOR_IPP
13
14#include <boost/config.hpp>
15#include <boost/iterator_adaptors.hpp>
16#include <boost/type_traits/add_const.hpp>
17#include <boost/mpl/if.hpp>
18#include <boost/type_traits/is_same.hpp>
19#include <boost/spirit/core/nil.hpp>  // for nil_t
20#include <boost/detail/iterator.hpp> // for boost::detail::iterator_traits
21
22namespace boost { namespace spirit {
23
24///////////////////////////////////////////////////////////////////////////////
25//
26//  position_policy<file_position_without_column>
27//
28//  Specialization to handle file_position_without_column. Only take care of
29//  newlines since no column tracking is needed.
30//
31///////////////////////////////////////////////////////////////////////////////
32template <>
33class position_policy<file_position_without_column> {
34
35public:
36    void next_line(file_position_without_column& pos)
37    {
38        ++pos.line;
39    }
40
41    void set_tab_chars(unsigned int /*chars*/){}
42    void next_char(file_position_without_column& /*pos*/)    {}
43    void tabulation(file_position_without_column& /*pos*/)   {}
44};
45
46///////////////////////////////////////////////////////////////////////////////
47//
48//  position_policy<file_position>
49//
50//  Specialization to handle file_position. Track characters and tabulation
51//  to compute the current column correctly.
52//
53//  Default tab size is 4. You can change this with the set_tabchars member
54//  of position_iterator.
55//
56///////////////////////////////////////////////////////////////////////////////
57template <>
58class position_policy<file_position> {
59
60public:
61    position_policy()
62        : m_CharsPerTab(4)
63    {}
64
65    void next_line(file_position& pos)
66    {
67        ++pos.line;
68        pos.column = 1;
69    }
70
71    void set_tab_chars(unsigned int chars)
72    {
73        m_CharsPerTab = chars;
74    }
75
76    void next_char(file_position& pos)
77    {
78        ++pos.column;
79    }
80
81    void tabulation(file_position& pos)
82    {
83        pos.column += m_CharsPerTab - (pos.column - 1) % m_CharsPerTab;
84    }
85
86private:
87    unsigned int m_CharsPerTab;
88};
89
90/* namespace boost::spirit { */ namespace iterator_ { namespace impl {
91
92///////////////////////////////////////////////////////////////////////////////
93//
94//  position_iterator_base_generator
95//
96//  Metafunction to generate the iterator type using boost::iterator_adaptors,
97//  hiding all the metaprogramming thunking code in it. It is used
98//  mainly to keep the public interface (position_iterator) cleanear.
99//
100///////////////////////////////////////////////////////////////////////////////
101template <typename MainIterT, typename ForwardIterT, typename PositionT>
102struct position_iterator_base_generator
103{
104private:
105    typedef boost::detail::iterator_traits<ForwardIterT> traits;
106    typedef typename traits::value_type value_type;
107    typedef typename traits::iterator_category iter_category_t;
108
109    // Position iterator is always a non-mutable iterator
110    typedef typename boost::add_const<value_type>::type const_value_type;
111
112public:
113    // Check if the MainIterT is nil. If it's nil, it means that the actual
114    //  self type is position_iterator. Otherwise, it's a real type we
115    //  must use
116    typedef typename boost::mpl::if_<
117        typename boost::is_same<MainIterT, nil_t>::type,
118        position_iterator<ForwardIterT, PositionT, nil_t>,
119        MainIterT
120    >::type main_iter_t;
121
122    typedef boost::iterator_adaptor<
123        main_iter_t,
124        ForwardIterT,
125        const_value_type
126    > type;
127};
128
129}}}} /* namespace boost::spirit::iterator_::impl */
130
131#endif
Note: See TracBrowser for help on using the repository browser.