Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3 http://spirit.sourceforge.net/
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #ifndef BOOST_SPIRIT_MODIFY_OCTOBER_25_2008_0142PM
|
Chris@16
|
9 #define BOOST_SPIRIT_MODIFY_OCTOBER_25_2008_0142PM
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER)
|
Chris@16
|
12 #pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/spirit/include/phoenix_limits.hpp> // needs to be included before proto
|
Chris@16
|
16 #include <boost/proto/proto.hpp>
|
Chris@16
|
17 #include <boost/mpl/if.hpp>
|
Chris@16
|
18 #include <boost/type_traits/is_base_of.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost { namespace spirit
|
Chris@16
|
22 {
|
Chris@16
|
23 template <typename Domain, typename T, typename Enable = void>
|
Chris@16
|
24 struct is_modifier_directive;
|
Chris@16
|
25
|
Chris@16
|
26 // Testing if a modifier set includes a modifier T involves
|
Chris@16
|
27 // checking for inheritance (i.e. Modifiers is derived from T)
|
Chris@16
|
28 template <typename Modifiers, typename T>
|
Chris@16
|
29 struct has_modifier
|
Chris@16
|
30 : is_base_of<T, Modifiers> {};
|
Chris@16
|
31
|
Chris@16
|
32 // Adding modifiers is done using multi-inheritance
|
Chris@16
|
33 template <typename Current, typename New, typename Enable = void>
|
Chris@16
|
34 struct compound_modifier : Current, New
|
Chris@16
|
35 {
|
Chris@16
|
36 compound_modifier()
|
Chris@16
|
37 : Current(), New() {}
|
Chris@16
|
38
|
Chris@16
|
39 compound_modifier(Current const& current, New const& new_)
|
Chris@16
|
40 : Current(current), New(new_) {}
|
Chris@16
|
41 };
|
Chris@16
|
42
|
Chris@16
|
43 // Don't add if New is already in Current
|
Chris@16
|
44 template <typename Current, typename New>
|
Chris@16
|
45 struct compound_modifier<
|
Chris@16
|
46 Current, New, typename enable_if<has_modifier<Current, New> >::type>
|
Chris@16
|
47 : Current
|
Chris@16
|
48 {
|
Chris@16
|
49 compound_modifier()
|
Chris@16
|
50 : Current() {}
|
Chris@16
|
51
|
Chris@16
|
52 compound_modifier(Current const& current, New const&)
|
Chris@16
|
53 : Current(current) {}
|
Chris@16
|
54 };
|
Chris@16
|
55
|
Chris@16
|
56 // Special case if Current is unused_type
|
Chris@16
|
57 template <typename New, typename Enable>
|
Chris@16
|
58 struct compound_modifier<unused_type, New, Enable> : New
|
Chris@16
|
59 {
|
Chris@16
|
60 compound_modifier()
|
Chris@16
|
61 : New() {}
|
Chris@16
|
62
|
Chris@16
|
63 compound_modifier(unused_type, New const& new_)
|
Chris@16
|
64 : New(new_) {}
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 // Domains may specialize this modify metafunction to allow
|
Chris@16
|
68 // directives to add information to the Modifier template
|
Chris@16
|
69 // parameter that is passed to the make_component metafunction.
|
Chris@16
|
70 // By default, we return the modifiers untouched
|
Chris@16
|
71 template <typename Domain, typename Enable = void>
|
Chris@16
|
72 struct modify
|
Chris@16
|
73 {
|
Chris@16
|
74 template <typename Sig>
|
Chris@16
|
75 struct result;
|
Chris@16
|
76
|
Chris@16
|
77 template <typename This, typename Tag, typename Modifiers>
|
Chris@16
|
78 struct result<This(Tag, Modifiers)>
|
Chris@16
|
79 {
|
Chris@16
|
80 typedef typename remove_const<
|
Chris@16
|
81 typename remove_reference<Tag>::type>::type
|
Chris@16
|
82 tag_type;
|
Chris@16
|
83 typedef typename remove_const<
|
Chris@16
|
84 typename remove_reference<Modifiers>::type>::type
|
Chris@16
|
85 modifiers_type;
|
Chris@16
|
86
|
Chris@16
|
87 typedef typename mpl::if_<
|
Chris@16
|
88 is_modifier_directive<Domain, tag_type>
|
Chris@16
|
89 , compound_modifier<modifiers_type, tag_type>
|
Chris@16
|
90 , Modifiers>::type
|
Chris@16
|
91 type;
|
Chris@16
|
92 };
|
Chris@16
|
93
|
Chris@16
|
94 template <typename Tag, typename Modifiers>
|
Chris@16
|
95 typename result<modify(Tag, Modifiers)>::type
|
Chris@16
|
96 operator()(Tag tag, Modifiers modifiers) const
|
Chris@16
|
97 {
|
Chris@16
|
98 return op(tag, modifiers, is_modifier_directive<Domain, Tag>());
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 template <typename Tag, typename Modifiers>
|
Chris@16
|
102 Modifiers
|
Chris@16
|
103 op(Tag /*tag*/, Modifiers modifiers, mpl::false_) const
|
Chris@16
|
104 {
|
Chris@16
|
105 return modifiers;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 template <typename Tag, typename Modifiers>
|
Chris@16
|
109 compound_modifier<Modifiers, Tag>
|
Chris@16
|
110 op(Tag tag, Modifiers modifiers, mpl::true_) const
|
Chris@16
|
111 {
|
Chris@16
|
112 return compound_modifier<Modifiers, Tag>(modifiers, tag);
|
Chris@16
|
113 }
|
Chris@16
|
114 };
|
Chris@16
|
115 }}
|
Chris@16
|
116
|
Chris@16
|
117 namespace boost { namespace proto
|
Chris@16
|
118 {
|
Chris@16
|
119 template <typename Domain, typename Enable>
|
Chris@16
|
120 struct is_callable<spirit::modify<Domain, Enable> >
|
Chris@16
|
121 : mpl::true_ {};
|
Chris@16
|
122 }}
|
Chris@16
|
123
|
Chris@16
|
124 #endif
|