Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66771 - in sandbox/SOC/2010/phoenix3: boost/phoenix/core boost/phoenix/statement libs/phoenix/test libs/phoenix/test/statement
From: thom.heller_at_[hidden]
Date: 2010-11-26 11:15:30


Author: theller
Date: 2010-11-26 11:15:22 EST (Fri, 26 Nov 2010)
New Revision: 66771
URL: http://svn.boost.org/trac/boost/changeset/66771

Log:
switch refactoring complete
Text files modified:
   sandbox/SOC/2010/phoenix3/boost/phoenix/core/function_equal.hpp | 85 +++++++++++++++++++++-------
   sandbox/SOC/2010/phoenix3/boost/phoenix/statement/switch.hpp | 117 +++++++++++++++++++++++++--------------
   sandbox/SOC/2010/phoenix3/libs/phoenix/test/Jamfile | 2
   sandbox/SOC/2010/phoenix3/libs/phoenix/test/statement/switch_tests.cpp | 5 -
   4 files changed, 139 insertions(+), 70 deletions(-)

Modified: sandbox/SOC/2010/phoenix3/boost/phoenix/core/function_equal.hpp
==============================================================================
--- sandbox/SOC/2010/phoenix3/boost/phoenix/core/function_equal.hpp (original)
+++ sandbox/SOC/2010/phoenix3/boost/phoenix/core/function_equal.hpp 2010-11-26 11:15:22 EST (Fri, 26 Nov 2010)
@@ -9,6 +9,9 @@
 #define PHOENIX_CORE_FUNCTION_EQUAL_HPP
 
 #include <boost/phoenix/core/terminal.hpp>
+#include <boost/preprocessor/control/if.hpp>
+#include <boost/preprocessor/facilities/empty.hpp>
+#include <boost/preprocessor/facilities/identity.hpp>
 
 namespace boost
 {
@@ -33,12 +36,17 @@
             // hard wiring reference_wrapper and weak_ptr here ...
             // **TODO** find out why boost bind does this ...
             template <typename A0, typename A1>
- result_type operator()(reference_wrapper<A0> const & a0, reference_wrapper<A1> const & a1) const
+ result_type
+ operator()(
+ reference_wrapper<A0> const & a0
+ , reference_wrapper<A1> const & a1
+ ) const
             {
                 return a0.get_pointer() == a1.get_pointer();
             }
             template <typename A0, typename A1>
- result_type operator()(weak_ptr<A0> const & a0, weak_ptr<A1> const & a1) const
+ result_type
+ operator()(weak_ptr<A0> const & a0, weak_ptr<A1> const & a1) const
             {
                 return !(a0 < a1) && !(a1 < a0);
             }
@@ -51,19 +59,28 @@
                 proto::if_<
                     proto::matches<proto::_, proto::_state>()
                   , proto::or_<
- /*proto::when<
- rule::custom_terminal
- , compare(proto::_value, proto::_value(proto::_state))
- >
- ,*/ proto::when<
+ proto::when<
                             proto::terminal<proto::_>
                           , proto::if_<
                                 mpl::false_()//is_custom_terminal<proto::_value>()
                               , compare(
- proto::lazy<custom_terminal<proto::_value>(proto::_value)>
- , proto::lazy<custom_terminal<proto::_value(proto::_state)>(proto::_value(proto::_state))>
+ proto::lazy<
+ custom_terminal<proto::_value>(
+ proto::_value
+ )
+ >
+ , proto::lazy<
+ custom_terminal<
+ proto::_value(proto::_state)
+ >(
+ proto::_value(proto::_state)
+ )
+ >
+ )
+ , compare(
+ proto::_value
+ , proto::_value(proto::_state)
                                 )
- , compare(proto::_value, proto::_value(proto::_state))
>
>
                       , proto::otherwise<test(proto::_, proto::_state)>
@@ -86,27 +103,53 @@
             template <typename Expr1>
             result_type operator()(Expr1 const& e1, Expr1 const& e2) const
             {
- return eval(e1, e2, typename proto::arity_of<Expr1>::type());
+ return
+ this->evaluate(
+ e1
+ , e2
+ , typename proto::arity_of<Expr1>::type()
+ );
             }
 
             private:
- #define FUNCTION_EQUAL_R(Z, N, DATA) \
- BOOST_PP_IF(N, &&, BOOST_PP_EMPTY()) \
- function_equal_()( \
+ #define PHOENIX_FUNCTION_EQUAL_R(Z, N, DATA) \
+ && function_equal_()( \
                             proto::child_c< N >(e1) \
                           , proto::child_c< N >(e2) \
                         ) \
                 /**/
 
- #define FUNCTION_EQUAL(Z, N, DATA) \
- template <typename Expr1> \
- result_type eval(Expr1 const& e1, Expr1 const& e2, mpl::long_< N >) const \
- { \
- return BOOST_PP_REPEAT(N, FUNCTION_EQUAL_R, _); \
- } \
+ #define PHOENIX_FUNCTION_EQUAL(Z, N, DATA) \
+ template <typename Expr1> \
+ result_type \
+ evaluate( \
+ Expr1 const& e1 \
+ , Expr1 const& e2 \
+ , mpl::long_< N > \
+ ) const \
+ { \
+ return \
+ function_equal_()( \
+ proto::child_c<0>(e1) \
+ , proto::child_c<0>(e2) \
+ ) \
+ BOOST_PP_REPEAT_FROM_TO( \
+ 1 \
+ , N \
+ , PHOENIX_FUNCTION_EQUAL_R \
+ , _ \
+ ); \
+ } \
                 /**/
 
- BOOST_PP_REPEAT(BOOST_PROTO_MAX_ARITY, FUNCTION_EQUAL, _)
+ BOOST_PP_REPEAT_FROM_TO(
+ 1
+ , BOOST_PROTO_MAX_ARITY
+ , PHOENIX_FUNCTION_EQUAL
+ , _
+ )
+ #undef PHOENIX_FUNCTION_EQUAL_R
+ #undef PHOENIX_FUNCTION_EQUAL
         };
     }
 

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 11:15:22 EST (Fri, 26 Nov 2010)
@@ -77,7 +77,7 @@
                 proto::when<
                     proto::comma<
                         switch_case_is_nullary
