Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66763 - in sandbox/SOC/2010/phoenix3: boost/phoenix boost/phoenix/statement libs/phoenix/test/statement
From: thom.heller_at_[hidden]
Date: 2010-11-26 02:33:53


Author: theller
Date: 2010-11-26 02:33:48 EST (Fri, 26 Nov 2010)
New Revision: 66763
URL: http://svn.boost.org/trac/boost/changeset/66763

Log:
switch refactoring temporary commit
Text files modified:
   sandbox/SOC/2010/phoenix3/boost/phoenix/statement.hpp | 2
   sandbox/SOC/2010/phoenix3/boost/phoenix/statement/switch.hpp | 302 +++++++++++++++++++++------------------
   sandbox/SOC/2010/phoenix3/libs/phoenix/test/statement/switch_tests.cpp | 11 +
   3 files changed, 170 insertions(+), 145 deletions(-)

Modified: sandbox/SOC/2010/phoenix3/boost/phoenix/statement.hpp
==============================================================================
--- sandbox/SOC/2010/phoenix3/boost/phoenix/statement.hpp (original)
+++ sandbox/SOC/2010/phoenix3/boost/phoenix/statement.hpp 2010-11-26 02:33:48 EST (Fri, 26 Nov 2010)
@@ -12,7 +12,7 @@
 #include <boost/phoenix/statement/for.hpp>
 #include <boost/phoenix/statement/if.hpp>
 #include <boost/phoenix/statement/sequence.hpp>
-//#include <boost/phoenix/statement/switch.hpp>
+#include <boost/phoenix/statement/switch.hpp>
 //#include <boost/phoenix/statement/throw.hpp>
 //#include <boost/phoenix/statement/try_catch.hpp>
 #include <boost/phoenix/statement/while.hpp>

Modified: sandbox/SOC/2010/phoenix3/boost/phoenix/statement/switch.hpp
==============================================================================
--- sandbox/SOC/2010/phoenix3/boost/phoenix/statement/switch.hpp (original)
+++ sandbox/SOC/2010/phoenix3/boost/phoenix/statement/switch.hpp 2010-11-26 02:33:48 EST (Fri, 26 Nov 2010)
@@ -10,186 +10,204 @@
 
 #include <boost/phoenix/core/limits.hpp>
 #include <boost/phoenix/core/meta_grammar.hpp>
