Boost logo

Boost-Commit :

From: chochlik_at_[hidden]
Date: 2008-05-16 13:32:02


Author: matus.chochlik
Date: 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
New Revision: 45438
URL: http://svn.boost.org/trac/boost/changeset/45438

Log:
- Simplified the implementation of for_each
- Fixed minor bugs
Text files modified:
   sandbox/mirror/boost/mirror/algorithm/detail/for_each.hpp | 54 ++-----------
   sandbox/mirror/boost/mirror/algorithm/detail/iterative.hpp | 150 ++++++++++++++++++++++++++++++---------
   sandbox/mirror/boost/mirror/algorithm/detail/no_op.hpp | 2
   sandbox/mirror/boost/mirror/algorithm/detail/reverse_for_each.hpp | 53 ++-----------
   sandbox/mirror/boost/mirror/algorithm/for_each.hpp | 58 +++++++++++++-
   sandbox/mirror/boost/mirror/algorithm/reverse_for_each.hpp | 59 +++++++++++++--
   sandbox/mirror/boost/mirror/function/identity_op.hpp | 2
   sandbox/mirror/boost/mirror/function/select_base_name.hpp | 2
   sandbox/mirror/boost/mirror/function/select_full_name.hpp | 2
   sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp | 11 +-
   10 files changed, 249 insertions(+), 144 deletions(-)

Modified: sandbox/mirror/boost/mirror/algorithm/detail/for_each.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/detail/for_each.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/detail/for_each.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -10,56 +10,24 @@
 #ifndef BOOST_MIRROR_ALGORITHM_DETAIL_FOR_EACH_HPP
 #define BOOST_MIRROR_ALGORITHM_DETAIL_FOR_EACH_HPP
 
-// mirror common definitions
-#include <boost/mirror/common_defs.hpp>
-// forward declarations
-#include <boost/mirror/meta_data_fwd.hpp>
-// mirror::at
-#include <boost/mirror/algorithm/at.hpp>
+#include <boost/mirror/algorithm/detail/iterative.hpp>
+#include <boost/mirror/algorithm/next.hpp>
 
 namespace boost {
 namespace mirror {
-
 namespace detail {
 
-/** Implementation of the for_each function
- */
-template <class MetaObjectSequence, class Size>
-struct for_each_meta_object
-{
-protected:
- template <class MetaObjectOp, class TransformOp, int I>
- static inline void do_apply_to(MetaObjectOp op, TransformOp transf, mpl::int_<I> pos)
- {
- typedef typename boost::mirror::at<
- MetaObjectSequence, mpl::int_<I>
- >:: type meta_object;
- op(transf(meta_object()));
- }
-
- typedef mpl::int_< -1 > begin;
-
- template <class MetaObjectOp, class TransformOp>
- static inline void apply_to(MetaObjectOp op, TransformOp transf, begin){ }
-
- template <class MetaObjectOp, class TransformOp, int I>
- static inline void apply_to(MetaObjectOp op, TransformOp transf, mpl::int_<I> pos)
- {
- apply_to(op, transf, mpl::int_<I - 1>()),
- do_apply_to(op, transf, pos);
- }
-public:
- template <class MetaObjectOp, class TransformOp>
- static inline MetaObjectOp perform(MetaObjectOp op, TransformOp transf)
- {
- typedef mpl::int_<Size::value - 1> last;
- apply_to(op, transf, last());
- return op;
- }
-};
+ /** Implementation of the for_each function
+ */
+ template <class IteratorBegin, class IteratorEnd>
+ struct for_each_meta_object
+ : perform_on_range<
+ IteratorBegin,
+ IteratorEnd,
+ mpl::int_< 1 >
+ >{ };
 
 } // namespace detail
-
 } // namespace mirror
 } // namespace boost
 

Modified: sandbox/mirror/boost/mirror/algorithm/detail/iterative.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/detail/iterative.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/detail/iterative.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -16,50 +16,130 @@
 #include <boost/mirror/meta_data_fwd.hpp>
 // mirror::size
 #include <boost/mirror/algorithm/size.hpp>