- , rule::switch_default_case
+ , proto::or_<rule::switch_default_case, rule::switch_case>
>
                   , mpl::and_<
                         switch_case_is_nullary(proto::_child_c<0>, proto::_state)
@@ -85,23 +85,9 @@
>()
>
               , proto::when<
- 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<
- rule::switch_default_case
+ proto::or_<rule::switch_default_case, rule::switch_case>
                   , evaluator(proto::_child_c<0>, proto::_state)
>
- , proto::when<
- rule::switch_case
- , evaluator(proto::_child_c<1>, proto::_state)
- >
>
         {};
 
@@ -116,17 +102,41 @@
         {};
         struct switch_case_grammar
             : proto::or_<
- proto::comma<switch_case_grammar, rule::switch_case>
- , rule::switch_case
+ proto::when<
+ proto::comma<switch_case_grammar, rule::switch_case>
+ , proto::if_<
+ is_same<mpl::prior<proto::_data>(), proto::_state>()
+ , proto::_child_c<1>
+ , switch_case_grammar(proto::_child_c<0>, proto::_state, mpl::prior<proto::_data>())
+ >
+ >
+ , proto::when<rule::switch_case, proto::_>
>
         {};
 
         struct switch_case_with_default_grammar
             : proto::or_<
- proto::comma<switch_case_grammar, rule::switch_default_case>
- , rule::switch_default_case
+ proto::when<
+ proto::comma<switch_case_grammar, rule::switch_default_case>
+ , proto::if_<
+ is_same<mpl::prior<proto::_data>(), proto::_state>()
+ , proto::_child_c<1>
+ , switch_case_grammar(proto::_child_c<0>, proto::_state, mpl::prior<proto::_data>())
+ >
+ >
+ , proto::when<rule::switch_default_case, proto::_>
>
         {};
+
+ struct switch_size
+ : proto::or_<
+ proto::when<
+ proto::comma<switch_size, proto::_>
+ , mpl::next<switch_size(proto::_left)>()
+ >
+ , proto::when<proto::_, mpl::int_<1>()>
+ >
+ {};
     }
 
     struct switch_eval
@@ -143,46 +153,67 @@
         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());
+ this->evaluate(
+ env
+ , cond
+ , cases
+ , typename boost::result_of<detail::switch_size(Cases)>::type()
+ , typename proto::matches<Cases, detail::switch_case_with_default_grammar>::type()
+ );
         }
 
