container.hpp 15 KB

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