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