- //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 \
+ #define PHOENIX_SWITCH_EVAL_TYPEDEF_R(Z, N, DATA) \
+ typedef \
+ typename boost::result_of< \
+ detail::switch_grammar( \
+ Cases \
+ , mpl::int_<N> \
+ , mpl::int_<DATA> \
+ ) \
+ >::type \
+ BOOST_PP_CAT(case, N); \
+ typedef \
+ typename proto::result_of::value< \
+ typename proto::result_of::child_c< \
+ BOOST_PP_CAT(case, N) \
+ , 0 \
+ >::type \
>::type \
- >::type::value : \
- eval(proto::child_c<1>(fusion::at_c<N>(cases)), env);
+ BOOST_PP_CAT(case_label, N); \
+ mpl::int_<N> BOOST_PP_CAT(idx, N); \
         /**/
+
+ #define PHOENIX_SWITCH_EVAL_R(Z, N, DATA) \
+ case BOOST_PP_CAT(case_label, N)::value : \
+ eval(proto::child_c<1>(detail::switch_grammar()(cases, BOOST_PP_CAT(idx, N), size)), env); \
+ break;
+ /**/
+
         #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\
+ result_type evaluate(Env & env, Cond const & cond, Cases const & cases, mpl::int_<N> size, mpl::false_) const\
             { \
+ BOOST_PP_REPEAT(N, PHOENIX_SWITCH_EVAL_TYPEDEF_R, N) \
+ switch(eval(cond, env)) \
+ { \
+ BOOST_PP_REPEAT(N, PHOENIX_SWITCH_EVAL_R, _) \
+ } \
             } \
             \
             template <typename Env, typename Cond, typename Cases> \
- result_type evaluate(Env & env, Cond const & cond, Cases const & cases, mpl::int_<N>, mpl::true_) const\
+ result_type evaluate(Env & env, Cond const & cond, Cases const & cases, mpl::int_<N> size, mpl::true_) const\
             { \
+ BOOST_PP_REPEAT(BOOST_PP_DEC(N), PHOENIX_SWITCH_EVAL_TYPEDEF_R, N) \
+ mpl::int_<BOOST_PP_DEC(N)> BOOST_PP_CAT(idx, BOOST_PP_DEC(N)); \
                 switch(eval(cond, env)) \
                 { \
                     BOOST_PP_REPEAT(BOOST_PP_DEC(N), PHOENIX_SWITCH_EVAL_R, _) \
- default: eval(fusion::at_c<N-1>(cases), env); \
+ default: eval(proto::child_c<0>(detail::switch_grammar()(cases, BOOST_PP_CAT(idx, BOOST_PP_DEC(N)), size)), env); \
                 } \
             } \
         /**/
- BOOST_PP_REPEAT(PHOENIX_LIMIT, PHOENIX_SWITCH_EVAL, _)
+ BOOST_PP_REPEAT_FROM_TO(0, PHOENIX_LIMIT, PHOENIX_SWITCH_EVAL, _)
     };
     
     template <typename Dummy>
@@ -191,7 +222,7 @@
             switch_eval(
                 _env
               , proto::_child_c<0> // Cond
- , proto::functional::flatten(proto::_child_c<1>) // Cases
+ , proto::_child_c<1> // Cases
             )
>
     {};

Modified: sandbox/SOC/2010/phoenix3/libs/phoenix/test/Jamfile
==============================================================================
--- sandbox/SOC/2010/phoenix3/libs/phoenix/test/Jamfile (original)
+++ sandbox/SOC/2010/phoenix3/libs/phoenix/test/Jamfile 2010-11-26 11:15:22 EST (Fri, 26 Nov 2010)
@@ -53,7 +53,7 @@
 test-suite phoenix_statement :
     [ run statement/if_tests.cpp ]
     [ run statement/loops_tests.cpp ]
-# [ run statement/switch_tests.cpp ]
+ [ run statement/switch_tests.cpp ]
 # [ run statement/exceptions.cpp ]
     ;
         

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 11:15:22 EST (Fri, 26 Nov 2010)
@@ -20,7 +20,6 @@
 {
     using boost::phoenix::arg_names::_1;
     using boost::phoenix::case_;
- //using boost::phoenix::default_;
     using boost::phoenix::switch_;
     using boost::phoenix::ref;
     using boost::phoenix::val;
@@ -33,7 +32,6 @@
     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)
         [
@@ -41,7 +39,6 @@
             case_<4>(cout << val("<4>") << endl)
         ]
     );
- */
 
     cout << endl;
     for_each(v.begin(), v.end(),
@@ -54,7 +51,6 @@
 
     cout << endl;
     
- /*
     for_each(v.begin(), v.end(),
         switch_(_1)
         [
@@ -64,7 +60,6 @@
             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