]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Hartmut Kaiser | |
3 | Copyright (c) 2001-2014 Joel de Guzman | |
4 | Copyright (c) 2013 Agustin Berge | |
5 | ||
6 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
7 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
8 | ==============================================================================*/ | |
9 | #ifndef BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM | |
10 | #define BOOST_SPIRIT_X3_ATTR_JUL_23_2008_0956AM | |
11 | ||
12 | #include <boost/spirit/home/x3/core/parser.hpp> | |
13 | #include <boost/spirit/home/x3/support/unused.hpp> | |
14 | #include <boost/spirit/home/x3/support/traits/container_traits.hpp> | |
15 | #include <boost/spirit/home/x3/support/traits/move_to.hpp> | |
16 | #include <boost/type_traits/is_same.hpp> | |
17 | #include <boost/type_traits/remove_cv.hpp> | |
18 | #include <boost/type_traits/remove_reference.hpp> | |
19 | #include <algorithm> | |
20 | #include <cstddef> | |
21 | #include <string> | |
22 | #include <utility> | |
23 | ||
24 | namespace boost { namespace spirit { namespace x3 | |
25 | { | |
26 | template <typename Value> | |
27 | struct attr_parser : parser<attr_parser<Value>> | |
28 | { | |
29 | typedef Value attribute_type; | |
30 | ||
31 | static bool const has_attribute = | |
32 | !is_same<unused_type, attribute_type>::value; | |
33 | static bool const handles_container = | |
34 | traits::is_container<attribute_type>::value; | |
35 | ||
36 | attr_parser(Value const& value) | |
37 | : value_(value) {} | |
38 | attr_parser(Value&& value) | |
39 | : value_(std::move(value)) {} | |
40 | ||
41 | template <typename Iterator, typename Context | |
42 | , typename RuleContext, typename Attribute> | |
43 | bool parse(Iterator& first, Iterator const& last | |
44 | , Context const& context, RuleContext&, Attribute& attr_) const | |
45 | { | |
46 | // $$$ Change to copy_to once we have it $$$ | |
47 | traits::move_to(value_, attr_); | |
48 | return true; | |
49 | } | |
50 | ||
51 | Value value_; | |
52 | ||
53 | private: | |
54 | // silence MSVC warning C4512: assignment operator could not be generated | |
55 | attr_parser& operator= (attr_parser const&); | |
56 | }; | |
57 | ||
58 | template <typename Value, std::size_t N> | |
59 | struct attr_parser<Value[N]> : parser<attr_parser<Value[N]>> | |
60 | { | |
61 | typedef Value attribute_type[N]; | |
62 | ||
63 | static bool const has_attribute = | |
64 | !is_same<unused_type, attribute_type>::value; | |
65 | static bool const handles_container = true; | |
66 | ||
67 | attr_parser(Value const (&value)[N]) | |
68 | { | |
69 | std::copy(value + 0, value + N, value_ + 0); | |
70 | } | |
71 | ||
72 | attr_parser(Value (&&value)[N]) | |
73 | { | |
74 | std::move(value + 0, value + N, value_ + 0); | |
75 | } | |
76 | ||
77 | template <typename Iterator, typename Context | |
78 | , typename RuleContext, typename Attribute> | |
79 | bool parse(Iterator& first, Iterator const& last | |
80 | , Context const& context, RuleContext&, Attribute& attr_) const | |
81 | { | |
82 | // $$$ Change to copy_to once we have it $$$ | |
83 | traits::move_to(value_ + 0, value_ + N, attr_); | |
84 | return true; | |
85 | } | |
86 | ||
87 | Value value_[N]; | |
88 | ||
89 | private: | |
90 | // silence MSVC warning C4512: assignment operator could not be generated | |
91 | attr_parser& operator= (attr_parser const&); | |
92 | }; | |
93 | ||
94 | template <typename Value> | |
95 | struct get_info<attr_parser<Value>> | |
96 | { | |
97 | typedef std::string result_type; | |
98 | std::string operator()(attr_parser<Value> const& /*p*/) const | |
99 | { | |
100 | return "attr"; | |
101 | } | |
102 | }; | |
103 | ||
104 | struct attr_gen | |
105 | { | |
106 | template <typename Value> | |
107 | attr_parser<typename remove_cv< | |
108 | typename remove_reference<Value>::type>::type> | |
109 | operator()(Value&& value) const | |
110 | { | |
111 | return { std::forward<Value>(value) }; | |
112 | } | |
113 | ||
114 | template <typename Value, std::size_t N> | |
115 | attr_parser<typename remove_cv<Value>::type[N]> | |
116 | operator()(Value (&value)[N]) const | |
117 | { | |
118 | return { value }; | |
119 | } | |
120 | template <typename Value, std::size_t N> | |
121 | attr_parser<typename remove_cv<Value>::type[N]> | |
122 | operator()(Value (&&value)[N]) const | |
123 | { | |
124 | return { value }; | |
125 | } | |
126 | }; | |
127 | ||
128 | auto const attr = attr_gen{}; | |
129 | }}} | |
130 | ||
131 | #endif |