liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//  Copyright (c) 2001-2011 Hartmut Kaiser
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
#if !defined(BOOST_SPIRIT_KARMA_EXTRACT_FROM_SEP_30_2009_0732AM)
#define BOOST_SPIRIT_KARMA_EXTRACT_FROM_SEP_30_2009_0732AM
 
#if defined(_MSC_VER)
#pragma once
#endif
 
#include <boost/spirit/home/support/unused.hpp>
#include <boost/spirit/home/support/attributes_fwd.hpp>
#include <boost/spirit/home/karma/detail/attributes.hpp>
#include <boost/spirit/home/support/container.hpp>
 
#include <boost/ref.hpp>
#include <boost/optional.hpp>
 
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace traits
{
    ///////////////////////////////////////////////////////////////////////////
    //  This file contains attribute extraction utilities. The utilities
    //  provided also accept spirit's unused_type; all no-ops. Compiler
    //  optimization will easily strip these away.
    ///////////////////////////////////////////////////////////////////////////
 
    namespace detail
    {
        ///////////////////////////////////////////////////////////////////////
        // extract first and second element of a fusion sequence
        template <typename T>
        struct add_const_ref
          : add_reference<typename add_const<T>::type>
        {};
 
        template <typename T, int N>
        struct value_at_c
          : add_const_ref<typename fusion::result_of::value_at_c<T, N>::type>
        {};
    }
 
    // This is the default case: the plain attribute values
    template <typename Attribute, typename Exposed
      , bool IsOneElemSeq = traits::one_element_sequence<Attribute>::value>
    struct extract_from_attribute_base
    {
        typedef Attribute const& type;
 
        template <typename Context>
        static type call(Attribute const& attr, Context&)
        {
            return attr;
        }
    };
 
    // This handles the case where the attribute is a single element fusion
    // sequence. We silently extract the only element and treat it as the
    // attribute to generate output from.
    template <typename Attribute, typename Exposed>
    struct extract_from_attribute_base<Attribute, Exposed, true>
    {
        typedef typename remove_const<
            typename remove_reference<
                typename fusion::result_of::at_c<Attribute, 0>::type
            >::type
        >::type elem_type;
 
        typedef typename result_of::extract_from<Exposed, elem_type>::type type;
 
        template <typename Context>
        static type call(Attribute const& attr, Context& ctx)
        {
            return extract_from<Exposed>(fusion::at_c<0>(attr), ctx);
        }
    };
 
    template <typename Attribute, typename Exposed, typename Enable/*= void*/>
    struct extract_from_attribute
      : extract_from_attribute_base<Attribute, Exposed>
    {};
 
    // This handles optional attributes.
    template <typename Attribute, typename Exposed>
    struct extract_from_attribute<boost::optional<Attribute>, Exposed>
    {
        typedef Attribute const& type;
 
        template <typename Context>
        static type call(boost::optional<Attribute> const& attr, Context& ctx)
        {
            return extract_from<Exposed>(boost::get<Attribute>(attr), ctx);
        }
    };
 
    template <typename Attribute, typename Exposed>
    struct extract_from_attribute<boost::optional<Attribute const>, Exposed>
    {
        typedef Attribute const& type;
 
        template <typename Context>
        static type call(boost::optional<Attribute const> const& attr, Context& ctx)
        {
            return extract_from<Exposed>(boost::get<Attribute const>(attr), ctx);
        }
    };
 
    // This handles attributes wrapped inside a boost::ref().
    template <typename Attribute, typename Exposed>
    struct extract_from_attribute<reference_wrapper<Attribute>, Exposed>
    {
        typedef Attribute const& type;
 
        template <typename Context>
        static type call(reference_wrapper<Attribute> const& attr, Context& ctx)
        {
            return extract_from<Exposed>(attr.get(), ctx);
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    template <typename Attribute, typename Exposed, typename Enable>
    struct extract_from_container
    {
        typedef typename traits::container_value<Attribute const>::type
            value_type;
        typedef typename is_convertible<value_type, Exposed>::type
            is_convertible_to_value_type;
 
        typedef typename mpl::if_<
           mpl::or_<
                is_same<value_type, Exposed>, is_same<Attribute, Exposed> >
          , Exposed const&, Exposed
        >::type type;
 
        // handle case where container value type is convertible to result type
        // we simply return the front element of the container
        template <typename Context, typename Pred>
        static type call(Attribute const& attr, Context&, mpl::true_, Pred)
        {
            // return first element from container
            typedef typename traits::container_iterator<Attribute const>::type
                iterator_type;
 
            iterator_type it = traits::begin(attr);
            type result = *it;
            ++it;
            return result;
        }
 
        // handle strings
        template <typename Iterator>
        static void append_to_string(Exposed& result, Iterator begin, Iterator end)
        {
            for (Iterator i = begin; i != end; ++i)
                push_back(result, *i);
        }
 
        template <typename Context>
        static type call(Attribute const& attr, Context&, mpl::false_, mpl::true_)
        {
            typedef typename char_type_of<Attribute>::type char_type;
 
            Exposed result;
            append_to_string(result, traits::get_begin<char_type>(attr)
              , traits::get_end<char_type>(attr));
            return result;
        }
 
        // everything else gets just passed through
        template <typename Context>
        static type call(Attribute const& attr, Context&, mpl::false_, mpl::false_)
        {
            return type(attr);
        }
 
        template <typename Context>
        static type call(Attribute const& attr, Context& ctx)
        {
            typedef typename mpl::and_<
                traits::is_string<Exposed>, traits::is_string<Attribute>
            >::type handle_strings;
 
            // return first element from container
            return call(attr, ctx, is_convertible_to_value_type()
              , handle_strings());
        }
    };
 
    template <typename Attribute>
    struct extract_from_container<Attribute, Attribute>
    {
        typedef Attribute const& type;
 
        template <typename Context>
        static type call(Attribute const& attr, Context&)
        {
            return attr;
        }
    };
 
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        // overload for non-container attributes
        template <typename Exposed, typename Attribute, typename Context>
        inline typename spirit::result_of::extract_from<Exposed, Attribute>::type
        extract_from(Attribute const& attr, Context& ctx, mpl::false_)
        {
            return extract_from_attribute<Attribute, Exposed>::call(attr, ctx);
        }
 
        // overload for containers (but not for variants or optionals
        // holding containers)
        template <typename Exposed, typename Attribute, typename Context>
        inline typename spirit::result_of::extract_from<Exposed, Attribute>::type
        extract_from(Attribute const& attr, Context& ctx, mpl::true_)
        {
            return extract_from_container<Attribute, Exposed>::call(attr, ctx);
        }
    }
 
    template <typename Exposed, typename Attribute, typename Context>
    inline typename spirit::result_of::extract_from<Exposed, Attribute>::type
    extract_from(Attribute const& attr, Context& ctx
#if (defined(__GNUC__) && (__GNUC__ < 4)) || \
    (defined(__APPLE__) && defined(__INTEL_COMPILER))
      , typename enable_if<traits::not_is_unused<Attribute> >::type*
#endif
    )
    {
        typedef typename mpl::and_<
            traits::is_container<Attribute>
          , traits::not_is_variant<Attribute>
          , traits::not_is_optional<Attribute>
        >::type is_not_wrapped_container;
 
        return detail::extract_from<Exposed>(attr, ctx
          , is_not_wrapped_container());
    }
 
    template <typename Exposed, typename Context>
    inline unused_type extract_from(unused_type, Context&)
    {
        return unused;
    }
}}}
 
///////////////////////////////////////////////////////////////////////////////
namespace boost { namespace spirit { namespace result_of
{
    template <typename Exposed, typename Attribute>
    struct extract_from
      : mpl::if_<
            mpl::and_<
                traits::is_container<Attribute>
              , traits::not_is_variant<Attribute>
              , traits::not_is_optional<Attribute> >
          , traits::extract_from_container<Attribute, Exposed>
          , traits::extract_from_attribute<Attribute, Exposed> >::type
    {};
 
    template <typename Exposed>
    struct extract_from<Exposed, unused_type>
    {
        typedef unused_type type;
    };
 
    template <typename Exposed>
    struct extract_from<Exposed, unused_type const>
    {
        typedef unused_type type;
    };
}}}
 
#endif