preprocessor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // Copyright Daniel Wallin 2006.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/parameter/preprocessor.hpp>
  6. #include <boost/parameter/binding.hpp>
  7. #include <boost/parameter/config.hpp>
  8. #include "basics.hpp"
  9. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  10. #include <type_traits>
  11. #else
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/mpl/if.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #endif
  16. namespace test {
  17. BOOST_PARAMETER_BASIC_FUNCTION((int), f, test::tag,
  18. (required
  19. (tester, *)
  20. (name, *)
  21. )
  22. (optional
  23. (value, *)
  24. (index, (int))
  25. )
  26. )
  27. {
  28. typedef typename boost::parameter::binding<
  29. Args,test::tag::index,int&
  30. >::type index_type;
  31. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  32. static_assert(
  33. std::is_same<index_type,int&>::value
  34. , "index_type == int&"
  35. );
  36. #else
  37. BOOST_MPL_ASSERT((
  38. typename boost::mpl::if_<
  39. boost::is_same<index_type,int&>
  40. , boost::mpl::true_
  41. , boost::mpl::false_
  42. >::type
  43. ));
  44. #endif
  45. args[test::_tester](
  46. args[test::_name]
  47. , args[test::_value | 1.f]
  48. , args[test::_index | 2]
  49. );
  50. return 1;
  51. }
  52. } // namespace test
  53. #include <boost/parameter/value_type.hpp>
  54. #if !defined(BOOST_PARAMETER_CAN_USE_MP11)
  55. #include <boost/type_traits/remove_const.hpp>
  56. #endif
  57. namespace test {
  58. BOOST_PARAMETER_BASIC_FUNCTION((int), g, test::tag,
  59. (required
  60. (tester, *)
  61. (name, *)
  62. )
  63. (optional
  64. (value, *)
  65. (index, (int))
  66. )
  67. )
  68. {
  69. typedef typename boost::parameter::value_type<
  70. Args,test::tag::index,int
  71. >::type index_type;
  72. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  73. static_assert(
  74. std::is_same<
  75. typename std::remove_const<index_type>::type
  76. , int
  77. >::value
  78. , "remove_const<index_type>::type == int"
  79. );
  80. #else
  81. BOOST_MPL_ASSERT((
  82. typename boost::mpl::if_<
  83. boost::is_same<
  84. typename boost::remove_const<index_type>::type
  85. , int
  86. >
  87. , boost::mpl::true_
  88. , boost::mpl::false_
  89. >::type
  90. ));
  91. #endif
  92. args[test::_tester](
  93. args[test::_name]
  94. , args[test::_value | 1.f]
  95. , args[test::_index | 2]
  96. );
  97. return 1;
  98. }
  99. } // namespace test
  100. #if !defined(BOOST_PARAMETER_CAN_USE_MP11)
  101. #include <boost/type_traits/remove_reference.hpp>
  102. #endif
  103. namespace test {
  104. BOOST_PARAMETER_FUNCTION((int), h, test::tag,
  105. (required
  106. (tester, *)
  107. (name, *)
  108. )
  109. (optional
  110. (value, *, 1.f)
  111. (index, (int), 2)
  112. )
  113. )
  114. {
  115. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  116. static_assert(
  117. std::is_same<
  118. typename std::remove_const<
  119. typename std::remove_reference<index_type>::type
  120. >::type
  121. , int
  122. >::value
  123. , "remove_cref<index_type>::type == int"
  124. );
  125. #elif !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) && \
  126. !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  127. BOOST_MPL_ASSERT((
  128. typename boost::mpl::if_<
  129. boost::is_same<
  130. typename boost::remove_const<
  131. typename boost::remove_reference<index_type>::type
  132. >::type
  133. , int
  134. >
  135. , boost::mpl::true_
  136. , boost::mpl::false_
  137. >::type
  138. ));
  139. #endif // BOOST_PARAMETER_CAN_USE_MP11 || Borland/MSVC workarounds not needed
  140. tester(name, value, index);
  141. return 1;
  142. }
  143. BOOST_PARAMETER_FUNCTION((int), h2, test::tag,
  144. (required
  145. (tester, *)
  146. (name, *)
  147. )
  148. (optional
  149. (value, *, 1.f)
  150. (index, (int), static_cast<int>(value * 2))
  151. )
  152. )
  153. {
  154. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  155. static_assert(
  156. std::is_same<
  157. typename std::remove_const<
  158. typename std::remove_reference<index_type>::type
  159. >::type
  160. , int
  161. >::value
  162. , "remove_cref<index_type>::type == int"
  163. );
  164. #elif !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) && \
  165. !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  166. BOOST_MPL_ASSERT((
  167. typename boost::mpl::if_<
  168. boost::is_same<
  169. typename boost::remove_const<
  170. typename boost::remove_reference<index_type>::type
  171. >::type
  172. , int
  173. >
  174. , boost::mpl::true_
  175. , boost::mpl::false_
  176. >::type
  177. ));
  178. #endif // BOOST_PARAMETER_CAN_USE_MP11 || Borland/MSVC workarounds not needed
  179. tester(name, value, index);
  180. return 1;
  181. }
  182. } // namespace test
  183. #include <string>
  184. #if !defined(BOOST_NO_SFINAE)
  185. #include <boost/parameter/aux_/preprocessor/nullptr.hpp>
  186. #include <boost/core/enable_if.hpp>
  187. #if !defined(BOOST_PARAMETER_CAN_USE_MP11)
  188. #include <boost/type_traits/is_base_of.hpp>
  189. #include <boost/type_traits/is_convertible.hpp>
  190. #endif
  191. #endif
  192. namespace test {
  193. struct base_0
  194. {
  195. float f;
  196. int i;
  197. template <typename Args>
  198. explicit base_0(
  199. Args const& args
  200. #if !defined(BOOST_NO_SFINAE)
  201. , typename boost::disable_if<
  202. typename boost::mpl::if_<
  203. boost::is_base_of<base_0,Args>
  204. , boost::mpl::true_
  205. , boost::mpl::false_
  206. >::type
  207. >::type* = BOOST_PARAMETER_AUX_PP_NULLPTR
  208. #endif // BOOST_NO_SFINAE
  209. ) : f(args[test::_value | 1.f]), i(args[test::_index | 2])
  210. {
  211. }
  212. };
  213. struct class_0 : test::base_0
  214. {
  215. BOOST_PARAMETER_CONSTRUCTOR(class_0, (test::base_0), test::tag,
  216. (optional
  217. (value, *)
  218. (index, *)
  219. )
  220. )
  221. BOOST_PARAMETER_BASIC_FUNCTION_CALL_OPERATOR((int), test::tag,
  222. (optional
  223. (value, *)
  224. (index, *)
  225. )
  226. )
  227. {
  228. this->f = args[test::_value | 2.f];
  229. this->i = args[test::_index | 1];
  230. return 1;
  231. }
  232. };
  233. struct base_1
  234. {
  235. template <typename Args>
  236. explicit base_1(
  237. Args const& args
  238. #if !defined(BOOST_NO_SFINAE)
  239. , typename boost::disable_if<
  240. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  241. std::is_base_of<base_1,Args>
  242. #else
  243. typename boost::mpl::if_<
  244. boost::is_base_of<base_1,Args>
  245. , boost::mpl::true_
  246. , boost::mpl::false_
  247. >::type
  248. #endif
  249. >::type* = BOOST_PARAMETER_AUX_PP_NULLPTR
  250. #endif // BOOST_NO_SFINAE
  251. )
  252. {
  253. args[test::_tester](
  254. args[test::_name]
  255. , args[test::_value | 1.f]
  256. , args[test::_index | 2]
  257. );
  258. }
  259. };
  260. struct class_1 : test::base_1
  261. {
  262. BOOST_PARAMETER_CONSTRUCTOR(class_1, (test::base_1), test::tag,
  263. (required
  264. (tester, *)
  265. (name, *)
  266. )
  267. (optional
  268. (value, *)
  269. (index, *)
  270. )
  271. )
  272. BOOST_PARAMETER_BASIC_MEMBER_FUNCTION((int), f, test::tag,
  273. (required
  274. (tester, *)
  275. (name, *)
  276. )
  277. (optional
  278. (value, *)
  279. (index, *)
  280. )
  281. )
  282. {
  283. args[test::_tester](
  284. args[test::_name]
  285. , args[test::_value | 1.f]
  286. , args[test::_index | 2]
  287. );
  288. return 1;
  289. }
  290. BOOST_PARAMETER_BASIC_CONST_MEMBER_FUNCTION((int), f, test::tag,
  291. (required
  292. (tester, *)
  293. (name, *)
  294. )
  295. (optional
  296. (value, *)
  297. (index, *)
  298. )
  299. )
  300. {
  301. args[test::_tester](
  302. args[test::_name]
  303. , args[test::_value | 1.f]
  304. , args[test::_index | 2]
  305. );
  306. return 1;
  307. }
  308. BOOST_PARAMETER_MEMBER_FUNCTION((int), f2, test::tag,
  309. (required
  310. (tester, *)
  311. (name, *)
  312. )
  313. (optional
  314. (value, *, 1.f)
  315. (index, *, 2)
  316. )
  317. )
  318. {
  319. tester(name, value, index);
  320. return 1;
  321. }
  322. BOOST_PARAMETER_CONST_MEMBER_FUNCTION((int), f2, test::tag,
  323. (required
  324. (tester, *)
  325. (name, *)
  326. )
  327. (optional
  328. (value, *, 1.f)
  329. (index, *, 2)
  330. )
  331. )
  332. {
  333. tester(name, value, index);
  334. return 1;
  335. }
  336. BOOST_PARAMETER_MEMBER_FUNCTION((int), static f_static, test::tag,
  337. (required
  338. (tester, *)
  339. (name, *)
  340. )
  341. (optional
  342. (value, *, 1.f)
  343. (index, *, 2)
  344. )
  345. )
  346. {
  347. tester(name, value, index);
  348. return 1;
  349. }
  350. BOOST_PARAMETER_FUNCTION_CALL_OPERATOR((int), test::tag,
  351. (required
  352. (tester, *)
  353. (name, *)
  354. )
  355. (optional
  356. (value, *, 1.f)
  357. (index, *, 2)
  358. )
  359. )
  360. {
  361. tester(name, value, index);
  362. return 1;
  363. }
  364. BOOST_PARAMETER_CONST_FUNCTION_CALL_OPERATOR((int), test::tag,
  365. (required
  366. (tester, *)
  367. (name, *)
  368. )
  369. (optional
  370. (value, *, 1.f)
  371. (index, *, 2)
  372. )
  373. )
  374. {
  375. tester(name, value, index);
  376. return 1;
  377. }
  378. };
  379. BOOST_PARAMETER_FUNCTION((int), sfinae, test::tag,
  380. (required
  381. (name, (std::string))
  382. )
  383. )
  384. {
  385. return 1;
  386. }
  387. #if !defined(BOOST_NO_SFINAE)
  388. // On compilers that actually support SFINAE, add another overload
  389. // that is an equally good match and can only be in the overload set
  390. // when the others are not. This tests that the SFINAE is actually
  391. // working. On all other compilers we're just checking that everything
  392. // about SFINAE-enabled code will work, except of course the SFINAE.
  393. template <typename A0>
  394. typename boost::enable_if<
  395. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  396. std::is_same<int,A0>
  397. #else
  398. typename boost::mpl::if_<
  399. boost::is_same<int,A0>
  400. , boost::mpl::true_
  401. , boost::mpl::false_
  402. >::type
  403. #endif
  404. , int
  405. >::type
  406. sfinae(A0 const& a0)
  407. {
  408. return 0;
  409. }
  410. #endif // BOOST_NO_SFINAE
  411. struct predicate
  412. {
  413. template <typename T, typename Args>
  414. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  415. using fn = std::is_convertible<T,std::string>;
  416. #else
  417. struct apply
  418. : boost::mpl::if_<
  419. boost::is_convertible<T,std::string>
  420. , boost::mpl::true_
  421. , boost::mpl::false_
  422. >
  423. {
  424. };
  425. #endif
  426. BOOST_PARAMETER_BASIC_CONST_FUNCTION_CALL_OPERATOR((bool), test::tag,
  427. (required
  428. (value, *)
  429. (index, *)
  430. )
  431. )
  432. {
  433. return args[test::_value] < args[test::_index];
  434. }
  435. };
  436. BOOST_PARAMETER_FUNCTION((int), sfinae1, test::tag,
  437. (required
  438. (name, *(test::predicate))
  439. )
  440. )
  441. {
  442. return 1;
  443. }
  444. #if !defined(BOOST_NO_SFINAE)
  445. // On compilers that actually support SFINAE, add another overload
  446. // that is an equally good match and can only be in the overload set
  447. // when the others are not. This tests that the SFINAE is actually
  448. // working. On all other compilers we're just checking that everything
  449. // about SFINAE-enabled code will work, except of course the SFINAE.
  450. template <typename A0>
  451. typename boost::enable_if<
  452. #if defined(BOOST_PARAMETER_CAN_USE_MP11)
  453. std::is_same<int,A0>
  454. #else
  455. typename boost::mpl::if_<
  456. boost::is_same<int,A0>
  457. , boost::mpl::true_
  458. , boost::mpl::false_
  459. >::type
  460. #endif
  461. , int
  462. >::type
  463. sfinae1(A0 const& a0)
  464. {
  465. return 0;
  466. }
  467. #endif // BOOST_NO_SFINAE
  468. struct udt
  469. {
  470. udt(int foo_, int bar_) : foo(foo_), bar(bar_)
  471. {
  472. }
  473. int foo;
  474. int bar;
  475. };
  476. BOOST_PARAMETER_FUNCTION((int), lazy_defaults, test::tag,
  477. (required
  478. (name, *)
  479. )
  480. (optional
  481. (value, *, name.foo)
  482. (index, *, name.bar)
  483. )
  484. )
  485. {
  486. return 0;
  487. }
  488. } // namespace test
  489. #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
  490. #include <boost/parameter/aux_/as_lvalue.hpp>
  491. #endif
  492. #include <boost/core/lightweight_test.hpp>
  493. int main()
  494. {
  495. test::f(
  496. test::values(std::string("foo"), 1.f, 2)
  497. , std::string("foo")
  498. );
  499. test::f(
  500. test::_tester = test::values(std::string("foo"), 1.f, 2)
  501. , test::_name = std::string("foo")
  502. );
  503. int index_lvalue = 2;
  504. test::f(
  505. test::_tester = test::values(std::string("foo"), 1.f, 2)
  506. , test::_name = std::string("foo")
  507. , test::_value = 1.f
  508. , test::_index = index_lvalue
  509. );
  510. test::f(
  511. test::values(std::string("foo"), 1.f, 2)
  512. , std::string("foo")
  513. , 1.f
  514. , index_lvalue
  515. );
  516. test::g(
  517. test::values(std::string("foo"), 1.f, 2)
  518. , std::string("foo")
  519. , 1.f
  520. #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
  521. , boost::parameter::aux::as_lvalue(2)
  522. #else
  523. , 2
  524. #endif
  525. );
  526. test::h(
  527. test::values(std::string("foo"), 1.f, 2)
  528. , std::string("foo")
  529. , 1.f
  530. #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
  531. , boost::parameter::aux::as_lvalue(2)
  532. #else
  533. , 2
  534. #endif
  535. );
  536. test::h2(
  537. test::_tester = test::values(std::string("foo"), 1.f, 2)
  538. , test::_name = std::string("foo")
  539. , test::_value = 1.f
  540. );
  541. test::class_0 u;
  542. BOOST_TEST(2 == u.i);
  543. BOOST_TEST(1.f == u.f);
  544. u();
  545. BOOST_TEST(1 == u.i);
  546. BOOST_TEST(2.f == u.f);
  547. test::class_1 x(
  548. test::values(std::string("foo"), 1.f, 2)
  549. , std::string("foo")
  550. , test::_index = 2
  551. );
  552. x.f(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  553. x.f(
  554. test::_tester = test::values(std::string("foo"), 1.f, 2)
  555. , test::_name = std::string("foo")
  556. );
  557. x.f2(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  558. x.f2(
  559. test::_tester = test::values(std::string("foo"), 1.f, 2)
  560. , test::_name = std::string("foo")
  561. );
  562. x(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  563. x(
  564. test::_tester = test::values(std::string("foo"), 1.f, 2)
  565. , test::_name = std::string("foo")
  566. );
  567. test::class_1 const& x_const = x;
  568. x_const.f(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  569. x_const.f(
  570. test::_tester = test::values(std::string("foo"), 1.f, 2)
  571. , test::_name = std::string("foo")
  572. );
  573. x_const.f2(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  574. x_const.f2(
  575. test::_tester = test::values(std::string("foo"), 1.f, 2)
  576. , test::_name = std::string("foo")
  577. );
  578. test::class_1::f_static(
  579. test::values(std::string("foo"), 1.f, 2)
  580. , std::string("foo")
  581. );
  582. test::class_1::f_static(
  583. test::_tester = test::values(std::string("foo"), 1.f, 2)
  584. , test::_name = std::string("foo")
  585. );
  586. x_const(test::values(std::string("foo"), 1.f, 2), std::string("foo"));
  587. x_const(
  588. test::_tester = test::values(std::string("foo"), 1.f, 2)
  589. , test::_name = std::string("foo")
  590. );
  591. test::predicate p;
  592. test::predicate const& p_const = p;
  593. BOOST_TEST(p_const(3, 4));
  594. BOOST_TEST(!p_const(4, 3));
  595. BOOST_TEST(!p_const(test::_index = 3, test::_value = 4));
  596. #if !defined(BOOST_NO_SFINAE) && \
  597. !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
  598. // GCC 3- tries to bind string literals
  599. // to non-const references to char const*.
  600. // BOOST_TEST(test::sfinae("foo") == 1);
  601. char const* foo_str = "foo";
  602. BOOST_TEST(test::sfinae(foo_str) == 1);
  603. BOOST_TEST(test::sfinae(1) == 0);
  604. #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
  605. // Sun actually eliminates the desired overload for some reason.
  606. // Disabling this part of the test because SFINAE abilities are
  607. // not the point of this test.
  608. BOOST_TEST(test::sfinae1(foo_str) == 1);
  609. #endif
  610. BOOST_TEST(test::sfinae1(1) == 0);
  611. #endif
  612. test::lazy_defaults(test::_name = test::udt(0, 1));
  613. test::lazy_defaults(test::_name = 0, test::_value = 1, test::_index = 2);
  614. return boost::report_errors();
  615. }