PageRenderTime 48ms CodeModel.GetById 2ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/spirit/home/qi/detail/pass_container.hpp

http://hadesmem.googlecode.com/
C++ Header | 362 lines | 233 code | 43 blank | 86 comment | 3 complexity | 5a03fcfdf0d3a286e7073b944dbd786c MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2011 Joel de Guzman
  3    Copyright (c) 2001-2011 Hartmut Kaiser
  4
  5    Distributed under the Boost Software License, Version 1.0. (See accompanying
  6    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7=============================================================================*/
  8#if !defined(SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM)
  9#define SPIRIT_PASS_CONTAINER_JANUARY_06_2009_0802PM
 10
 11#if defined(_MSC_VER)
 12#pragma once
 13#endif
 14
 15#include <boost/spirit/home/qi/detail/attributes.hpp>
 16#include <boost/spirit/home/support/container.hpp>
 17#include <boost/spirit/home/support/handles_container.hpp>
 18#include <boost/type_traits/is_base_of.hpp>
 19#include <boost/type_traits/is_convertible.hpp>
 20#include <boost/mpl/bool.hpp>
 21#include <boost/mpl/and.hpp>
 22#include <boost/mpl/or.hpp>
 23#include <boost/preprocessor/cat.hpp>
 24#include <boost/preprocessor/repetition/repeat.hpp>
 25
 26namespace boost { namespace spirit { namespace qi { namespace detail
 27{
 28    // Helper meta-function allowing to evaluate weak substitutability and
 29    // negate the result if the predicate (Sequence) is not true
 30    template <typename Sequence, typename Attribute, typename ValueType>
 31    struct negate_weak_substitute_if_not
 32      : mpl::if_<
 33            Sequence
 34          , typename traits::is_weak_substitute<Attribute, ValueType>::type
 35          , typename mpl::not_<
 36                traits::is_weak_substitute<Attribute, ValueType> 
 37            >::type>
 38    {};
 39
 40    // pass_through_container: utility to check decide whether a provided 
 41    // container attribute needs to be passed through to the current component 
 42    // or of we need to split the container by passing along instances of its
 43    // value type
 44
 45    // if the expected attribute of the current component is neither a Fusion 
 46    // sequence nor a container, we will pass through the provided container 
 47    // only if its value type is not compatible with the component
 48    template <typename Container, typename ValueType, typename Attribute
 49      , typename Sequence, typename Enable = void>
 50    struct pass_through_container_base
 51      : negate_weak_substitute_if_not<Sequence, Attribute, ValueType>
 52    {};
 53
 54    // Specialization for fusion sequences, in this case we check whether all
 55    // the types in the sequence are convertible to the lhs attribute.
 56    // 
 57    // We return false if the rhs attribute itself is a fusion sequence, which
 58    // is compatible with the LHS sequence (we want to pass through this 
 59    // attribute without it being split apart).
 60    template <typename Container, typename ValueType, typename Attribute
 61      , typename Sequence = mpl::true_>
 62    struct not_compatible_element
 63      : mpl::and_<
 64            negate_weak_substitute_if_not<Sequence, Attribute, Container>
 65          , negate_weak_substitute_if_not<Sequence, Attribute, ValueType> >
 66    {};
 67
 68    // If the value type of the container is not a Fusion sequence, we pass
 69    // through the container if each of the elements of the Attribute 
 70    // sequence is compatible with either the container or its value type.
 71    template <typename Container, typename ValueType, typename Attribute
 72      , typename Sequence
 73      , bool IsSequence = fusion::traits::is_sequence<ValueType>::value>
 74    struct pass_through_container_fusion_sequence
 75    {
 76        typedef typename mpl::find_if<
 77            Attribute, not_compatible_element<Container, ValueType, mpl::_1>
 78        >::type iter;
 79        typedef typename mpl::end<Attribute>::type end;
 80
 81        typedef typename is_same<iter, end>::type type;
 82    };
 83
 84    // If both, the Attribute and the value type of the provided container
 85    // are Fusion sequences, we pass the container only if the two 
 86    // sequences are not compatible.
 87    template <typename Container, typename ValueType, typename Attribute
 88      , typename Sequence>
 89    struct pass_through_container_fusion_sequence<
 90            Container, ValueType, Attribute, Sequence, true>
 91    {
 92        typedef typename mpl::find_if<
 93            Attribute
 94          , not_compatible_element<Container, ValueType, mpl::_1, Sequence>
 95        >::type iter;
 96        typedef typename mpl::end<Attribute>::type end;
 97
 98        typedef typename is_same<iter, end>::type type;
 99    };
100
101    template <typename Container, typename ValueType, typename Attribute
102      , typename Sequence>
103    struct pass_through_container_base<Container, ValueType, Attribute
104          , Sequence
105          , typename enable_if<fusion::traits::is_sequence<Attribute> >::type>
106      : pass_through_container_fusion_sequence<
107            Container, ValueType, Attribute, Sequence>
108    {};
109
110    // Specialization for containers
111    //
112    // If the value type of the attribute of the current component is not
113    // a Fusion sequence, we have to pass through the provided container if 
114    // both are compatible.
115    template <typename Container, typename ValueType, typename Attribute
116      , typename Sequence, typename AttributeValueType
117      , bool IsSequence = fusion::traits::is_sequence<AttributeValueType>::value>
118    struct pass_through_container_container
119      : mpl::or_<
120            traits::is_weak_substitute<Attribute, Container> 
121          , traits::is_weak_substitute<AttributeValueType, Container> >
122    {};
123
124    // If the value type of the exposed container attribute is a Fusion
125    // sequence, we use the already existing logic for those.
126    template <typename Container, typename ValueType, typename Attribute
127      , typename Sequence, typename AttributeValueType>
128    struct pass_through_container_container<
129            Container, ValueType, Attribute, Sequence, AttributeValueType, true>
130      : pass_through_container_fusion_sequence<
131            Container, ValueType, AttributeValueType, Sequence>
132    {};
133
134    template <typename Container, typename ValueType, typename Attribute
135      , typename Sequence>
136    struct pass_through_container_base<
137            Container, ValueType, Attribute, Sequence
138          , typename enable_if<traits::is_container<Attribute> >::type>
139      : detail::pass_through_container_container<
140            Container, ValueType, Attribute, Sequence
141          , typename traits::container_value<Attribute>::type>
142    {};
143
144    // Specialization for exposed optional attributes
145    //
146    // If the type embedded in the exposed optional is not a Fusion 
147    // sequence we pass through the container attribute if it is compatible
148    // either to the optionals embedded type or to the containers value 
149    // type.
150    template <typename Container, typename ValueType, typename Attribute
151      , typename Sequence
152      , bool IsSequence = fusion::traits::is_sequence<Attribute>::value>
153    struct pass_through_container_optional
154      : mpl::or_<
155            traits::is_weak_substitute<Attribute, Container> 
156          , traits::is_weak_substitute<Attribute, ValueType> >
157    {};
158
159    // If the embedded type of the exposed optional attribute is a Fusion
160    // sequence, we use the already existing logic for those.
161    template <typename Container, typename ValueType, typename Attribute
162      , typename Sequence>
163    struct pass_through_container_optional<
164                Container, ValueType, Attribute, Sequence, true>
165      : pass_through_container_fusion_sequence<
166            Container, ValueType, Attribute, Sequence>
167    {};
168
169    ///////////////////////////////////////////////////////////////////////////
170    template <typename Container, typename ValueType, typename Attribute
171      , typename Sequence>
172    struct pass_through_container
173      : pass_through_container_base<Container, ValueType, Attribute, Sequence> 
174    {};
175
176    // Handle optional attributes
177    template <typename Container, typename ValueType, typename Attribute
178      , typename Sequence>
179    struct pass_through_container<
180                Container, ValueType, boost::optional<Attribute>, Sequence>
181      : pass_through_container_optional<
182            Container, ValueType, Attribute, Sequence> 
183    {};
184
185    // If both, the containers value type and the exposed attribute type are
186    // optionals we are allowed to pass through the the container only if the
187    // embedded types of those optionals are not compatible.
188    template <typename Container, typename ValueType, typename Attribute
189      , typename Sequence>
190    struct pass_through_container<
191            Container, boost::optional<ValueType>, boost::optional<Attribute>
192          , Sequence>
193      : mpl::not_<traits::is_weak_substitute<Attribute, ValueType> > 
194    {};
195
196    // Specialization for exposed variant attributes
197    // 
198    // We pass through the container attribute if at least one of the embedded 
199    // types in the variant requires to pass through the attribute
200
201#define BOOST_SPIRIT_PASS_THROUGH_CONTAINER(z, N, _)                          \
202    pass_through_container<Container, ValueType,                              \
203        BOOST_PP_CAT(T, N), Sequence>::type::value ||                         \
204    /***/
205
206    // make sure unused variant parameters do not affect the outcome
207    template <typename Container, typename ValueType, typename Sequence>
208    struct pass_through_container<Container, ValueType
209          , boost::detail::variant::void_, Sequence>
210      : mpl::false_
211    {};
212
213    template <typename Container, typename ValueType, typename Sequence
214      , BOOST_VARIANT_ENUM_PARAMS(typename T)>
215    struct pass_through_container<Container, ValueType
216          , boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>, Sequence>
217      : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
218          , BOOST_SPIRIT_PASS_THROUGH_CONTAINER, _) false>
219    {};
220
221#undef BOOST_SPIRIT_PASS_THROUGH_CONTAINER
222}}}}
223
224///////////////////////////////////////////////////////////////////////////////
225namespace boost { namespace spirit { namespace traits
226{
227    ///////////////////////////////////////////////////////////////////////////
228    // forwarding customization point for domain qi::domain
229    template <typename Container, typename ValueType, typename Attribute
230      , typename Sequence>
231    struct pass_through_container<
232            Container, ValueType, Attribute, Sequence, qi::domain>
233      : qi::detail::pass_through_container<
234            Container, ValueType, Attribute, Sequence>
235    {};
236}}}
237
238namespace boost { namespace spirit { namespace qi { namespace detail
239{
240    ///////////////////////////////////////////////////////////////////////////
241    // This function handles the case where the attribute (Attr) given
242    // the sequence is an STL container. This is a wrapper around F.
243    // The function F does the actual parsing.
244    template <typename F, typename Attr, typename Sequence>
245    struct pass_container
246    {
247        typedef typename F::context_type context_type;
248        typedef typename F::iterator_type iterator_type;
249
250        pass_container(F const& f, Attr& attr)
251          : f(f), attr(attr) {}
252
253        // this is for the case when the current element exposes an attribute
254        // which is pushed back onto the container
255        template <typename Component>
256        bool dispatch_container(Component const& component, mpl::false_) const
257        {
258            // synthesized attribute needs to be default constructed
259            typename traits::container_value<Attr>::type val =
260                typename traits::container_value<Attr>::type();
261
262            iterator_type save = f.first;
263            bool r = f(component, val);
264            if (!r)
265            {
266                // push the parsed value into our attribute
267                r = !traits::push_back(attr, val);
268                if (r)
269                    f.first = save;
270            }
271            return r;
272        }
273
274        // this is for the case when the current element is able to handle an 
275        // attribute which is a container itself, this element will push its 
276        // data directly into the attribute container
277        template <typename Component>
278        bool dispatch_container(Component const& component, mpl::true_) const
279        {
280            return f(component, attr);
281        }
282
283        ///////////////////////////////////////////////////////////////////////
284        // this is for the case when the current element doesn't expect an 
285        // attribute
286        template <typename Component>
287        bool dispatch_attribute(Component const& component, mpl::false_) const
288        {
289            return f(component, unused);
290        }
291
292        // the current element expects an attribute
293        template <typename Component>
294        bool dispatch_attribute(Component const& component, mpl::true_) const
295        {
296            typedef typename traits::container_value<Attr>::type value_type;
297            typedef typename traits::attribute_of<
298                Component, context_type, iterator_type>::type
299            rhs_attribute;
300
301            // this predicate detects, whether the attribute of the current 
302            // element is a substitute for the value type of the container
303            // attribute 
304            typedef mpl::and_<
305                traits::handles_container<
306                    Component, Attr, context_type, iterator_type> 
307              , traits::pass_through_container<
308                    Attr, value_type, rhs_attribute, Sequence, qi::domain>
309            > predicate;
310
311            return dispatch_container(component, predicate());
312        }
313
314        // Dispatches to dispatch_main depending on the attribute type
315        // of the Component
316        template <typename Component>
317        bool operator()(Component const& component) const
318        {
319            // we need to dispatch depending on the type of the attribute
320            // of the current element (component). If this is has no attribute
321            // we shouldn't pass an attribute at all.
322            typedef typename traits::not_is_unused<
323                typename traits::attribute_of<
324                    Component, context_type, iterator_type
325                >::type
326            >::type predicate;
327
328            // ensure the attribute is actually a container type
329            traits::make_container(attr);
330
331            return dispatch_attribute(component, predicate());
332        }
333
334        F f;
335        Attr& attr;
336
337    private:
338        // silence MSVC warning C4512: assignment operator could not be generated
339        pass_container& operator= (pass_container const&);
340    };
341
342    ///////////////////////////////////////////////////////////////////////////
343    // Utility function to make a pass_container for container components 
344    // (kleene, list, plus, repeat)
345    template <typename F, typename Attr>
346    inline pass_container<F, Attr, mpl::false_>
347    make_pass_container(F const& f, Attr& attr)
348    {
349        return pass_container<F, Attr, mpl::false_>(f, attr);
350    }
351
352    // Utility function to make a pass_container for sequences
353    template <typename F, typename Attr>
354    inline pass_container<F, Attr, mpl::true_>
355    make_sequence_pass_container(F const& f, Attr& attr)
356    {
357        return pass_container<F, Attr, mpl::true_>(f, attr);
358    }
359}}}}
360
361#endif
362