+#include <boost/mirror/algorithm/iterator_equal.hpp>
+//
+#include <boost/ref.hpp>
+
 
 namespace boost {
 namespace mirror {
 namespace detail {
 
- /** Declaration of the default iterative_algorithm
- * helper template.
- */
- template <class MetaObjectSequence, template <class, class> class AlgoImpl >
- struct iterative_algorithm { };
-
- /** Specialization of iterative_algorithm<MetaObjectSequence,AlgoImpl>
- * for meta_class_attributes<>
- */
- template <class Class, class VariantTag, template <class, class> class AlgoImpl>
- struct iterative_algorithm<meta_class_attributes<Class, VariantTag>, AlgoImpl >
- : AlgoImpl<
- meta_class_attributes<Class, VariantTag>,
- size<meta_class_attributes<Class, VariantTag> >
- >{ };
-
- /** Specialization of iterative_algorithm<MetaObjectSequence,AlgoImpl>
- * for meta_class_all_attributes<>
- */
- template <class Class, class VariantTag, template <class, class> class AlgoImpl>
- struct iterative_algorithm<meta_class_all_attributes<Class, VariantTag>, AlgoImpl >
- : AlgoImpl<
- meta_class_all_attributes<Class, VariantTag>,
- size<meta_class_all_attributes<Class, VariantTag> >
- >{ };
-
+ template <
+ class IteratorBegin,
+ class IteratorEnd,
+ template <class, class> class AlgoImpl
+ >
+ struct iterative_algorithm
+ : AlgoImpl<IteratorBegin, IteratorEnd>{ };
 
- /** Specialization of iterative_algorithm<MetaObjectSequence,AlgoImpl>
- * for meta_base_classes<>
+ /** Implementation of the for-each-like templates
          */
- template <class Class, class VariantTag, template <class, class> class AlgoImpl>
- struct iterative_algorithm<meta_base_classes<Class, VariantTag>, AlgoImpl >
- : AlgoImpl<
- meta_base_classes<Class, VariantTag>,
- size<meta_base_classes<Class, VariantTag> >
- >{ };
+ template <
+ class IteratorBegin,
+ class IteratorEnd,
+ class Direction
+ >
+ struct perform_on_range
+ {
+ private:
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void do_apply_to(
+ reference_wrapper<MetaObjectOp> op_ref,
+ reference_wrapper<TransformOp> transf_ref,
+ Iterator
+ )
+ {
+ typedef typename boost::mirror::deref<
+ Iterator
+ >:: type meta_object;
+ MetaObjectOp& op(op_ref);
+ TransformOp& transf(transf_ref);
+ op(transf(meta_object()));
+ }
+
+ typedef typename IteratorBegin begin;
+ typedef typename IteratorEnd end;
+ typedef typename mpl::int_<1> forward;
+ typedef typename mpl::int_<-1> reverse;
+
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void pre_apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator,
+ forward
+ )
+ {
+ do_apply_to(op, transf, Iterator());
+ }
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void pre_apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator,
+ reverse
+ ){ }
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void post_apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator,
+ forward
+ ){ }
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void post_apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator,
+ reverse
+ )
+ {
+ do_apply_to(op, transf, Iterator());
+ }
+
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator,
+ mpl::true_
+ ){ }
+
+ template <class MetaObjectOp, class TransformOp, class Iterator>
+ static inline void apply_to(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf,
+ Iterator i,
+ mpl::false_
+ )
+ {
+ Direction dir;
+ pre_apply_to(op, transf, i, dir);
+ typedef typename next<Iterator>::type J;
+ typename iterator_equal<J, end>::type done;
+ apply_to(op, transf, J(), done);
+ post_apply_to(op, transf, i, dir);
+ }
+ public:
+ template <class MetaObjectOp, class TransformOp>
+ static inline reference_wrapper<MetaObjectOp> perform(
+ reference_wrapper<MetaObjectOp> op,
+ reference_wrapper<TransformOp> transf
+ )
+ {
+ typename iterator_equal<begin, end>::type done;
+ apply_to(op, transf, begin(), done);
+ return op;
+ }
+ };
 
 } // namespace detail
-
 } // namespace mirror
 } // namespace boost
 

Modified: sandbox/mirror/boost/mirror/algorithm/detail/no_op.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/detail/no_op.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/detail/no_op.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -18,7 +18,7 @@
 struct no_op
 {
         template <typename ArgType>
- inline ArgType operator()(ArgType arg){return arg;}
+ inline ArgType operator()(ArgType arg) const {return arg;}
 };
 
 } // namespace detail

Modified: sandbox/mirror/boost/mirror/algorithm/detail/reverse_for_each.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/detail/reverse_for_each.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/detail/reverse_for_each.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -10,55 +10,24 @@
 #ifndef BOOST_MIRROR_ALGORITHM_DETAIL_REVERSE_FOR_EACH_HPP
 #define BOOST_MIRROR_ALGORITHM_DETAIL_REVERSE_FOR_EACH_HPP
 
