container_actor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*==============================================================================
  2. Copyright (c) 2005-2010 Joel de Guzman
  3. Copyright (c) 2010 Thomas Heller
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/phoenix/core.hpp>
  8. #include <boost/phoenix/function.hpp>
  9. #include <boost/phoenix/stl/container.hpp>
  10. #include <boost/phoenix/stl/algorithm.hpp>
  11. #include <vector>
  12. #include <iostream>
  13. namespace phoenix = boost::phoenix;
  14. using phoenix::actor;
  15. using phoenix::function;
  16. using phoenix::arg_names::arg1;
  17. struct size_impl
  18. {
  19. // result_of protocol:
  20. template <typename Sig>
  21. struct result;
  22. template <typename This, typename Container>
  23. struct result<This(Container)>
  24. {
  25. // Note, remove reference here, because Container can be anything
  26. typedef typename boost::remove_reference<Container>::type container_type;
  27. // The result will be size_type
  28. typedef typename container_type::size_type type;
  29. };
  30. template <typename Container>
  31. typename result<size_impl(Container const&)>::type
  32. operator()(Container const& container) const
  33. {
  34. return container.size();
  35. }
  36. };
  37. template <typename Expr>
  38. struct container_actor
  39. : actor<Expr>
  40. {
  41. typedef actor<Expr> base_type;
  42. typedef container_actor<Expr> that_type;
  43. container_actor( base_type const& base = base_type() )
  44. : base_type( base ) {}
  45. typename phoenix::expression::function<phoenix::stl::begin, that_type>::type const
  46. begin() const
  47. {
  48. return phoenix::begin(*this);
  49. }
  50. typename phoenix::expression::function<phoenix::stl::end, that_type>::type const
  51. end() const
  52. {
  53. return phoenix::end(*this);
  54. }
  55. typename phoenix::expression::function<size_impl, that_type>::type const
  56. size() const
  57. {
  58. function<size_impl> const f = size_impl();
  59. return f(*this);
  60. }
  61. typename phoenix::expression::function<phoenix::stl::max_size, that_type>::type const
  62. max_size() const
  63. {
  64. return phoenix::max_size(*this);
  65. }
  66. typename phoenix::expression::function<phoenix::stl::empty, that_type>::type const
  67. empty() const
  68. {
  69. return phoenix::empty(*this);
  70. }
  71. template <typename Container>
  72. typename phoenix::expression::function<phoenix::impl::swap, that_type, Container>::type const
  73. swap(actor<Container> const& expr) const
  74. {
  75. return phoenix::swap(*this, expr);
  76. }
  77. };
  78. template <typename Expr>
  79. container_actor<Expr> const
  80. container( actor<Expr> const& expr )
  81. {
  82. return expr;
  83. }
  84. int main()
  85. {
  86. container_actor<phoenix::expression::argument<1>::type> const con1;
  87. std::vector<int> v;
  88. v.push_back(0);
  89. v.push_back(1);
  90. v.push_back(2);
  91. v.push_back(3);
  92. std::cout << (container(arg1).size())(v) << " == " << v.size() << "\n";
  93. std::cout << (con1.size())(v) << " == " << v.size() << "\n";
  94. }