From fce61f0defdef39d7139b2d714d5caed42c06c9b Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 20 Dec 2012 00:09:05 +0900 Subject: [PATCH] add static_variant_visitor --- sprout/variant.hpp | 2 ++ sprout/variant/apply_visitor.hpp | 34 +----------------- sprout/variant/static_variant_visitor.hpp | 22 ++++++++++++ sprout/variant/static_visitor.hpp | 3 ++ sprout/variant/variant.hpp | 42 ++++++++++++++++++----- sprout/variant/visitor_result.hpp | 41 ++++++++++++++++++++++ 6 files changed, 103 insertions(+), 41 deletions(-) create mode 100644 sprout/variant/static_variant_visitor.hpp create mode 100644 sprout/variant/visitor_result.hpp diff --git a/sprout/variant.hpp b/sprout/variant.hpp index d95fb9b7..5ef1f8dc 100644 --- a/sprout/variant.hpp +++ b/sprout/variant.hpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #endif // #ifndef SPROUT_VARIANT_HPP diff --git a/sprout/variant/apply_visitor.hpp b/sprout/variant/apply_visitor.hpp index 205a5ed7..99e26cbb 100644 --- a/sprout/variant/apply_visitor.hpp +++ b/sprout/variant/apply_visitor.hpp @@ -4,41 +4,9 @@ #include #include #include -#include +#include namespace sprout { - // - // has_visitor_result - // - SPROUT_HAS_XXX_TEMPLATE_DEF_LAZY(visitor_result); - - namespace detail { - template - struct visitor_result_impl; - template - struct visitor_result_impl< - Visitor, Visitable, - typename std::enable_if::value>::type - > - : public Visitable::template visitor_result - {}; - template - struct visitor_result_impl< - Visitor, Visitable, - typename std::enable_if::value>::type - > { - public: - typedef typename Visitor::result_type type; - }; - } // namespace detail - // - // visitor_result - // - template - struct visitor_result - : public sprout::detail::visitor_result_impl - {}; - // // apply_visitor // diff --git a/sprout/variant/static_variant_visitor.hpp b/sprout/variant/static_variant_visitor.hpp new file mode 100644 index 00000000..44bbf61b --- /dev/null +++ b/sprout/variant/static_variant_visitor.hpp @@ -0,0 +1,22 @@ +#ifndef SPROUT_VARIANT_STATIC_VARIANT_VISITOR_HPP +#define SPROUT_VARIANT_STATIC_VARIANT_VISITOR_HPP + +#include +#include +#include + +namespace sprout { + // + // static_variant_visitor + // + class static_variant_visitor { + public: + template + struct visitor_result { + public: + typedef sprout::variant::type...> type; + }; + }; +} // namespace sprout + +#endif // #ifndef SPROUT_VARIANT_STATIC_VARIANT_VISITOR_HPP diff --git a/sprout/variant/static_visitor.hpp b/sprout/variant/static_visitor.hpp index 265c13cf..e7bd40e9 100644 --- a/sprout/variant/static_visitor.hpp +++ b/sprout/variant/static_visitor.hpp @@ -4,6 +4,9 @@ #include namespace sprout { + // + // static_visitor + // template class static_visitor { public: diff --git a/sprout/variant/variant.hpp b/sprout/variant/variant.hpp index 1ce6c193..5a62bf7c 100644 --- a/sprout/variant/variant.hpp +++ b/sprout/variant/variant.hpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace sprout { template @@ -75,6 +76,15 @@ namespace sprout { public: typedef typename impl_type::tuple_type tuple_type; private: + template + struct visitor_result_impl_2; + template + struct visitor_result_impl_2 > { + public: + typedef typename Visitor::template visitor_result< + decltype((std::declval())(sprout::tuples::get(std::declval())))... + >::type type; + }; template struct visitor_result_impl_1; template @@ -91,19 +101,35 @@ namespace sprout { template struct visitor_result_impl< Visitor, Tuple, - typename std::enable_if >::value>::type - > - : public visitor_result_impl_1::value>::type> - {}; - template - struct visitor_result_impl< - Visitor, Tuple, - typename std::enable_if >::value>::type + typename std::enable_if< + sprout::has_result_type >::value + >::type > { public: typedef typename sprout::weak_result_type::result_type type; }; + template + struct visitor_result_impl< + Visitor, Tuple, + typename std::enable_if< + sprout::has_visitor_result::value + && !sprout::has_result_type >::value + >::type + > + : public visitor_result_impl_2::value>::type> + {}; + template + struct visitor_result_impl< + Visitor, Tuple, + typename std::enable_if< + !sprout::has_visitor_result::value + && !sprout::has_result_type >::value + >::type + > + : public visitor_result_impl_1::value>::type> + {}; public: + // visitation support template struct visitor_result : public visitor_result_impl diff --git a/sprout/variant/visitor_result.hpp b/sprout/variant/visitor_result.hpp new file mode 100644 index 00000000..3fcc1836 --- /dev/null +++ b/sprout/variant/visitor_result.hpp @@ -0,0 +1,41 @@ +#ifndef SPROUT_VARIANT_VISITOR_RESULT_HPP +#define SPROUT_VARIANT_VISITOR_RESULT_HPP + +#include +#include + +namespace sprout { + // + // has_visitor_result + // + SPROUT_HAS_XXX_TEMPLATE_DEF_LAZY(visitor_result); + + namespace detail { + template + struct visitor_result_impl; + template + struct visitor_result_impl< + Visitor, Visitable, + typename std::enable_if::value>::type + > + : public Visitable::template visitor_result + {}; + template + struct visitor_result_impl< + Visitor, Visitable, + typename std::enable_if::value>::type + > { + public: + typedef typename Visitor::result_type type; + }; + } // namespace detail + // + // visitor_result + // + template + struct visitor_result + : public sprout::detail::visitor_result_impl + {}; +} // namespace sprout + +#endif // #ifndef SPROUT_VARIANT_VISITOR_RESULT_HPP