container.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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_CONTAINER_FEBRUARY_06_2007_1001AM)
  9. #define BOOST_SPIRIT_CONTAINER_FEBRUARY_06_2007_1001AM
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/spirit/home/support/unused.hpp>
  14. #include <boost/spirit/home/support/attributes_fwd.hpp>
  15. #include <boost/mpl/has_xxx.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/optional.hpp>
  18. #include <boost/variant.hpp>
  19. #include <boost/preprocessor/cat.hpp>
  20. #include <boost/preprocessor/repeat.hpp>
  21. #include <boost/range/iterator_range.hpp>
  22. #include <iterator> // for std::iterator_traits
  23. namespace boost { namespace spirit { namespace traits
  24. {
  25. ///////////////////////////////////////////////////////////////////////////
  26. // This file contains some container utils for stl containers. The
  27. // utilities provided also accept spirit's unused_type; all no-ops.
  28. // Compiler optimization will easily strip these away.
  29. ///////////////////////////////////////////////////////////////////////////
  30. namespace detail
  31. {
  32. BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type)
  33. BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator)
  34. BOOST_MPL_HAS_XXX_TRAIT_DEF(size_type)
  35. BOOST_MPL_HAS_XXX_TRAIT_DEF(reference)
  36. }
  37. template <typename T, typename Enable/* = void*/>
  38. struct is_container
  39. : mpl::bool_<
  40. detail::has_value_type<T>::value &&
  41. detail::has_iterator<T>::value &&
  42. detail::has_size_type<T>::value &&
  43. detail::has_reference<T>::value>
  44. {};
  45. template <typename T>
  46. struct is_container<T&>
  47. : is_container<T>
  48. {};
  49. template <typename T>
  50. struct is_container<boost::optional<T> >
  51. : is_container<T>
  52. {};
  53. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  54. template<typename T>
  55. struct is_container<boost::variant<T> >
  56. : is_container<T>
  57. {};
  58. template<typename T0, typename T1, typename ...TN>
  59. struct is_container<boost::variant<T0, T1, TN...> >
  60. : mpl::bool_<is_container<T0>::value ||
  61. is_container<boost::variant<T1, TN...> >::value>
  62. {};
  63. #else
  64. #define BOOST_SPIRIT_IS_CONTAINER(z, N, data) \
  65. is_container<BOOST_PP_CAT(T, N)>::value || \
  66. /***/
  67. // make sure unused variant parameters do not affect the outcome
  68. template <>
  69. struct is_container<boost::detail::variant::void_>
  70. : mpl::false_
  71. {};
  72. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  73. struct is_container<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  74. : mpl::bool_<BOOST_PP_REPEAT(BOOST_VARIANT_LIMIT_TYPES
  75. , BOOST_SPIRIT_IS_CONTAINER, _) false>
  76. {};
  77. #undef BOOST_SPIRIT_IS_CONTAINER
  78. #endif
  79. template <typename T, typename Enable/* = void*/>
  80. struct is_iterator_range
  81. : mpl::false_
  82. {};
  83. template <typename T>
  84. struct is_iterator_range<iterator_range<T> >
  85. : mpl::true_
  86. {};
  87. ///////////////////////////////////////////////////////////////////////////
  88. namespace detail
  89. {
  90. template <typename T>
  91. struct remove_value_const
  92. {
  93. typedef T type;
  94. };
  95. template <typename T>
  96. struct remove_value_const<T const>
  97. : remove_value_const<T>
  98. {};
  99. template <typename F, typename S>
  100. struct remove_value_const<std::pair<F, S> >
  101. {
  102. typedef typename remove_value_const<F>::type first_type;
  103. typedef typename remove_value_const<S>::type second_type;
  104. typedef std::pair<first_type, second_type> type;
  105. };
  106. }
  107. ///////////////////////////////////////////////////////////////////////
  108. //[customization_container_value_default
  109. template <typename Container, typename Enable/* = void*/>
  110. struct container_value
  111. : detail::remove_value_const<typename Container::value_type>
  112. {};
  113. //]
  114. template <typename T>
  115. struct container_value<T&>
  116. : container_value<T>
  117. {};
  118. // this will be instantiated if the optional holds a container
  119. template <typename T>
  120. struct container_value<boost::optional<T> >
  121. : container_value<T>
  122. {};
  123. // this will be instantiated if the variant holds a container
  124. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  125. struct container_value<variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  126. {
  127. typedef typename
  128. variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types
  129. types;
  130. typedef typename
  131. mpl::find_if<types, is_container<mpl::_1> >::type
  132. iter;
  133. typedef typename container_value<
  134. typename mpl::if_<
  135. is_same<iter, typename mpl::end<types>::type>
  136. , unused_type, typename mpl::deref<iter>::type
  137. >::type
  138. >::type type;
  139. };
  140. //[customization_container_value_unused
  141. template <>
  142. struct container_value<unused_type>
  143. {
  144. typedef unused_type type;
  145. };
  146. //]
  147. template <>
  148. struct container_value<unused_type const>
  149. {
  150. typedef unused_type type;
  151. };
  152. ///////////////////////////////////////////////////////////////////////////
  153. template <typename Container, typename Enable/* = void*/>
  154. struct container_iterator
  155. {
  156. typedef typename Container::iterator type;
  157. };
  158. template <typename Container>
  159. struct container_iterator<Container&>
  160. : container_iterator<Container>
  161. {};
  162. template <typename Container>
  163. struct container_iterator<Container const>
  164. {
  165. typedef typename Container::const_iterator type;
  166. };
  167. template <typename T>
  168. struct container_iterator<optional<T> >
  169. : container_iterator<T>
  170. {};
  171. template <typename T>
  172. struct container_iterator<optional<T> const>
  173. : container_iterator<T const>
  174. {};
  175. template <typename Iterator>
  176. struct container_iterator<iterator_range<Iterator> >
  177. {
  178. typedef typename range_const_iterator<
  179. iterator_range<Iterator> >::type type;
  180. };
  181. template <>
  182. struct container_iterator<unused_type>
  183. {
  184. typedef unused_type const* type;
  185. };
  186. template <>
  187. struct container_iterator<unused_type const>
  188. {
  189. typedef unused_type const* type;
  190. };
  191. ///////////////////////////////////////////////////////////////////////////
  192. template <typename T, typename Enable/* = void*/>
  193. struct optional_attribute
  194. {
  195. typedef T const& type;
  196. static type call(T const& val)
  197. {
  198. return val;
  199. }
  200. static bool is_valid(T const&)
  201. {
  202. return true;
  203. }
  204. };
  205. template <typename T>
  206. struct optional_attribute<boost::optional<T> >
  207. {
  208. typedef T const& type;
  209. static type call(boost::optional<T> const& val)
  210. {
  211. return boost::get<T>(val);
  212. }
  213. static bool is_valid(boost::optional<T> const& val)
  214. {
  215. return !!val;
  216. }
  217. };
  218. template <typename T>
  219. typename optional_attribute<T>::type
  220. optional_value(T const& val)
  221. {
  222. return optional_attribute<T>::call(val);
  223. }
  224. inline unused_type optional_value(unused_type)
  225. {
  226. return unused;
  227. }
  228. template <typename T>
  229. bool has_optional_value(T const& val)
  230. {
  231. return optional_attribute<T>::is_valid(val);
  232. }
  233. inline bool has_optional_value(unused_type)
  234. {
  235. return true;
  236. }
  237. ///////////////////////////////////////////////////////////////////////////
  238. template <typename Container, typename T>
  239. bool push_back(Container& c, T const& val);
  240. //[customization_push_back_default
  241. template <typename Container, typename T, typename Enable/* = void*/>
  242. struct push_back_container
  243. {
  244. static bool call(Container& c, T const& val)
  245. {
  246. c.insert(c.end(), val);
  247. return true;
  248. }
  249. };
  250. //]
  251. template <typename Container, typename T>
  252. struct push_back_container<optional<Container>, T>
  253. {
  254. static bool call(boost::optional<Container>& c, T const& val)
  255. {
  256. if (!c)
  257. c = Container();
  258. return push_back(boost::get<Container>(c), val);
  259. }
  260. };
  261. namespace detail
  262. {
  263. template <typename T>
  264. struct push_back_visitor : public static_visitor<>
  265. {
  266. typedef bool result_type;
  267. push_back_visitor(T const& t) : t_(t) {}
  268. template <typename Container>
  269. bool push_back_impl(Container& c, mpl::true_) const
  270. {
  271. return push_back(c, t_);
  272. }
  273. template <typename T_>
  274. bool push_back_impl(T_&, mpl::false_) const
  275. {
  276. // this variant doesn't hold a container
  277. BOOST_ASSERT(false && "This variant doesn't hold a container");
  278. return false;
  279. }
  280. template <typename T_>
  281. bool operator()(T_& c) const
  282. {
  283. return push_back_impl(c, typename is_container<T_>::type());
  284. }
  285. T const& t_;
  286. };
  287. }
  288. template <BOOST_VARIANT_ENUM_PARAMS(typename T_), typename T>
  289. struct push_back_container<variant<BOOST_VARIANT_ENUM_PARAMS(T_)>, T>
  290. {
  291. static bool call(variant<BOOST_VARIANT_ENUM_PARAMS(T_)>& c, T const& val)
  292. {
  293. return apply_visitor(detail::push_back_visitor<T>(val), c);
  294. }
  295. };
  296. template <typename Container, typename T>
  297. bool push_back(Container& c, T const& val)
  298. {
  299. return push_back_container<Container, T>::call(c, val);
  300. }
  301. //[customization_push_back_unused
  302. template <typename Container>
  303. bool push_back(Container&, unused_type)
  304. {
  305. return true;
  306. }
  307. //]
  308. template <typename T>
  309. bool push_back(unused_type, T const&)
  310. {
  311. return true;
  312. }
  313. inline bool push_back(unused_type, unused_type)
  314. {
  315. return true;
  316. }
  317. ///////////////////////////////////////////////////////////////////////////
  318. template <typename Container, typename Enable/* = void*/>
  319. struct is_empty_container
  320. {
  321. static bool call(Container const& c)
  322. {
  323. return c.empty();
  324. }
  325. };
  326. template <typename Container>
  327. bool is_empty(Container const& c)
  328. {
  329. return is_empty_container<Container>::call(c);
  330. }
  331. inline bool is_empty(unused_type)
  332. {
  333. return true;
  334. }
  335. ///////////////////////////////////////////////////////////////////////////
  336. // Ensure the attribute is actually a container type
  337. template <typename Container, typename Enable/* = void*/>
  338. struct make_container_attribute
  339. {
  340. static void call(Container&)
  341. {
  342. // for static types this function does nothing
  343. }
  344. };
  345. template <typename T>
  346. void make_container(T& t)
  347. {
  348. make_container_attribute<T>::call(t);
  349. }
  350. inline void make_container(unused_type)
  351. {
  352. }
  353. ///////////////////////////////////////////////////////////////////////////
  354. template <typename Container, typename Enable/* = void*/>
  355. struct begin_container
  356. {
  357. static typename container_iterator<Container>::type call(Container& c)
  358. {
  359. return c.begin();
  360. }
  361. };
  362. template <typename Container>
  363. typename spirit::result_of::begin<Container>::type
  364. begin(Container& c)
  365. {
  366. return begin_container<Container>::call(c);
  367. }
  368. inline unused_type const*
  369. begin(unused_type)
  370. {
  371. return &unused;
  372. }
  373. ///////////////////////////////////////////////////////////////////////////
  374. template <typename Container, typename Enable/* = void*/>
  375. struct end_container
  376. {
  377. static typename container_iterator<Container>::type call(Container& c)
  378. {
  379. return c.end();
  380. }
  381. };
  382. template <typename Container>
  383. inline typename spirit::result_of::end<Container>::type
  384. end(Container& c)
  385. {
  386. return end_container<Container>::call(c);
  387. }
  388. inline unused_type const*
  389. end(unused_type)
  390. {
  391. return &unused;
  392. }
  393. ///////////////////////////////////////////////////////////////////////////
  394. template <typename Iterator, typename Enable/* = void*/>
  395. struct deref_iterator
  396. {
  397. typedef typename std::iterator_traits<Iterator>::reference type;
  398. static type call(Iterator& it)
  399. {
  400. return *it;
  401. }
  402. };
  403. template <typename Iterator>
  404. typename deref_iterator<Iterator>::type
  405. deref(Iterator& it)
  406. {
  407. return deref_iterator<Iterator>::call(it);
  408. }
  409. inline unused_type
  410. deref(unused_type const*)
  411. {
  412. return unused;
  413. }
  414. ///////////////////////////////////////////////////////////////////////////
  415. template <typename Iterator, typename Enable/* = void*/>
  416. struct next_iterator
  417. {
  418. static void call(Iterator& it)
  419. {
  420. ++it;
  421. }
  422. };
  423. template <typename Iterator>
  424. void next(Iterator& it)
  425. {
  426. next_iterator<Iterator>::call(it);
  427. }
  428. inline void next(unused_type const*)
  429. {
  430. // do nothing
  431. }
  432. ///////////////////////////////////////////////////////////////////////////
  433. template <typename Iterator, typename Enable/* = void*/>
  434. struct compare_iterators
  435. {
  436. static bool call(Iterator const& it1, Iterator const& it2)
  437. {
  438. return it1 == it2;
  439. }
  440. };
  441. template <typename Iterator>
  442. bool compare(Iterator& it1, Iterator& it2)
  443. {
  444. return compare_iterators<Iterator>::call(it1, it2);
  445. }
  446. inline bool compare(unused_type const*, unused_type const*)
  447. {
  448. return false;
  449. }
  450. }}}
  451. ///////////////////////////////////////////////////////////////////////////////
  452. namespace boost { namespace spirit { namespace result_of
  453. {
  454. ///////////////////////////////////////////////////////////////////////////
  455. template <typename T>
  456. struct optional_value
  457. {
  458. typedef T type;
  459. };
  460. template <typename T>
  461. struct optional_value<boost::optional<T> >
  462. {
  463. typedef T type;
  464. };
  465. template <typename T>
  466. struct optional_value<boost::optional<T> const>
  467. {
  468. typedef T const type;
  469. };
  470. template <>
  471. struct optional_value<unused_type>
  472. {
  473. typedef unused_type type;
  474. };
  475. template <>
  476. struct optional_value<unused_type const>
  477. {
  478. typedef unused_type type;
  479. };
  480. ///////////////////////////////////////////////////////////////////////////
  481. template <typename Container>
  482. struct begin
  483. : traits::container_iterator<Container>
  484. {};
  485. template <typename Container>
  486. struct end
  487. : traits::container_iterator<Container>
  488. {};
  489. template <typename Iterator>
  490. struct deref
  491. : traits::deref_iterator<Iterator>
  492. {};
  493. template <>
  494. struct deref<unused_type const*>
  495. {
  496. typedef unused_type type;
  497. };
  498. }}}
  499. #endif