-#include <boost/phoenix/core/compose.hpp>
+#include <boost/phoenix/core/expression.hpp>
+#include <boost/phoenix/core/is_nullary.hpp>
 #include <boost/phoenix/support/iterate.hpp>
 #include <boost/proto/proto_fwd.hpp>
 #include <boost/proto/make_expr.hpp>
 
 namespace boost { namespace phoenix
 {
- struct switch_case_tag {};
- struct switch_default_tag {};
+ namespace tag
+ {
+ struct switch_case {};
+ struct switch_default_case {};
+ }
 
- namespace detail
+ namespace expression
     {
- struct case_label
- : proto::terminal<proto::_>
+ template <typename N, typename A>
+ struct switch_case
+ : proto::binary_expr<tag::switch_case, N, A>
         {};
-
- struct case_statement
- : proto::binary_expr<switch_case_tag, case_label, eval_grammar>
+
+ template <typename A>
+ struct switch_default_case
+ : proto::unary_expr<tag::switch_default_case, A>
         {};
+ }
 
- struct default_statement
- : proto::unary_expr<switch_default_tag, eval_grammar>
+ namespace rule
+ {
+ struct switch_case
+ : expression::switch_case<
+ proto::terminal<proto::_>
+ , meta_grammar
+ >
         {};
-
- struct switch_size
+
+ struct switch_default_case
+ : expression::switch_default_case<
+ meta_grammar
+ >
+ {};
+ }
+ namespace detail
+ {
+ struct switch_case_grammar;
+ struct switch_case_with_default_grammar;
+ struct switch_grammar
             : proto::or_<
- proto::when<
- proto::comma<switch_size, proto::_>
- , mpl::next<switch_size(proto::_left)>()
- >
- , proto::when<proto::_, mpl::int_<1>()>
+ detail::switch_case_grammar
+ , detail::switch_case_with_default_grammar
>
         {};
+ }
 
- struct switch_has_default
+ PHOENIX_DEFINE_EXPRESSION(
+ switch_
+ , (meta_grammar) // Cond
+ (detail::switch_grammar) // Cases
+ )
+
+ namespace detail
+ {
+ struct switch_case_is_nullary
             : proto::or_<
                 proto::when<
- proto::comma<switch_size, case_statement>
- , mpl::false_()
+ proto::comma<
+ switch_case_is_nullary
+ , rule::switch_default_case
+ >
+ , mpl::and_<
+ switch_case_is_nullary(proto::_child_c<0>, proto::_state)
+ , evaluator(proto::_child_c<0>(proto::_child_c<1>), proto::_state)
+ >()
>
               , proto::when<
- proto::comma<switch_size, default_statement>
- , mpl::true_()
+ proto::comma<
+ switch_case_is_nullary
+ , rule::switch_case
+ >
+ , mpl::and_<
+ switch_case_is_nullary(proto::_child_c<0>, proto::_state)
+ , evaluator(proto::_child_c<1>(proto::_child_c<1>), proto::_state)
+ >()
>
               , proto::when<
- case_statement
- , mpl::false_()
+ rule::switch_default_case
+ , evaluator(proto::_child_c<0>, proto::_state)
>
               , proto::when<
- default_statement
- , mpl::true_()
+ rule::switch_case
+ , evaluator(proto::_child_c<1>, proto::_state)
>
>
         {};
 
- struct switch_case_statement
- : proto::or_<
- proto::when<
- proto::comma<switch_case_statement, proto::_>
- , proto::if_<
- is_same<mpl::prior<proto::_data>(), proto::_state>()
- , proto::_right
- , switch_case_statement(proto::_left, proto::_state, mpl::prior<proto::_data>())
- >
- >
- , proto::otherwise<proto::_>
+ template <typename Dummy>
+ struct is_nullary_::when<rule::switch_, Dummy>
+ : proto::make<
+ mpl::and_<
+ evaluator(proto::_child_c<0>, _env)
+ , switch_case_is_nullary(proto::_child_c<1>, _env)
+ >()
>
         {};
- }
-
- namespace result_of
- {
- template <typename Cases>
- struct cases_size : boost::result_of<detail::switch_size(Cases)> {};
-
- template <typename Cases, typename N>
- struct case_compound
- : mpl::eval_if<
- mpl::less<N, typename cases_size<Cases>::type >
- , boost::result_of<detail::switch_case_statement(Cases, N, typename cases_size<Cases>::type)>
- , mpl::void_
+ struct switch_case_grammar
+ : proto::or_<
+ proto::comma<switch_case_grammar, rule::switch_case>
+ , rule::switch_case
>
         {};
 
- template <typename Case>
- struct case_statement
- : proto::result_of::right<Case>
- {};
-
- template <typename Case>
- struct case_label
- : proto::result_of::value<
- typename proto::result_of::left<
- Case
- >::type
+ struct switch_case_with_default_grammar
+ : proto::or_<
+ proto::comma<switch_case_grammar, rule::switch_default_case>
+ , rule::switch_default_case
>
         {};
-
- template <typename Cases>
- struct switch_has_default : boost::result_of<detail::switch_has_default(Cases)> {};
-
- template <typename Case>
- struct is_default_case
- : proto::matches<Case, detail::default_statement>
- {};
- }
-
- template <int N, typename Cases>
- typename result_of::case_compound<Cases, mpl::int_<N> >::type const &
- case_compound_c(Cases const& cases)
- {
- typename result_of::cases_size<Cases>::type size;
- return detail::switch_case_statement()(cases, mpl::int_<N>(), size);
- }
-
- template <typename N, typename Cases>
- typename result_of::case_compound<Cases, N>::type const &
- case_compound(Cases const& cases)
- {
- typename result_of::cases_size<Cases>::type size;
- return detail::switch_case_statement()(cases, N(), size);
     }
 
- template <typename Case>
- typename result_of::case_statement<Case>::type const &
- case_statement(Case const& case_)
+ struct switch_eval
     {
- return proto::right(case_);
- }
-
- template <
- typename Cond,
- typename Cases,
- int N = result_of::cases_size<Cases>::type::value,
- typename Dummy = void>
- struct make_switch;
-
- // Bring in the rest ....
- #include <boost/phoenix/statement/detail/switch.hpp>
+ typedef void result_type;
+
+ template <typename Env>
+ result_type
+ operator()(Env & env) const
+ {
+ }
 
- template <int N, typename A0>
- typename proto::result_of::make_expr<
- switch_case_tag
- , default_domain_with_basic_expr
- , mpl::int_<N>
- , A0
- >::type const
- case_(A0 const& a0)
- {
- typedef typename
- proto::result_of::make_expr<
- switch_case_tag
- , default_domain_with_basic_expr
- , mpl::int_<N>
- , A0
- >::type
- expr_type;
+ template <typename Env, typename Cond, typename Cases>
+ result_type
+ operator()(Env & env, Cond const & cond, Cases const & cases) const
+ {
+ /*
+ typedef typename fusion::result_of::as_vector<Cases>::type as_vector;
+ std::cout << typeid(Cases).name() << "\n";
+ std::cout << fusion::result_of::size<Cases>::value << "\n";
+ std::cout << typeid(typename fusion::result_of::at_c<as_vector, 0>::type).name() << "\n";
+ std::cout << typeid(typename proto::matches<typename Cases::expr_type, detail::switch_case_with_default_grammar>::type).name() << "\n";
+ */
+ //std::cout << typeid(typename fusion::result_of::at_c<flattened_expr, 2>::type).name() << "\n";
+ this->evaluate(env, cond, fusion::as_vector(cases), mpl::int_<fusion::result_of::size<Cases>::value>(), typename proto::matches<typename Cases::expr_type, detail::switch_case_with_default_grammar>::type());
+ }
 
- expr_type const e = {mpl::int_<N>(), a0};
- return e;
- }
+ //proto::result_of::child_c<typename fusion::result_of::at_c<Cases, N>::type, 0>::type::value :
+ private:
+ #define PHOENIX_SWITCH_EVAL_R(Z, N, DATA) \
+ case \
+ proto::value< \
+ typename proto::result_of::child_c<\
+ typename fusion::result_of::at_c<Cases, N>::type \
+ , 0 \
+ >::type \
+ >::type::value : \
+ eval(proto::child_c<1>(fusion::at_c<N>(cases)), env);
+ /**/
+ #define PHOENIX_SWITCH_EVAL(Z, N, DATA) \
+ template <typename Env, typename Cond, typename Cases> \
+ result_type evaluate(Env & env, Cond const & cond, Cases const & cases, mpl::int_<N>, mpl::false_) const\
+ { \
+ } \
+ \
+ template <typename Env, typename Cond, typename Cases> \
+ result_type evaluate(Env & env, Cond const & cond, Cases const & cases, mpl::int_<N>, mpl::true_) const\
+ { \
+ switch(eval(cond, env)) \
+ { \
+ BOOST_PP_REPEAT(BOOST_PP_DEC(N), PHOENIX_SWITCH_EVAL_R, _) \
+ default: eval(fusion::at_c<N-1>(cases), env); \
+ } \
+ } \
+ /**/
+ BOOST_PP_REPEAT(PHOENIX_LIMIT, PHOENIX_SWITCH_EVAL, _)
+ };
     
- template <typename A0>
- typename proto::result_of::make_expr<
- switch_default_tag
- , default_domain_with_basic_expr
- , A0
- >::type const
- default_(A0 const& a0)
- {
- typedef typename
- proto::result_of::make_expr<
- switch_default_tag
- , default_domain_with_basic_expr
- , A0
- >::type
- expr_type;
-
- expr_type const e = {a0};
- return e;
+ template <typename Dummy>
+ struct default_actions::when<rule::switch_, Dummy>
+ : proto::call<
+ switch_eval(
+ _env
+ , proto::_child_c<0> // Cond
+ , proto::functional::flatten(proto::_child_c<1>) // Cases
+ )
+ >
+ {};
+
+ template <int N, typename A>
+ typename proto::result_of::make_expr<tag::switch_case, default_domain_with_basic_expr, mpl::int_<N>, A>::type const
+ case_(A const & a)
+ {
+ return proto::make_expr<tag::switch_case, default_domain_with_basic_expr>(mpl::int_<N>(), a);
+ }
+
+ template <typename A>
+ typename proto::result_of::make_expr<tag::switch_default_case, default_domain_with_basic_expr, A>::type const
+ default_(A const& a)
+ {
+ return proto::make_expr<tag::switch_default_case, default_domain_with_basic_expr>(a);
     }
 
     template <typename Cond>
@@ -198,15 +216,15 @@
         switch_gen(Cond const& cond) : cond(cond) {}
 
         template <typename Cases>
- typename make_switch<
+ typename expression::switch_<
             Cond
           , Cases
>::type
         operator[](Cases const& cases) const
         {
- BOOST_MPL_ASSERT((proto::matches<Cases, detail::switch_case_statement>));
+ BOOST_MPL_ASSERT((proto::matches<Cases, detail::switch_grammar>));
 
- return make_switch<Cond, Cases>()(cond, cases);
+ return expression::switch_<Cond, Cases>::make(cond, cases);
         }
 
         Cond const& cond;

Modified: sandbox/SOC/2010/phoenix3/libs/phoenix/test/statement/switch_tests.cpp
==============================================================================
--- sandbox/SOC/2010/phoenix3/libs/phoenix/test/statement/switch_tests.cpp (original)
+++ sandbox/SOC/2010/phoenix3/libs/phoenix/test/statement/switch_tests.cpp 2010-11-26 02:33:48 EST (Fri, 26 Nov 2010)
@@ -4,20 +4,23 @@
     Distributed under the Boost Software License, Version 1.0. (See accompanying
     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 ==============================================================================*/
+
+#define PHOENIX_LIMIT 10
+
 #include <iostream>
 #include <vector>
 #include <algorithm>
 #include <boost/detail/lightweight_test.hpp>
+#include <boost/phoenix/core.hpp>
 #include <boost/phoenix/statement.hpp>
 #include <boost/phoenix/operator.hpp>
-#include <boost/phoenix/core.hpp>
 
 int
 main()
 {
     using boost::phoenix::arg_names::_1;
     using boost::phoenix::case_;
- using boost::phoenix::default_;
+ //using boost::phoenix::default_;
     using boost::phoenix::switch_;
     using boost::phoenix::ref;
     using boost::phoenix::val;
@@ -30,6 +33,7 @@
     int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
     vector<int> v(init, init+10);
 
+ /*
     for_each(v.begin(), v.end(),
         switch_(_1)
         [
@@ -37,6 +41,7 @@
             case_<4>(cout << val("<4>") << endl)
         ]
     );
+ */
 
     cout << endl;
     for_each(v.begin(), v.end(),
@@ -49,6 +54,7 @@
 
     cout << endl;
     
+ /*
     for_each(v.begin(), v.end(),
         switch_(_1)
         [
@@ -58,6 +64,7 @@
             case_<4>(cout << val("<4>") << endl)
         ]
     );
+ */
 
     cout << endl;
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk