switch_tests.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/statement.hpp>
  12. #include <boost/phoenix/operator.hpp>
  13. int
  14. main()
  15. {
  16. using boost::phoenix::arg_names::_1;
  17. using boost::phoenix::case_;
  18. using boost::phoenix::switch_;
  19. using boost::phoenix::ref;
  20. using boost::phoenix::val;
  21. using std::cout;
  22. using std::endl;
  23. using std::vector;
  24. using std::for_each;
  25. int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  26. vector<int> v(init, init+10);
  27. for_each(v.begin(), v.end(),
  28. switch_(_1)
  29. [
  30. // wierd case, why not just use if(...), but valid, nonetheless
  31. case_<4>(cout << val("<4>") << endl)
  32. ]
  33. );
  34. cout << endl;
  35. for_each(v.begin(), v.end(),
  36. switch_(_1)
  37. [
  38. // wierd case, but valid, nonetheless
  39. default_(cout << val("<any...>") << endl)
  40. ]
  41. );
  42. cout << endl;
  43. for_each(v.begin(), v.end(),
  44. switch_(_1)
  45. [
  46. case_<1>(cout << val("<1>") << endl),
  47. case_<2>(cout << val("<2>") << endl),
  48. case_<3>(cout << val("<3>") << endl),
  49. case_<4>(cout << val("<4>") << endl)
  50. ]
  51. );
  52. cout << endl;
  53. for_each(v.begin(), v.end(),
  54. switch_(_1)
  55. [
  56. case_<1>(cout << val("<1>") << endl),
  57. case_<2>(cout << val("<2>") << endl),
  58. case_<3>(cout << val("<3>") << endl),
  59. case_<4>(cout << val("<4>") << endl),
  60. default_(cout << val("<over 4>") << endl)
  61. ]
  62. );
  63. return boost::report_errors();
  64. }