-// mirror common definitions
-#include <boost/mirror/common_defs.hpp>
-// forward declarations
-#include <boost/mirror/meta_data_fwd.hpp>
-// mirror::at
-#include <boost/mirror/algorithm/at.hpp>
+#include <boost/mirror/algorithm/detail/iterative.hpp>
+#include <boost/mirror/algorithm/prior.hpp>
 
 namespace boost {
 namespace mirror {
 namespace detail {
 
-/** Implementation of the for_each function
- */
-template <class MetaObjectSequence, class Size>
-struct reverse_for_each_meta_object
-{
-protected:
- template <class MetaObjectOp, class TransformOp, int I>
- static void do_apply_to(MetaObjectOp op, TransformOp transf, mpl::int_<I> pos)
- {
- typedef typename boost::mirror::at<
- MetaObjectSequence, mpl::int_<I>
- >:: type meta_object;
- op(transf(meta_object()));
- }
-
- typedef typename mpl::int_<Size::value> end;
-
- template <class MetaObjectOp, class TransformOp>
- static void apply_to(MetaObjectOp op, TransformOp transf, end){ }
-
- template <class MetaObjectOp, class TransformOp, int I>
- static void apply_to(MetaObjectOp op, TransformOp transf, mpl::int_<I> pos)
- {
- apply_to(op, transf, mpl::int_<I + 1>()),
- do_apply_to(op, transf, pos);
- }
-public:
- template <class MetaObjectOp, class TransformOp>
- static MetaObjectOp perform(MetaObjectOp op, TransformOp transf)
- {
- typedef mpl::int_<0> first;
- apply_to(op, transf, first());
- return op;
- }
-};
+ /** Implementation of the for_each function
+ */
+ template <class IteratorBegin, class IteratorEnd>
+ struct reverse_for_each_meta_object
+ : perform_on_range<
+ IteratorBegin,
+ IteratorEnd,
+ mpl::int_< -1 >
+ >{ };
 
 } // namespace detail
-
 } // namespace mirror
 } // namespace boost
 

Modified: sandbox/mirror/boost/mirror/algorithm/for_each.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/for_each.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/for_each.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -11,8 +11,9 @@
 #define BOOST_MIRROR_ALGORITHM_FOR_EACH_HPP
 
 #include <boost/mirror/algorithm/detail/for_each.hpp>
-#include <boost/mirror/algorithm/detail/iterative.hpp>
 #include <boost/mirror/algorithm/detail/no_op.hpp>
+#include <boost/mirror/algorithm/begin.hpp>
+#include <boost/mirror/algorithm/end.hpp>
 
 namespace boost {
 namespace mirror {
@@ -21,10 +22,13 @@
         /** Declaration of the default for_each_impl
          * helper template.
          */
- template <class MetaObjectSequence>
+ template <class IteratorBegin, class IteratorEnd>
         struct for_each_impl
- : iterative_algorithm<MetaObjectSequence, for_each_meta_object>
- { };
+ : iterative_algorithm<
+ IteratorBegin,
+ IteratorEnd,
+ for_each_meta_object
+ >{ };
 
 } // namespace detail
 
@@ -32,9 +36,42 @@
         class MetaObjectSequence,
         class Functor
>
+static inline reference_wrapper<Functor> for_each(
+ reference_wrapper<Functor> fn
+)
+{
+ return detail::for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(fn, cref(detail::no_op()));
+}
+
+template <
+ class MetaObjectSequence,
+ class Functor
+>
 static inline Functor for_each(Functor fn)
 {
- return detail::for_each_impl<MetaObjectSequence> ::perform(fn, detail::no_op());
+ return detail::for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(ref(fn), cref(detail::no_op()));
+}
+
+template <
+ class MetaObjectSequence,
+ class TransformOp,
+ class Functor
+>
+static inline reference_wrapper<Functor> for_each(
+ reference_wrapper<TransformOp> transf,
+ reference_wrapper<Functor> fn
+)
+{
+ return detail::for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(fn, transf);
 }
 
 template <
@@ -42,11 +79,18 @@
         class TransformOp,
         class Functor
>
-static inline Functor for_each(TransformOp transf, Functor fn)
+static inline Functor for_each(
+ TransformOp transf,
+ Functor fn
+)
 {
- return detail::for_each_impl<MetaObjectSequence> ::perform(fn, transf);
+ return detail::for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(ref(fn), ref(transf));
 }
 
+
 } // namespace mirror
 } // namespace boost
 

Modified: sandbox/mirror/boost/mirror/algorithm/reverse_for_each.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/algorithm/reverse_for_each.hpp (original)
+++ sandbox/mirror/boost/mirror/algorithm/reverse_for_each.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -11,20 +11,24 @@
 #define BOOST_MIRROR_ALGORITHM_REVERSE_FOR_EACH_HPP
 
 #include <boost/mirror/algorithm/detail/reverse_for_each.hpp>
-#include <boost/mirror/algorithm/detail/iterative.hpp>
 #include <boost/mirror/algorithm/detail/no_op.hpp>
