/*============================================================================= Copyright (c) 2011-2013 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP #define SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP #include #include #include #include #include #include #include #include #include #include namespace sprout { namespace adaptors { // // transformed_range // template class transformed_range : public sprout::adaptors::detail::adapted_range_default< LRange, sprout::transform_iterator< BinaryFunction, typename sprout::container_traits::iterator, typename sprout::container_traits::iterator > > { public: typedef BinaryFunction functor_type; typedef LRange range1_type; typedef RRange range2_type; typedef sprout::adaptors::detail::adapted_range_default< LRange, sprout::transform_iterator< BinaryFunction, typename sprout::container_traits::iterator, typename sprout::container_traits::iterator > > base_type; typedef typename base_type::iterator iterator; public: transformed_range() = default; transformed_range(transformed_range const&) = default; SPROUT_CONSTEXPR transformed_range(BinaryFunction func, range1_type& range1, range2_type& range2) : base_type( iterator(sprout::begin(range1), sprout::begin(range2), func), iterator(sprout::end(range1), sprout::end(range2), func) ) {} }; template class transformed_range : public sprout::adaptors::detail::adapted_range_default< Range, sprout::transform_iterator< UnaryFunction, typename sprout::container_traits::iterator > > { public: typedef UnaryFunction functor_type; typedef Range range1_type; typedef sprout::adaptors::detail::adapted_range_default< Range, sprout::transform_iterator< UnaryFunction, typename sprout::container_traits::iterator > > base_type; typedef typename base_type::iterator iterator; public: transformed_range() = default; transformed_range(transformed_range const&) = default; SPROUT_CONSTEXPR transformed_range(UnaryFunction func, range1_type& range) : base_type( iterator(sprout::begin(range), func), iterator(sprout::end(range), func) ) {} }; // // transform_holder // template class transform_holder { public: typedef BinaryFunction functor_type; typedef RRange range2_type; private: BinaryFunction func_; sprout::value_holder range_; public: transform_holder() = default; transform_holder(transform_holder const&) = default; SPROUT_CONSTEXPR transform_holder(BinaryFunction func, range2_type& range) : func_(func) , range_(range) {} SPROUT_CONSTEXPR BinaryFunction functor() const { return func_; } SPROUT_CONSTEXPR range2_type& range() const { return range_; } }; template class transform_holder { public: typedef UnaryFunction functor_type; private: UnaryFunction func_; public: transform_holder() = default; transform_holder(transform_holder const&) = default; explicit SPROUT_CONSTEXPR transform_holder(UnaryFunction func) : func_(func) {} SPROUT_CONSTEXPR UnaryFunction functor() const { return func_; } }; // // transformed_forwarder // class transformed_forwarder { public: template SPROUT_CONSTEXPR sprout::adaptors::transform_holder< BinaryFunction, typename std::remove_reference::type>::type > operator()(RRange&& range, BinaryFunction func) const { return sprout::adaptors::transform_holder< BinaryFunction, typename std::remove_reference::type>::type >( func, sprout::lvalue_forward(range) ); } template SPROUT_CONSTEXPR sprout::adaptors::transform_holder operator()(UnaryFunction func) const { return sprout::adaptors::transform_holder(func); } }; // // transformed // namespace { SPROUT_STATIC_CONSTEXPR sprout::adaptors::transformed_forwarder transformed = {}; } // anonymous-namespace // // operator| // template inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range< BinaryFunction, typename std::remove_reference::type>::type, RRange > operator|(LRange&& lhs, sprout::adaptors::transform_holder const& rhs) { return sprout::adaptors::transformed_range< BinaryFunction, typename std::remove_reference::type>::type, RRange >( rhs.functor(), sprout::lvalue_forward(lhs), rhs.range() ); } template inline SPROUT_CONSTEXPR sprout::adaptors::transformed_range< UnaryFunction, typename std::remove_reference::type>::type > operator|(Range&& lhs, sprout::adaptors::transform_holder const& rhs) { return sprout::adaptors::transformed_range< UnaryFunction, typename std::remove_reference::type>::type >( rhs.functor(), sprout::lvalue_forward(lhs) ); } } // namespace adaptors // // container_construct_traits // template struct container_construct_traits > : public sprout::container_construct_traits::base_type> {}; } // namespace sprout #endif // #ifndef SPROUT_RANGE_ADAPTOR_TRANSFORMD_HPP