|
| 1 | +// case.hpp |
| 2 | +// |
| 3 | +// Copyright (c) 2008 |
| 4 | +// Steven Watanabe |
| 5 | +// |
| 6 | +// Distributed under the Boost Software License, Version 1.0. (See |
| 7 | +// accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | + |
| 10 | +#ifndef BOOST_CONTROL_CASE_HPP_INCLUDED |
| 11 | +#define BOOST_CONTROL_CASE_HPP_INCLUDED |
| 12 | + |
| 13 | +#include <boost/utility/enable_if.hpp> |
| 14 | +#include <boost/mpl/int.hpp> |
| 15 | +#include <boost/mpl/joint_view.hpp> |
| 16 | +#include <boost/mpl/single_view.hpp> |
| 17 | +#include <boost/mpl/range_c.hpp> |
| 18 | + |
| 19 | +namespace boost { |
| 20 | + |
| 21 | +namespace control { |
| 22 | + |
| 23 | +namespace switch_detail { |
| 24 | + |
| 25 | +// N is the number of cases not including the default |
| 26 | +template<int N> |
| 27 | +struct range_switch_impl; |
| 28 | + |
| 29 | +struct empty_set { |
| 30 | + template<class T> |
| 31 | + static char lookup(T); |
| 32 | +}; |
| 33 | + |
| 34 | +template<class Base, class T> |
| 35 | +struct set : Base { |
| 36 | + static char (&lookup(T))[2]; |
| 37 | + using Base::lookup; |
| 38 | + typedef set type; |
| 39 | +}; |
| 40 | + |
| 41 | +template<int N> |
| 42 | +struct make_set { |
| 43 | + template<class Iter> |
| 44 | + struct apply { |
| 45 | + typedef set<typename make_set<N - 1>::template apply<typename boost::mpl::next<Iter>::type>::type, typename boost::mpl::deref<Iter>::type> type; |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +template<> |
| 50 | +struct make_set<0> { |
| 51 | + template<class Iter> |
| 52 | + struct apply { |
| 53 | + typedef empty_set type; |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +template<class S, class N> |
| 58 | +struct contains_impl { |
| 59 | + typedef typename make_set<boost::mpl::size<S>::value>::template apply<typename boost::mpl::begin<S>::type>::type as_set; |
| 60 | + static const bool value = (sizeof(as_set::lookup(N())) != 1); |
| 61 | +}; |
| 62 | +template<class T, int low, int high, class N> |
| 63 | +struct contains_impl<mpl::range_c<T, low, high>, N> { |
| 64 | + static const bool value = (low <= (N::value)) && ((N::value) < high); |
| 65 | +}; |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +template<class Case> |
| 70 | +class restrict_case_t { |
| 71 | +public: |
| 72 | + template<class T> |
| 73 | + explicit restrict_case_t(T& t) : impl(t) {} |
| 74 | + typedef typename Case::labels labels; |
| 75 | + template<class R, class N> |
| 76 | + typename boost::enable_if_c< |
| 77 | + switch_detail::contains_impl<typename Case::labels, N>::value, |
| 78 | + R |
| 79 | + >::type apply(N n) const { |
| 80 | + return(impl.template apply<R>(n)); |
| 81 | + } |
| 82 | + const Case& get() const { |
| 83 | + return(impl); |
| 84 | + } |
| 85 | +private: |
| 86 | + Case impl; |
| 87 | +}; |
| 88 | + |
| 89 | +template<class Case1, class Case2> |
| 90 | +class binary_case_t : public Case1, public Case2 { |
| 91 | +public: |
| 92 | + template<class T> |
| 93 | + explicit binary_case_t(const T& t) : Case1(t), Case2(t) {} |
| 94 | + binary_case_t(const Case1& case1, const Case2& case2) : |
| 95 | + Case1(case1), |
| 96 | + Case2(case2) {} |
| 97 | + // msvc does not like mpl::joint_view |
| 98 | + // if the labels are not reversed. |
| 99 | + typedef typename mpl::joint_view< |
| 100 | + typename Case2::labels, |
| 101 | + typename Case1::labels |
| 102 | + > labels; |
| 103 | + using Case1::apply; |
| 104 | + using Case2::apply; |
| 105 | +}; |
| 106 | + |
| 107 | +template<class N, class F> |
| 108 | +class single_case_t { |
| 109 | +public: |
| 110 | + single_case_t(F f) : impl(f) {} |
| 111 | + typedef mpl::single_view<N> labels; |
| 112 | + template<class R> |
| 113 | + R apply(N n) const { |
| 114 | + return(impl(n)); |
| 115 | + } |
| 116 | +private: |
| 117 | + F impl; |
| 118 | +}; |
| 119 | + |
| 120 | +template<class S, class F> |
| 121 | +class case_group_t { |
| 122 | +public: |
| 123 | + case_group_t(F f) : impl(f) {} |
| 124 | + typedef S labels; |
| 125 | + template<class R, class N> |
| 126 | + R apply(N n) const { |
| 127 | + return(impl(n)); |
| 128 | + } |
| 129 | + const F& get() const { |
| 130 | + return(impl); |
| 131 | + } |
| 132 | +private: |
| 133 | + F impl; |
| 134 | +}; |
| 135 | + |
| 136 | +template<class F> |
| 137 | +struct add_to_group { |
| 138 | + template<class Prev, class Current> |
| 139 | + struct apply { |
| 140 | + typedef binary_case_t<Prev, single_case_t<Current, F> > type; |
| 141 | + }; |
| 142 | +}; |
| 143 | + |
| 144 | +template<class Case> |
| 145 | +class expression_template_case_t { |
| 146 | +public: |
| 147 | + typedef typename Case::labels labels; |
| 148 | + template<class T> |
| 149 | + explicit expression_template_case_t(T& t) : impl(t) {} |
| 150 | + template<class T0, class T1> |
| 151 | + expression_template_case_t(T0& t0, T1& t1) : impl(t0, t1) {} |
| 152 | + template<class R, class N> |
| 153 | + R apply(N n) const { |
| 154 | + return(impl.template apply<R>(n)); |
| 155 | + } |
| 156 | + Case& get() { return(impl); } |
| 157 | + const Case& get() const { return(impl); } |
| 158 | +private: |
| 159 | + Case impl; |
| 160 | +}; |
| 161 | + |
| 162 | +// allows a Case to be used in the expression template |
| 163 | +// framework. |
| 164 | +template<class Case> |
| 165 | +expression_template_case_t<restrict_case_t<Case> > make_case(Case& c) { |
| 166 | + return(expression_template_case_t<restrict_case_t<Case> >(c)); |
| 167 | +} |
| 168 | + |
| 169 | +template<class Case1, class Case2> |
| 170 | +expression_template_case_t<binary_case_t<Case1, Case2> > |
| 171 | +operator,(const expression_template_case_t<Case1>& c1, |
| 172 | + const expression_template_case_t<Case2>& c2) { |
| 173 | + return(expression_template_case_t<binary_case_t<Case1, Case2> >( |
| 174 | + c1.get(), c2.get())); |
| 175 | +} |
| 176 | + |
| 177 | +template<class S, class F> |
| 178 | +expression_template_case_t<restrict_case_t<case_group_t<S,F> > > case_(F f) { |
| 179 | + return(expression_template_case_t<restrict_case_t<case_group_t<S,F> > >(f)); |
| 180 | +} |
| 181 | + |
| 182 | +template<int N, class F> |
| 183 | +expression_template_case_t<single_case_t<mpl::int_<N>, F> > case_c(F f) { |
| 184 | + return(expression_template_case_t<single_case_t<mpl::int_<N>, F> >(f)); |
| 185 | +} |
| 186 | + |
| 187 | +template<int L, int H, class F> |
| 188 | +expression_template_case_t<restrict_case_t<case_group_t<mpl::range_c<int, L, H>, F> > > case_range_c(F f) { |
| 189 | + return(expression_template_case_t<restrict_case_t<case_group_t<mpl::range_c<int, L, H>, F> > >(f)); |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +template<class R, class N, class T, T low, T high, class F> |
| 194 | +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)) { |
| 195 | + typedef switch_detail::range_switch_impl<high - low> impl; |
| 196 | + switch_detail::default_construct<R> default_; |
| 197 | + return(impl::template apply<R, T, low, high>(n, cases.get().get().get(), default_)); |
| 198 | +} |
| 199 | + |
| 200 | +template<class R, class N, class T, T low, T high, class F, class D> |
| 201 | +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)) { |
| 202 | + typedef switch_detail::range_switch_impl<high - low> impl; |
| 203 | + return(impl::template apply<R, T, low, high>(n, cases.get().get().get(), d)); |
| 204 | +} |
| 205 | + |
| 206 | +} // namespace control |
| 207 | + |
| 208 | +} // namespace boost |
| 209 | + |
| 210 | +#endif |
0 commit comments