assign_to.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_ASSIGN_TO_APR_16_2006_0812PM)
  9. #define BOOST_SPIRIT_ASSIGN_TO_APR_16_2006_0812PM
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/spirit/home/qi/detail/construct.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. #include <boost/spirit/home/qi/detail/attributes.hpp>
  16. #include <boost/spirit/home/support/container.hpp>
  17. #include <boost/fusion/include/copy.hpp>
  18. #include <boost/ref.hpp>
  19. #include <boost/range/iterator_range.hpp>
  20. namespace boost { namespace spirit { namespace traits
  21. {
  22. ///////////////////////////////////////////////////////////////////////////
  23. // This file contains assignment utilities. The utilities provided also
  24. // accept spirit's unused_type; all no-ops. Compiler optimization will
  25. // easily strip these away.
  26. ///////////////////////////////////////////////////////////////////////////
  27. namespace detail
  28. {
  29. template <typename T>
  30. struct is_iter_range : mpl::false_ {};
  31. template <typename I>
  32. struct is_iter_range<boost::iterator_range<I> > : mpl::true_ {};
  33. template <typename C>
  34. struct is_container_of_ranges
  35. : is_iter_range<typename C::value_type> {};
  36. }
  37. template <typename Attribute, typename Iterator, typename Enable>
  38. struct assign_to_attribute_from_iterators
  39. {
  40. // Common case
  41. static void
  42. call(Iterator const& first, Iterator const& last, Attribute& attr, mpl::false_)
  43. {
  44. if (traits::is_empty(attr))
  45. attr = Attribute(first, last);
  46. else {
  47. for (Iterator i = first; i != last; ++i)
  48. push_back(attr, *i);
  49. }
  50. }
  51. // If Attribute is a container with value_type==iterator_range<T> just push the
  52. // iterator_range into it
  53. static void
  54. call(Iterator const& first, Iterator const& last, Attribute& attr, mpl::true_)
  55. {
  56. typename Attribute::value_type rng(first, last);
  57. push_back(attr, rng);
  58. }
  59. static void
  60. call(Iterator const& first, Iterator const& last, Attribute& attr)
  61. {
  62. call(first, last, attr, detail::is_container_of_ranges<Attribute>());
  63. }
  64. };
  65. template <typename Attribute, typename Iterator>
  66. struct assign_to_attribute_from_iterators<
  67. reference_wrapper<Attribute>, Iterator>
  68. {
  69. static void
  70. call(Iterator const& first, Iterator const& last
  71. , reference_wrapper<Attribute> attr)
  72. {
  73. if (traits::is_empty(attr))
  74. attr = Attribute(first, last);
  75. else {
  76. for (Iterator i = first; i != last; ++i)
  77. push_back(attr, *i);
  78. }
  79. }
  80. };
  81. template <typename Attribute, typename Iterator>
  82. struct assign_to_attribute_from_iterators<
  83. boost::optional<Attribute>, Iterator>
  84. {
  85. static void
  86. call(Iterator const& first, Iterator const& last
  87. , boost::optional<Attribute>& attr)
  88. {
  89. Attribute val;
  90. assign_to(first, last, val);
  91. attr = val;
  92. }
  93. };
  94. template <typename Iterator>
  95. struct assign_to_attribute_from_iterators<
  96. iterator_range<Iterator>, Iterator>
  97. {
  98. static void
  99. call(Iterator const& first, Iterator const& last
  100. , iterator_range<Iterator>& attr)
  101. {
  102. attr = iterator_range<Iterator>(first, last);
  103. }
  104. };
  105. template <typename Iterator, typename Attribute>
  106. inline void
  107. assign_to(Iterator const& first, Iterator const& last, Attribute& attr)
  108. {
  109. assign_to_attribute_from_iterators<Attribute, Iterator>::
  110. call(first, last, attr);
  111. }
  112. template <typename Iterator>
  113. inline void
  114. assign_to(Iterator const&, Iterator const&, unused_type)
  115. {
  116. }
  117. ///////////////////////////////////////////////////////////////////////////
  118. template <typename T, typename Attribute>
  119. void assign_to(T const& val, Attribute& attr);
  120. template <typename Attribute, typename T, typename Enable>
  121. struct assign_to_attribute_from_value
  122. {
  123. typedef typename traits::one_element_sequence<Attribute>::type
  124. is_one_element_sequence;
  125. typedef typename mpl::eval_if<
  126. is_one_element_sequence
  127. , fusion::result_of::at_c<Attribute, 0>
  128. , mpl::identity<Attribute&>
  129. >::type type;
  130. template <typename T_>
  131. static void
  132. call(T_ const& val, Attribute& attr, mpl::false_)
  133. {
  134. attr = static_cast<Attribute>(val);
  135. }
  136. // This handles the case where the attribute is a single element fusion
  137. // sequence. We silently assign to the only element and treat it as the
  138. // attribute to parse the results into.
  139. template <typename T_>
  140. static void
  141. call(T_ const& val, Attribute& attr, mpl::true_)
  142. {
  143. typedef typename fusion::result_of::value_at_c<Attribute, 0>::type
  144. element_type;
  145. fusion::at_c<0>(attr) = static_cast<element_type>(val);
  146. }
  147. static void
  148. call(T const& val, Attribute& attr)
  149. {
  150. call(val, attr, is_one_element_sequence());
  151. }
  152. };
  153. template <typename Attribute>
  154. struct assign_to_attribute_from_value<Attribute, Attribute>
  155. {
  156. static void
  157. call(Attribute const& val, Attribute& attr)
  158. {
  159. attr = val;
  160. }
  161. };
  162. template <typename Attribute, typename T>
  163. struct assign_to_attribute_from_value<Attribute, reference_wrapper<T>
  164. , typename disable_if<is_same<Attribute, reference_wrapper<T> > >::type>
  165. {
  166. static void
  167. call(reference_wrapper<T> const& val, Attribute& attr)
  168. {
  169. assign_to(val.get(), attr);
  170. }
  171. };
  172. template <typename Attribute, typename T>
  173. struct assign_to_attribute_from_value<Attribute, boost::optional<T>
  174. , typename disable_if<is_same<Attribute, boost::optional<T> > >::type>
  175. {
  176. static void
  177. call(boost::optional<T> const& val, Attribute& attr)
  178. {
  179. assign_to(val.get(), attr);
  180. }
  181. };
  182. namespace detail
  183. {
  184. template <typename A, typename B>
  185. struct is_same_size_sequence
  186. : mpl::bool_<fusion::result_of::size<A>::value
  187. == fusion::result_of::size<B>::value>
  188. {};
  189. }
  190. template <typename Attribute, typename T>
  191. struct assign_to_attribute_from_value<Attribute, T,
  192. mpl::and_<
  193. fusion::traits::is_sequence<Attribute>,
  194. fusion::traits::is_sequence<T>,
  195. detail::is_same_size_sequence<Attribute, T>
  196. >
  197. >
  198. {
  199. static void
  200. call(T const& val, Attribute& attr)
  201. {
  202. fusion::copy(val, attr);
  203. }
  204. };
  205. ///////////////////////////////////////////////////////////////////////////
  206. template <typename Attribute, typename T, typename Enable>
  207. struct assign_to_container_from_value
  208. {
  209. // T is not a container and not a string
  210. template <typename T_>
  211. static void call(T_ const& val, Attribute& attr, mpl::false_, mpl::false_)
  212. {
  213. traits::push_back(attr, val);
  214. }
  215. // T is a container (but not a string), and T is convertible to the
  216. // value_type of the Attribute container
  217. template <typename T_>
  218. static void
  219. append_to_container_not_string(T_ const& val, Attribute& attr, mpl::true_)
  220. {
  221. traits::push_back(attr, val);
  222. }
  223. // T is a container (but not a string), generic overload
  224. template <typename T_>
  225. static void
  226. append_to_container_not_string(T_ const& val, Attribute& attr, mpl::false_)
  227. {
  228. typedef typename traits::container_iterator<T_ const>::type
  229. iterator_type;
  230. iterator_type end = traits::end(val);
  231. for (iterator_type i = traits::begin(val); i != end; traits::next(i))
  232. traits::push_back(attr, traits::deref(i));
  233. }
  234. // T is a container (but not a string)
  235. template <typename T_>
  236. static void call(T_ const& val, Attribute& attr, mpl::true_, mpl::false_)
  237. {
  238. typedef typename container_value<Attribute>::type value_type;
  239. typedef typename is_convertible<T, value_type>::type is_value_type;
  240. append_to_container_not_string(val, attr, is_value_type());
  241. }
  242. ///////////////////////////////////////////////////////////////////////
  243. // T is a string
  244. template <typename Iterator>
  245. static void append_to_string(Attribute& attr, Iterator begin, Iterator end)
  246. {
  247. for (Iterator i = begin; i != end; ++i)
  248. traits::push_back(attr, *i);
  249. }
  250. // T is string, but not convertible to value_type of container
  251. template <typename T_>
  252. static void append_to_container(T_ const& val, Attribute& attr, mpl::false_)
  253. {
  254. typedef typename char_type_of<T_>::type char_type;
  255. append_to_string(attr, traits::get_begin<char_type>(val)
  256. , traits::get_end<char_type>(val));
  257. }
  258. // T is string, and convertible to value_type of container
  259. template <typename T_>
  260. static void append_to_container(T_ const& val, Attribute& attr, mpl::true_)
  261. {
  262. traits::push_back(attr, val);
  263. }
  264. template <typename T_, typename Pred>
  265. static void call(T_ const& val, Attribute& attr, Pred, mpl::true_)
  266. {
  267. typedef typename container_value<Attribute>::type value_type;
  268. typedef typename is_convertible<T, value_type>::type is_value_type;
  269. append_to_container(val, attr, is_value_type());
  270. }
  271. ///////////////////////////////////////////////////////////////////////
  272. static void call(T const& val, Attribute& attr)
  273. {
  274. typedef typename traits::is_container<T>::type is_container;
  275. typedef typename traits::is_string<T>::type is_string;
  276. call(val, attr, is_container(), is_string());
  277. }
  278. };
  279. template <typename Attribute>
  280. struct assign_to_container_from_value<Attribute, Attribute>
  281. {
  282. static void
  283. call(Attribute const& val, Attribute& attr)
  284. {
  285. attr = val;
  286. }
  287. };
  288. template <typename Attribute, typename T>
  289. struct assign_to_container_from_value<Attribute, boost::optional<T>
  290. , typename disable_if<is_same<Attribute, boost::optional<T> > >::type>
  291. {
  292. static void
  293. call(boost::optional<T> const& val, Attribute& attr)
  294. {
  295. assign_to(val.get(), attr);
  296. }
  297. };
  298. template <typename Attribute, typename T>
  299. struct assign_to_container_from_value<Attribute, reference_wrapper<T>
  300. , typename disable_if<is_same<Attribute, reference_wrapper<T> > >::type>
  301. {
  302. static void
  303. call(reference_wrapper<T> const& val, Attribute& attr)
  304. {
  305. assign_to(val.get(), attr);
  306. }
  307. };
  308. ///////////////////////////////////////////////////////////////////////////
  309. namespace detail
  310. {
  311. // overload for non-container attributes
  312. template <typename T, typename Attribute>
  313. inline void
  314. assign_to(T const& val, Attribute& attr, mpl::false_)
  315. {
  316. assign_to_attribute_from_value<Attribute, T>::call(val, attr);
  317. }
  318. // overload for containers (but not for variants or optionals
  319. // holding containers)
  320. template <typename T, typename Attribute>
  321. inline void
  322. assign_to(T const& val, Attribute& attr, mpl::true_)
  323. {
  324. assign_to_container_from_value<Attribute, T>::call(val, attr);
  325. }
  326. }
  327. template <typename T, typename Attribute>
  328. inline void
  329. assign_to(T const& val, Attribute& attr)
  330. {
  331. typedef typename mpl::and_<
  332. traits::is_container<Attribute>
  333. , traits::not_is_variant<Attribute>
  334. , traits::not_is_optional<Attribute>
  335. >::type is_not_wrapped_container;
  336. detail::assign_to(val, attr, is_not_wrapped_container());
  337. }
  338. template <typename T>
  339. inline void
  340. assign_to(T const&, unused_type)
  341. {
  342. }
  343. }}}
  344. #endif