source: NonGTP/Boost/boost/spirit/core/non_terminal/parser_id.hpp @ 857

Revision 857, 3.7 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2001-2003 Joel de Guzman
3    Copyright (c) 2001 Daniel Nuffer
4    http://spirit.sourceforge.net/
5
6    Use, modification and distribution is subject to the Boost Software
7    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10#if !defined(BOOST_SPIRIT_PARSER_ID_HPP)
11#define BOOST_SPIRIT_PARSER_ID_HPP
12
13#if defined(BOOST_SPIRIT_DEBUG)
14#   include <ostream>
15#endif
16
17///////////////////////////////////////////////////////////////////////////////
18namespace boost { namespace spirit {
19
20    ///////////////////////////////////////////////////////////////////////////
21    //
22    //  parser_id class
23    //
24    ///////////////////////////////////////////////////////////////////////////
25    class parser_id
26    {
27    public:
28                    parser_id()                     : p(0) {}
29        explicit    parser_id(void const* prule)    : p(prule) {}
30                    parser_id(std::size_t l_)       : l(l_) {}
31
32        bool operator==(parser_id const& x) const   { return p == x.p; }
33        bool operator!=(parser_id const& x) const   { return !(*this == x); }
34        bool operator<(parser_id const& x) const    { return p < x.p; }
35        std::size_t to_long() const                 { return l; }
36
37    private:
38
39        union
40        {
41            void const* p;
42            std::size_t l;
43        };
44    };
45
46    #if defined(BOOST_SPIRIT_DEBUG)
47    inline std::ostream&
48    operator<<(std::ostream& out, parser_id const& rid)
49    {
50        out << rid.to_long();
51        return out;
52    }
53    #endif
54
55    ///////////////////////////////////////////////////////////////////////////
56    //
57    //  parser_tag_base class: base class of all parser tags
58    //
59    ///////////////////////////////////////////////////////////////////////////
60    struct parser_tag_base {};
61   
62    ///////////////////////////////////////////////////////////////////////////
63    //
64    //  parser_address_tag class: tags a parser with its address
65    //
66    ///////////////////////////////////////////////////////////////////////////
67    struct parser_address_tag : parser_tag_base
68    {
69        parser_id id() const
70        { return parser_id(reinterpret_cast<std::size_t>(this)); }
71    };
72
73    ///////////////////////////////////////////////////////////////////////////
74    //
75    //  parser_tag class: tags a parser with an integer ID
76    //
77    ///////////////////////////////////////////////////////////////////////////
78    template <int N>
79    struct parser_tag : parser_tag_base
80    {
81        static parser_id id()
82        { return parser_id(std::size_t(N)); }
83    };
84
85    ///////////////////////////////////////////////////////////////////////////
86    //
87    //  dynamic_parser_tag class: tags a parser with a dynamically changeable
88    //  integer ID
89    //
90    ///////////////////////////////////////////////////////////////////////////
91    class dynamic_parser_tag : public parser_tag_base
92    {
93    public:
94   
95        dynamic_parser_tag()
96        : tag(std::size_t(0)) {}
97       
98        parser_id
99        id() const
100        {
101            return
102                tag.to_long()
103                ? tag
104                : parser_id(reinterpret_cast<std::size_t>(this));
105        }
106
107        void set_id(parser_id id) { tag = id; }
108       
109    private:
110   
111        parser_id tag;
112    };
113
114///////////////////////////////////////////////////////////////////////////////
115}} // namespace boost::spirit
116
117#endif
118
Note: See TracBrowser for help on using the repository browser.