source: NonGTP/Boost/boost/spirit/utility/impl/chset.ipp @ 857

Revision 857, 9.3 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2001-2003 Joel de Guzman
3    Copyright (c) 2001-2003 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#ifndef BOOST_SPIRIT_CHSET_IPP
11#define BOOST_SPIRIT_CHSET_IPP
12
13///////////////////////////////////////////////////////////////////////////////
14#include <boost/limits.hpp>
15#include <boost/spirit/utility/chset.hpp>
16
17///////////////////////////////////////////////////////////////////////////////
18namespace boost { namespace spirit {
19
20///////////////////////////////////////////////////////////////////////////////
21//
22//  chset class
23//
24///////////////////////////////////////////////////////////////////////////////
25namespace utility { namespace impl {
26    template <typename CharT>
27    inline void
28    detach(boost::shared_ptr<basic_chset<CharT> >& ptr)
29    {
30        if (!ptr.unique())
31            ptr = boost::shared_ptr<basic_chset<CharT> >
32                (new basic_chset<CharT>(*ptr));
33    }
34
35    template <typename CharT>
36    inline void
37    detach_clear(boost::shared_ptr<basic_chset<CharT> >& ptr)
38    {
39        if (ptr.unique())
40            ptr->clear();
41        else
42            ptr.reset(new basic_chset<CharT>());
43    }
44
45    template <typename CharT, typename CharT2>
46    void construct_chset(boost::shared_ptr<basic_chset<CharT> >& ptr,
47            CharT2 const* definition)
48    {
49        CharT2 ch = *definition++;
50        while (ch)
51        {
52            CharT2 next = *definition++;
53            if (next == '-')
54            {
55                next = *definition++;
56                if (next == 0)
57                {
58                    ptr->set(ch);
59                    ptr->set('-');
60                    break;
61                }
62                ptr->set(ch, next);
63            }
64            else
65            {
66                ptr->set(ch);
67            }
68            ch = next;
69        }
70    }
71
72    //////////////////////////////////
73
74#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
75
76    template <typename CharT, typename FakeT>
77    void chset_negated_set(boost::shared_ptr<basic_chset<CharT> > &ptr, chlit<CharT> const &ch,
78            FakeT)
79    {
80        if(ch.ch != (std::numeric_limits<CharT>::min)()) {
81            ptr->set((std::numeric_limits<CharT>::min)(), ch.ch - 1);
82        }
83        if(ch.ch != (std::numeric_limits<CharT>::max)()) {
84            ptr->set(ch.ch + 1, (std::numeric_limits<CharT>::max)());
85        }
86    }
87   
88    template <typename CharT, typename FakeT>
89    void chset_negated_set(boost::shared_ptr<basic_chset<CharT> > &ptr,
90            spirit::range<CharT> const &rng, FakeT)
91    {
92        if(rng.first != (std::numeric_limits<CharT>::min)()) {
93            ptr->set((std::numeric_limits<CharT>::min)(), rng.first - 1);
94        }
95        if(rng.last != (std::numeric_limits<CharT>::max)()) {
96            ptr->set(rng.last + 1, (std::numeric_limits<CharT>::max)());
97        }
98    }
99
100#endif // BOOST_WORKAROUND(BOOST_MSVC, < 1300)
101
102//////////////////////////////////
103
104}} // namespace utility::impl
105
106template <typename CharT>
107inline chset<CharT>::chset()
108: ptr(new basic_chset<CharT>()) {}
109
110template <typename CharT>
111inline chset<CharT>::chset(chset const& arg_)
112: ptr(new basic_chset<CharT>(*arg_.ptr)) {}
113
114template <typename CharT>
115inline chset<CharT>::chset(CharT arg_)
116: ptr(new basic_chset<CharT>())
117{ ptr->set(arg_); }
118
119template <typename CharT>
120inline chset<CharT>::chset(anychar_parser /*arg*/)
121: ptr(new basic_chset<CharT>())
122{
123    ptr->set(
124        (std::numeric_limits<CharT>::min)(),
125        (std::numeric_limits<CharT>::max)()
126    );
127}
128
129template <typename CharT>
130inline chset<CharT>::chset(nothing_parser arg_)
131: ptr(new basic_chset<CharT>()) {}
132
133template <typename CharT>
134inline chset<CharT>::chset(chlit<CharT> const& arg_)
135: ptr(new basic_chset<CharT>())
136{ ptr->set(arg_.ch); }
137
138template <typename CharT>
139inline chset<CharT>::chset(range<CharT> const& arg_)
140: ptr(new basic_chset<CharT>())
141{ ptr->set(arg_.first, arg_.last); }
142
143#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
144
145template <typename CharT>
146inline chset<CharT>::chset(negated_char_parser<chlit<CharT> > const& arg_)
147: ptr(new basic_chset<CharT>())
148{
149    set(arg_);
150}
151
152template <typename CharT>
153inline chset<CharT>::chset(negated_char_parser<range<CharT> > const& arg_)
154: ptr(new basic_chset<CharT>())
155{
156    set(arg_);
157}
158
159#endif // !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
160
161template <typename CharT>
162inline chset<CharT>::~chset() {}
163
164template <typename CharT>
165inline chset<CharT>&
166chset<CharT>::operator=(chset const& rhs)
167{
168    ptr = rhs.ptr;
169    return *this;
170}
171
172template <typename CharT>
173inline chset<CharT>&
174chset<CharT>::operator=(CharT rhs)
175{
176    utility::impl::detach_clear(ptr);
177    ptr->set(rhs);
178    return *this;
179}
180
181template <typename CharT>
182inline chset<CharT>&
183chset<CharT>::operator=(anychar_parser rhs)
184{
185    utility::impl::detach_clear(ptr);
186    ptr->set(
187        (std::numeric_limits<CharT>::min)(),
188        (std::numeric_limits<CharT>::max)()
189    );
190    return *this;
191}
192
193template <typename CharT>
194inline chset<CharT>&
195chset<CharT>::operator=(nothing_parser rhs)
196{
197    utility::impl::detach_clear(ptr);
198    return *this;
199}
200
201template <typename CharT>
202inline chset<CharT>&
203chset<CharT>::operator=(chlit<CharT> const& rhs)
204{
205    utility::impl::detach_clear(ptr);
206    ptr->set(rhs.ch);
207    return *this;
208}
209
210template <typename CharT>
211inline chset<CharT>&
212chset<CharT>::operator=(range<CharT> const& rhs)
213{
214    utility::impl::detach_clear(ptr);
215    ptr->set(rhs.first, rhs.last);
216    return *this;
217}
218
219#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
220
221template <typename CharT>
222inline chset<CharT>&
223chset<CharT>::operator=(negated_char_parser<chlit<CharT> > const& rhs)
224{
225    utility::impl::detach_clear(ptr);
226    set(rhs);
227    return *this;
228}
229
230template <typename CharT>
231inline chset<CharT>&
232chset<CharT>::operator=(negated_char_parser<range<CharT> > const& rhs)
233{
234    utility::impl::detach_clear(ptr);
235    set(rhs);
236    return *this;
237}
238
239#endif // !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
240
241template <typename CharT>
242inline void
243chset<CharT>::set(range<CharT> const& arg_)
244{
245    utility::impl::detach(ptr);
246    ptr->set(arg_.first, arg_.last);
247}
248
249#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
250
251template <typename CharT>
252inline void
253chset<CharT>::set(negated_char_parser<chlit<CharT> > const& arg_)
254{
255    utility::impl::detach(ptr);
256   
257    if(arg_.positive.ch != (std::numeric_limits<CharT>::min)()) {
258        ptr->set((std::numeric_limits<CharT>::min)(), arg_.positive.ch - 1);
259    }
260    if(arg_.positive.ch != (std::numeric_limits<CharT>::max)()) {
261        ptr->set(arg_.positive.ch + 1, (std::numeric_limits<CharT>::max)());
262    }
263}
264
265template <typename CharT>
266inline void
267chset<CharT>::set(negated_char_parser<range<CharT> > const& arg_)
268{
269    utility::impl::detach(ptr);
270   
271    if(arg_.positive.first != (std::numeric_limits<CharT>::min)()) {
272        ptr->set((std::numeric_limits<CharT>::min)(), arg_.positive.first - 1);
273    }
274    if(arg_.positive.last != (std::numeric_limits<CharT>::max)()) {
275        ptr->set(arg_.positive.last + 1, (std::numeric_limits<CharT>::max)());
276    }
277}
278
279#endif // !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
280
281template <typename CharT>
282inline void
283chset<CharT>::clear(range<CharT> const& arg_)
284{
285    utility::impl::detach(ptr);
286    ptr->clear(arg_.first, arg_.last);
287}
288
289template <typename CharT>
290inline void
291chset<CharT>::clear(negated_char_parser<range<CharT> > const& arg_)
292{
293    utility::impl::detach(ptr);
294
295    if(arg_.positive.first != (std::numeric_limits<CharT>::min)()) {
296        ptr->clear((std::numeric_limits<CharT>::min)(), arg_.positive.first - 1);
297    }
298    if(arg_.positive.last != (std::numeric_limits<CharT>::max)()) {
299        ptr->clear(arg_.positive.last + 1, (std::numeric_limits<CharT>::max)());
300    }
301}
302
303template <typename CharT>
304inline bool
305chset<CharT>::test(CharT ch) const
306{ return ptr->test(ch); }
307
308template <typename CharT>
309inline chset<CharT>&
310chset<CharT>::inverse()
311{
312    utility::impl::detach(ptr);
313    ptr->inverse();
314    return *this;
315}
316
317template <typename CharT>
318inline void
319chset<CharT>::swap(chset& x)
320{ ptr.swap(x.ptr); }
321
322template <typename CharT>
323inline chset<CharT>&
324chset<CharT>::operator|=(chset const& x)
325{
326    utility::impl::detach(ptr);
327    *ptr |= *x.ptr;
328    return *this;
329}
330
331template <typename CharT>
332inline chset<CharT>&
333chset<CharT>::operator&=(chset const& x)
334{
335    utility::impl::detach(ptr);
336    *ptr &= *x.ptr;
337    return *this;
338}
339
340template <typename CharT>
341inline chset<CharT>&
342chset<CharT>::operator-=(chset const& x)
343{
344    utility::impl::detach(ptr);
345    *ptr -= *x.ptr;
346    return *this;
347}
348
349template <typename CharT>
350inline chset<CharT>&
351chset<CharT>::operator^=(chset const& x)
352{
353    utility::impl::detach(ptr);
354    *ptr ^= *x.ptr;
355    return *this;
356}
357
358///////////////////////////////////////////////////////////////////////////////
359}} // namespace boost::spirit
360
361#endif
362
Note: See TracBrowser for help on using the repository browser.