lambda_traits.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // - lambda_traits.hpp --- Boost Lambda Library ----------------------------
  2. //
  3. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see www.boost.org
  10. // -------------------------------------------------------------------------
  11. #ifndef BOOST_LAMBDA_LAMBDA_TRAITS_HPP
  12. #define BOOST_LAMBDA_LAMBDA_TRAITS_HPP
  13. #include "boost/type_traits/transform_traits.hpp"
  14. #include "boost/type_traits/cv_traits.hpp"
  15. #include "boost/type_traits/function_traits.hpp"
  16. #include "boost/type_traits/object_traits.hpp"
  17. #include "boost/tuple/tuple.hpp"
  18. namespace boost {
  19. namespace lambda {
  20. // -- if construct ------------------------------------------------
  21. // Proposed by Krzysztof Czarnecki and Ulrich Eisenecker
  22. namespace detail {
  23. template <bool If, class Then, class Else> struct IF { typedef Then RET; };
  24. template <class Then, class Else> struct IF<false, Then, Else> {
  25. typedef Else RET;
  26. };
  27. // An if construct that doesn't instantiate the non-matching template:
  28. // Called as:
  29. // IF_type<condition, A, B>::type
  30. // The matching template must define the typeded 'type'
  31. // I.e. A::type if condition is true, B::type if condition is false
  32. // Idea from Vesa Karvonen (from C&E as well I guess)
  33. template<class T>
  34. struct IF_type_
  35. {
  36. typedef typename T::type type;
  37. };
  38. template<bool C, class T, class E>
  39. struct IF_type
  40. {
  41. typedef typename
  42. IF_type_<typename IF<C, T, E>::RET >::type type;
  43. };
  44. // helper that can be used to give typedef T to some type
  45. template <class T> struct identity_mapping { typedef T type; };
  46. // An if construct for finding an integral constant 'value'
  47. // Does not instantiate the non-matching branch
  48. // Called as IF_value<condition, A, B>::value
  49. // If condition is true A::value must be defined, otherwise B::value
  50. template<class T>
  51. struct IF_value_
  52. {
  53. BOOST_STATIC_CONSTANT(int, value = T::value);
  54. };
  55. template<bool C, class T, class E>
  56. struct IF_value
  57. {
  58. BOOST_STATIC_CONSTANT(int, value = (IF_value_<typename IF<C, T, E>::RET>::value));
  59. };
  60. // --------------------------------------------------------------
  61. // removes reference from other than function types:
  62. template<class T> class remove_reference_if_valid
  63. {
  64. typedef typename boost::remove_reference<T>::type plainT;
  65. public:
  66. typedef typename IF<
  67. boost::is_function<plainT>::value,
  68. T,
  69. plainT
  70. >::RET type;
  71. };
  72. template<class T> struct remove_reference_and_cv {
  73. typedef typename boost::remove_cv<
  74. typename boost::remove_reference<T>::type
  75. >::type type;
  76. };
  77. // returns a reference to the element of tuple T
  78. template<int N, class T> struct tuple_element_as_reference {
  79. typedef typename
  80. boost::tuples::access_traits<
  81. typename boost::tuples::element<N, T>::type
  82. >::non_const_type type;
  83. };
  84. // returns the cv and reverence stripped type of a tuple element
  85. template<int N, class T> struct tuple_element_stripped {
  86. typedef typename
  87. remove_reference_and_cv<
  88. typename boost::tuples::element<N, T>::type
  89. >::type type;
  90. };
  91. // is_lambda_functor -------------------------------------------------
  92. template <class T> struct is_lambda_functor_ {
  93. BOOST_STATIC_CONSTANT(bool, value = false);
  94. };
  95. template <class Arg> struct is_lambda_functor_<lambda_functor<Arg> > {
  96. BOOST_STATIC_CONSTANT(bool, value = true);
  97. };
  98. } // end detail
  99. template <class T> struct is_lambda_functor {
  100. BOOST_STATIC_CONSTANT(bool,
  101. value =
  102. detail::is_lambda_functor_<
  103. typename detail::remove_reference_and_cv<T>::type
  104. >::value);
  105. };
  106. namespace detail {
  107. // -- parameter_traits_ ---------------------------------------------
  108. // An internal parameter type traits class that respects
  109. // the reference_wrapper class.
  110. // The conversions performed are:
  111. // references -> compile_time_error
  112. // T1 -> T2,
  113. // reference_wrapper<T> -> T&
  114. // const array -> ref to const array
  115. // array -> ref to array
  116. // function -> ref to function
  117. // ------------------------------------------------------------------------
  118. template<class T1, class T2>
  119. struct parameter_traits_ {
  120. typedef T2 type;
  121. };
  122. // Do not instantiate with reference types
  123. template<class T, class Any> struct parameter_traits_<T&, Any> {
  124. typedef typename
  125. generate_error<T&>::
  126. parameter_traits_class_instantiated_with_reference_type type;
  127. };
  128. // Arrays can't be stored as plain types; convert them to references
  129. template<class T, int n, class Any> struct parameter_traits_<T[n], Any> {
  130. typedef T (&type)[n];
  131. };
  132. template<class T, int n, class Any>
  133. struct parameter_traits_<const T[n], Any> {
  134. typedef const T (&type)[n];
  135. };
  136. template<class T, int n, class Any>
  137. struct parameter_traits_<volatile T[n], Any> {
  138. typedef volatile T (&type)[n];
  139. };
  140. template<class T, int n, class Any>
  141. struct parameter_traits_<const volatile T[n], Any> {
  142. typedef const volatile T (&type)[n];
  143. };
  144. template<class T, class Any>
  145. struct parameter_traits_<boost::reference_wrapper<T>, Any >{
  146. typedef T& type;
  147. };
  148. template<class T, class Any>
  149. struct parameter_traits_<const boost::reference_wrapper<T>, Any >{
  150. typedef T& type;
  151. };
  152. template<class T, class Any>
  153. struct parameter_traits_<volatile boost::reference_wrapper<T>, Any >{
  154. typedef T& type;
  155. };
  156. template<class T, class Any>
  157. struct parameter_traits_<const volatile boost::reference_wrapper<T>, Any >{
  158. typedef T& type;
  159. };
  160. template<class Any>
  161. struct parameter_traits_<void, Any> {
  162. typedef void type;
  163. };
  164. template<class Arg, class Any>
  165. struct parameter_traits_<lambda_functor<Arg>, Any > {
  166. typedef lambda_functor<Arg> type;
  167. };
  168. template<class Arg, class Any>
  169. struct parameter_traits_<const lambda_functor<Arg>, Any > {
  170. typedef lambda_functor<Arg> type;
  171. };
  172. // Are the volatile versions needed?
  173. template<class Arg, class Any>
  174. struct parameter_traits_<volatile lambda_functor<Arg>, Any > {
  175. typedef lambda_functor<Arg> type;
  176. };
  177. template<class Arg, class Any>
  178. struct parameter_traits_<const volatile lambda_functor<Arg>, Any > {
  179. typedef lambda_functor<Arg> type;
  180. };
  181. } // end namespace detail
  182. // ------------------------------------------------------------------------
  183. // traits classes for lambda expressions (bind functions, operators ...)
  184. // must be instantiated with non-reference types
  185. // The default is const plain type -------------------------
  186. // const T -> const T,
  187. // T -> const T,
  188. // references -> compile_time_error
  189. // reference_wrapper<T> -> T&
  190. // array -> const ref array
  191. template<class T>
  192. struct const_copy_argument {
  193. typedef typename
  194. detail::parameter_traits_<
  195. T,
  196. typename detail::IF<boost::is_function<T>::value, T&, const T>::RET
  197. >::type type;
  198. };
  199. // T may be a function type. Without the IF test, const would be added
  200. // to a function type, which is illegal.
  201. // all arrays are converted to const.
  202. // This traits template is used for 'const T&' parameter passing
  203. // and thus the knowledge of the potential
  204. // non-constness of an actual argument is lost.
  205. template<class T, int n> struct const_copy_argument <T[n]> {
  206. typedef const T (&type)[n];
  207. };
  208. template<class T, int n> struct const_copy_argument <volatile T[n]> {
  209. typedef const volatile T (&type)[n];
  210. };
  211. template<class T>
  212. struct const_copy_argument<T&> {};
  213. // do not instantiate with references
  214. // typedef typename detail::generate_error<T&>::references_not_allowed type;
  215. template<>
  216. struct const_copy_argument<void> {
  217. typedef void type;
  218. };
  219. template<>
  220. struct const_copy_argument<void const> {
  221. typedef void type;
  222. };
  223. // Does the same as const_copy_argument, but passes references through as such
  224. template<class T>
  225. struct bound_argument_conversion {
  226. typedef typename const_copy_argument<T>::type type;
  227. };
  228. template<class T>
  229. struct bound_argument_conversion<T&> {
  230. typedef T& type;
  231. };
  232. // The default is non-const reference -------------------------
  233. // const T -> const T&,
  234. // T -> T&,
  235. // references -> compile_time_error
  236. // reference_wrapper<T> -> T&
  237. template<class T>
  238. struct reference_argument {
  239. typedef typename detail::parameter_traits_<T, T&>::type type;
  240. };
  241. template<class T>
  242. struct reference_argument<T&> {
  243. typedef typename detail::generate_error<T&>::references_not_allowed type;
  244. };
  245. template<class Arg>
  246. struct reference_argument<lambda_functor<Arg> > {
  247. typedef lambda_functor<Arg> type;
  248. };
  249. template<class Arg>
  250. struct reference_argument<const lambda_functor<Arg> > {
  251. typedef lambda_functor<Arg> type;
  252. };
  253. // Are the volatile versions needed?
  254. template<class Arg>
  255. struct reference_argument<volatile lambda_functor<Arg> > {
  256. typedef lambda_functor<Arg> type;
  257. };
  258. template<class Arg>
  259. struct reference_argument<const volatile lambda_functor<Arg> > {
  260. typedef lambda_functor<Arg> type;
  261. };
  262. template<>
  263. struct reference_argument<void> {
  264. typedef void type;
  265. };
  266. namespace detail {
  267. // Array to pointer conversion
  268. template <class T>
  269. struct array_to_pointer {
  270. typedef T type;
  271. };
  272. template <class T, int N>
  273. struct array_to_pointer <const T[N]> {
  274. typedef const T* type;
  275. };
  276. template <class T, int N>
  277. struct array_to_pointer <T[N]> {
  278. typedef T* type;
  279. };
  280. template <class T, int N>
  281. struct array_to_pointer <const T (&) [N]> {
  282. typedef const T* type;
  283. };
  284. template <class T, int N>
  285. struct array_to_pointer <T (&) [N]> {
  286. typedef T* type;
  287. };
  288. // ---------------------------------------------------------------------------
  289. // The call_traits for bind
  290. // Respects the reference_wrapper class.
  291. // These templates are used outside of bind functions as well.
  292. // the bind_tuple_mapper provides a shorter notation for default
  293. // bound argument storing semantics, if all arguments are treated
  294. // uniformly.
  295. // from template<class T> foo(const T& t) : bind_traits<const T>::type
  296. // from template<class T> foo(T& t) : bind_traits<T>::type
  297. // Conversions:
  298. // T -> const T,
  299. // cv T -> cv T,
  300. // T& -> T&
  301. // reference_wrapper<T> -> T&
  302. // const reference_wrapper<T> -> T&
  303. // array -> const ref array
  304. // make bound arguments const, this is a deliberate design choice, the
  305. // purpose is to prevent side effects to bound arguments that are stored
  306. // as copies
  307. template<class T>
  308. struct bind_traits {
  309. typedef const T type;
  310. };
  311. template<class T>
  312. struct bind_traits<T&> {
  313. typedef T& type;
  314. };
  315. // null_types are an exception, we always want to store them as non const
  316. // so that other templates can assume that null_type is always without const
  317. template<>
  318. struct bind_traits<null_type> {
  319. typedef null_type type;
  320. };
  321. // the bind_tuple_mapper, bind_type_generators may
  322. // introduce const to null_type
  323. template<>
  324. struct bind_traits<const null_type> {
  325. typedef null_type type;
  326. };
  327. // Arrays can't be stored as plain types; convert them to references.
  328. // All arrays are converted to const. This is because bind takes its
  329. // parameters as const T& and thus the knowledge of the potential
  330. // non-constness of actual argument is lost.
  331. template<class T, int n> struct bind_traits <T[n]> {
  332. typedef const T (&type)[n];
  333. };
  334. template<class T, int n>
  335. struct bind_traits<const T[n]> {
  336. typedef const T (&type)[n];
  337. };
  338. template<class T, int n> struct bind_traits<volatile T[n]> {
  339. typedef const volatile T (&type)[n];
  340. };
  341. template<class T, int n>
  342. struct bind_traits<const volatile T[n]> {
  343. typedef const volatile T (&type)[n];
  344. };
  345. template<class R>
  346. struct bind_traits<R()> {
  347. typedef R(&type)();
  348. };
  349. template<class R, class Arg1>
  350. struct bind_traits<R(Arg1)> {
  351. typedef R(&type)(Arg1);
  352. };
  353. template<class R, class Arg1, class Arg2>
  354. struct bind_traits<R(Arg1, Arg2)> {
  355. typedef R(&type)(Arg1, Arg2);
  356. };
  357. template<class R, class Arg1, class Arg2, class Arg3>
  358. struct bind_traits<R(Arg1, Arg2, Arg3)> {
  359. typedef R(&type)(Arg1, Arg2, Arg3);
  360. };
  361. template<class R, class Arg1, class Arg2, class Arg3, class Arg4>
  362. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4)> {
  363. typedef R(&type)(Arg1, Arg2, Arg3, Arg4);
  364. };
  365. template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
  366. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5)> {
  367. typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5);
  368. };
  369. template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
  370. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)> {
  371. typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
  372. };
  373. template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
  374. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7)> {
  375. typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
  376. };
  377. template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7, class Arg8>
  378. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8)> {
  379. typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8);
  380. };
  381. template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7, class Arg8, class Arg9>
  382. struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9)> {
  383. typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9);
  384. };
  385. template<class T>
  386. struct bind_traits<reference_wrapper<T> >{
  387. typedef T& type;
  388. };
  389. template<class T>
  390. struct bind_traits<const reference_wrapper<T> >{
  391. typedef T& type;
  392. };
  393. template<>
  394. struct bind_traits<void> {
  395. typedef void type;
  396. };
  397. template <
  398. class T0 = null_type, class T1 = null_type, class T2 = null_type,
  399. class T3 = null_type, class T4 = null_type, class T5 = null_type,
  400. class T6 = null_type, class T7 = null_type, class T8 = null_type,
  401. class T9 = null_type
  402. >
  403. struct bind_tuple_mapper {
  404. typedef
  405. tuple<typename bind_traits<T0>::type,
  406. typename bind_traits<T1>::type,
  407. typename bind_traits<T2>::type,
  408. typename bind_traits<T3>::type,
  409. typename bind_traits<T4>::type,
  410. typename bind_traits<T5>::type,
  411. typename bind_traits<T6>::type,
  412. typename bind_traits<T7>::type,
  413. typename bind_traits<T8>::type,
  414. typename bind_traits<T9>::type> type;
  415. };
  416. // bind_traits, except map const T& -> const T
  417. // this is needed e.g. in currying. Const reference arguments can
  418. // refer to temporaries, so it is not safe to store them as references.
  419. template <class T> struct remove_const_reference {
  420. typedef typename bind_traits<T>::type type;
  421. };
  422. template <class T> struct remove_const_reference<const T&> {
  423. typedef const T type;
  424. };
  425. // maps the bind argument types to the resulting lambda functor type
  426. template <
  427. class T0 = null_type, class T1 = null_type, class T2 = null_type,
  428. class T3 = null_type, class T4 = null_type, class T5 = null_type,
  429. class T6 = null_type, class T7 = null_type, class T8 = null_type,
  430. class T9 = null_type
  431. >
  432. class bind_type_generator {
  433. typedef typename
  434. detail::bind_tuple_mapper<
  435. T0, T1, T2, T3, T4, T5, T6, T7, T8, T9
  436. >::type args_t;
  437. BOOST_STATIC_CONSTANT(int, nof_elems = boost::tuples::length<args_t>::value);
  438. typedef
  439. action<
  440. nof_elems,
  441. function_action<nof_elems>
  442. > action_type;
  443. public:
  444. typedef
  445. lambda_functor<
  446. lambda_functor_base<
  447. action_type,
  448. args_t
  449. >
  450. > type;
  451. };
  452. } // detail
  453. template <class T> inline const T& make_const(const T& t) { return t; }
  454. } // end of namespace lambda
  455. } // end of namespace boost
  456. #endif // BOOST_LAMBDA_TRAITS_HPP