1 ///////////////////////////////////////////////////////////////////////////////
2 // traits_utils.hpp
3 //
4 //  Copyright 2008 Eric Niebler. Distributed under the Boost
5 //  Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_TRAITS_UTILS_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_UTILITY_TRAITS_UTILS_HPP_EAN_10_04_2005
10 
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER)
13 # pragma once
14 # pragma warning(push)
15 # pragma warning(disable : 4100) // unreferenced formal parameter
16 #endif
17 
18 #include <string>
19 #include <boost/mpl/bool.hpp>
20 #include <boost/mpl/assert.hpp>
21 #include <boost/utility/enable_if.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/iterator/transform_iterator.hpp>
24 #include <boost/xpressive/detail/utility/algorithm.hpp>
25 
26 namespace boost { namespace xpressive { namespace detail
27 {
28 
29     ///////////////////////////////////////////////////////////////////////////////
30     // char_cast
31     //
32     template<typename ToChar, typename FromChar, typename Traits>
33     inline ToChar
char_cast(FromChar from,Traits const &,typename enable_if<is_same<ToChar,FromChar>>::type * =0)34     char_cast(FromChar from, Traits const &, typename enable_if<is_same<ToChar, FromChar> >::type * = 0)
35     {
36         return from;
37     }
38 
39     template<typename ToChar, typename FromChar, typename Traits>
40     inline ToChar
char_cast(FromChar from,Traits const & tr,typename disable_if<is_same<ToChar,FromChar>>::type * =0)41     char_cast(FromChar from, Traits const &tr, typename disable_if<is_same<ToChar, FromChar> >::type * = 0)
42     {
43         BOOST_MPL_ASSERT((is_same<FromChar, char>));
44         return tr.widen(from);
45     }
46 
47     ///////////////////////////////////////////////////////////////////////////////
48     // widen_fun
49     //
50     template<typename Traits>
51     struct widen_fun
52     {
53         typedef typename Traits::char_type result_type;
widen_funboost::xpressive::detail::widen_fun54         explicit widen_fun(Traits const &tr)
55           : traits_(tr)
56         {}
57 
operator ()boost::xpressive::detail::widen_fun58         result_type operator()(char ch) const
59         {
60             return this->traits_.widen(ch);
61         }
62 
63         Traits const &traits_;
64     };
65 
66     ///////////////////////////////////////////////////////////////////////////////
67     // string_cast_
68     //
69     template<
70         typename To
71       , typename From
72       , typename ToChar = typename detail::range_data<To>::type
73       , typename FromChar = typename detail::range_data<From>::type
74     >
75     struct string_cast_
76     {
77         BOOST_MPL_ASSERT((is_same<FromChar, char>));
78         typedef To const result_type;
79         template<typename Traits>
operator ()boost::xpressive::detail::string_cast_80         result_type operator()(From const &from, Traits const &tr) const
81         {
82             widen_fun<Traits> widen(tr);
83             To to(
84                 boost::make_transform_iterator(detail::data_begin(from), widen)
85               , boost::make_transform_iterator(detail::data_end(from), widen)
86             );
87             return to;
88         }
89     };
90 
91     template<typename To, typename From, typename Char>
92     struct string_cast_<To, From, Char, Char>
93     {
94         typedef To const result_type;
95         template<typename Traits>
operator ()boost::xpressive::detail::string_cast_96         result_type operator()(From const &from, Traits const &) const
97         {
98             To to(detail::data_begin(from), detail::data_end(from));
99             return to;
100         }
101     };
102 
103     template<typename From, typename Char>
104     struct string_cast_<From, From, Char, Char>
105     {
106         typedef From const &result_type;
107         template<typename Traits>
operator ()boost::xpressive::detail::string_cast_108         result_type operator()(From const &from, Traits const &) const
109         {
110             return from;
111         }
112     };
113 
114     ///////////////////////////////////////////////////////////////////////////////
115     // string_cast
116     //
117     template<typename To, typename From, typename Traits>
118     typename string_cast_<To, From>::result_type
string_cast(From const & from,Traits const & tr)119     string_cast(From const &from, Traits const &tr)
120     {
121         return string_cast_<To, From>()(from, tr);
122     }
123 
124     ///////////////////////////////////////////////////////////////////////////////
125     // translate
126     //
127     template<typename Char, typename Traits>
translate(Char ch,Traits const & tr,mpl::false_)128     inline Char translate(Char ch, Traits const &tr, mpl::false_) // case-sensitive
129     {
130         return tr.translate(ch);
131     }
132 
133     template<typename Char, typename Traits>
translate(Char ch,Traits const & tr,mpl::true_)134     inline Char translate(Char ch, Traits const &tr, mpl::true_) // case-insensitive
135     {
136         return tr.translate_nocase(ch);
137     }
138 
139 }}} // namespace boost::xpressive::detail
140 
141 #if defined(_MSC_VER)
142 # pragma warning(pop)
143 #endif
144 
145 #endif
146