local_function.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright (C) 2009-2012 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0
  3. // (see accompanying file LICENSE_1_0.txt or a copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Home at http://www.boost.org/libs/local_function
  6. #ifndef BOOST_LOCAL_FUNCTION_HPP_
  7. #define BOOST_LOCAL_FUNCTION_HPP_
  8. #ifndef DOXYGEN
  9. #include <boost/local_function/aux_/macro/decl.hpp>
  10. #include <boost/local_function/aux_/macro/name.hpp>
  11. #include <boost/local_function/aux_/macro/typeof.hpp>
  12. #include <boost/local_function/aux_/preprocessor/traits/decl.hpp>
  13. #include <boost/local_function/detail/preprocessor/line_counter.hpp>
  14. #include <boost/local_function/detail/preprocessor/void_list.hpp>
  15. #include <boost/config.hpp>
  16. // PUBLIC //
  17. #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
  18. # define BOOST_LOCAL_FUNCTION_ID(id, declarations) \
  19. BOOST_LOCAL_FUNCTION_AUX_DECL(id, 0 /* not within template */, \
  20. BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
  21. BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST( \
  22. declarations)))
  23. # define BOOST_LOCAL_FUNCTION(declarations) \
  24. BOOST_LOCAL_FUNCTION_ID( \
  25. BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, declarations)
  26. # define BOOST_LOCAL_FUNCTION_ID_TPL(id, declarations) \
  27. BOOST_LOCAL_FUNCTION_AUX_DECL(id, 1 /* within template */, \
  28. BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
  29. BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST( \
  30. declarations)))
  31. # define BOOST_LOCAL_FUNCTION_TPL(declarations) \
  32. BOOST_LOCAL_FUNCTION_ID_TPL( \
  33. BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, declarations)
  34. #else // VARIADIC
  35. # define BOOST_LOCAL_FUNCTION_ID(id, ...) \
  36. BOOST_LOCAL_FUNCTION_AUX_DECL(id, 0 /* not within template */, \
  37. BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
  38. BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST(__VA_ARGS__)))
  39. # define BOOST_LOCAL_FUNCTION(...) \
  40. BOOST_LOCAL_FUNCTION_ID( \
  41. BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, __VA_ARGS__)
  42. # define BOOST_LOCAL_FUNCTION_ID_TPL(id, ...) \
  43. BOOST_LOCAL_FUNCTION_AUX_DECL(id, 1 /* within template */, \
  44. BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
  45. BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST(__VA_ARGS__)))
  46. # define BOOST_LOCAL_FUNCTION_TPL(...) \
  47. BOOST_LOCAL_FUNCTION_ID_TPL( \
  48. BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, __VA_ARGS__)
  49. #endif // VARIADIC
  50. #define BOOST_LOCAL_FUNCTION_NAME(qualified_name) \
  51. BOOST_LOCAL_FUNCTION_AUX_NAME(0 /* not within template */, qualified_name)
  52. #define BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name) \
  53. BOOST_LOCAL_FUNCTION_AUX_NAME(1 /* within template */, qualified_name)
  54. #define BOOST_LOCAL_FUNCTION_TYPEOF(bound_variable_name) \
  55. BOOST_LOCAL_FUNCTION_AUX_TYPEOF_TYPE(bound_variable_name)
  56. // DOCUMENTATION //
  57. #else // DOXYGEN
  58. /** @file
  59. @brief Local functions allow to program functions locally, within other
  60. functions, and directly within the scope where they are needed.
  61. */
  62. /**
  63. @brief This macro is used to start a local function declaration.
  64. This macro must be used within a declarative context, it must follow the local
  65. function result type, it must be followed by the local function body code, and
  66. then by the @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro (see the
  67. @RefSect{tutorial, Tutorial} and @RefSect{advanced_topics, Advanced Topics}
  68. sections):
  69. @code
  70. { // Some declarative context.
  71. ...
  72. result_type BOOST_LOCAL_FUNCTION(declarations) {
  73. ... // Body code.
  74. } BOOST_LOCAL_FUNCTION_NAME(qualified_name)
  75. ...
  76. }
  77. @endcode
  78. As usual, exceptions specifications can be optionally programmed just after the
  79. macro and before the body code block <c>{ ... }</c> (but the exception
  80. specifications will only apply to the body code and not to the library code
  81. automatically generated by the macro expansion, see the
  82. @RefSect{advanced_topics, Advanced Topics} section).
  83. Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_TPL}
  84. and @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used.
  85. @Params
  86. @Param{declarations,
  87. On compilers that support variadic macros\, the parameter declarations are
  88. defined by the following grammar:
  89. @code
  90. declarations:
  91. void | declaration_tuple | declaration_sequence
  92. declaration_tuple:
  93. declaration\, declaration\, ...
  94. declaration_sequence:
  95. (declaration) (declaration) ...
  96. declaration:
  97. bound_variable | parameter | default_value | result_type
  98. bound_variable:
  99. [const] bind [(variable_type)] [&] variable_name
  100. parameter:
  101. [auto | register] parameter_type parameter_name
  102. default_value:
  103. default parameter_default_value
  104. result_type:
  105. return function_result_type
  106. @endcode
  107. On compilers that do not support variadic macros\, <c>declaration_tuple</c>
  108. cannot be used:
  109. @code
  110. declarations:
  111. void | declaration_sequence
  112. @endcode
  113. (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
  114. <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
  115. <c>{expression}</c> means the token resulting from the expression.)
  116. }
  117. @EndParams
  118. Note that on compilers that support variadic macros, commas can be used to
  119. separate the declarations resembling more closely the usual C++ function
  120. declaration syntax (this is the preferred syntax).
  121. However, for portability, on all C++ compilers (with and without variadic
  122. macros) the same library macros also accept parameter declarations specified as
  123. a Boost.Preprocessor sequence separated by round parenthesis <c>()</c>.
  124. When binding the object <c>this</c>, the special symbol <c>this_</c> needs to
  125. be used instead of <c>this</c> as the name of the variable to bind and also
  126. within the local function body to access the object.
  127. (Mistakenly using <c>this</c> instead of <c>this_</c> might not always result in a compiler error and will in general result in undefined behaviour.)
  128. The result type must either be specified just before the macro or within the
  129. macro declarations prefixed by <c>return</c> (but not in both places).
  130. Within the local function body it possible to access the result type using <c>result_type</c>, the type of the first parameter using <c>arg1_type</c>, the type of the second parameter using <c>arg2_type</c>, etc.
  131. The bound variable types can be accessed using @RefMacro{BOOST_LOCAL_FUNCTION_TYPEOF}.
  132. This macro cannot be portably expanded multiple times on the same line.
  133. In these cases, use the @RefMacro{BOOST_LOCAL_FUNCTION_ID} macro instead.
  134. The maximum number of local function parameters (excluding bound variables) is
  135. specified by the configuration macro
  136. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX}.
  137. The maximum number of bound variables is specified by the configuration macro
  138. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_BIND_MAX}.
  139. The configuration macro
  140. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS} can be used to force
  141. optimizations that reduce the local function call run-time overhead.
  142. @Note Local functions are functors so they can be assigned to other functors
  143. like <c>boost::function</c> (see Boost.Function).
  144. @See @RefSect{tutorial, Tutorial} section,
  145. @RefSect{advanced_topics, Advanced Topics} section,
  146. @RefMacro{BOOST_LOCAL_FUNCTION_NAME}, @RefMacro{BOOST_LOCAL_FUNCTION_TPL},
  147. @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL},
  148. @RefMacro{BOOST_LOCAL_FUNCTION_TYPEOF}, @RefMacro{BOOST_LOCAL_FUNCTION_ID},
  149. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX},
  150. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_BIND_MAX},
  151. @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS}.
  152. */
  153. #define BOOST_LOCAL_FUNCTION(declarations)
  154. /**
  155. @brief This macro is used to start a local function declaration within
  156. templates.
  157. This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION} when
  158. declaring a local function within a template.
  159. A part from that, this macro has the exact same syntax a
  160. @RefMacro{BOOST_LOCAL_FUNCTION} (see @RefMacro{BOOST_LOCAL_FUNCTION} for more
  161. information):
  162. @code
  163. { // Some declarative context within a template.
  164. ...
  165. result_type BOOST_LOCAL_FUNCTION_TPL(declarations) {
  166. ... // Body code.
  167. } BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name)
  168. ...
  169. }
  170. @endcode
  171. Note that @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used with this
  172. macro instead of @RefMacro{BOOST_LOCAL_FUNCTION_NAME}.
  173. This macro cannot be portably expanded multiple times on the same line.
  174. In these cases, use the @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL} macro instead.
  175. @Note C++03 does not allow to use <c>typename</c> outside templates.
  176. This library internally manipulates types, these operations require
  177. <c>typename</c> but only within templates.
  178. This macro is used to indicate to the library when the enclosing scope is a
  179. template so the library can correctly use <c>typename</c>.
  180. @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_LOCAL_FUNCTION},
  181. @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL},
  182. @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL}.
  183. */
  184. #define BOOST_LOCAL_FUNCTION_TPL(declarations)
  185. /**
  186. @brief This macro allows to declare multiple local functions on the same line.
  187. This macro is equivalent to @RefMacro{BOOST_LOCAL_FUNCTION} but it can be
  188. expanded multiple times on the same line if different identifiers <c>id</c> are
  189. provided for each expansion (see the
  190. @RefSect{advanced_topics, Advanced Topics} section).
  191. @Params
  192. @Param{id,
  193. A unique identifier token which can be concatenated by the preprocessor
  194. (<c>__LINE__</c>\, <c>local_function_number_1_on_line_123</c>\, etc).
  195. }
  196. @Param{declarations,
  197. Same as the <c>declarations</c> parameter of the
  198. @RefMacro{BOOST_LOCAL_FUNCTION} macro.
  199. }
  200. @EndParams
  201. The @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro should be used to end each one
  202. of the multiple local function declarations as usual (and it will specify a
  203. unique name for each local function).
  204. Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL}
  205. must be used.
  206. @Note This macro can be useful when the local function macros are expanded
  207. within user-defined macros (because macros all expand on the same line).
  208. On some compilers (e.g., MSVC which supports the non-standard
  209. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  210. the use of this macro when expanding multiple local function macros on the same
  211. line is always necessary to ensure portability (this is because this library
  212. can only portably use <c>__LINE__</c> to internally generate unique
  213. identifiers).
  214. @See @RefSect{advanced_topics, Advanced Topics} section,
  215. @RefMacro{BOOST_LOCAL_FUNCTION}, @RefMacro{BOOST_LOCAL_FUNCTION_NAME},
  216. @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL}.
  217. */
  218. #define BOOST_LOCAL_FUNCTION_ID(id, declarations)
  219. /**
  220. @brief This macro allows to declare multiple local functions on the same line
  221. within templates.
  222. This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION_TPL} when
  223. declaring multiple local functions on the same line within a template.
  224. A part from that, this macro has the exact same syntax as
  225. @RefMacro{BOOST_LOCAL_FUNCTION_TPL} (see @RefMacro{BOOST_LOCAL_FUNCTION_TPL}
  226. for more information).
  227. @Params
  228. @Param{id,
  229. A unique identifier token which can be concatenated by the preprocessor
  230. (<c>__LINE__</c>\, <c>local_function_number_1_on_line_123</c>\, etc).
  231. }
  232. @Param{declarations,
  233. Same as the <c>declarations</c> parameter of the
  234. @RefMacro{BOOST_LOCAL_FUNCTION_TPL} macro.
  235. }
  236. @EndParams
  237. The @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro should be used to end each one
  238. of the multiple local function declarations as usual (and it will specify a
  239. unique name for each local function).
  240. Outside template, the macro @RefMacro{BOOST_LOCAL_FUNCTION_ID} should be used
  241. to declare multiple local functions on the same line.
  242. @Note This macro can be useful when the local function macros are expanded
  243. within user-defined macros (because macros all expand on the same line).
  244. On some compilers (e.g., MSVC which supports the non-standard
  245. <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
  246. the use of this macro when expanding multiple local function macros on the same
  247. line is always necessary to ensure portability (this is because this library
  248. can only portably use <c>__LINE__</c> to internally generate unique
  249. identifiers).
  250. @See @RefSect{advanced_topics, Advanced Topics} section,
  251. @RefMacro{BOOST_LOCAL_FUNCTION_TPL}, @RefMacro{BOOST_LOCAL_FUNCTION_NAME},
  252. @RefMacro{BOOST_LOCAL_FUNCTION_ID}.
  253. */
  254. #define BOOST_LOCAL_FUNCTION_ID_TPL(id, declarations)
  255. /**
  256. @brief This macro is used to end a local function declaration specifying its
  257. name.
  258. This macro must follow the local function body code block <c>{ ... }</c>:
  259. @code
  260. { // Some declarative context.
  261. ...
  262. result_type BOOST_LOCAL_FUNCTION(declarations) {
  263. ... // Body code.
  264. } BOOST_LOCAL_FUNCTION_NAME(qualified_name)
  265. ...
  266. }
  267. @endcode
  268. Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_TPL} and
  269. @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used.
  270. @Params
  271. @Param{qualified_name,
  272. The name of the local function optionally qualified as follow:
  273. @code
  274. name:
  275. [inline] [recursive] local_function_name
  276. @endcode
  277. (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
  278. <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
  279. <c>{expression}</c> means the token resulting from the expression.)
  280. }
  281. @EndParams
  282. The local function name can be qualified by prefixing it with the keyword
  283. <c>inline</c> (see the @RefSect{advanced_topics, Advanced Topics} section):
  284. @code
  285. BOOST_LOCAL_FUNCTION_NAME(inline local_function_name)
  286. @endcode
  287. This increases the chances that the compiler will be able to inline the local
  288. function calls (thus reducing run-time).
  289. However, inline local functions cannot be passed as template parameters (e.g., to <c>std::for_each</c>) or assigned to other functors (e.g., to
  290. <c>boost::function</c>).
  291. That is true on C++03 compilers but inline local functions can instead be
  292. passed as template parameters on C++11 compilers.
  293. On C++11 compilers, there is no need to declare a local function lined because
  294. this library will automatically use C++11 specific features to inline the local
  295. function while always allowing to pass it as a template parameter.
  296. This optimization is automatically enabled when the Boost.Config macro
  297. <c>BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS</c> is not defined but it also be
  298. forced using @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS}.
  299. The local function name can also be qualified by prefixing it with the
  300. "keyword" <c>recursive</c> (see the
  301. @RefSect{advanced_topics, Advanced Topics} section):
  302. @code
  303. BOOST_LOCAL_FUNCTION_NAME(recursive local_function_name)
  304. @endcode
  305. This allows the local function to recursively call itself from its body (as
  306. usual in C++).
  307. However, recursive local functions should only be called within their
  308. declaration scope (otherwise the result is undefined behaviour).
  309. Finally, compilers have not been observed to be able to inline recursive local
  310. function calls, not even when the recursive local function is also declared
  311. inline:
  312. @code
  313. BOOST_LOCAL_FUNCTION(inline recursive local_function_name)
  314. @endcode
  315. @Note The local function name cannot be the name of an operator
  316. <c>operator...</c> and it cannot be the same name of another local function
  317. declared within the same enclosing scope (but <c>boost::overloaded_function</c>
  318. can be used to overload local functions, see
  319. Boost.Functional/OverloadedFunction and the
  320. @RefSect{advanced_topics, Advanced Topics} section).
  321. @See @RefSect{tutorial, Tutorial} section,
  322. @RefSect{advanced_topics, Advanced Topics} section,
  323. @RefMacro{BOOST_LOCAL_FUNCTION},
  324. @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL}.
  325. */
  326. #define BOOST_LOCAL_FUNCTION_NAME(qualified_name)
  327. /**
  328. @brief This macro is used to end a local function declaration specifying its
  329. name within templates.
  330. This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION_NAME} when
  331. declaring a local function within a template.
  332. A part from that, this macro has the exact same syntax a
  333. @RefMacro{BOOST_LOCAL_FUNCTION_NAME} (see @RefMacro{BOOST_LOCAL_FUNCTION_NAME}
  334. for more information):
  335. @code
  336. { // Some declarative context within a template.
  337. ...
  338. result_type BOOST_LOCAL_FUNCTION_TPL(declarations) {
  339. ... // Body code.
  340. } BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name)
  341. ...
  342. }
  343. @endcode
  344. Note that @RefMacro{BOOST_LOCAL_FUNCTION_TPL} must be used with this macro
  345. instead of @RefMacro{BOOST_LOCAL_FUNCTION}.
  346. @Note C++03 does not allow to use <c>typename</c> outside templates.
  347. This library internally manipulates types, these operations require
  348. <c>typename</c> but only within templates.
  349. This macro is used to indicate to the library when the enclosing scope is a
  350. template so the library can correctly use <c>typename</c>.
  351. @See @RefSect{tutorial, Tutorial} section,
  352. @RefMacro{BOOST_LOCAL_FUNCTION_NAME}, @RefMacro{BOOST_LOCAL_FUNCTION_TPL}.
  353. */
  354. #define BOOST_LOCAL_FUNCTION_NAME_TPL(name)
  355. /**
  356. @brief This macro expands to the type of the specified bound variable.
  357. This macro can be used within the local functions body to refer to the bound
  358. variable types so to declare local variables, check concepts (using
  359. Boost.ConceptCheck), etc (see the @RefSect{advanced_topics, Advanced Topics}
  360. section).
  361. This way the local function can be programmed entirely without explicitly
  362. specifying the bound variable types thus facilitating maintenance (e.g., if
  363. the type of a bound variable changes in the enclosing scope, the local function
  364. code does not have to change).
  365. @Params
  366. @Param{bound_variable_name,
  367. The name of one of the local function's bound variables.
  368. }
  369. @EndParams
  370. The type returned by the macro is fully qualified in that it contains the extra
  371. constant and reference qualifiers when the specified variable is bound by
  372. constant and by reference.
  373. For example, if a variable named <c>t</c> of type <c>T</c> is:
  374. @li Bound by value using <c>bind t</c> then
  375. <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>T</c>.
  376. @li Bound by constant value using <c>const bind t</c> then
  377. <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>const T</c>.
  378. @li Bound by reference using <c>bind& t</c> then
  379. <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>T&</c>.
  380. @li Bound by constant reference using <c>const bind& t</c> then
  381. <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>const T&</c>.
  382. This macro must be prefixed by <c>typename</c> when used within templates.
  383. @Note It is best to use this macro instead of Boost.Typeof so to reduce the
  384. number of times Boost.Typeof is used to deduce types (see the
  385. @RefSect{advanced_topics, Advanced Topics} section).
  386. @See @RefSect{advanced_topics, Advanced Topics} section,
  387. @RefMacro{BOOST_LOCAL_FUNCTION}.
  388. */
  389. #define BOOST_LOCAL_FUNCTION_TYPEOF(bound_variable_name)
  390. #endif // DOXYGEN
  391. #endif // #include guard