source: NonGTP/Boost/boost/wave/util/functor_input.hpp @ 857

Revision 857, 4.1 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3
4    http://www.boost.org/
5
6    Copyright (c) 2001-2005 Hartmut Kaiser. Distributed under the Boost
7    Software License, Version 1.0. (See accompanying file
8    LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10
11#if !defined(FUNCTOR_INPUT_HPP_ED3A4C21_8F8A_453F_B438_08214FAC106A_INCLUDED)
12#define FUNCTOR_INPUT_HPP_ED3A4C21_8F8A_453F_B438_08214FAC106A_INCLUDED
13
14#include <boost/assert.hpp>
15#include <boost/spirit/iterator/multi_pass.hpp>
16
17///////////////////////////////////////////////////////////////////////////////
18namespace boost {
19namespace wave {
20namespace util {
21
22///////////////////////////////////////////////////////////////////////////////
23//
24//  class functor_input
25//
26//      Implementation of the InputPolicy used by multi_pass
27//      functor_input gets tokens from a functor
28//      Note: the functor must have a typedef for result_type
29//            It also must have a static variable of type result_type defined
30//            to represent eof that is called eof.
31//
32//      This functor input policy template is essentially the same as the
33//      predefined multi_pass functor_input policy. The difference is,
34//      that the first token is not read at initialization time, but only
35//      just before returning the first token.
36//
37///////////////////////////////////////////////////////////////////////////////
38struct functor_input {
39
40    template <typename FunctorT>
41    class inner {
42    private:
43        typedef typename FunctorT::result_type result_type;
44
45    public:
46        typedef result_type value_type;
47
48    private:
49        struct Data {
50            Data(FunctorT const &ftor_)
51            :   ftor(ftor_), was_initialized(false)
52            {}
53           
54            FunctorT ftor;
55            value_type curtok;
56            bool was_initialized;
57        };
58       
59       // Needed by compilers not implementing the resolution to DR45. For
60       // reference, see
61       // http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#45.
62
63       friend struct Data;
64
65    public:
66        typedef std::ptrdiff_t difference_type;
67        typedef result_type *pointer;
68        typedef result_type &reference;
69
70    protected:
71        inner()
72        :   data(0)
73        {}
74
75        inner(FunctorT const &x)
76        :   data(new Data(x))
77        {}
78
79        inner(inner const &x)
80        :   data(x.data)
81        {}
82
83        void destroy()
84        {
85            delete data;
86            data = 0;
87        }
88
89        bool same_input(inner const &x) const
90        {
91            return data == x.data;
92        }
93
94        void swap(inner &x)
95        {
96            boost::spirit::impl::mp_swap(data, x.data);
97        }
98       
99        void ensure_initialized() const
100        {
101            if (data && !data->was_initialized) {
102                data->curtok = (data->ftor)();    // get the first token
103                data->was_initialized = true;
104            }
105        }
106       
107    public:
108        reference get_input() const
109        {
110            ensure_initialized();
111            return data->curtok;
112        }
113
114        void advance_input()
115        {
116            BOOST_ASSERT(0 != data);
117            data->curtok = (data->ftor)();
118            data->was_initialized = true;
119        }
120
121        bool input_at_eof() const
122        {
123            ensure_initialized();
124            return !data || data->curtok == data->ftor.eof;
125        }
126
127        FunctorT& get_functor() const
128        {
129            BOOST_ASSERT(0 != data);
130            return data->ftor;
131        }
132
133    private:
134        mutable Data *data;
135    };
136};
137
138///////////////////////////////////////////////////////////////////////////////
139}   // namespace util
140}   // namespace wave
141}   // namespace boost
142
143#endif // !defined(FUNCTOR_INPUT_HPP_ED3A4C21_8F8A_453F_B438_08214FAC106A_INCLUDED)
Note: See TracBrowser for help on using the repository browser.