1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //  Copyright (c) 2001-2011 Joel de Guzman
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
5 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(BOOST_SPIRIT_GET_ENCODING_JANUARY_13_2009_1255PM)
8 #define BOOST_SPIRIT_GET_ENCODING_JANUARY_13_2009_1255PM
9 
10 #if defined(_MSC_VER)
11 #pragma once
12 #endif
13 
14 #include <boost/mpl/identity.hpp>
15 #include <boost/type_traits/is_same.hpp>
16 
17 namespace boost { namespace spirit { namespace detail
18 {
19     template <typename Modifiers, typename Encoding>
20     struct get_implicit_encoding
21     {
22         // Extract the implicit encoding from the Modifiers
23         // If one is not found, Encoding is used. The explicit
24         // encoding is the first viable encoding that can be
25         // extracted from the Modifiers (there can be more than one).
26 
27         typedef typename
28             mpl::find_if<
29                 char_encodings,
30                 has_modifier<Modifiers, tag::char_encoding_base<mpl::_1> >
31             >::type
32         iter;
33 
34         typedef typename
35             mpl::eval_if<
36                 is_same<iter, typename mpl::end<char_encodings>::type>,
37                 mpl::identity<Encoding>,
38                 mpl::deref<iter>
39             >::type
40         type;
41     };
42 
43     template <typename Modifiers, typename Encoding>
44     struct get_encoding
45     {
46         // Extract the explicit encoding from the Modifiers
47         // If one is not found, get implicit encoding (see above).
48         // Explicit encoding is the encoding explicitly declared
49         // using the encoding[c] directive.
50 
51         typedef typename
52             mpl::find_if<
53                 char_encodings,
54                 has_modifier<Modifiers, tag::char_code<tag::encoding, mpl::_1> >
55             >::type
56         iter;
57 
58         typedef typename
59             mpl::eval_if<
60                 is_same<iter, typename mpl::end<char_encodings>::type>,
61                 get_implicit_encoding<Modifiers, Encoding>,
62                 mpl::deref<iter>
63             >::type
64         type;
65     };
66 
67     template <typename Modifiers, typename Encoding, bool case_modifier = false>
68     struct get_encoding_with_case : mpl::identity<Encoding> {};
69 
70     template <typename Modifiers, typename Encoding>
71     struct get_encoding_with_case<Modifiers, Encoding, true>
72         : get_encoding<Modifiers, Encoding> {};
73 }}}
74 
75 #endif
76