container.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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/range_fwd.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 Iterator type;
  179. };
  180. template <>
  181. struct container_iterator<unused_type>
  182. {
  183. typedef unused_type const* type;
  184. };
  185. template <>
  186. struct container_iterator<unused_type const>
  187. {
  188. typedef unused_type const* type;
  189. };
  190. ///////////////////////////////////////////////////////////////////////////
  191. template <typename T, typename Enable/* = void*/>
  192. struct optional_attribute
  193. {
  194. typedef T const& type;
  195. static type call(T const& val)
  196. {
  197. return val;
  198. }
  199. static bool is_valid(T const&)
  200. {
  201. return true;
  202. }
  203. };
  204. template <typename T>
  205. struct optional_attribute<boost::optional<T> >
  206. {
  207. typedef T const& type;
  208. static type call(boost::optional<T> const& val)
  209. {
  210. return boost::get<T>(val);
  211. }
  212. static bool is_valid(boost::optional<T> const& val)
  213. {
  214. return !!val;
  215. }
  216. };
  217. template <typename T>
  218. typename optional_attribute<T>::type
  219. optional_value(T const& val)
  220. {
  221. return optional_attribute<T>::call(val);
  222. }
  223. inline unused_type optional_value(unused_type)
  224. {
  225. return unused;
  226. }
  227. template <typename T>
  228. bool has_optional_value(T const& val)
  229. {
  230. return optional_attribute<T>::is_valid(val);
  231. }
  232. inline bool has_optional_value(unused_type)
  233. {
  234. return true;
  235. }
  236. ///////////////////////////////////////////////////////////////////////////
  237. template <typename Container, typename T>
  238. bool push_back(Container& c, T const& val);
  239. //[customization_push_back_default
  240. template <typename Container, typename T, typename Enable/* = void*/>
  241. struct push_back_container
  242. {
  243. static bool call(Container& c, T const& val)
  244. {
  245. c.insert(c.end(), val);
  246. return true;
  247. }
  248. };
  249. //]
  250. template <typename Container, typename T>
  251. struct push_back_container<optional<Container>, T>
  252. {
  253. static bool call(boost::optional<Container>& c, T const& val)
  254. {
  255. if (!c)
  256. c = Container();
  257. return push_back(boost::get<Container>(c), val);
  258. }
  259. };
  260. namespace detail
  261. {
  262. template <typename T>
  263. struct push_back_visitor : public static_visitor<>
  264. {
  265. typedef bool result_type;
  266. push_back_visitor(T const& t) : t_(t) {}
  267. template <typename Container>
  268. bool push_back_impl(Container& c, mpl::true_) const
  269. {
  270. return push_back(c, t_);
  271. }
  272. template <typename T_>
  273. bool push_back_impl(T_&, mpl::false_) const
  274. {
  275. // this variant doesn't hold a container
  276. BOOST_ASSERT(false && "This variant doesn't hold a container");
  277. return false;
  278. }
  279. template <typename T_>
  280. bool operator()(T_& c) const
  281. {
  282. return push_back_impl(c, typename is_container<T_>::type());
  283. }
  284. T const& t_;
  285. };
  286. }
  287. template <BOOST_VARIANT_ENUM_PARAMS(typename T_), typename T>
  288. struct push_back_container<variant<BOOST_VARIANT_ENUM_PARAMS(T_)>, T>
  289. {
  290. static bool call(variant<BOOST_VARIANT_ENUM_PARAMS(T_)>& c, T const& val)
  291. {
  292. return apply_visitor(detail::push_back_visitor<T>(val), c);
  293. }
  294. };
  295. template <typename Container, typename T>
  296. bool push_back(Container& c, T const& val)
  297. {
  298. return push_back_container<Container, T>::call(c, val);
  299. }
  300. //[customization_push_back_unused
  301. template <typename Container>
  302. bool push_back(Container&, unused_type)
  303. {
  304. return true;
  305. }
  306. //]
  307. template <typename T>
  308. bool push_back(unused_type, T const&)
  309. {
  310. return true;
  311. }
  312. inline bool push_back(unused_type, unused_type)
  313. {
  314. return true;
  315. }
  316. ///////////////////////////////////////////////////////////////////////////
  317. template <typename Container, typename Enable/* = void*/>
  318. struct is_empty_container
  319. {
  320. static bool call(Container const& c)
  321. {
  322. return c.empty();
  323. }
  324. };
  325. template <typename Container>
  326. bool is_empty(Container const& c)
  327. {
  328. return is_empty_container<Container>::call(c);
  329. }
  330. inline bool is_empty(unused_type)
  331. {
  332. return true;
  333. }
  334. ///////////////////////////////////////////////////////////////////////////
  335. // Ensure the attribute is actually a container type
  336. template <typename Container, typename Enable/* = void*/>
  337. struct make_container_attribute
  338. {
  339. static void call(Container&)
  340. {
  341. // for static types this function does nothing
  342. }
  343. };
  344. template <typename T>
  345. void make_container(T& t)
  346. {
  347. make_container_attribute<T>::call(t);
  348. }
  349. inline void make_container(unused_type)
  350. {
  351. }
  352. ///////////////////////////////////////////////////////////////////////////
  353. template <typename Container, typename Enable/* = void*/>
  354. struct begin_container
  355. {
  356. static typename container_iterator<Container>::type call(Container& c)
  357. {
  358. return c.begin();
  359. }
  360. };
  361. template <typename Container>
  362. typename spirit::result_of::begin<Container>::type
  363. begin(Container& c)
  364. {
  365. return begin_container<Container>::call(c);
  366. }
  367. inline unused_type const*
  368. begin(unused_type)
  369. {
  370. return &unused;
  371. }
  372. ///////////////////////////////////////////////////////////////////////////
  373. template <typename Container, typename Enable/* = void*/>
  374. struct end_container
  375. {
  376. static typename container_iterator<Container>::type call(Container& c)
  377. {
  378. return c.end();
  379. }
  380. };
  381. template <typename Container>
  382. inline typename spirit::result_of::end<Container>::type
  383. end(Container& c)
  384. {
  385. return end_container<Container>::call(c);
  386. }
  387. inline unused_type const*
  388. end(unused_type)
  389. {
  390. return &unused;
  391. }
  392. ///////////////////////////////////////////////////////////////////////////
  393. template <typename Iterator, typename Enable/* = void*/>
  394. struct deref_iterator
  395. {
  396. typedef typename std::iterator_traits<Iterator>::reference type;
  397. static type call(Iterator& it)
  398. {
  399. return *it;
  400. }
  401. };
  402. template <typename Iterator>
  403. typename deref_iterator<Iterator>::type
  404. deref(Iterator& it)
  405. {
  406. return deref_iterator<Iterator>::call(it);
  407. }
  408. inline unused_type
  409. deref(unused_type const*)
  410. {
  411. return unused;
  412. }
  413. ///////////////////////////////////////////////////////////////////////////
  414. template <typename Iterator, typename Enable/* = void*/>
  415. struct next_iterator
  416. {
  417. static void call(Iterator& it)
  418. {
  419. ++it;
  420. }
  421. };
  422. template <typename Iterator>
  423. void next(Iterator& it)
  424. {
  425. next_iterator<Iterator>::call(it);
  426. }
  427. inline void next(unused_type const*)
  428. {
  429. // do nothing
  430. }
  431. ///////////////////////////////////////////////////////////////////////////
  432. template <typename Iterator, typename Enable/* = void*/>
  433. struct compare_iterators
  434. {
  435. static bool call(Iterator const& it1, Iterator const& it2)
  436. {
  437. return it1 == it2;
  438. }
  439. };
  440. template <typename Iterator>
  441. bool compare(Iterator& it1, Iterator& it2)
  442. {
  443. return compare_iterators<Iterator>::call(it1, it2);
  444. }
  445. inline bool compare(unused_type const*, unused_type const*)
  446. {
  447. return false;
  448. }
  449. }}}
  450. ///////////////////////////////////////////////////////////////////////////////
  451. namespace boost { namespace spirit { namespace result_of
  452. {
  453. ///////////////////////////////////////////////////////////////////////////
  454. template <typename T>
  455. struct optional_value
  456. {
  457. typedef T type;
  458. };
  459. template <typename T>
  460. struct optional_value<boost::optional<T> >
  461. {
  462. typedef T type;
  463. };
  464. template <typename T>
  465. struct optional_value<boost::optional<T> const>
  466. {
  467. typedef T const type;
  468. };
  469. template <>
  470. struct optional_value<unused_type>
  471. {
  472. typedef unused_type type;
  473. };
  474. template <>
  475. struct optional_value<unused_type const>
  476. {
  477. typedef unused_type type;
  478. };
  479. ///////////////////////////////////////////////////////////////////////////
  480. template <typename Container>
  481. struct begin
  482. : traits::container_iterator<Container>
  483. {};
  484. template <typename Container>
  485. struct end
  486. : traits::container_iterator<Container>
  487. {};
  488. template <typename Iterator>
  489. struct deref
  490. : traits::deref_iterator<Iterator>
  491. {};
  492. template <>
  493. struct deref<unused_type const*>
  494. {
  495. typedef unused_type type;
  496. };
  497. }}}
  498. #endif