+#include <boost/mirror/algorithm/begin.hpp>
+#include <boost/mirror/algorithm/end.hpp>
 
 namespace boost {
 namespace mirror {
 namespace detail {
 
- /** Declaration of the default for_each_impl
+ /** Declaration of the default reverse_for_each_impl
          * helper template.
          */
- template <class MetaObjectSequence>
+ template <class IteratorBegin, class IteratorEnd>
         struct reverse_for_each_impl
- : iterative_algorithm<MetaObjectSequence, reverse_for_each_meta_object>
- { };
+ : iterative_algorithm<
+ IteratorBegin,
+ IteratorEnd,
+ reverse_for_each_meta_object
+ >{ };
 
 } // namespace detail
 
@@ -32,9 +36,42 @@
         class MetaObjectSequence,
         class Functor
>
+static reference_wrapper<Functor> reverse_for_each(
+ reference_wrapper<Functor> fn
+)
+{
+ return detail::reverse_for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(fn, cref(detail::no_op()));
+}
+
+template <
+ class MetaObjectSequence,
+ class Functor
+>
 static Functor reverse_for_each(Functor fn)
 {
- return detail::reverse_for_each_impl<MetaObjectSequence> ::perform(fn, detail::no_op());
+ return detail::reverse_for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(ref(fn), cref(detail::no_op()));
+}
+
+template <
+ class MetaObjectSequence,
+ class TransformOp,
+ class Functor
+>
+static reference_wrapper<Functor> reverse_for_each(
+ reference_wrapper<TransformOp> transf,
+ reference_wrapper<Functor> fn
+)
+{
+ return detail::reverse_for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(fn, transf);
 }
 
 template <
@@ -42,9 +79,15 @@
         class TransformOp,
         class Functor
>
-static Functor reverse_for_each(TransformOp transf, Functor fn)
+static Functor reverse_for_each(
+ TransformOp transf,
+ Functor fn
+)
 {
- return detail::reverse_for_each_impl<MetaObjectSequence> ::perform(fn, transf);
+ return detail::reverse_for_each_impl<
+ typename mirror::begin<MetaObjectSequence>::type,
+ typename mirror::end<MetaObjectSequence>::type
+ > ::perform(ref(fn), ref(transf));
 }
 
 } // namespace mirror

Modified: sandbox/mirror/boost/mirror/function/identity_op.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/function/identity_op.hpp (original)
+++ sandbox/mirror/boost/mirror/function/identity_op.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -17,7 +17,7 @@
 struct identity_op
 {
         template <class ArgumentType>
- inline ArgumentType operator()(ArgumentType arg)
+ inline ArgumentType operator()(ArgumentType arg) const
         {
                 return arg;
         }

Modified: sandbox/mirror/boost/mirror/function/select_base_name.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/function/select_base_name.hpp (original)
+++ sandbox/mirror/boost/mirror/function/select_base_name.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -17,7 +17,7 @@
 struct select_base_name
 {
         template <class MetaObject>
- inline const bchar* operator()(MetaObject)
+ inline const bchar* operator()(MetaObject) const
         {
                 return MetaObject::base_name();
         }

Modified: sandbox/mirror/boost/mirror/function/select_full_name.hpp
==============================================================================
--- sandbox/mirror/boost/mirror/function/select_full_name.hpp (original)
+++ sandbox/mirror/boost/mirror/function/select_full_name.hpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -17,7 +17,7 @@
 struct select_full_name
 {
         template <class MetaObject>
- inline const bchar* operator()(MetaObject)
+ inline const bchar* operator()(MetaObject) const
         {
                 return MetaObject::full_name();
         }

Modified: sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp
==============================================================================
--- sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp (original)
+++ sandbox/mirror/libs/mirror/example/special/boost_tuple.cpp 2008-05-16 13:32:01 EDT (Fri, 16 May 2008)
@@ -56,9 +56,11 @@
 {
         void operator()(const ::boost::bchar* str) const
         {
- using namespace ::std;
- using namespace ::boost;
- bcout << str << ", ";
+ ::boost::bcout << str << ", ";
+ }
+ ~str_printer(void)
+ {
+ ::boost::bcout << ::std::endl;
         }
 };
 
@@ -124,8 +126,7 @@
         bcout << "---------------------------------------------------" << endl;
         reverse_for_each<meta_X::all_attributes>(p);
         bcout << "---------------------------------------------------" << endl;
- for_each<meta_X::all_attributes>(select_base_name(), str_printer());
- bcout << endl;
+ for_each<meta_X::all_attributes>(cref(select_base_name()), cref(str_printer()));
         bcout << "---------------------------------------------------" << endl;
         bcout << "Finished" << 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