statement.qbk 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  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. [section Statement]
  9. [*/Lazy statements.../]
  10. The expressions presented so far are sufficiently
  11. powerful to construct quite elaborate structures. We have presented lazy-functions
  12. and lazy-operators. How about lazy-statements? First, an appetizer:
  13. Print all odd-numbered contents of an STL container using `std::for_each`
  14. ([@../../example/all_odds.cpp all_odds.cpp]):
  15. std::for_each(c.begin(), c.end(),
  16. if_(arg1 % 2 == 1)
  17. [
  18. cout << arg1 << ' '
  19. ]
  20. );
  21. Huh? Is that valid C++? Read on...
  22. Yes, it is valid C++. The sample code above is as close as you can get to the
  23. syntax of C++. This stylized C++ syntax differs from actual C++ code. First, the
  24. `if` has a trailing underscore. Second, the block uses square brackets instead
  25. of the familiar curly braces {}.
  26. [note *C++ in C++?*
  27. In as much as __spirit__ attempts to mimic EBNF in C++,
  28. Phoenix attempts to mimic C++ in C++!!!
  29. ]
  30. [note Unlike lazy functions and lazy operators, lazy statements always
  31. return void.]
  32. Here are more examples with annotations. The code almost speaks for itself.
  33. [section Block Statement]
  34. [/ #include <boost/phoenix/statement/sequence.hpp>]
  35. Syntax:
  36. statement,
  37. statement,
  38. ....
  39. statement
  40. Basically, these are comma separated statements. Take note that unlike the C/C++
  41. semicolon, the comma is a separator put *in-between* statements. This is like
  42. Pascal's semicolon separator, rather than C/C++'s semicolon terminator. For
  43. example:
  44. statement,
  45. statement,
  46. statement, // ERROR!
  47. Is an error. The last statement should not have a comma. Block statements can be
  48. grouped using the parentheses. Again, the last statement in a group should not
  49. have a trailing comma.
  50. statement,
  51. statement,
  52. (
  53. statement,
  54. statement
  55. ),
  56. statement
  57. Outside the square brackets, block statements should be grouped. For example:
  58. std::for_each(c.begin(), c.end(),
  59. (
  60. do_this(arg1),
  61. do_that(arg1)
  62. )
  63. );
  64. Wrapping a comma operator chain around a parentheses pair blocks the
  65. interpretation as an argument separator. The reason for the exception for
  66. the square bracket operator is that the operator always takes exactly one
  67. argument, so it "transforms" any attempt at multiple arguments with a comma
  68. operator chain (and spits out an error for zero arguments).
  69. [endsect]
  70. [section if_ Statement]
  71. #include <boost/phoenix/statement/if.hpp>
  72. We have seen the `if_` statement. The syntax is:
  73. if_(conditional_expression)
  74. [
  75. sequenced_statements
  76. ]
  77. [endsect]
  78. [section '''if_else_''' Statement]
  79. #include <boost/phoenix/statement/if.hpp>
  80. The syntax is
  81. if_(conditional_expression)
  82. [
  83. sequenced_statements
  84. ]
  85. .else_
  86. [
  87. sequenced_statements
  88. ]
  89. Take note that `else` has a leading dot and a trailing underscore: `.else_`
  90. Example: This code prints out all the elements and appends `" > 5"`, `" == 5"`
  91. or `" < 5"` depending on the element's actual value:
  92. std::for_each(c.begin(), c.end(),
  93. if_(arg1 > 5)
  94. [
  95. cout << arg1 << " > 5\n"
  96. ]
  97. .else_
  98. [
  99. if_(arg1 == 5)
  100. [
  101. cout << arg1 << " == 5\n"
  102. ]
  103. .else_
  104. [
  105. cout << arg1 << " < 5\n"
  106. ]
  107. ]
  108. );
  109. Notice how the `if_else_` statement is nested.
  110. [/note `if_` is one example of a customized actor. See [link phoenix.advanced.extending.extending_actors Extending Actors] for more details]
  111. [endsect]
  112. [section switch_ Statement]
  113. #include <boost/phoenix/statement/switch.hpp>
  114. The syntax is:
  115. switch_(integral_expression)
  116. [
  117. case_<integral_value>(sequenced_statements),
  118. ...
  119. default_(sequenced_statements)
  120. ]
  121. A comma separated list of cases, and an optional default can be provided. Note unlike
  122. a normal switch statement, cases do not fall through.
  123. Example: This code prints out `"one"`, `"two"` or `"other value"` depending on the
  124. element's actual value:
  125. std::for_each(c.begin(), c.end(),
  126. switch_(arg1)
  127. [
  128. case_<1>(std::cout << val("one") << '\n'),
  129. case_<2>(std::cout << val("two") << '\n'),
  130. default_(std::cout << val("other value") << '\n')
  131. ]
  132. );
  133. [endsect]
  134. [section while_ Statement]
  135. #include <boost/phoenix/statement/while.hpp>
  136. The syntax is:
  137. while_(conditional_expression)
  138. [
  139. sequenced_statements
  140. ]
  141. Example: This code decrements each element until it reaches zero and prints out
  142. the number at each step. A newline terminates the printout of each value.
  143. std::for_each(c.begin(), c.end(),
  144. (
  145. while_(arg1--)
  146. [
  147. cout << arg1 << ", "
  148. ],
  149. cout << val("\n")
  150. )
  151. );
  152. [endsect]
  153. [section '''do_while_''' Statement]
  154. #include <boost/phoenix/statement/do_while.hpp>
  155. The syntax is:
  156. do_
  157. [
  158. sequenced_statements
  159. ]
  160. .while_(conditional_expression)
  161. Again, take note that `while` has a leading dot and a trailing underscore:
  162. `.while_`
  163. Example: This code is almost the same as the previous example above with a
  164. slight twist in logic.
  165. std::for_each(c.begin(), c.end(),
  166. (
  167. do_
  168. [
  169. cout << arg1 << ", "
  170. ]
  171. .while_(arg1--),
  172. cout << val("\n")
  173. )
  174. );
  175. [/note `do_` is one example of a customized actor. See [link phoenix.advanced.extending.extending_actors Extending Actors] for more details]
  176. [endsect]
  177. [section:for_statement for_ Statement]
  178. #include <boost/phoenix/statement/for.hpp>
  179. The syntax is:
  180. for_(init_statement, conditional_expression, step_statement)
  181. [
  182. sequenced_statements
  183. ]
  184. It is again very similar to the C++ for statement. Take note that the
  185. init_statement, conditional_expression and '''step_statement''' are separated by the
  186. comma instead of the semi-colon and each must be present (i.e. `for_(,,)` is
  187. invalid). This is a case where the [link phoenix.modules.core.nothing `nothing`]
  188. actor can be useful.
  189. Example: This code prints each element N times where N is the element's value. A
  190. newline terminates the printout of each value.
  191. int iii;
  192. std::for_each(c.begin(), c.end(),
  193. (
  194. for_(ref(iii) = 0, ref(iii) < arg1, ++ref(iii))
  195. [
  196. cout << arg1 << ", "
  197. ],
  198. cout << val("\n")
  199. )
  200. );
  201. As before, all these are lazily evaluated. The result of such statements are in
  202. fact expressions that are passed on to STL's for_each function. In the viewpoint
  203. of `for_each`, what was passed is just a functor, no more, no less.
  204. [endsect]
  205. [section try_ catch_ Statement]
  206. #include <boost/phoenix/statement/try_catch.hpp>
  207. The syntax is:
  208. try_
  209. [
  210. sequenced_statements
  211. ]
  212. .catch_<exception_type>()
  213. [
  214. sequenced_statements
  215. ]
  216. .catch_<another_exception_type>(local-id)
  217. [
  218. sequenced_statements
  219. ]
  220. ...
  221. .catch_all
  222. [
  223. sequenced_statement
  224. ]
  225. Note the usual underscore after try and catch, and the extra parentheses required
  226. after the catch.
  227. The second form of catch statement can refer thrown exception using specified
  228. local-id, which is __phoenix_local_variable__, in sequenced_statements.
  229. [/note `do_` is one example of a customized actor. See [link phoenix.advanced.extending.extending_actors Extending Actors] for more details]
  230. Example: The following code calls the (lazy) function `f` for each element, and
  231. prints messages about different exception types it catches.
  232. try_
  233. [
  234. f(arg1)
  235. ]
  236. .catch_<runtime_error>()
  237. [
  238. cout << val("caught runtime error or derived\n")
  239. ]
  240. .catch_<exception>(e_)
  241. [
  242. cout << val("caught exception or derived: ") << bind(&exception::what, e_) << val("\n")
  243. ]
  244. .catch_all
  245. [
  246. cout << val("caught some other type of exception\n")
  247. ]
  248. [endsect]
  249. [section throw_]
  250. #include <boost/phoenix/statement/throw.hpp>
  251. As a natural companion to the try/catch support, the statement module provides
  252. lazy throwing and re-throwing of exceptions.
  253. The syntax to throw an exception is:
  254. throw_(exception_expression)
  255. The syntax to re-throw an exception is:
  256. throw_()
  257. Example: This code extends the try/catch example, re-throwing exceptions derived from
  258. runtime_error or exception, and translating other exception types to runtime_errors.
  259. try_
  260. [
  261. f(arg1)
  262. ]
  263. .catch_<runtime_error>()
  264. [
  265. cout << val("caught runtime error or derived\n"),
  266. throw_()
  267. ]
  268. .catch_<exception>()
  269. [
  270. cout << val("caught exception or derived\n"),
  271. throw_()
  272. ]
  273. .catch_all
  274. [
  275. cout << val("caught some other type of exception\n"),
  276. throw_(runtime_error("translated exception"))
  277. ]
  278. [endsect]
  279. [endsect]