Boost logo

Boost-Commit :

From: steven_at_[hidden]
Date: 2008-04-02 12:59:51


Author: steven_watanabe
Date: 2008-04-02 12:59:51 EDT (Wed, 02 Apr 2008)
New Revision: 43999
URL: http://svn.boost.org/trac/boost/changeset/43999

Log:
Added overload for range_c
Text files modified:
   sandbox/switch/libs/switch/alternate/switch/case.hpp | 28 +++++++++++++++++++++++++
   sandbox/switch/libs/switch/alternate/switch/switch.hpp | 44 ++++++++++++++++++++++++++++++++++++++-
   sandbox/switch/libs/switch/alternate/switch/test_case.cpp | 10 ++++----
   3 files changed, 75 insertions(+), 7 deletions(-)

Modified: sandbox/switch/libs/switch/alternate/switch/case.hpp
==============================================================================
--- sandbox/switch/libs/switch/alternate/switch/case.hpp (original)
+++ sandbox/switch/libs/switch/alternate/switch/case.hpp 2008-04-02 12:59:51 EDT (Wed, 02 Apr 2008)
@@ -18,6 +18,8 @@
 
 namespace boost {
 
+namespace control {
+
 namespace switch_detail {
 
 struct empty_set {
@@ -53,6 +55,10 @@
     typedef typename make_set<boost::mpl::size<S>::value>::template apply<typename boost::mpl::begin<S>::type>::type as_set;
     static const bool value = (sizeof(as_set::lookup(N())) != 1);
 };
+template<class T, int low, int high, class N>
+struct contains_impl<mpl::range_c<T, low, high>, N> {
+ static const bool value = (low <= (N::value)) && ((N::value) < high);
+};
 
 }
 
@@ -69,6 +75,9 @@
>::type apply(N n) const {
         return(impl.template apply<R>(n));
     }
+ const Case& get() const {
+ return(impl);
+ }
 private:
     Case impl;
 };
@@ -113,6 +122,9 @@
     R apply(N n) const {
         return(impl(n));
     }
+ const F& get() const {
+ return(impl);
+ }
 private:
     F impl;
 };
@@ -189,6 +201,22 @@
     return(expression_template_case_t<restrict_case_t<case_group_t<mpl::range_c<int, L, H>, F> > >(f));
 }
 
+
+template<class R, class N, class T, T low, T high, class F>
+inline R switch_(N n, expression_template_case_t<restrict_case_t<case_group_t<mpl::range_c<T, low, high>, F> > > cases BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(R)) {
+ typedef switch_detail::range_switch_impl<high - low> impl;
+ switch_detail::default_construct<R> default_;
+ return(impl::template apply<R, T, low, high>(n, cases.get().get().get(), default_));
 }
 
+template<class R, class N, class T, T low, T high, class F, class D>
+inline R switch_(N n, expression_template_case_t<restrict_case_t<case_group_t<mpl::range_c<T, low, high>, F> > > cases, D d BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(R)) {
+ typedef switch_detail::range_switch_impl<high - low> impl;
+ return(impl::template apply<R, T, low, high>(n, cases.get().get().get(), d));
+}
+
+} // namespace control
+
+} // namespace boost
+
 #endif

Modified: sandbox/switch/libs/switch/alternate/switch/switch.hpp
==============================================================================
--- sandbox/switch/libs/switch/alternate/switch/switch.hpp (original)
+++ sandbox/switch/libs/switch/alternate/switch/switch.hpp 2008-04-02 12:59:51 EDT (Wed, 02 Apr 2008)
@@ -30,6 +30,8 @@
 
 namespace boost {
 
+namespace control {
+
 namespace switch_detail {
 
 // Avoid the need to create all the specializations of switch_impl
@@ -39,7 +41,7 @@
 template<class R>
 struct default_construct {
     template<class Int>
- R operator()(Int i) const {
+ R operator()(Int) const {
         return(R());
     }
 };
@@ -124,6 +126,42 @@
 #undef BOOST_SWITCH_IMPL
 #undef BOOST_SWITCH_CASE
 
+// N is the number of cases not including the default
+template<int N>
+struct range_switch_impl;
+
+// specialize for 0 separately to avoid warnings
+template<>
+struct range_switch_impl<0> {
+ template<class R, class T, T low, T high, class I, class F, class D>
+ static R apply(I i, F, D d) {
+ return(d(i));
+ }
+};
+
+#define BOOST_SWITCH_RANGE_CASE(z, n, data) \
+ case data + n: return(f(mpl::integral_c<T, data + n>()));
+
+#define BOOST_SWITCH_RANGE_SWITCH_IMPL(z, n, data) \
+ template<> \
+ struct range_switch_impl<n> { \
+ template<class R, class T, T low, T high, class I, class F, class D>\
+ static R apply(I i, F f, D d) { \
+ switch(i) { \
+ BOOST_PP_REPEAT_##z(n, BOOST_SWITCH_RANGE_CASE, low) \
+ default: return(d(i)); \
+ } \
+ } \
+ };
+
+#define BOOST_PP_LOCAL_LIMITS (1, BOOST_SWITCH_LIMIT)
+#define BOOST_PP_LOCAL_MACRO(n) BOOST_SWITCH_RANGE_SWITCH_IMPL(1, n, ~)
+#include BOOST_PP_LOCAL_ITERATE()
+
+#undef BOOST_SWITCH_RANGE_SWITCH_IMPL
+#undef BOOST_SWITCH_RANGE_CASE
+
+
 }
 
 template<class R, class N, class S>
@@ -139,6 +177,8 @@
     return(impl::template apply<R>(n, s, d));
 }
 
-}
+} // namespace control
+
+} // namespace boost
 
 #endif

Modified: sandbox/switch/libs/switch/alternate/switch/test_case.cpp
==============================================================================
--- sandbox/switch/libs/switch/alternate/switch/test_case.cpp (original)
+++ sandbox/switch/libs/switch/alternate/switch/test_case.cpp 2008-04-02 12:59:51 EDT (Wed, 02 Apr 2008)
@@ -15,7 +15,7 @@
 #include <boost/mpl/range_c.hpp>
 #include <boost/mpl/vector_c.hpp>
 
-#include <boost/test/auto_unit_test.hpp>
+#include <boost/test/unit_test.hpp>
 
 namespace {
     struct f {
@@ -28,13 +28,13 @@
 }
 
 BOOST_AUTO_TEST_CASE(test1) {
- using boost::case_;
- BOOST_CHECK_EQUAL(boost::switch_<int>(2, (case_<boost::mpl::range_c<int, 0, 5> >(f()))), 4);
+ using boost::control::case_;
+ BOOST_CHECK_EQUAL(boost::control::switch_<int>(2, (case_<boost::mpl::range_c<int, 0, 5> >(f()))), 4);
 }
 
 BOOST_AUTO_TEST_CASE(test2) {
- using boost::case_;
- BOOST_CHECK_EQUAL(boost::switch_<int>(2,
+ using boost::control::case_;
+ BOOST_CHECK_EQUAL(boost::control::switch_<int>(2,
         (case_<boost::mpl::vector_c<int, 0> >(f()),
         case_<boost::mpl::vector_c<int, 1> >(f()),
         case_<boost::mpl::vector_c<int, 2> >(f()),


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