view.qbk 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2006 Dan Marsden
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ===============================================================================/]
  8. [section View]
  9. Views are sequences that do not actually contain data, but instead impart
  10. an alternative presentation over the data from one or more underlying
  11. sequences. Views are proxies. They provide an efficient yet purely
  12. functional way to work on potentially expensive sequence operations. Views
  13. are inherently lazy. Their elements are only computed on demand only when
  14. the elements of the underlying sequence(s) are actually accessed. Views'
  15. lazy nature make them very cheap to copy and be passed around by value.
  16. [heading Header]
  17. #include <boost/fusion/view.hpp>
  18. #include <boost/fusion/include/view.hpp>
  19. [section single_view]
  20. `single_view` is a view into a value as a single element sequence.
  21. [heading Header]
  22. #include <boost/fusion/view/single_view.hpp>
  23. #include <boost/fusion/include/single_view.hpp>
  24. [heading Synopsis]
  25. template <typename T>
  26. struct single_view;
  27. [heading Template parameters]
  28. [table
  29. [[Parameter] [Description] [Default]]
  30. [[`T`] [Any type] []]
  31. ]
  32. [heading Model of]
  33. * __random_access_sequence__
  34. [variablelist Notation
  35. [[`S`] [A `single_view` type]]
  36. [[`s`, `s2`] [Instances of `single_view`]]
  37. [[`x`] [An instance of `T`]]
  38. ]
  39. [heading Expression Semantics]
  40. Semantics of an expression is defined only where it differs from, or is not
  41. defined in __random_access_sequence__.
  42. [table
  43. [[Expression] [Semantics]]
  44. [[`S(x)`] [Creates a `single_view` from `x`.]]
  45. [[`S(s)`] [Copy constructs a `single_view` from another `single_view`, `s`.]]
  46. [[`s = s2`] [Assigns to a `single_view`, `s`, from another `single_view`, `s2`.]]
  47. ]
  48. [heading Example]
  49. single_view<int> view(3);
  50. std::cout << view << std::endl;
  51. [endsect]
  52. [section filter_view]
  53. [heading Description]
  54. `filter_view` is a view into a subset of its underlying sequence's elements
  55. satisfying a given predicate (an __mpl__ metafunction). The `filter_view`
  56. presents only those elements for which its predicate evaluates to
  57. `mpl::true_`.
  58. [heading Header]
  59. #include <boost/fusion/view/filter_view.hpp>
  60. #include <boost/fusion/include/filter_view.hpp>
  61. [heading Synopsis]
  62. template <typename Sequence, typename Pred>
  63. struct filter_view;
  64. [heading Template parameters]
  65. [table
  66. [[Parameter] [Description] [Default]]
  67. [[`Sequence`] [A __forward_sequence__] []]
  68. [[`Pred`] [A unary __mpl_lambda_expression__] []]
  69. ]
  70. [heading Model of]
  71. * __forward_sequence__
  72. * __associative_sequence__ if `Sequence` implements the __associative_sequence__ model.
  73. [variablelist Notation
  74. [[`F`] [A `filter_view` type]]
  75. [[`f`, `f2`] [Instances of `filter_view`]]
  76. [[`s`] [A __forward_sequence__]]
  77. ]
  78. [heading Expression Semantics]
  79. Semantics of an expression is defined only where it differs from, or is not
  80. defined in the implemented models.
  81. [table
  82. [[Expression] [Semantics]]
  83. [[`F(s)`] [Creates a `filter_view` given a sequence, `s`.]]
  84. [[`F(f)`] [Copy constructs a `filter_view` from another `filter_view`, `f`.]]
  85. [[`f = f2`] [Assigns to a `filter_view`, `f`, from another `filter_view`, `f2`.]]
  86. ]
  87. [heading Example]
  88. using boost::mpl::_;
  89. using boost::mpl::not_;
  90. using boost::is_class;
  91. typedef __vector__<std::string, char, long, bool, double> vector_type;
  92. vector_type v("a-string", '@', 987654, true, 6.6);
  93. filter_view<vector_type const, not_<is_class<_> > > view(v);
  94. std::cout << view << std::endl;
  95. [endsect]
  96. [section iterator_range]
  97. [heading Description]
  98. `iterator_range` presents a sub-range of its underlying sequence delimited
  99. by a pair of iterators.
  100. [heading Header]
  101. #include <boost/fusion/view/iterator_range.hpp>
  102. #include <boost/fusion/include/iterator_range.hpp>
  103. [heading Synopsis]
  104. template <typename First, typename Last>
  105. struct iterator_range;
  106. [heading Template parameters]
  107. [table
  108. [[Parameter] [Description] [Default]]
  109. [[`First`] [A fusion __iterator__] []]
  110. [[`Last`] [A fusion __iterator__] []]
  111. ]
  112. [heading Model of]
  113. * __forward_sequence__, __bidirectional_sequence__ or
  114. __random_access_sequence__ depending on the traversal characteristics (see
  115. __traversal_concept__) of its underlying sequence.
  116. * __associative_sequence__ if `First` and `Last` implement the __associative_iterator__ model.
  117. [variablelist Notation
  118. [[`IR`] [An `iterator_range` type]]
  119. [[`f`] [An instance of `First`]]
  120. [[`l`] [An instance of `Last`]]
  121. [[`ir`, `ir2`] [Instances of `iterator_range`]]
  122. ]
  123. [heading Expression Semantics]
  124. Semantics of an expression is defined only where it differs from, or is not
  125. defined in the implemented models.
  126. [table
  127. [[Expression] [Semantics]]
  128. [[`IR(f, l)`] [Creates an `iterator_range` given iterators, `f` and `l`.]]
  129. [[`IR(ir)`] [Copy constructs an `iterator_range` from another `iterator_range`, `ir`.]]
  130. [[`ir = ir2`] [Assigns to a `iterator_range`, `ir`, from another `iterator_range`, `ir2`.]]
  131. ]
  132. [heading Example]
  133. char const* s = "Ruby";
  134. typedef __vector__<int, char, double, char const*> vector_type;
  135. vector_type vec(1, 'x', 3.3, s);
  136. typedef __result_of_begin__<vector_type>::type A;
  137. typedef __result_of_end__<vector_type>::type B;
  138. typedef __result_of_next__<A>::type C;
  139. typedef __result_of_prior__<B>::type D;
  140. C c(vec);
  141. D d(vec);
  142. iterator_range<C, D> range(c, d);
  143. std::cout << range << std::endl;
  144. [endsect]
  145. [section joint_view]
  146. [heading Description]
  147. `joint_view` presents a view which is a concatenation of two sequences.
  148. [heading Header]
  149. #include <boost/fusion/view/joint_view.hpp>
  150. #include <boost/fusion/include/joint_view.hpp>
  151. [heading Synopsis]
  152. template <typename Sequence1, typename Sequence2>
  153. struct joint_view;
  154. [heading Template parameters]
  155. [table
  156. [[Parameter] [Description] [Default]]
  157. [[`Sequence1`] [A __forward_sequence__] []]
  158. [[`Sequence2`] [A __forward_sequence__] []]
  159. ]
  160. [heading Model of]
  161. * __forward_sequence__
  162. * __associative_sequence__ if `Sequence1` and `Sequence2` implement the __associative_sequence__ model.
  163. [variablelist Notation
  164. [[`JV`] [A `joint_view` type]]
  165. [[`s1`] [An instance of `Sequence1`]]
  166. [[`s2`] [An instance of `Sequence2`]]
  167. [[`jv`, `jv2`] [Instances of `joint_view`]]
  168. ]
  169. [heading Expression Semantics]
  170. Semantics of an expression is defined only where it differs from, or is not
  171. defined in the implemented models.
  172. [table
  173. [[Expression] [Semantics]]
  174. [[`JV(s1, s2)`] [Creates a `joint_view` given sequences, `s1` and `s2`.]]
  175. [[`JV(jv)`] [Copy constructs a `joint_view` from another `joint_view`, `jv`.]]
  176. [[`jv = jv2`] [Assigns to a `joint_view`, `jv`, from another `joint_view`, `jv2`.]]
  177. ]
  178. [heading Example]
  179. __vector__<int, char> v1(3, 'x');
  180. __vector__<std::string, int> v2("hello", 123);
  181. joint_view<
  182. __vector__<int, char>
  183. , __vector__<std::string, int>
  184. > view(v1, v2);
  185. std::cout << view << std::endl;
  186. [endsect]
  187. [section zip_view]
  188. [heading Description]
  189. `zip_view` presents a view which iterates over a collection of __sequence__(s) in parallel. A `zip_view`
  190. is constructed from a __sequence__ of references to the component `__sequence__`s.
  191. [heading Header]
  192. #include <boost/fusion/view/zip_view.hpp>
  193. #include <boost/fusion/include/zip_view.hpp>
  194. [heading Synopsis]
  195. template <typename Sequences>
  196. struct zip_view;
  197. [heading Template parameters]
  198. [table
  199. [[Parameter] [Description] [Default]]
  200. [[`Sequences`] [A __forward_sequence__ of references to other Fusion `__sequence__`s] []]
  201. ]
  202. [heading Model of]
  203. * __forward_sequence__, __bidirectional_sequence__ or
  204. __random_access_sequence__ depending on the traversal characteristics (see
  205. __traversal_concept__) of its underlying sequence.
  206. [variablelist Notation
  207. [[`ZV`] [A `zip_view` type]]
  208. [[`s`] [An instance of `Sequences`]]
  209. [[`zv1`, `zv2`] [Instances of `ZV`]]
  210. ]
  211. [heading Expression Semantics]
  212. Semantics of an expression is defined only where it differs from, or is not
  213. defined in __forward_sequence__.
  214. [table
  215. [[Expression] [Semantics]]
  216. [[`ZV(s)`] [Creates a `zip_view` given a sequence of references to the component `__sequence__`s.]]
  217. [[`ZV(zv1)`] [Copy constructs a `zip_view` from another `zip_view`, `zv`.]]
  218. [[`zv1 = zv2`] [Assigns to a `zip_view`, `zv`, from another `zip_view`, `zv2`.]]
  219. ]
  220. [heading Example]
  221. typedef __vector__<int,int> vec1;
  222. typedef __vector__<char,char> vec2;
  223. vec1 v1(1,2);
  224. vec2 v2('a','b');
  225. typedef __vector__<vec1&, vec2&> sequences;
  226. std::cout << zip_view<sequences>(sequences(v1, v2)) << std::endl; // ((1 a) (2 b))
  227. [endsect]
  228. [section transform_view]
  229. The unary version of `transform_view` presents a view of its underlying
  230. sequence given a unary function object or function pointer. The binary
  231. version of `transform_view` presents a view of 2 underlying sequences,
  232. given a binary function object or function pointer. The `transform_view`
  233. inherits the traversal characteristics (see __traversal_concept__) of
  234. its underlying sequence or sequences.
  235. [heading Header]
  236. #include <boost/fusion/view/transform_view.hpp>
  237. #include <boost/fusion/include/transform_view.hpp>
  238. [heading Synopsis]
  239. [*Unary Version]
  240. template <typename Sequence, typename F1>
  241. struct transform_view;
  242. [*Binary Version]
  243. template <typename Sequence1, typename Sequence2, typename F2>
  244. struct transform_view;
  245. [heading Template parameters]
  246. [table
  247. [[Parameter] [Description] [Default]]
  248. [[`Sequence`] [A __forward_sequence__] []]
  249. [[`Sequence1`] [A __forward_sequence__] []]
  250. [[`Sequence2`] [A __forward_sequence__] []]
  251. [[`F1`] [A unary function object or function pointer. `__boost_result_of_call__<F1(E)>::type` is the return type of an instance of `F1` when called with a value of each element type `E` in the input sequence.] []]
  252. [[`F2`] [A binary function object or function pointer. `__boost_result_of_call__<F2(E1, E2)>::type` is the return type of an instance of `F2` when called with a value of each corresponding pair of element type `E1` and `E2` in the input sequences.] []]
  253. ]
  254. [heading Model of]
  255. * __forward_sequence__, __bidirectional_sequence__ or
  256. __random_access_sequence__ depending on the traversal characteristics (see
  257. __traversal_concept__) of its underlying sequence.
  258. [variablelist Notation
  259. [[`TV`] [A `transform_view` type]]
  260. [[`BTV`] [A binary `transform_view` type]]
  261. [[`UTV`] [A unary `transform_view` type]]
  262. [[`f1`] [An instance of `F1`]]
  263. [[`f2`] [An instance of `F2`]]
  264. [[`s`] [An instance of `Sequence`]]
  265. [[`s1`] [An instance of `Sequence1`]]
  266. [[`s2`] [An instance of `Sequence2`]]
  267. [[`tv`, `tv2`] [Instances of `transform_view`]]
  268. ]
  269. [heading Expression Semantics]
  270. Semantics of an expression is defined only where it differs from, or is not
  271. defined in __forward_sequence__, __bidirectional_sequence__ or
  272. __random_access_sequence__ depending on the traversal characteristics (see
  273. __traversal_concept__) of its underlying sequence or sequences.
  274. [table
  275. [[Expression] [Semantics]]
  276. [[`UTV(s, f1)`] [Creates a unary `transform_view` given sequence,
  277. `s` and unary function object or function pointer, `f1`.]]
  278. [[`BTV(s1, s2, f2)`] [Creates a binary `transform_view` given sequences, `s1` and `s2`
  279. and binary function object or function pointer, `f2`.]]
  280. [[`TV(tv)`] [Copy constructs a `transform_view` from another `transform_view`, `tv`.]]
  281. [[`tv = tv2`] [Assigns to a `transform_view`, `tv`, from another `transform_view`, `tv2`.]]
  282. ]
  283. [heading Example]
  284. struct square
  285. {
  286. template<typename Sig>
  287. struct result;
  288. template<typename U>
  289. struct result<square(U)>
  290. : remove_reference<U>
  291. {};
  292. template <typename T>
  293. T operator()(T x) const
  294. {
  295. return x * x;
  296. }
  297. };
  298. typedef __vector__<int, short, double> vector_type;
  299. vector_type vec(2, 5, 3.3);
  300. transform_view<vector_type, square> transform(vec, square());
  301. std::cout << transform << std::endl;
  302. [endsect]
  303. [section reverse_view]
  304. `reverse_view` presents a reversed view of underlying sequence. The first
  305. element will be its last and the last element will be its first.
  306. [heading Header]
  307. #include <boost/fusion/view/reverse_view.hpp>
  308. #include <boost/fusion/include/reverse_view.hpp>
  309. [heading Synopsis]
  310. template <typename Sequence>
  311. struct reverse_view;
  312. [heading Template parameters]
  313. [table
  314. [[Parameter] [Description] [Default]]
  315. [[`Sequence`] [A __bidirectional_sequence__] []]
  316. ]
  317. [heading Model of]
  318. * A model of __bidirectional_sequence__ if `Sequence` is a __bidirectional_sequence__
  319. else, __random_access_sequence__ if `Sequence` is a __random_access_sequence__.
  320. * __associative_sequence__ if `Sequence` implements the __associative_sequence__ model.
  321. [variablelist Notation
  322. [[`RV`] [A `reverse_view` type]]
  323. [[`s`] [An instance of `Sequence`]]
  324. [[`rv`, `rv2`] [Instances of `reverse_view`]]
  325. ]
  326. [heading Expression Semantics]
  327. Semantics of an expression is defined only where it differs from, or is not
  328. defined in the implemented models.
  329. [table
  330. [[Expression] [Semantics]]
  331. [[`RV(s)`] [Creates a unary `reverse_view` given sequence, `s`.]]
  332. [[`RV(rv)`] [Copy constructs a `reverse_view` from another `reverse_view`, `rv`.]]
  333. [[`rv = rv2`] [Assigns to a `reverse_view`, `rv`, from another `reverse_view`, `rv2`.]]
  334. ]
  335. [heading Example]
  336. typedef __vector__<int, short, double> vector_type;
  337. vector_type vec(2, 5, 3.3);
  338. reverse_view<vector_type> reverse(vec);
  339. std::cout << reverse << std::endl;
  340. [endsect]
  341. [section nview]
  342. [heading Description]
  343. `nview` presents a view which iterates over a given __sequence__ in a specified order.
  344. An `nview` is constructed from an arbitrary __sequence__ and a list of indices specifying
  345. the elements to iterate over.
  346. [heading Header]
  347. #include <boost/fusion/view/nview.hpp>
  348. #include <boost/fusion/include/nview.hpp>
  349. [heading Synopsis]
  350. template <typename Sequence, typename Indices>
  351. struct nview;
  352. template <typename Sequence, int I1, int I2 = -1, ...>
  353. typename result_of::nview<Sequence, I1, I2, ...>::type
  354. as_nview(Sequence& s);
  355. [heading Template parameters]
  356. [table
  357. [[Parameter] [Description] [Default]]
  358. [[`Sequence`] [An arbitrary Fusion __forward_sequence__]
  359. []]
  360. [[`Indices`] [A `mpl::vector_c<int, ...>` holding the indices defining
  361. the required iteration order.] []]
  362. [[`I1`, `I2`, `I3`...] [A list of integers specifying the required
  363. iteration order.] [`INT_MAX` for `I2`, `I3`...]]
  364. ]
  365. [heading Model of]
  366. * __random_access_sequence__ (see __traversal_concept__)
  367. [variablelist Notation
  368. [[`NV`] [A `nview` type]]
  369. [[`s`] [An instance of `Sequences`]]
  370. [[`nv1`, `nv2`] [Instances of `NV`]]
  371. ]
  372. [heading Expression Semantics]
  373. Semantics of an expression is defined only where it differs from, or is not
  374. defined in __random_access_sequence__.
  375. [table
  376. [[Expression] [Semantics]]
  377. [[`NV(s)`] [Creates an `nview` given a sequence and a list of indices.]]
  378. [[`NV(nv1)`] [Copy constructs an `nview` from another `nview`, `nv1`.]]
  379. [[`nv1 = nv2`] [Assigns to an `nview`, `nv1`, from another `nview`, `nv2`.]]
  380. ]
  381. The `nview` internally stores a Fusion __vector__ of references to the elements
  382. of the original Fusion __sequence__
  383. [heading Example]
  384. typedef __vector__<int, char, double> vec;
  385. typedef mpl::vector_c<int, 2, 1, 0, 2, 0> indices;
  386. vec v1(1, 'c', 2.0);
  387. std::cout << nview<vec, indices>(v1) << std::endl; // (2.0 c 1 2.0 1)
  388. std::cout << as_nview<2, 1, 1, 0>(v1) << std::endl; // (2.0 c c 1)
  389. [endsect]
  390. [section repetitive_view]
  391. [heading Description]
  392. `repetitive_view` presents a view which iterates over a given
  393. __sequence__ repeatedly. Because a `repetitive_view`
  394. has infinite length, it can only be used when some external
  395. condition determines the end. Thus, initializing a fixed
  396. length sequence with a `repetitive_view` is okay, but
  397. printing a `repetitive_view` to `std::cout` is not.
  398. [heading Header]
  399. #include <boost/fusion/view/repetitive_view.hpp>
  400. #include <boost/fusion/include/repetitive_view.hpp>
  401. [heading Synopsis]
  402. template <typename Sequence>
  403. struct repetitive_view;
  404. [heading Template parameters]
  405. [table
  406. [[Parameter] [Description] [Default]]
  407. [[`Sequence`] [An arbitrary Fusion __forward_sequence__]
  408. []]
  409. ]
  410. [variablelist Notation
  411. [[`RV`] [A `repetitive_view` type]]
  412. [[`s`] [An instance of `Sequences`]]
  413. [[`rv`, `rv1`, `rv2`] [Instances of `RV`]]
  414. ]
  415. [heading Expression Semantics]
  416. [table
  417. [[Expression] [Return Type] [Semantics]]
  418. [[`RV(s)`] [] [Creates an `repetitive_view` given the underlying sequence.]]
  419. [[`RV(rv1)`] [] [Copy constructs an `repetitive_view` from another `repetitive_view`, `rv1`.]]
  420. [[`rv1 = rv2`] [] [Assigns to a `repetitive_view`, `rv1`, from another `repetitive_view`, `rv2`.]]
  421. [[`__begin__(rv)`] [__forward_iterator__] []]
  422. [[`__end__(rv)`] [__forward_iterator__] [Creates an unreachable iterator (since the sequence is infinite)]]
  423. ]
  424. [heading Result Type Expressions]
  425. [table
  426. [[Expression]]
  427. [[`__result_of_begin__<RV>::type`]]
  428. [[`__result_of_end__<RV>::type`]]
  429. ]
  430. [heading Example]
  431. typedef __vector__<int, char, double> vec1;
  432. typedef __vector__<int, char, double, int, char> vec2;
  433. vec1 v1(1, 'c', 2.0);
  434. vec2 v2(repetitive_view<vec1>(v1));
  435. std::cout << v2 << std::endl; // 1, 'c', 2.0, 1, 'c'
  436. [endsect]
  437. [section flatten_view]
  438. [heading Description]
  439. `flatten_view` presents a view which iterates over its elements recursively in depth-first order.
  440. [heading Header]
  441. #include <boost/fusion/view/flatten_view.hpp>
  442. #include <boost/fusion/include/flatten_view.hpp>
  443. [heading Synopsis]
  444. template <typename Sequence>
  445. struct flatten_view;
  446. [heading Template parameters]
  447. [table
  448. [[Parameter] [Description] [Default]]
  449. [[`Sequence`] [A __forward_sequence__] []]
  450. ]
  451. [heading Model of]
  452. * __forward_sequence__
  453. [variablelist Notation
  454. [[`F`] [A `flatten_view` type]]
  455. [[`s`] [An instance of `Sequence`]]
  456. [[`f`, `f2`] [Instances of `F`]]
  457. ]
  458. [heading Expression Semantics]
  459. Semantics of an expression is defined only where it differs from, or is not
  460. defined in __forward_sequence__.
  461. [table
  462. [[Expression] [Semantics]]
  463. [[`F(s)`] [Creates a `flatten_view` given sequence, `s`.]]
  464. [[`F(f)`] [Copy constructs a `flatten_view` from another `flatten_view`, `f`.]]
  465. [[`f = f2`] [Assigns to a `flatten_view`, `f`, from another `flatten_view`, `f2`.]]
  466. ]
  467. [heading Example]
  468. typedef __vector__<int, int, __vector__<int, int>, int> sequence_type;
  469. sequence_type seq;
  470. __flatten_view__<sequence_type> flattened(seq);
  471. __copy__(__make_vector__(1, 2, 3, 4, 5), flattened);
  472. assert(seq == __make_vector__(1, 2, __make_vector__(3, 4), 5));
  473. [endsect]
  474. [endsect]