1 /*=============================================================================
2     Copyright (c) 2001-2003 Joel de Guzman
3     Copyright (c) 2001 Daniel Nuffer
4     http://spirit.sourceforge.net/
5 
6   Distributed under the Boost Software License, Version 1.0. (See accompanying
7   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_PARSER_ID_HPP)
10 #define BOOST_SPIRIT_PARSER_ID_HPP
11 
12 #if defined(BOOST_SPIRIT_DEBUG)
13 #   include <ostream>
14 #endif
15 #include <boost/spirit/home/classic/namespace.hpp>
16 
17 ///////////////////////////////////////////////////////////////////////////////
18 namespace boost { namespace spirit {
19 
20 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
21 
22     ///////////////////////////////////////////////////////////////////////////
23     //
24     //  parser_id class
25     //
26     ///////////////////////////////////////////////////////////////////////////
27     class parser_id
28     {
29     public:
parser_id()30                     parser_id()                     : p(0) {}
parser_id(void const * prule)31         explicit    parser_id(void const* prule)    : p(prule) {}
parser_id(std::size_t l_)32                     parser_id(std::size_t l_)       : l(l_) {}
33 
operator ==(parser_id const & x) const34         bool operator==(parser_id const& x) const   { return p == x.p; }
operator !=(parser_id const & x) const35         bool operator!=(parser_id const& x) const   { return !(*this == x); }
operator <(parser_id const & x) const36         bool operator<(parser_id const& x) const    { return p < x.p; }
to_long() const37         std::size_t to_long() const                 { return l; }
38 
39     private:
40 
41         union
42         {
43             void const* p;
44             std::size_t l;
45         };
46     };
47 
48     #if defined(BOOST_SPIRIT_DEBUG)
49     inline std::ostream&
operator <<(std::ostream & out,parser_id const & rid)50     operator<<(std::ostream& out, parser_id const& rid)
51     {
52         out << (unsigned int)rid.to_long();
53         return out;
54     }
55     #endif
56 
57     ///////////////////////////////////////////////////////////////////////////
58     //
59     //  parser_tag_base class: base class of all parser tags
60     //
61     ///////////////////////////////////////////////////////////////////////////
62     struct parser_tag_base {};
63 
64     ///////////////////////////////////////////////////////////////////////////
65     //
66     //  parser_address_tag class: tags a parser with its address
67     //
68     ///////////////////////////////////////////////////////////////////////////
69     struct parser_address_tag : parser_tag_base
70     {
idboost::spirit::parser_address_tag71         parser_id id() const
72         { return parser_id(reinterpret_cast<std::size_t>(this)); }
73     };
74 
75     ///////////////////////////////////////////////////////////////////////////
76     //
77     //  parser_tag class: tags a parser with an integer ID
78     //
79     ///////////////////////////////////////////////////////////////////////////
80     template <int N>
81     struct parser_tag : parser_tag_base
82     {
idboost::spirit::parser_tag83         static parser_id id()
84         { return parser_id(std::size_t(N)); }
85     };
86 
87     ///////////////////////////////////////////////////////////////////////////
88     //
89     //  dynamic_parser_tag class: tags a parser with a dynamically changeable
90     //  integer ID
91     //
92     ///////////////////////////////////////////////////////////////////////////
93     class dynamic_parser_tag : public parser_tag_base
94     {
95     public:
96 
dynamic_parser_tag()97         dynamic_parser_tag()
98         : tag(std::size_t(0)) {}
99 
100         parser_id
id() const101         id() const
102         {
103             return
104                 tag.to_long()
105                 ? tag
106                 : parser_id(reinterpret_cast<std::size_t>(this));
107         }
108 
set_id(parser_id id_)109         void set_id(parser_id id_) { tag = id_; }
110 
111     private:
112 
113         parser_id tag;
114     };
115 
116 ///////////////////////////////////////////////////////////////////////////////
117 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
118 
119 }} // namespace BOOST_SPIRIT_CLASSIC_NS
120 
121 #endif
122 
123